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

fix(api): take groups from previous wf #5797

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}