Skip to content

Commit eb3c114

Browse files
authored
feat(sdk): add version 2 of workflow export entities (#5021)
1 parent b1b927a commit eb3c114

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3031
-1493
lines changed

cli/cdsctl/workflow_init.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,7 @@ func interactiveChoosePipeline(pkey, defaultPipeline string) (string, *sdk.Pipel
243243

244244
func craftWorkflowFile(workflowName, appName, pipName, destinationDir string) (string, error) {
245245
// Crafting the workflow
246-
wkflw := exportentities.Workflow{
247-
Version: exportentities.WorkflowVersion1,
248-
Name: workflowName,
249-
ApplicationName: appName,
250-
PipelineName: pipName,
251-
}
252-
246+
wkflw := exportentities.InitWorkflow(workflowName, appName, pipName)
253247
b, err := exportentities.Marshal(wkflw, exportentities.FormatYAML)
254248
if err != nil {
255249
return "", fmt.Errorf("Unable to write workflow file format: %v", err)

engine/api/templates.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,21 +353,24 @@ func (api *API) applyTemplate(ctx context.Context, u sdk.Identifiable, p *sdk.Pr
353353
// parse the generated workflow to find its name an update it in instance if not detached
354354
// also set the template path in generated workflow if not detached
355355
if !req.Detached {
356-
var wor exportentities.Workflow
357-
if err := yaml.Unmarshal([]byte(result.Workflow), &wor); err != nil {
356+
wor, err := exportentities.UnmarshalWorkflow([]byte(result.Workflow))
357+
if err != nil {
358358
return result, sdk.NewError(sdk.Error{
359359
ID: sdk.ErrWrongRequest.ID,
360360
Message: "Cannot parse generated workflow",
361361
}, err)
362362
}
363363

364-
wti.WorkflowName = wor.Name
364+
wti.WorkflowName = wor.GetName()
365365
if err := workflowtemplate.UpdateInstance(tx, wti); err != nil {
366366
return result, err
367367
}
368368

369369
templatePath := fmt.Sprintf("%s/%s", wt.Group.Name, wt.Slug)
370-
wor.Template = &templatePath
370+
wor, err = exportentities.SetTemplate(wor, templatePath)
371+
if err != nil {
372+
return result, err
373+
}
371374
b, err := yaml.Marshal(wor)
372375
if err != nil {
373376
return result, sdk.NewError(sdk.Error{
@@ -455,7 +458,7 @@ func (api *API) postTemplateApplyHandler() service.Handler {
455458
log.Debug("postTemplateApplyHandler> template %s applied (withImport=%v)", wt.Slug, withImport)
456459

457460
buf := new(bytes.Buffer)
458-
if err := workflowtemplate.Tar(ctx, wt, res, buf); err != nil {
461+
if err := workflowtemplate.Tar(ctx, res, buf); err != nil {
459462
return err
460463
}
461464

@@ -607,7 +610,7 @@ func (api *API) postTemplateBulkHandler() service.Handler {
607610
}
608611

609612
buf := new(bytes.Buffer)
610-
if err := workflowtemplate.Tar(ctx, wt, res, buf); err != nil {
613+
if err := workflowtemplate.Tar(ctx, res, buf); err != nil {
611614
if errD := errorDefer(err); errD != nil {
612615
log.Error(ctx, "%v", errD)
613616
return

engine/api/templates_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func generateTemplate(groupID int64, pipelineName string) *sdk.WorkflowTemplate
2727
Slug: slug.Convert(name),
2828
Workflow: base64.StdEncoding.EncodeToString([]byte(
2929
`name: [[.name]]
30-
version: v1.0
30+
version: v2.0
3131
workflow:
3232
Node-1:
3333
pipeline: ` + pipelineName,
@@ -150,7 +150,7 @@ func Test_postTemplateBulkHandler(t *testing.T) {
150150
Slug: slug.Convert(name),
151151
Workflow: base64.StdEncoding.EncodeToString([]byte(
152152
`name: [[.name]]
153-
version: v1.0
153+
version: v2.0
154154
workflow:
155155
Node-1:
156156
pipeline: ` + pipelineName,

engine/api/user_schema.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
v2 "github.com/ovh/cds/sdk/exportentities/v2"
78
"net/http"
89
"reflect"
910

@@ -30,7 +31,7 @@ func (api *API) getUserJSONSchema() service.Handler {
3031

3132
var sch *jsonschema.Schema
3233
if filter == "" || filter == "workflow" {
33-
sch = ref.ReflectFromType(reflect.TypeOf(exportentities.Workflow{}))
34+
sch = ref.ReflectFromType(reflect.TypeOf(v2.Workflow{}))
3435
buf, _ := json.Marshal(sch)
3536
res.Workflow = string(buf)
3637
}

engine/api/workflow.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212

1313
"github.com/go-gorp/gorp"
1414
"github.com/gorilla/mux"
15-
yaml "gopkg.in/yaml.v2"
16-
1715
"github.com/ovh/cds/engine/api/application"
1816
"github.com/ovh/cds/engine/api/environment"
1917
"github.com/ovh/cds/engine/api/event"
@@ -210,8 +208,8 @@ func (api *API) postWorkflowRollbackHandler() service.Handler {
210208
return sdk.WrapError(err, "cannot load workflow audit %s/%s", key, workflowName)
211209
}
212210

213-
var exportWf exportentities.Workflow
214-
if err := yaml.Unmarshal([]byte(audit.DataBefore), &exportWf); err != nil {
211+
exportWf, err := exportentities.UnmarshalWorkflow([]byte(audit.DataBefore))
212+
if err != nil {
215213
return sdk.WrapError(err, "cannot unmarshal data before")
216214
}
217215

@@ -223,9 +221,9 @@ func (api *API) postWorkflowRollbackHandler() service.Handler {
223221
_ = tx.Rollback()
224222
}()
225223

226-
newWf, _, err := workflow.ParseAndImport(ctx, tx, api.Cache, *proj, wf, &exportWf, u, workflow.ImportOptions{Force: true, WorkflowName: workflowName})
227-
if err != nil {
228-
return sdk.WrapError(err, "cannot parse and import previous workflow")
224+
newWf, _, errP := workflow.ParseAndImport(ctx, tx, api.Cache, *proj, wf, exportWf, u, workflow.ImportOptions{Force: true, WorkflowName: workflowName})
225+
if errP != nil {
226+
return sdk.WrapError(errP, "cannot parse and import previous workflow")
229227
}
230228

231229
if err := tx.Commit(); err != nil {

engine/api/workflow/as_code.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/base64"
77
"fmt"
8+
v2 "github.com/ovh/cds/sdk/exportentities/v2"
89
"time"
910

1011
"github.com/go-gorp/gorp"
@@ -26,7 +27,7 @@ func UpdateWorkflowAsCode(ctx context.Context, store cache.Store, db gorp.SqlExe
2627

2728
var wp exportentities.WorkflowPulled
2829
buffw := new(bytes.Buffer)
29-
if _, err := exportWorkflow(ctx, wf, exportentities.FormatYAML, buffw, exportentities.WorkflowSkipIfOnlyOneRepoWebhook); err != nil {
30+
if _, err := exportWorkflow(ctx, wf, exportentities.FormatYAML, buffw, v2.WorkflowSkipIfOnlyOneRepoWebhook); err != nil {
3031
return nil, sdk.WrapError(err, "unable to export workflow")
3132
}
3233
wp.Workflow.Name = wf.Name
@@ -47,7 +48,7 @@ func MigrateAsCode(ctx context.Context, db *gorp.DbMap, store cache.Store, proj
4748
}
4849

4950
// Export workflow
50-
pull, err := Pull(ctx, db, store, proj, wf.Name, exportentities.FormatYAML, encryptFunc, exportentities.WorkflowSkipIfOnlyOneRepoWebhook)
51+
pull, err := Pull(ctx, db, store, proj, wf.Name, exportentities.FormatYAML, encryptFunc, v2.WorkflowSkipIfOnlyOneRepoWebhook)
5152
if err != nil {
5253
return nil, sdk.WrapError(err, "cannot pull workflow")
5354
}

engine/api/workflow/dao.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ func checkApplication(store cache.Store, db gorp.SqlExecutor, proj sdk.Project,
13291329
if n.Context.ApplicationName != "" {
13301330
appDB, err := application.LoadByName(db, store, proj.Key, n.Context.ApplicationName, application.LoadOptions.WithDeploymentStrategies, application.LoadOptions.WithVariables)
13311331
if err != nil {
1332-
if sdk.ErrorIs(err, sdk.ErrPipelineNotFound) {
1332+
if sdk.ErrorIs(err, sdk.ErrApplicationNotFound) {
13331333
return sdk.WithStack(sdk.ErrorWithData(sdk.ErrApplicationNotFound, n.Context.ApplicationName))
13341334
}
13351335
return sdk.WrapError(err, "unable to load application %s", n.Context.ApplicationName)
@@ -1390,12 +1390,13 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj
13901390
oldWf = opts.OldWorkflow
13911391
} else {
13921392
// load the workflow from database if exists
1393-
workflowExists, err = Exists(db, proj.Key, data.wrkflw.Name)
1393+
workflowExists, err = Exists(db, proj.Key, data.wrkflw.GetName())
13941394
if err != nil {
13951395
return nil, nil, nil, sdk.WrapError(err, "Cannot check if workflow exists")
13961396
}
13971397
if workflowExists {
1398-
oldWf, err = Load(ctx, db, store, *proj, data.wrkflw.Name, LoadOptions{WithIcon: true})
1398+
oldWf, err = Load(ctx, db, store, *proj, data.wrkflw.GetName(), LoadOptions{WithIcon: true})
1399+
13991400
if err != nil {
14001401
return nil, nil, nil, sdk.WrapError(err, "Unable to load existing workflow")
14011402
}
@@ -1477,9 +1478,9 @@ func Push(ctx context.Context, db *gorp.DbMap, store cache.Store, proj *sdk.Proj
14771478
importOptions.HookUUID = opts.HookUUID
14781479
}
14791480

1480-
wf, msgList, err := ParseAndImport(ctx, tx, store, *proj, oldWf, &data.wrkflw, u, importOptions)
1481+
wf, msgList, err := ParseAndImport(ctx, tx, store, *proj, oldWf, data.wrkflw, u, importOptions)
14811482
if err != nil {
1482-
return msgList, nil, nil, sdk.WrapError(err, "unable to import workflow %s", data.wrkflw.Name)
1483+
return msgList, nil, nil, sdk.WrapError(err, "unable to import workflow %s", data.wrkflw.GetName())
14831484
}
14841485

14851486
// If the workflow is "as-code", it should always be linked to a git repository

engine/api/workflow/dao_run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func UpdateWorkflowRun(ctx context.Context, db gorp.SqlExecutor, wr *sdk.Workflo
6363

6464
wr.LastModified = time.Now()
6565
for _, info := range wr.Infos {
66-
if info.IsError && info.SubNumber == wr.LastSubNumber {
67-
wr.Status = string(sdk.StatusFail)
66+
if info.Type == sdk.RunInfoTypeError && info.SubNumber == wr.LastSubNumber {
67+
wr.Status = sdk.StatusFail
6868
}
6969
}
7070

engine/api/workflow/execute_node_run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,10 @@ func executeNodeRun(ctx context.Context, db gorp.SqlExecutor, store cache.Store,
347347
log.Error(ctx, "workflow.execute> Unable to load mutex-locked workflow rnode un: %v", errWRun)
348348
return report, nil
349349
}
350-
AddWorkflowRunInfo(workflowRun, false, sdk.SpawnMsg{
350+
AddWorkflowRunInfo(workflowRun, sdk.SpawnMsg{
351351
ID: sdk.MsgWorkflowNodeMutexRelease.ID,
352352
Args: []interface{}{waitingRun.WorkflowNodeName},
353+
Type: sdk.MsgWorkflowNodeMutexRelease.Type,
353354
})
354355

355356
if err := UpdateWorkflowRun(ctx, db, workflowRun); err != nil {

engine/api/workflow/process.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ func checkCondition(ctx context.Context, wr *sdk.WorkflowRun, conditions sdk.Wor
5151
luacheck, err := luascript.NewCheck()
5252
if err != nil {
5353
log.Warning(ctx, "processWorkflowNodeRun> WorkflowCheckConditions error: %s", err)
54-
AddWorkflowRunInfo(wr, true, sdk.SpawnMsg{
54+
AddWorkflowRunInfo(wr, sdk.SpawnMsg{
5555
ID: sdk.MsgWorkflowError.ID,
5656
Args: []interface{}{fmt.Sprintf("Error init LUA System: %v", err)},
57+
Type: sdk.MsgWorkflowError.Type,
5758
})
5859
}
5960
luacheck.SetVariables(sdk.ParametersToMap(params))
@@ -62,22 +63,23 @@ func checkCondition(ctx context.Context, wr *sdk.WorkflowRun, conditions sdk.Wor
6263
}
6364
if errc != nil {
6465
log.Warning(ctx, "processWorkflowNodeRun> WorkflowCheckConditions error: %s", errc)
65-
AddWorkflowRunInfo(wr, true, sdk.SpawnMsg{
66+
AddWorkflowRunInfo(wr, sdk.SpawnMsg{
6667
ID: sdk.MsgWorkflowError.ID,
6768
Args: []interface{}{fmt.Sprintf("Error on LUA Condition: %v", errc)},
69+
Type: sdk.MsgWorkflowError.Type,
6870
})
6971
return false
7072
}
7173
return conditionsOK
7274
}
7375

7476
// AddWorkflowRunInfo add WorkflowRunInfo on a WorkflowRun
75-
func AddWorkflowRunInfo(run *sdk.WorkflowRun, isError bool, infos ...sdk.SpawnMsg) {
77+
func AddWorkflowRunInfo(run *sdk.WorkflowRun, infos ...sdk.SpawnMsg) {
7678
for _, i := range infos {
7779
run.Infos = append(run.Infos, sdk.WorkflowRunInfo{
7880
APITime: time.Now(),
7981
Message: i,
80-
IsError: isError,
82+
Type: i.Type,
8183
SubNumber: run.LastSubNumber,
8284
})
8385
}

0 commit comments

Comments
 (0)