Skip to content

Commit

Permalink
feat(api): get workflows by repository (#5141)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Apr 22, 2020
1 parent 06dc738 commit d06e020
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 16 deletions.
23 changes: 23 additions & 0 deletions engine/api/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,35 @@ func (api *API) getWorkflowsHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
key := vars[permProjectKey]
filterByRepo := r.FormValue("repo")

ws, err := workflow.LoadAll(api.mustDB(), key)
if err != nil {
return err
}

if filterByRepo != "" {
mapApps := make(map[int64]sdk.Application)
apps, err := application.LoadAll(api.mustDB(), key)
if err != nil {
return err
}

for _, app := range apps {
mapApps[app.ID] = app
}

ws = ws.Filter(
func(w sdk.Workflow) bool {
if w.WorkflowData.Node.Context != nil {
app, _ := mapApps[w.WorkflowData.Node.Context.ApplicationID]
return app.RepositoryFullname == filterByRepo
}
return false
},
)
}

names := ws.Names()
perms, err := permission.LoadWorkflowMaxLevelPermission(ctx, api.mustDB(), key, names, getAPIConsumer(ctx).GetGroupIDs())
if err != nil {
Expand Down
82 changes: 82 additions & 0 deletions engine/api/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,3 +1827,85 @@ func Test_putWorkflowWithDuplicateHooksShouldRaiseAnError(t *testing.T) {
assert.Equal(t, 400, w.Code)

}

func Test_getWorkflowsHandler_FilterByRepo(t *testing.T) {
api, tsURL, tsClose := newTestServer(t)
defer tsClose()

admin, _ := assets.InsertAdminUser(t, api.mustDB())
localConsumer, err := authentication.LoadConsumerByTypeAndUserID(context.TODO(), api.mustDB(), sdk.ConsumerLocal, admin.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
require.NoError(t, err)

_, jws, err := builtin.NewConsumer(context.TODO(), api.mustDB(), sdk.RandomString(10), sdk.RandomString(10), localConsumer, admin.GetGroupIDs(),
sdk.NewAuthConsumerScopeDetails(sdk.AuthConsumerScopeProject))

u, _ := assets.InsertLambdaUser(t, api.mustDB())

pkey := sdk.RandomString(10)
proj := assets.InsertTestProject(t, api.mustDB(), api.Cache, pkey, pkey)
require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{
GroupID: proj.ProjectGroups[0].Group.ID,
AuthentifiedUserID: u.ID,
Admin: true,
}))

repofullName := sdk.RandomString(10)

app := &sdk.Application{
Name: sdk.RandomString(10),
RepositoryFullname: "ovh/" + repofullName,
}
require.NoError(t, application.Insert(api.mustDB(), *proj, app))

pip := sdk.Pipeline{
ProjectID: proj.ID,
ProjectKey: proj.Key,
Name: "pip1",
}
test.NoError(t, pipeline.InsertPipeline(api.mustDB(), &pip))

wf := sdk.Workflow{
Name: "workflow1",
ProjectID: proj.ID,
ProjectKey: proj.Key,
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "root",
Context: &sdk.NodeContext{
PipelineID: pip.ID,
ApplicationID: app.ID,
},
},
},
}
test.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj, &wf))

wf2 := sdk.Workflow{
Name: "workflow2",
ProjectID: proj.ID,
ProjectKey: proj.Key,
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "root",
Context: &sdk.NodeContext{
PipelineID: pip.ID,
},
},
},
}
test.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj, &wf2))

// Call with an admin
sdkclientAdmin := cdsclient.New(cdsclient.Config{
Host: tsURL,
BuitinConsumerAuthenticationToken: jws,
})

wfs, err := sdkclientAdmin.WorkflowList(proj.Key, cdsclient.WithQueryParameter("repo", "ovh/"+repofullName))
require.NoError(t, err)
require.Len(t, wfs, 1)
require.Equal(t, wf.Name, wfs[0].Name)
require.Equal(t, app.ID, wfs[0].WorkflowData.Node.Context.ApplicationID)
require.Equal(t, pip.ID, wfs[0].WorkflowData.Node.Context.PipelineID)

}
4 changes: 2 additions & 2 deletions sdk/cdsclient/client_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/ovh/cds/sdk"
)

func (c *client) WorkflowList(projectKey string) ([]sdk.Workflow, error) {
func (c *client) WorkflowList(projectKey string, opts ...RequestModifier) ([]sdk.Workflow, error) {
url := fmt.Sprintf("/project/%s/workflows", projectKey)
w := []sdk.Workflow{}
if _, err := c.GetJSON(context.Background(), url, &w); err != nil {
if _, err := c.GetJSON(context.Background(), url, &w, opts...); err != nil {
return nil, err
}
return w, nil
Expand Down
3 changes: 2 additions & 1 deletion sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ type HookClient interface {

// WorkflowClient exposes workflows functions
type WorkflowClient interface {
WorkflowList(projectKey string) ([]sdk.Workflow, error)
WorkflowList(projectKey string, opts ...RequestModifier) ([]sdk.Workflow, error)
WorkflowGet(projectKey, name string, opts ...RequestModifier) (*sdk.Workflow, error)
WorkflowUpdate(projectKey, name string, wf *sdk.Workflow) error
WorkflowDelete(projectKey string, workflowName string) error
Expand Down Expand Up @@ -342,6 +342,7 @@ type IntegrationClient interface {
}

// Interface is the main interface for cdsclient package
// generate mock with "mockgen -source=interface.go -destination=mock_cdsclient/interface_mock.go Interface" from directory ${GOPATH}/src/github.com/ovh/cds/sdk/cdsclient
type Interface interface {
Raw
AuthClient
Expand Down
36 changes: 23 additions & 13 deletions sdk/cdsclient/mock_cdsclient/interface_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sdk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func (workflows Workflows) Names() []string {
return res
}

func (workflows Workflows) Filter(f func(w Workflow) bool) Workflows {
var res = make(Workflows, 0, len(workflows))
for i := range workflows {
if f(workflows[i]) {
res = append(res, workflows[i])
}
}
return res
}

// GetApplication retrieve application from workflow
func (w *Workflow) GetApplication(ID int64) Application {
return w.Applications[ID]
Expand Down

0 comments on commit d06e020

Please sign in to comment.