diff --git a/engine/api/workflow.go b/engine/api/workflow.go index af1aa0bdf3..f444c9e18f 100644 --- a/engine/api/workflow.go +++ b/engine/api/workflow.go @@ -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 { @@ -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) @@ -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) diff --git a/engine/api/workflow_test.go b/engine/api/workflow_test.go index f7d90e0beb..da3a2c073e 100644 --- a/engine/api/workflow_test.go +++ b/engine/api/workflow_test.go @@ -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, @@ -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) + } diff --git a/sdk/workflow_run.go b/sdk/workflow_run.go index a875cc467d..597a0cccd6 100644 --- a/sdk/workflow_run.go +++ b/sdk/workflow_run.go @@ -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