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

add approval task #3628

Merged
merged 14 commits into from
Jul 25, 2024
167 changes: 165 additions & 2 deletions pkg/cli/upgradeassistant/cmd/migrate/310.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"fmt"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"

"github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/internal/upgradepath"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
Expand All @@ -32,8 +35,6 @@ import (
"github.com/koderover/zadig/v2/pkg/tool/log"
"github.com/koderover/zadig/v2/pkg/types"
"github.com/koderover/zadig/v2/pkg/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

func init() {
Expand All @@ -60,6 +61,18 @@ func V300ToV310() error {
return err
}

log.Infof("-------- start to migrate approval config for all workflows --------")
if err := migrateApprovalForAllWorkflows(); err != nil {
log.Infof("migrate: %v", err)
return err
}

log.Infof("-------- start to migrate approval config for all user supplied workflow templates--------")
if err := migrateApprovalForAllWorkflowTemplates(); err != nil {
log.Infof("migrate: %v", err)
return err
}

return nil
}

Expand Down Expand Up @@ -277,6 +290,156 @@ func generateReleaseStat(startTime, endTime time.Time) (*statmodels.MonthlyRelea
return stat, nil
}

func migrateApprovalForAllWorkflows() error {
// list all workflows
workflows, _, err := mongodb.NewWorkflowV4Coll().List(&mongodb.ListWorkflowV4Option{}, 0, 0)

if err != nil {
log.Errorf("failed to list all custom workflows to do the migration, error: %s", err)
return fmt.Errorf("failed to list all custom workflows to do the migration, error: %s", err)
}

for _, workflow := range workflows {
newStages := make([]*models.WorkflowStage, 0)
changed := false
count := 0

for _, stage := range workflow.Stages {
if stage.Approval != nil && stage.Approval.Enabled {
changed = true

// default timeout is 60
timeout := 60
switch stage.Approval.Type {
case config.NativeApproval:
timeout = stage.Approval.NativeApproval.Timeout
case config.DingTalkApproval:
timeout = stage.Approval.DingTalkApproval.Timeout
case config.LarkApproval:
timeout = stage.Approval.LarkApproval.Timeout
case config.WorkWXApproval:
timeout = stage.Approval.WorkWXApproval.Timeout
}

approvalJob := []*models.Job{
{
Name: fmt.Sprintf("approval-%d", count),
JobType: config.JobApproval,
Skipped: false,
Spec: &models.ApprovalJobSpec{
Timeout: int64(timeout),
Type: stage.Approval.Type,
Description: stage.Approval.Description,
NativeApproval: stage.Approval.NativeApproval,
LarkApproval: stage.Approval.LarkApproval,
DingTalkApproval: stage.Approval.DingTalkApproval,
WorkWXApproval: stage.Approval.WorkWXApproval,
},
},
}

newStages = append(newStages, &models.WorkflowStage{
Name: fmt.Sprintf("approval-%d", count),
Parallel: false,
Jobs: approvalJob,
})

count++
}

newStages = append(newStages, stage)
}

// if there are approval stage in the workflow, we use the generated stages and update it
if changed {
workflow.Stages = newStages

err := mongodb.NewWorkflowV4Coll().Update(workflow.ID.Hex(), workflow)
if err != nil {
log.Errorf("failed to update workflow: %s, error: %s", workflow.Name, err)
return fmt.Errorf("failed to update workflow: %s, error: %s", workflow.Name, err)
}
}
}
return nil
}

func migrateApprovalForAllWorkflowTemplates() error {
// list all workflow templates
workflowTemplates, err := mongodb.NewWorkflowV4TemplateColl().List(&mongodb.WorkflowTemplateListOption{
ExcludeBuildIn: true,
})

if err != nil {
log.Errorf("failed to list all custom workflow templates to do the migration, error: %s", err)
return fmt.Errorf("failed to list all custom workflow templates to do the migration, error: %s", err)
}

for _, workflow := range workflowTemplates {
newStages := make([]*models.WorkflowStage, 0)
changed := false
count := 0

for _, stage := range workflow.Stages {
if stage.Approval != nil && stage.Approval.Enabled {
changed = true

// default timeout is 60
timeout := 60
switch stage.Approval.Type {
case config.NativeApproval:
timeout = stage.Approval.NativeApproval.Timeout
case config.DingTalkApproval:
timeout = stage.Approval.DingTalkApproval.Timeout
case config.LarkApproval:
timeout = stage.Approval.LarkApproval.Timeout
case config.WorkWXApproval:
timeout = stage.Approval.WorkWXApproval.Timeout
}

approvalJob := []*models.Job{
{
Name: fmt.Sprintf("approval-%d", count),
JobType: config.JobApproval,
Skipped: false,
Spec: &models.ApprovalJobSpec{
Timeout: int64(timeout),
Type: stage.Approval.Type,
Description: stage.Approval.Description,
NativeApproval: stage.Approval.NativeApproval,
LarkApproval: stage.Approval.LarkApproval,
DingTalkApproval: stage.Approval.DingTalkApproval,
WorkWXApproval: stage.Approval.WorkWXApproval,
},
},
}

newStages = append(newStages, &models.WorkflowStage{
Name: fmt.Sprintf("approval-%d", count),
Parallel: false,
Jobs: approvalJob,
})

count++
}

newStages = append(newStages, stage)
}

// if there are approval stage in the workflow, we use the generated stages and update it
if changed {
workflow.Stages = newStages

err := mongodb.NewWorkflowV4TemplateColl().Update(workflow)
if err != nil {
log.Errorf("failed to update workflow template: %s, error: %s", workflow.TemplateName, err)
return fmt.Errorf("failed to update workflow: %s, error: %s", workflow.TemplateName, err)
}
}
}
return nil
}

func migrateTestingAndScaningInfraField() error {
// change testing infrastructure field
cursor, err := mongodb.NewTestingColl().ListByCursor()
Expand Down
5 changes: 3 additions & 2 deletions pkg/microservice/aslan/config/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const (
StatusPrepare Status = "prepare"
StatusReject Status = "reject"
StatusDistributed Status = "distributed"
StatusWaitingApprove Status = "waitforapprove"
StatusWaitingApprove Status = "wait_for_approval"
StatusDebugBefore Status = "debug_before"
StatusDebugAfter Status = "debug_after"
)
Expand Down Expand Up @@ -244,6 +244,7 @@ const (
JobGuanceyunCheck JobType = "guanceyun-check"
JobGrafana JobType = "grafana"
JobBlueKing JobType = "blueking"
JobApproval JobType = "approval"
)

const (
Expand Down Expand Up @@ -545,7 +546,7 @@ type ReleasePlanStatus string

const (
StatusPlanning ReleasePlanStatus = "planning"
StatusWaitForApprove ReleasePlanStatus = "waitforapprove"
StatusWaitForApprove ReleasePlanStatus = "wait_for_approval"
StatusExecuting ReleasePlanStatus = "executing"
StatusSuccess ReleasePlanStatus = "success"
StatusCancel ReleasePlanStatus = "cancel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ type StageTask struct {
EndTime int64 `bson:"end_time" json:"end_time,omitempty"`
Parallel bool `bson:"parallel" json:"parallel,omitempty"`
ManualExec *ManualExec `bson:"manual_exec" json:"manual_exec,omitempty"`
Approval *Approval `bson:"approval" json:"approval,omitempty"`
Jobs []*JobTask `bson:"jobs" json:"jobs,omitempty"`
Error string `bson:"error" json:"error"`
}
Expand Down Expand Up @@ -137,7 +136,6 @@ type StagePreview struct {
StartTime int64 `bson:"start_time" json:"start_time,omitempty"`
EndTime int64 `bson:"end_time" json:"end_time,omitempty"`
Parallel bool `bson:"parallel" json:"parallel,omitempty"`
Approval *Approval `bson:"approval" json:"approval,omitempty"`
ManualExec *ManualExec `bson:"manual_exec" json:"manual_exec,omitempty"`
Jobs []*JobPreview `bson:"jobs" json:"jobs,omitempty"`
Error string `bson:"error" json:"error"`
Expand Down Expand Up @@ -508,6 +506,16 @@ type JobTaskBlueKingSpec struct {
InstanceName string `bson:"instance_name" json:"instance_name" yaml:"instance_name"`
}

type JobTaskApprovalSpec struct {
Timeout int64 `bson:"timeout" yaml:"timeout" json:"timeout"`
Type config.ApprovalType `bson:"type" yaml:"type" json:"type"`
Description string `bson:"description" yaml:"description" json:"description"`
NativeApproval *NativeApproval `bson:"native_approval" yaml:"native_approval,omitempty" json:"native_approval,omitempty"`
LarkApproval *LarkApproval `bson:"lark_approval" yaml:"lark_approval,omitempty" json:"lark_approval,omitempty"`
DingTalkApproval *DingTalkApproval `bson:"dingtalk_approval" yaml:"dingtalk_approval,omitempty" json:"dingtalk_approval,omitempty"`
WorkWXApproval *WorkWXApproval `bson:"workwx_approval" yaml:"workwx_approval,omitempty" json:"workwx_approval,omitempty"`
}

type JobTaskWorkflowTriggerSpec struct {
TriggerType config.WorkflowTriggerType `bson:"trigger_type" json:"trigger_type" yaml:"trigger_type"`
IsEnableCheck bool `bson:"is_enable_check" json:"is_enable_check" yaml:"is_enable_check"`
Expand Down Expand Up @@ -651,6 +659,5 @@ type WorkflowTaskCtx struct {
GlobalContextSet func(key, value string)
GlobalContextEach func(f func(k, v string) bool)
ClusterIDAdd func(clusterID string)
SetStatus func(status config.Status)
StartTime time.Time
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type WorkflowV4Template struct {
ID primitive.ObjectID `bson:"_id,omitempty" yaml:"id" json:"id"`
ID primitive.ObjectID `bson:"_id,omitempty" yaml:"id" json:"id"`
TemplateName string `bson:"template_name" yaml:"template_name" json:"template_name"`
Category setting.WorkflowCategory `bson:"category" yaml:"category" json:"category"`
KeyVals []*KeyVal `bson:"key_vals" yaml:"key_vals" json:"key_vals"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,16 @@ type BlueKingJobSpec struct {
Parameters []*blueking.GlobalVariable `bson:"parameters" json:"parameters" yaml:"parameters"`
}

type ApprovalJobSpec struct {
Timeout int64 `bson:"timeout" json:"timeout" yaml:"timeout"`
Type config.ApprovalType `bson:"type" yaml:"type" json:"type"`
Description string `bson:"description" yaml:"description" json:"description"`
NativeApproval *NativeApproval `bson:"native_approval" yaml:"native_approval,omitempty" json:"native_approval,omitempty"`
LarkApproval *LarkApproval `bson:"lark_approval" yaml:"lark_approval,omitempty" json:"lark_approval,omitempty"`
DingTalkApproval *DingTalkApproval `bson:"dingtalk_approval" yaml:"dingtalk_approval,omitempty" json:"dingtalk_approval,omitempty"`
WorkWXApproval *WorkWXApproval `bson:"workwx_approval" yaml:"workwx_approval,omitempty" json:"workwx_approval,omitempty"`
}

type JenkinsJobInfo struct {
JobName string `bson:"job_name" json:"job_name" yaml:"job_name"`
Parameters []*JenkinsJobParameter `bson:"parameters" json:"parameters" yaml:"parameters"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func initJobCtl(job *commonmodels.JobTask, workflowCtx *commonmodels.WorkflowTas
jobCtl = NewSQLJobCtl(job, workflowCtx, ack, logger)
case string(config.JobBlueKing):
jobCtl = NewBlueKingJobCtl(job, workflowCtx, ack, logger)
case string(config.JobApproval):
jobCtl = NewApprovalJobCtl(job, workflowCtx, ack, logger)
default:
jobCtl = NewFreestyleJobCtl(job, workflowCtx, ack, logger)
}
Expand Down
Loading
Loading