Navigation Menu

Skip to content

Commit

Permalink
feat(api): return workflow runs URLs (#5220)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <francois.samin@corp.ovh.com>
  • Loading branch information
fsamin committed Jun 1, 2020
1 parent ed15f15 commit e5876a9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
20 changes: 14 additions & 6 deletions engine/api/workflow.go
Expand Up @@ -75,14 +75,24 @@ func (api *API) getWorkflowsHandler() service.Handler {
}

w1 := &ws[i]
w1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowHandler, map[string]string{"key": w1.ProjectKey, "permWorkflowName": w1.Name})
w1.URLs.UIURL = api.Config.URL.UI + "/project/" + w1.ProjectKey + "/workflow/" + w1.Name
api.setWorkflowURLs(w1)
}

return service.WriteJSON(w, ws, http.StatusOK)
}
}

func (api *API) setWorkflowURLs(w1 *sdk.Workflow) {
w1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowHandler, map[string]string{"key": w1.ProjectKey, "permWorkflowName": w1.Name})
w1.URLs.UIURL = api.Config.URL.UI + "/project/" + w1.ProjectKey + "/workflow/" + w1.Name

for j := range w1.Runs {
r1 := &w1.Runs[j]
r1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{"key": w1.ProjectKey, "permWorkflowName": w1.Name, "number": strconv.FormatInt(r1.Number, 10)})
r1.URLs.UIURL = api.Config.URL.UI + "/project/" + w1.ProjectKey + "/workflow/" + w1.Name + "/run/" + strconv.FormatInt(r1.Number, 10)
}
}

// getWorkflowHandler returns a full workflow
func (api *API) getWorkflowHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
Expand Down Expand Up @@ -147,8 +157,7 @@ func (api *API) getWorkflowHandler() service.Handler {
}
}

w1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowHandler, map[string]string{"key": key, "permWorkflowName": w1.Name})
w1.URLs.UIURL = api.Config.URL.UI + "/project/" + key + "/workflow/" + w1.Name
api.setWorkflowURLs(w1)

//We filter project and workflow configuration key, because they are always set on insertHooks
w1.FilterHooksConfig(sdk.HookConfigProject, sdk.HookConfigWorkflow)
Expand Down Expand Up @@ -807,8 +816,7 @@ func (api *API) getSearchWorkflowHandler() service.Handler {
}

w1 := &ws[i]
w1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowHandler, map[string]string{"key": w1.ProjectKey, "permWorkflowName": w1.Name})
w1.URLs.UIURL = api.Config.URL.UI + "/project/" + w1.ProjectKey + "/workflow/" + w1.Name
api.setWorkflowURLs(w1)
}

return service.WriteJSON(w, ws, http.StatusOK)
Expand Down
20 changes: 20 additions & 0 deletions engine/api/workflow_test.go
Expand Up @@ -1959,6 +1959,20 @@ func Test_getSearchWorkflowHandler(t *testing.T) {
}
test.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj, &wf2))

// Run the workflow
consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), api.mustDB(), sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
wr, err := workflow.CreateRun(api.mustDB(), &wf, nil, admin)
assert.NoError(t, err)
wr.Workflow = wf
wr.Tag("git.branch", "master")
_, err = workflow.StartWorkflowRun(context.TODO(), api.mustDB(), api.Cache, *proj, wr, &sdk.WorkflowRunPostHandlerOption{
Manual: &sdk.WorkflowNodeRunManual{
Username: u.GetUsername(),
Payload: `{"git.branch": "master"}`,
},
}, consumer, nil)
require.NoError(t, err)

// Call with an admin
sdkclientAdmin := cdsclient.New(cdsclient.Config{
Host: tsURL,
Expand All @@ -1976,4 +1990,10 @@ func Test_getSearchWorkflowHandler(t *testing.T) {
require.NotEmpty(t, wfs[0].URLs.UIURL)
require.Equal(t, app.ID, wfs[0].WorkflowData.Node.Context.ApplicationID)
require.Equal(t, pip.ID, wfs[0].WorkflowData.Node.Context.PipelineID)
require.NotEmpty(t, wfs[0].Runs)
require.NotEmpty(t, wfs[0].Runs[0].URLs.APIURL)
require.NotEmpty(t, wfs[0].Runs[0].URLs.UIURL)

t.Logf("%+v", wfs[0].Runs[0].URLs)

}
1 change: 1 addition & 0 deletions sdk/workflow_run.go
Expand Up @@ -52,6 +52,7 @@ type WorkflowRun struct {
ToDelete bool `json:"to_delete" db:"to_delete" cli:"-"`
JoinTriggersRun map[int64]WorkflowNodeTriggerRun `json:"join_triggers_run,omitempty" db:"-"`
Header WorkflowRunHeaders `json:"header,omitempty" db:"-"`
URLs URL `json:"urls" yaml:"-" db:"-" cli:"-"`
}

// WorkflowNodeRunRelease represents the request struct use by release builtin action for workflow
Expand Down

0 comments on commit e5876a9

Please sign in to comment.