Skip to content

Commit

Permalink
support redirect in dingtalk and lark approval
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Dec 27, 2024
1 parent 431091a commit 5564b1a
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 79 deletions.
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
8 changes: 5 additions & 3 deletions pkg/microservice/aslan/config/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,13 @@ const (
SAEBatchReleaseTypeManual = "manual"
)

type ApproveOrReject string
type ApprovalStatus string

const (
Approve ApproveOrReject = "approve"
Reject ApproveOrReject = "reject"
Approve ApprovalStatus = "approve"
Reject ApprovalStatus = "reject"
Redirect ApprovalStatus = "redirect"
Done 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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ func EventHandler(appKey string, body []byte, signature, ts, nonce string) (*Eve
eventType := gjson.Get(data, "EventType").String()
log.Infof("receive dingtalk event type: %s instanceID: %s", eventType,
gjson.Get(data, "processInstanceId").String())
log.Debugf("receive dingtalk event data: %s", data)

switch eventType {
case EventTaskChange:
Expand Down
21 changes: 15 additions & 6 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 @@ -382,11 +384,18 @@ func (l *ApprovalManager) updateNodeUserApprovalResult(nodeID, userID string, re
case ApprovalStatusRejected:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.Reject
case ApprovalStatusTransferred:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.Redirect
case ApprovalStatusDone:
l.NodeMap[nodeID][userID] = result
result.ApproveOrReject = config.Done
}
}
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

0 comments on commit 5564b1a

Please sign in to comment.