Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support transfer in approval job #3915

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/samber/lo v1.37.0
github.com/sashabaranov/go-openai v1.24.0
github.com/segmentio/encoding v0.4.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/shirou/gopsutil/v3 v3.22.8
github.com/spf13/cobra v1.8.0
Expand All @@ -100,6 +101,7 @@ require (
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.30.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/ini.v1 v1.67.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -283,6 +285,7 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rubenv/sql-migrate v1.3.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/asm v1.1.3 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.9.2 // indirect
Expand Down Expand Up @@ -329,7 +332,6 @@ require (
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
k8s.io/apiserver v0.27.7 // indirect
k8s.io/component-base v0.27.7 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ github.com/sashabaranov/go-openai v1.24.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adO
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
github.com/segmentio/encoding v0.4.1 h1:KLGaLSW0jrmhB58Nn4+98spfvPvmo4Ci1P/WIQ9wn7w=
github.com/segmentio/encoding v0.4.1/go.mod h1:/d03Cd8PoaDeceuhUUUQWjU0KhWjrmYrWPgtJHYZSnI=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
Expand Down
9 changes: 6 additions & 3 deletions pkg/microservice/aslan/config/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,14 @@ const (
SAEBatchReleaseTypeManual = "manual"
)

type ApproveOrReject string
type ApprovalStatus string

const (
Approve ApproveOrReject = "approve"
Reject ApproveOrReject = "reject"
ApprovalStatusPending ApprovalStatus = ""
ApprovalStatusApprove ApprovalStatus = "approve"
ApprovalStatusReject ApprovalStatus = "reject"
ApprovalStatusRedirect ApprovalStatus = "redirect"
ApprovalStatusDone ApprovalStatus = "done"
)

type DeploySourceType string
Expand Down
52 changes: 26 additions & 26 deletions pkg/microservice/aslan/core/common/repository/models/workflow_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ type Approval struct {
}

type NativeApproval struct {
Timeout int `bson:"timeout" yaml:"timeout" json:"timeout"`
ApproveUsers []*User `bson:"approve_users" yaml:"approve_users" json:"approve_users"`
FloatApproveUsers []*User `bson:"-" yaml:"flat_approve_users" json:"flat_approve_users"`
NeededApprovers int `bson:"needed_approvers" yaml:"needed_approvers" json:"needed_approvers"`
RejectOrApprove config.ApproveOrReject `bson:"reject_or_approve" yaml:"-" json:"reject_or_approve"`
Timeout int `bson:"timeout" yaml:"timeout" json:"timeout"`
ApproveUsers []*User `bson:"approve_users" yaml:"approve_users" json:"approve_users"`
FloatApproveUsers []*User `bson:"-" yaml:"flat_approve_users" json:"flat_approve_users"`
NeededApprovers int `bson:"needed_approvers" yaml:"needed_approvers" json:"needed_approvers"`
RejectOrApprove config.ApprovalStatus `bson:"reject_or_approve" yaml:"-" json:"reject_or_approve"`
// InstanceCode: native approval instance code, save for working after restart aslan
InstanceCode string `bson:"instance_code" yaml:"instance_code" json:"instance_code"`
}
Expand All @@ -160,16 +160,16 @@ type DingTalkApproval struct {
type DingTalkApprovalNode struct {
ApproveUsers []*DingTalkApprovalUser `bson:"approve_users" yaml:"approve_users" json:"approve_users"`
Type dingtalk.ApprovalAction `bson:"type" yaml:"type" json:"type"`
RejectOrApprove config.ApproveOrReject `bson:"reject_or_approve" yaml:"-" json:"reject_or_approve"`
RejectOrApprove config.ApprovalStatus `bson:"reject_or_approve" yaml:"-" json:"reject_or_approve"`
}

type DingTalkApprovalUser struct {
ID string `bson:"id" yaml:"id" json:"id"`
Name string `bson:"name" yaml:"name" json:"name"`
Avatar string `bson:"avatar" yaml:"avatar" json:"avatar"`
RejectOrApprove config.ApproveOrReject `bson:"reject_or_approve,omitempty" yaml:"-" json:"reject_or_approve,omitempty"`
Comment string `bson:"comment,omitempty" yaml:"-" json:"comment,omitempty"`
OperationTime int64 `bson:"operation_time,omitempty" yaml:"-" json:"operation_time,omitempty"`
ID string `bson:"id" yaml:"id" json:"id"`
Name string `bson:"name" yaml:"name" json:"name"`
Avatar string `bson:"avatar" yaml:"avatar" json:"avatar"`
RejectOrApprove config.ApprovalStatus `bson:"reject_or_approve,omitempty" yaml:"-" json:"reject_or_approve,omitempty"`
Comment string `bson:"comment,omitempty" yaml:"-" json:"comment,omitempty"`
OperationTime int64 `bson:"operation_time,omitempty" yaml:"-" json:"operation_time,omitempty"`
}

type LarkApproval struct {
Expand Down Expand Up @@ -211,16 +211,16 @@ func (l LarkApproval) GetLarkApprovalNode() (resp []*lark.ApprovalNode) {
}

type LarkApprovalNode struct {
ApproveUsers []*LarkApprovalUser `bson:"approve_users" yaml:"approve_users" json:"approve_users"`
Type lark.ApproveType `bson:"type" yaml:"type" json:"type"`
RejectOrApprove config.ApproveOrReject `bson:"reject_or_approve" yaml:"-" json:"reject_or_approve"`
ApproveUsers []*LarkApprovalUser `bson:"approve_users" yaml:"approve_users" json:"approve_users"`
Type lark.ApproveType `bson:"type" yaml:"type" json:"type"`
RejectOrApprove config.ApprovalStatus `bson:"reject_or_approve" yaml:"-" json:"reject_or_approve"`
}

type LarkApprovalUser struct {
lark.UserInfo `bson:",inline" yaml:",inline" json:",inline"`
RejectOrApprove config.ApproveOrReject `bson:"reject_or_approve,omitempty" yaml:"-" json:"reject_or_approve,omitempty"`
Comment string `bson:"comment,omitempty" yaml:"-" json:"comment,omitempty"`
OperationTime int64 `bson:"operation_time,omitempty" yaml:"-" json:"operation_time,omitempty"`
RejectOrApprove config.ApprovalStatus `bson:"reject_or_approve,omitempty" yaml:"-" json:"reject_or_approve,omitempty"`
Comment string `bson:"comment,omitempty" yaml:"-" json:"comment,omitempty"`
OperationTime int64 `bson:"operation_time,omitempty" yaml:"-" json:"operation_time,omitempty"`
}

type WorkWXApproval struct {
Expand All @@ -235,14 +235,14 @@ type WorkWXApproval struct {
}

type User struct {
Type string `bson:"type" yaml:"type" json:"type"`
UserID string `bson:"user_id,omitempty" yaml:"user_id,omitempty" json:"user_id,omitempty"`
UserName string `bson:"user_name,omitempty" yaml:"user_name,omitempty" json:"user_name,omitempty"`
GroupID string `bson:"group_id,omitempty" yaml:"group_id,omitempty" json:"group_id,omitempty"`
GroupName string `bson:"group_name,omitempty" yaml:"group_name,omitempty" json:"group_name,omitempty"`
RejectOrApprove config.ApproveOrReject `bson:"reject_or_approve,omitempty" yaml:"-" json:"reject_or_approve,omitempty"`
Comment string `bson:"comment,omitempty" yaml:"-" json:"comment,omitempty"`
OperationTime int64 `bson:"operation_time,omitempty" yaml:"-" json:"operation_time,omitempty"`
Type string `bson:"type" yaml:"type" json:"type"`
UserID string `bson:"user_id,omitempty" yaml:"user_id,omitempty" json:"user_id,omitempty"`
UserName string `bson:"user_name,omitempty" yaml:"user_name,omitempty" json:"user_name,omitempty"`
GroupID string `bson:"group_id,omitempty" yaml:"group_id,omitempty" json:"group_id,omitempty"`
GroupName string `bson:"group_name,omitempty" yaml:"group_name,omitempty" json:"group_name,omitempty"`
RejectOrApprove config.ApprovalStatus `bson:"reject_or_approve,omitempty" yaml:"-" json:"reject_or_approve,omitempty"`
Comment string `bson:"comment,omitempty" yaml:"-" json:"comment,omitempty"`
OperationTime int64 `bson:"operation_time,omitempty" yaml:"-" json:"operation_time,omitempty"`
}

type Job struct {
Expand Down
12 changes: 6 additions & 6 deletions pkg/microservice/aslan/core/common/service/approval/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func (c *GlobalApproveManager) DoApproval(key, userName, userID, comment string,
user.Comment = comment
user.OperationTime = time.Now().Unix()
if approve {
user.RejectOrApprove = config.Approve
user.RejectOrApprove = config.ApprovalStatusApprove
meetUser = true
break
} else {
user.RejectOrApprove = config.Reject
user.RejectOrApprove = config.ApprovalStatusReject
meetUser = true
break
}
Expand All @@ -120,16 +120,16 @@ func (c *GlobalApproveManager) IsApproval(key string) (bool, bool, *commonmodels

ApproveCount := 0
for _, user := range approval.ApproveUsers {
if user.RejectOrApprove == config.Reject {
approval.RejectOrApprove = config.Reject
if user.RejectOrApprove == config.ApprovalStatusReject {
approval.RejectOrApprove = config.ApprovalStatusReject
return false, true, approval, nil
}
if user.RejectOrApprove == config.Approve {
if user.RejectOrApprove == config.ApprovalStatusApprove {
ApproveCount++
}
}
if ApproveCount >= approval.NeededApprovers {
approval.RejectOrApprove = config.Approve
approval.RejectOrApprove = config.ApprovalStatusApprove
return true, false, approval, nil
}
return false, false, approval, nil
Expand Down
51 changes: 36 additions & 15 deletions pkg/microservice/aslan/core/common/service/lark/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ const (
// ApprovalStatusNotFound not defined by lark open api, it just means not found in local manager.
ApprovalStatusNotFound = "NOTFOUND"

ApprovalStatusPending = "PENDING"
ApprovalStatusApproved = "APPROVED"
ApprovalStatusRejected = "REJECTED"
ApprovalStatusCanceled = "CANCELED"
ApprovalStatusDeleted = "DELETED"
ApprovalStatusPending = "PENDING"
ApprovalStatusApproved = "APPROVED"
ApprovalStatusRejected = "REJECTED"
ApprovalStatusTransferred = "TRANSFERRED"
ApprovalStatusDone = "DONE"
ApprovalStatusCanceled = "CANCELED"
ApprovalStatusDeleted = "DELETED"
)

type DepartmentInfo struct {
Expand Down Expand Up @@ -292,7 +294,7 @@ type ApprovalManager struct {

type UserApprovalResult struct {
Result string
ApproveOrReject config.ApproveOrReject
ApproveOrReject config.ApprovalStatus
OperationTime int64
}

Expand Down Expand Up @@ -341,6 +343,20 @@ func GetNodeUserApprovalResults(instanceID, nodeID string) map[string]*UserAppro
return approvalManager.getNodeUserApprovalResults(nodeID)
}

func GetUserApprovalResults(instanceID string) NodeUserApprovalResult {
approvalManager := GetLarkApprovalInstanceManager(instanceID)
copy := make(NodeUserApprovalResult)
for k, v := range approvalManager.NodeMap {
for k1, v1 := range v {
if _, ok := copy[k]; !ok {
copy[k] = make(map[string]*UserApprovalResult)
}
copy[k][k1] = v1
}
}
return approvalManager.NodeMap
}

func UpdateNodeUserApprovalResult(instanceID, nodeKey, nodeID, userID string, result *UserApprovalResult) {
writeKey := fmt.Sprint("lark-approval-lock-write-", instanceID)
writeMutex := cache.NewRedisLock(writeKey)
Expand Down Expand Up @@ -374,19 +390,24 @@ func (l *ApprovalManager) updateNodeUserApprovalResult(nodeID, userID string, re
if _, ok := l.NodeMap[nodeID]; !ok {
l.NodeMap[nodeID] = make(map[string]*UserApprovalResult)
}
if _, ok := l.NodeMap[nodeID][userID]; !ok && result != nil {
switch result.Result {
case ApprovalStatusApproved:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.Approve
case ApprovalStatusRejected:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.Reject
}
switch result.Result {
case ApprovalStatusApproved:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.ApprovalStatusApprove
case ApprovalStatusRejected:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.ApprovalStatusReject
case ApprovalStatusTransferred:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.ApprovalStatusRedirect
case ApprovalStatusDone:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.ApprovalStatusDone
}
return
}

// Node Custom Key => Node Key
func (l *ApprovalManager) GetNodeKeyMap() map[string]string {
m := make(map[string]string)
for k, v := range l.NodeKeyMap {
Expand Down
1 change: 1 addition & 0 deletions pkg/microservice/aslan/core/common/service/lark/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func EventHandler(appID, sign, ts, nonce, body string) (*EventHandlerResponse, e
log.Infof("get unknown callback event type %s, ignored", eventType)
return nil, nil
}
log.Debugf("event data: %s", string(callback.Event))
event := ApprovalTaskEvent{}
err = json.Unmarshal(callback.Event, &event)
if err != nil {
Expand Down
Loading
Loading