Skip to content

Commit c49ef96

Browse files
authored
fix(api): take groups from previous wf (#5797)
* fix(api): take groups from previous wf if no groups provided Signed-off-by: Yvonnick Esnault <[email protected]>
1 parent a4e32e5 commit c49ef96

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

engine/api/workflow/workflow_parser.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type ImportOptions struct {
2727
}
2828

2929
// Parse parse an exportentities.workflow and return the parsed workflow
30-
func Parse(ctx context.Context, proj sdk.Project, ew exportentities.Workflow) (*sdk.Workflow, error) {
30+
func Parse(ctx context.Context, proj sdk.Project, oldW *sdk.Workflow, ew exportentities.Workflow) (*sdk.Workflow, error) {
3131
log.Info(ctx, "Parse>> Parse workflow %s in project %s", ew.GetName(), proj.Key)
3232
log.Debug(ctx, "Parse>> Workflow: %+v", ew)
3333

@@ -39,11 +39,17 @@ func Parse(ctx context.Context, proj sdk.Project, ew exportentities.Workflow) (*
3939
w.ProjectID = proj.ID
4040
w.ProjectKey = proj.Key
4141

42-
// Get permission from project if needed
43-
if len(w.Groups) == 0 {
42+
// Get permission from old workflow if needed
43+
if oldW != nil && len(w.Groups) == 0 {
44+
w.Groups = make([]sdk.GroupPermission, 0, len(oldW.Groups))
45+
for _, g := range oldW.Groups {
46+
perm := sdk.GroupPermission{Group: sdk.Group{Name: g.Group.Name}, Permission: g.Permission}
47+
w.Groups = append(w.Groups, perm)
48+
}
49+
} else if len(w.Groups) == 0 {
4450
w.Groups = make([]sdk.GroupPermission, 0, len(proj.ProjectGroups))
45-
for _, gp := range proj.ProjectGroups {
46-
perm := sdk.GroupPermission{Group: sdk.Group{Name: gp.Group.Name}, Permission: gp.Permission}
51+
for _, g := range proj.ProjectGroups {
52+
perm := sdk.GroupPermission{Group: sdk.Group{Name: g.Group.Name}, Permission: g.Permission}
4753
w.Groups = append(w.Groups, perm)
4854
}
4955
}
@@ -58,7 +64,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store
5864
log.Info(ctx, "ParseAndImport>> Import workflow %s in project %s (force=%v)", ew.GetName(), proj.Key, opts.Force)
5965

6066
//Parse workflow
61-
w, err := Parse(ctx, proj, ew)
67+
w, err := Parse(ctx, proj, oldW, ew)
6268
if err != nil {
6369
return nil, nil, err
6470
}

engine/api/workflow_import.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,26 @@ func (api *API) postWorkflowPreviewHandler() service.Handler {
5858
return sdk.NewError(sdk.ErrWrongRequest, errw)
5959
}
6060

61-
wf, err := workflow.Parse(ctx, *proj, ew)
61+
// load the workflow from database if exists
62+
tx, err := api.mustDB().Begin()
63+
if err != nil {
64+
return sdk.WrapError(err, "unable to start transaction")
65+
}
66+
defer tx.Rollback() // nolint
67+
68+
workflowExists, err := workflow.Exists(tx, proj.Key, ew.GetName())
69+
if err != nil {
70+
return sdk.WrapError(err, "Cannot check if workflow exists")
71+
}
72+
var existingWorkflow *sdk.Workflow
73+
if workflowExists {
74+
existingWorkflow, err = workflow.Load(ctx, tx, api.Cache, *proj, ew.GetName(), workflow.LoadOptions{WithIcon: true})
75+
if err != nil {
76+
return sdk.WrapError(err, "unable to load existing workflow")
77+
}
78+
}
79+
80+
wf, err := workflow.Parse(ctx, *proj, existingWorkflow, ew)
6281
if err != nil {
6382
return sdk.WrapError(err, "unable import workflow %s", ew.GetName())
6483
}

engine/api/workflow_import_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ permissions:
10811081
assert.Equal(t, g2.Name, w.Groups[1].Group.Name)
10821082
assert.Equal(t, sdk.PermissionRead, w.Groups[1].Permission)
10831083

1084-
// Import again the workflow without permissions should reset to project permissions
1084+
// Import again the workflow without permissions should reset to previous workflow permissions
10851085
uri = api.Router.GetRoute("POST", api.postWorkflowImportHandler, map[string]string{
10861086
"permProjectKey": proj.Key,
10871087
})
@@ -1106,14 +1106,12 @@ workflow:
11061106
w, err = workflow.Load(context.TODO(), db, api.Cache, *proj, "test_1", workflow.LoadOptions{})
11071107
require.NoError(t, err)
11081108

1109-
require.Len(t, w.Groups, 3)
1109+
require.Len(t, w.Groups, 2)
11101110
sort.Slice(w.Groups, func(i, j int) bool {
11111111
return w.Groups[i].Group.Name < w.Groups[j].Group.Name
11121112
})
11131113
assert.Equal(t, proj.ProjectGroups[0].Group.Name, w.Groups[0].Group.Name)
11141114
assert.Equal(t, sdk.PermissionReadWriteExecute, w.Groups[0].Permission)
1115-
assert.Equal(t, g1.Name, w.Groups[1].Group.Name)
1116-
assert.Equal(t, sdk.PermissionReadExecute, w.Groups[1].Permission)
1117-
assert.Equal(t, g2.Name, w.Groups[2].Group.Name)
1118-
assert.Equal(t, sdk.PermissionReadExecute, w.Groups[2].Permission)
1115+
assert.Equal(t, g2.Name, w.Groups[1].Group.Name)
1116+
assert.Equal(t, sdk.PermissionRead, w.Groups[1].Permission)
11191117
}

0 commit comments

Comments
 (0)