Skip to content

Commit

Permalink
fix(api): take groups from previous wf (#5797)
Browse files Browse the repository at this point in the history
* fix(api): take groups from previous wf

if no groups provided

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Apr 19, 2021
1 parent a4e32e5 commit c49ef96
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
18 changes: 12 additions & 6 deletions engine/api/workflow/workflow_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ImportOptions struct {
}

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

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

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

//Parse workflow
w, err := Parse(ctx, proj, ew)
w, err := Parse(ctx, proj, oldW, ew)
if err != nil {
return nil, nil, err
}
Expand Down
21 changes: 20 additions & 1 deletion engine/api/workflow_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,26 @@ func (api *API) postWorkflowPreviewHandler() service.Handler {
return sdk.NewError(sdk.ErrWrongRequest, errw)
}

wf, err := workflow.Parse(ctx, *proj, ew)
// load the workflow from database if exists
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WrapError(err, "unable to start transaction")
}
defer tx.Rollback() // nolint

workflowExists, err := workflow.Exists(tx, proj.Key, ew.GetName())
if err != nil {
return sdk.WrapError(err, "Cannot check if workflow exists")
}
var existingWorkflow *sdk.Workflow
if workflowExists {
existingWorkflow, err = workflow.Load(ctx, tx, api.Cache, *proj, ew.GetName(), workflow.LoadOptions{WithIcon: true})
if err != nil {
return sdk.WrapError(err, "unable to load existing workflow")
}
}

wf, err := workflow.Parse(ctx, *proj, existingWorkflow, ew)
if err != nil {
return sdk.WrapError(err, "unable import workflow %s", ew.GetName())
}
Expand Down
10 changes: 4 additions & 6 deletions engine/api/workflow_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ permissions:
assert.Equal(t, g2.Name, w.Groups[1].Group.Name)
assert.Equal(t, sdk.PermissionRead, w.Groups[1].Permission)

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

require.Len(t, w.Groups, 3)
require.Len(t, w.Groups, 2)
sort.Slice(w.Groups, func(i, j int) bool {
return w.Groups[i].Group.Name < w.Groups[j].Group.Name
})
assert.Equal(t, proj.ProjectGroups[0].Group.Name, w.Groups[0].Group.Name)
assert.Equal(t, sdk.PermissionReadWriteExecute, w.Groups[0].Permission)
assert.Equal(t, g1.Name, w.Groups[1].Group.Name)
assert.Equal(t, sdk.PermissionReadExecute, w.Groups[1].Permission)
assert.Equal(t, g2.Name, w.Groups[2].Group.Name)
assert.Equal(t, sdk.PermissionReadExecute, w.Groups[2].Permission)
assert.Equal(t, g2.Name, w.Groups[1].Group.Name)
assert.Equal(t, sdk.PermissionRead, w.Groups[1].Permission)
}

0 comments on commit c49ef96

Please sign in to comment.