Skip to content

Commit d06e020

Browse files
authored
feat(api): get workflows by repository (#5141)
Signed-off-by: francois samin <[email protected]>
1 parent 06dc738 commit d06e020

File tree

6 files changed

+142
-16
lines changed

6 files changed

+142
-16
lines changed

engine/api/workflow.go

+23
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,35 @@ func (api *API) getWorkflowsHandler() service.Handler {
3232
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
3333
vars := mux.Vars(r)
3434
key := vars[permProjectKey]
35+
filterByRepo := r.FormValue("repo")
3536

3637
ws, err := workflow.LoadAll(api.mustDB(), key)
3738
if err != nil {
3839
return err
3940
}
4041

42+
if filterByRepo != "" {
43+
mapApps := make(map[int64]sdk.Application)
44+
apps, err := application.LoadAll(api.mustDB(), key)
45+
if err != nil {
46+
return err
47+
}
48+
49+
for _, app := range apps {
50+
mapApps[app.ID] = app
51+
}
52+
53+
ws = ws.Filter(
54+
func(w sdk.Workflow) bool {
55+
if w.WorkflowData.Node.Context != nil {
56+
app, _ := mapApps[w.WorkflowData.Node.Context.ApplicationID]
57+
return app.RepositoryFullname == filterByRepo
58+
}
59+
return false
60+
},
61+
)
62+
}
63+
4164
names := ws.Names()
4265
perms, err := permission.LoadWorkflowMaxLevelPermission(ctx, api.mustDB(), key, names, getAPIConsumer(ctx).GetGroupIDs())
4366
if err != nil {

engine/api/workflow_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -1827,3 +1827,85 @@ func Test_putWorkflowWithDuplicateHooksShouldRaiseAnError(t *testing.T) {
18271827
assert.Equal(t, 400, w.Code)
18281828

18291829
}
1830+
1831+
func Test_getWorkflowsHandler_FilterByRepo(t *testing.T) {
1832+
api, tsURL, tsClose := newTestServer(t)
1833+
defer tsClose()
1834+
1835+
admin, _ := assets.InsertAdminUser(t, api.mustDB())
1836+
localConsumer, err := authentication.LoadConsumerByTypeAndUserID(context.TODO(), api.mustDB(), sdk.ConsumerLocal, admin.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
1837+
require.NoError(t, err)
1838+
1839+
_, jws, err := builtin.NewConsumer(context.TODO(), api.mustDB(), sdk.RandomString(10), sdk.RandomString(10), localConsumer, admin.GetGroupIDs(),
1840+
sdk.NewAuthConsumerScopeDetails(sdk.AuthConsumerScopeProject))
1841+
1842+
u, _ := assets.InsertLambdaUser(t, api.mustDB())
1843+
1844+
pkey := sdk.RandomString(10)
1845+
proj := assets.InsertTestProject(t, api.mustDB(), api.Cache, pkey, pkey)
1846+
require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{
1847+
GroupID: proj.ProjectGroups[0].Group.ID,
1848+
AuthentifiedUserID: u.ID,
1849+
Admin: true,
1850+
}))
1851+
1852+
repofullName := sdk.RandomString(10)
1853+
1854+
app := &sdk.Application{
1855+
Name: sdk.RandomString(10),
1856+
RepositoryFullname: "ovh/" + repofullName,
1857+
}
1858+
require.NoError(t, application.Insert(api.mustDB(), *proj, app))
1859+
1860+
pip := sdk.Pipeline{
1861+
ProjectID: proj.ID,
1862+
ProjectKey: proj.Key,
1863+
Name: "pip1",
1864+
}
1865+
test.NoError(t, pipeline.InsertPipeline(api.mustDB(), &pip))
1866+
1867+
wf := sdk.Workflow{
1868+
Name: "workflow1",
1869+
ProjectID: proj.ID,
1870+
ProjectKey: proj.Key,
1871+
WorkflowData: sdk.WorkflowData{
1872+
Node: sdk.Node{
1873+
Name: "root",
1874+
Context: &sdk.NodeContext{
1875+
PipelineID: pip.ID,
1876+
ApplicationID: app.ID,
1877+
},
1878+
},
1879+
},
1880+
}
1881+
test.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj, &wf))
1882+
1883+
wf2 := sdk.Workflow{
1884+
Name: "workflow2",
1885+
ProjectID: proj.ID,
1886+
ProjectKey: proj.Key,
1887+
WorkflowData: sdk.WorkflowData{
1888+
Node: sdk.Node{
1889+
Name: "root",
1890+
Context: &sdk.NodeContext{
1891+
PipelineID: pip.ID,
1892+
},
1893+
},
1894+
},
1895+
}
1896+
test.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj, &wf2))
1897+
1898+
// Call with an admin
1899+
sdkclientAdmin := cdsclient.New(cdsclient.Config{
1900+
Host: tsURL,
1901+
BuitinConsumerAuthenticationToken: jws,
1902+
})
1903+
1904+
wfs, err := sdkclientAdmin.WorkflowList(proj.Key, cdsclient.WithQueryParameter("repo", "ovh/"+repofullName))
1905+
require.NoError(t, err)
1906+
require.Len(t, wfs, 1)
1907+
require.Equal(t, wf.Name, wfs[0].Name)
1908+
require.Equal(t, app.ID, wfs[0].WorkflowData.Node.Context.ApplicationID)
1909+
require.Equal(t, pip.ID, wfs[0].WorkflowData.Node.Context.PipelineID)
1910+
1911+
}

sdk/cdsclient/client_workflow.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"github.com/ovh/cds/sdk"
1616
)
1717

18-
func (c *client) WorkflowList(projectKey string) ([]sdk.Workflow, error) {
18+
func (c *client) WorkflowList(projectKey string, opts ...RequestModifier) ([]sdk.Workflow, error) {
1919
url := fmt.Sprintf("/project/%s/workflows", projectKey)
2020
w := []sdk.Workflow{}
21-
if _, err := c.GetJSON(context.Background(), url, &w); err != nil {
21+
if _, err := c.GetJSON(context.Background(), url, &w, opts...); err != nil {
2222
return nil, err
2323
}
2424
return w, nil

sdk/cdsclient/interface.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ type HookClient interface {
292292

293293
// WorkflowClient exposes workflows functions
294294
type WorkflowClient interface {
295-
WorkflowList(projectKey string) ([]sdk.Workflow, error)
295+
WorkflowList(projectKey string, opts ...RequestModifier) ([]sdk.Workflow, error)
296296
WorkflowGet(projectKey, name string, opts ...RequestModifier) (*sdk.Workflow, error)
297297
WorkflowUpdate(projectKey, name string, wf *sdk.Workflow) error
298298
WorkflowDelete(projectKey string, workflowName string) error
@@ -342,6 +342,7 @@ type IntegrationClient interface {
342342
}
343343

344344
// Interface is the main interface for cdsclient package
345+
// generate mock with "mockgen -source=interface.go -destination=mock_cdsclient/interface_mock.go Interface" from directory ${GOPATH}/src/github.com/ovh/cds/sdk/cdsclient
345346
type Interface interface {
346347
Raw
347348
AuthClient

sdk/cdsclient/mock_cdsclient/interface_mock.go

+23-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/workflow.go

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ func (workflows Workflows) Names() []string {
6969
return res
7070
}
7171

72+
func (workflows Workflows) Filter(f func(w Workflow) bool) Workflows {
73+
var res = make(Workflows, 0, len(workflows))
74+
for i := range workflows {
75+
if f(workflows[i]) {
76+
res = append(res, workflows[i])
77+
}
78+
}
79+
return res
80+
}
81+
7282
// GetApplication retrieve application from workflow
7383
func (w *Workflow) GetApplication(ID int64) Application {
7484
return w.Applications[ID]

0 commit comments

Comments
 (0)