From 1759beb375a6aca2f8d8ecdd7c0dc019806b0670 Mon Sep 17 00:00:00 2001 From: Richard LT Date: Fri, 7 Oct 2022 14:49:16 +0200 Subject: [PATCH] fix(api): workflow push opts initialization (#6324) --- engine/api/workflow/dao.go | 27 ++++++++++----------------- engine/api/workflow_import.go | 12 ++++++------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/engine/api/workflow/dao.go b/engine/api/workflow/dao.go index 7b22ad56ed..ffb31d104d 100644 --- a/engine/api/workflow/dao.go +++ b/engine/api/workflow/dao.go @@ -1306,6 +1306,11 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj } } + var fromRepo string + if opts != nil { + fromRepo = opts.FromRepository + } + tx, err := db.Begin() if err != nil { return nil, nil, nil, nil, sdk.WrapError(err, "unable to start tx") @@ -1318,10 +1323,6 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj EnvironmentdSecrets: make(map[int64][]sdk.Variable), } for _, app := range data.Applications { - var fromRepo string - if opts != nil { - fromRepo = opts.FromRepository - } appDB, appSecrets, msgList, err := application.ParseAndImport(ctx, tx, store, *proj, &app, application.ImportOptions{Force: true, FromRepository: fromRepo}, decryptFunc, consumer) allMsg = append(allMsg, msgList...) if err != nil { @@ -1332,10 +1333,6 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj } for _, env := range data.Environments { - var fromRepo string - if opts != nil { - fromRepo = opts.FromRepository - } envDB, envsSecrets, msgList, err := environment.ParseAndImport(ctx, tx, *proj, env, environment.ImportOptions{Force: true, FromRepository: fromRepo}, decryptFunc, consumer) allMsg = append(allMsg, msgList...) if err != nil { @@ -1346,10 +1343,6 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj } for _, pip := range data.Pipelines { - var fromRepo string - if opts != nil { - fromRepo = opts.FromRepository - } pipDB, msgList, err := pipeline.ParseAndImport(ctx, tx, store, *proj, &pip, consumer, pipeline.ImportOptions{Force: true, FromRepository: fromRepo}) allMsg = append(allMsg, msgList...) if err != nil { @@ -1359,17 +1352,17 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj } isDefaultBranch := true - if opts != nil { + if opts != nil && fromRepo != "" { isDefaultBranch = opts.IsDefaultBranch } var importOptions = ImportOptions{ - Force: true, + Force: true, + FromRepository: fromRepo, + IsDefaultBranch: isDefaultBranch, } if opts != nil { - importOptions.FromRepository = opts.FromRepository - importOptions.IsDefaultBranch = opts.IsDefaultBranch importOptions.FromBranch = opts.Branch importOptions.VCSServer = opts.VCSServer importOptions.RepositoryName = opts.RepositoryName @@ -1384,7 +1377,7 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj } // If the workflow is "as-code", it should always be linked to a git repository - if opts != nil && opts.FromRepository != "" { + if fromRepo != "" { if wf.WorkflowData.Node.Context.ApplicationID == 0 { return nil, nil, nil, nil, sdk.WithStack(sdk.ErrApplicationMandatoryOnWorkflowAsCode) } diff --git a/engine/api/workflow_import.go b/engine/api/workflow_import.go index 18cd408740..7760479d4e 100644 --- a/engine/api/workflow_import.go +++ b/engine/api/workflow_import.go @@ -294,13 +294,13 @@ func (api *API) postWorkflowPushHandler() service.Handler { consumer := getAPIConsumer(ctx) - var pushOptions *workflow.PushOption + pushOptions := &workflow.PushOption{} if r.Header.Get(sdk.WorkflowAsCodeHeader) != "" { - pushOptions = &workflow.PushOption{ - FromRepository: r.Header.Get(sdk.WorkflowAsCodeHeader), - IsDefaultBranch: true, - Force: service.FormBool(r, "force"), - } + pushOptions.FromRepository = r.Header.Get(sdk.WorkflowAsCodeHeader) + pushOptions.IsDefaultBranch = true + } + if service.FormBool(r, "force") { + pushOptions.Force = true } //Load project