Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/v2/pkg/setting"
"github.com/koderover/zadig/v2/pkg/tool/log"
mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo"
)

Expand Down Expand Up @@ -265,6 +266,95 @@ func (c *WorkflowV4Coll) List(opt *ListWorkflowV4Option, pageNum, pageSize int64
return resp, count, nil
}

type ListWorkflowV4InGlobalOption struct {
Keyword string
ProjectName string
ProjectNames []string
FavoriteWorkflowNames []string
CollModeWorkflowNames []string
PageNum int64
PageSize int64
SortBy setting.ListWorkflowV4InGlobalSortBy
OrderBy setting.ListWorkflowV4InGlobalOrderBy
}

func (c *WorkflowV4Coll) ListInGlobal(opt *ListWorkflowV4InGlobalOption) ([]*models.WorkflowV4, int64, error) {
resp := make([]*models.WorkflowV4, 0)
query := bson.M{}

// 构建查询条件
var conditions []bson.M

// 如果存在 FavoriteWorkflowNames,只查询这些工作流,忽略其他条件
if len(opt.FavoriteWorkflowNames) > 0 {
conditions = append(conditions, bson.M{"name": bson.M{"$in": opt.FavoriteWorkflowNames}})
} else {
// 项目条件
if opt.ProjectName != "" {
conditions = append(conditions, bson.M{"project": opt.ProjectName})
} else if len(opt.ProjectNames) > 0 {
conditions = append(conditions, bson.M{"project": bson.M{"$in": opt.ProjectNames}})
}
// 协作模式工作流条件
if len(opt.CollModeWorkflowNames) > 0 {
conditions = append(conditions, bson.M{"name": bson.M{"$in": opt.CollModeWorkflowNames}})
}
}

// 关键字搜索条件
if opt.Keyword != "" {
keywordRegex := bson.M{"$regex": opt.Keyword, "$options": "i"}
conditions = append(conditions, bson.M{
"$or": bson.A{
bson.M{"name": keywordRegex},
bson.M{"display_name": keywordRegex},
},
})
}

// 构建最终查询
if len(conditions) == 1 {
query = conditions[0]
} else if len(conditions) > 1 {
query = bson.M{"$and": conditions}
}

count, err := c.CountDocuments(context.TODO(), query)
if err != nil {
return nil, count, err
}

var findOption *options.FindOptions
if opt.PageSize == 0 {
opt.PageSize = 10
}
if opt.PageNum == 0 {
opt.PageNum = 1
}

findOption = options.Find().
SetSkip((opt.PageNum - 1) * opt.PageSize).
SetLimit(opt.PageSize)

if opt.SortBy != "" && opt.OrderBy != 0 {
findOption.SetSort(bson.D{
bson.E{Key: string(opt.SortBy), Value: opt.OrderBy},
})
}

log.Debugf("query: %+v", query)

cursor, err := c.Collection.Find(context.TODO(), query, findOption)
if err != nil {
return nil, count, err
}
err = cursor.All(context.TODO(), &resp)
if err != nil {
return nil, count, err
}
return resp, count, nil
}

func (c *WorkflowV4Coll) Find(name string) (*models.WorkflowV4, error) {
resp := new(models.WorkflowV4)
query := bson.M{"name": name}
Expand Down
1 change: 1 addition & 0 deletions pkg/microservice/aslan/core/workflow/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (*Router) Inject(router *gin.RouterGroup) {
workflowV4.POST("/workflowtask/:workflowName/field", SetWorkflowTasksCustomFields)
workflowV4.GET("/workflowtask/:workflowName/field", GetWorkflowTasksCustomFields)
workflowV4.GET("", ListWorkflowV4)
workflowV4.GET("/global", ListGlobalWorkflowV4)
workflowV4.POST("/auto", AutoCreateWorkflow)
workflowV4.GET("/trigger", ListWorkflowV4CanTrigger)
workflowV4.POST("/lint", LintWorkflowV4)
Expand Down
111 changes: 111 additions & 0 deletions pkg/microservice/aslan/core/workflow/handler/workflow_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/workflow/service/workflow"
"github.com/koderover/zadig/v2/pkg/shared/client/user"
internalhandler "github.com/koderover/zadig/v2/pkg/shared/handler"
"github.com/koderover/zadig/v2/pkg/tool/errors"
e "github.com/koderover/zadig/v2/pkg/tool/errors"
Expand Down Expand Up @@ -312,6 +313,116 @@ func ListWorkflowV4(c *gin.Context) {
ctx.RespErr = err
}

type ListGlobalWorkflowV4Request struct {
ProjectName string `json:"projectName" form:"projectName"`
IsFavorite bool `json:"isFavorite" form:"isFavorite,default=false"`
Keyword string `json:"keyword" form:"keyword"`
PageSize int64 `json:"pageSize" form:"pageSize,default=10"`
PageNum int64 `json:"pageNum" form:"pageNum,default=1"`
SortBy setting.ListWorkflowV4InGlobalSortBy `json:"sortBy" form:"sortBy" binding:"omitempty,oneof=create_time name"`
OrderBy setting.ListWorkflowV4InGlobalOrderBy `json:"orderBy" form:"orderBy" binding:"omitempty,oneof=1 -1"`
}

// @summary 全局工作流列表
// @description
// @tags workflow
// @accept json
// @produce json
// @Param projectName query string false "项目标识"
// @Param isFavorite query bool false "是否是收藏"
// @Param keyword query string false "关键字搜索"
// @Param pageSize query int true "每页条数"
// @Param pageNum query int true "页码"
// @Param sortBy query setting.ListWorkflowV4InGlobalSortBy false "排序字段"
// @Param orderBy query setting.ListWorkflowV4InGlobalOrderBy false "排序方式"
// @success 200 {object} workflow.ListGlobalWorkflowV4Response
// @router /api/aslan/workflow/v4/global [get]
func ListGlobalWorkflowV4(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
if err != nil {
ctx.RespErr = fmt.Errorf("authorization Info Generation failed: err %s", err)
ctx.UnAuthorized = true
return
}

args := &ListGlobalWorkflowV4Request{}
if err := c.ShouldBindQuery(args); err != nil {
ctx.RespErr = err
return
}

collModeWorkflowsWithVerb, err := internalhandler.ListAuthorizedWorkflowWithVerb(ctx.UserID, args.ProjectName)
if err != nil {
ctx.Logger.Errorf("failed to list collaboration mode authorized workflow resource, error: %s", err)
ctx.RespErr = err
return
}

bytes, _ := json.Marshal(collModeWorkflowsWithVerb)
log.Debugf("collModeWorkflowsWithVerb: %s", string(bytes))

query := &workflow.ListGlobalWorkflowV4Query{
ProjectName: args.ProjectName,
IsFavorite: args.IsFavorite,
Keyword: args.Keyword,
ProjectAuthMap: make(map[string]*workflow.ProjectAuthWorkflow),
PageNum: args.PageNum,
PageSize: args.PageSize,
SortBy: args.SortBy,
OrderBy: args.OrderBy,
}

for projectName, project := range ctx.Resources.ProjectAuthInfo {
if args.ProjectName != "" && projectName != args.ProjectName {
continue
}

var authWorkflow *workflow.ProjectAuthWorkflow
if project.IsProjectAdmin || project.Workflow.View {
authWorkflow = &workflow.ProjectAuthWorkflow{
ProjectName: projectName,
IsProjectAdmin: project.IsProjectAdmin,
Actions: project.Workflow,
CollModeWorkflowPermsMap: make(map[string]*workflow.WorkflowWithAction),
}
}

if collModeWorkflowsWithVerb.ProjectWorkflowActionsMap[projectName] != nil {
if authWorkflow == nil {
authWorkflow = &workflow.ProjectAuthWorkflow{
IsProjectAdmin: false,
Actions: &user.WorkflowActions{
View: false,
Edit: false,
Create: false,
Delete: false,
Execute: false,
},
ProjectName: projectName,
CollModeWorkflowPermsMap: make(map[string]*workflow.WorkflowWithAction),
}
}

for workflowName, workflowAction := range collModeWorkflowsWithVerb.ProjectWorkflowActionsMap[projectName] {
authWorkflow.CollModeWorkflowPermsMap[workflowName] = &workflow.WorkflowWithAction{
WorkflowName: workflowName,
Action: *workflowAction,
}
}
}

if authWorkflow != nil {
query.ProjectAuthMap[projectName] = authWorkflow
}
}

bytes, _ = json.Marshal(query)
log.Debugf("query: %s", string(bytes))

ctx.Resp, ctx.RespErr = workflow.ListWorkflowV4InGlobal(ctx, query)
}

func ListWorkflowV4CanTrigger(c *gin.Context) {
ctx, err := internalhandler.NewContextWithAuthorization(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()
Expand Down
Loading