Skip to content

Commit

Permalink
feat: filter applications by source repo URL (argoproj#5602) (argopro…
Browse files Browse the repository at this point in the history
…j#5603)

* feat: filter applications by source repo URL (argoproj#5602)

Signed-off-by: Samuel Suter <[email protected]>

* Unit test for FilterByRepo
  • Loading branch information
moensch authored Mar 12, 2021
1 parent 6757395 commit 89f5a71
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 146 deletions.
18 changes: 18 additions & 0 deletions assets/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@
"description": "the selector to to restrict returned list to applications only with matched labels.",
"name": "selector",
"in": "query"
},
{
"type": "string",
"description": "the repoURL to restrict returned list applications.",
"name": "repo",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -517,6 +523,12 @@
"description": "the selector to to restrict returned list to applications only with matched labels.",
"name": "selector",
"in": "query"
},
{
"type": "string",
"description": "the repoURL to restrict returned list applications.",
"name": "repo",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -3039,6 +3051,12 @@
"description": "the selector to to restrict returned list to applications only with matched labels.",
"name": "selector",
"in": "query"
},
{
"type": "string",
"description": "the repoURL to restrict returned list applications.",
"name": "repo",
"in": "query"
}
],
"responses": {
Expand Down
5 changes: 5 additions & 0 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ func NewApplicationListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
output string
selector string
projects []string
repo string
)
var command = &cobra.Command{
Use: "list",
Expand All @@ -1063,6 +1064,9 @@ func NewApplicationListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
if len(projects) != 0 {
appList = argo.FilterByProjects(appList, projects)
}
if repo != "" {
appList = argo.FilterByRepo(appList, repo)
}
switch output {
case "yaml", "json":
err := PrintResourceList(appList, output, false)
Expand All @@ -1079,6 +1083,7 @@ func NewApplicationListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
command.Flags().StringVarP(&output, "output", "o", "wide", "Output format. One of: wide|name|json|yaml")
command.Flags().StringVarP(&selector, "selector", "l", "", "List apps by label")
command.Flags().StringArrayVarP(&projects, "project", "p", []string{}, "Filter by project name")
command.Flags().StringVarP(&selector, "repo", "r", "", "List apps by source repo URL")
return command
}

Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ argocd app list [flags]
-h, --help help for list
-o, --output string Output format. One of: wide|name|json|yaml (default "wide")
-p, --project stringArray Filter by project name
-r, --repo string List apps by source repo URL
-l, --selector string List apps by label
```

Expand Down
341 changes: 195 additions & 146 deletions pkg/apiclient/application/application.pb.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,18 @@ func (s *Server) List(ctx context.Context, q *application.ApplicationQuery) (*ap
return nil, err
}
}

// Filter applications by name
newItems = argoutil.FilterByProjects(newItems, q.Projects)

// Filter applications by source repo URL
newItems = argoutil.FilterByRepo(newItems, q.Repo)

// Sort found applications by name
sort.Slice(newItems, func(i, j int) bool {
return newItems[i].Name < newItems[j].Name
})

appList := appv1.ApplicationList{
ListMeta: metav1.ListMeta{
ResourceVersion: s.appInformer.LastSyncResourceVersion(),
Expand Down
2 changes: 2 additions & 0 deletions server/application/application.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ message ApplicationQuery {
optional string resourceVersion = 4 [(gogoproto.nullable) = false];
// the selector to to restrict returned list to applications only with matched labels
optional string selector = 5 [(gogoproto.nullable) = false];
// the repoURL to restrict returned list applications
optional string repo = 6 [(gogoproto.nullable) = false];
}

message NodeQuery {
Expand Down
14 changes: 14 additions & 0 deletions util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ func FilterByProjects(apps []argoappv1.Application, projects []string) []argoapp

}

// FilterByRepo returns an application
func FilterByRepo(apps []argoappv1.Application, repo string) []argoappv1.Application {
if repo == "" {
return apps
}
items := make([]argoappv1.Application, 0)
for i := 0; i < len(apps); i++ {
if apps[i].Spec.Source.RepoURL == repo {
items = append(items, apps[i])
}
}
return items
}

// FilterByName returns an application
func FilterByName(apps []argoappv1.Application, name string) ([]argoappv1.Application, error) {
if name == "" {
Expand Down
34 changes: 34 additions & 0 deletions util/argo/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,40 @@ func TestFilterByProjects(t *testing.T) {
})
}

func TestFilterByRepo(t *testing.T) {
apps := []argoappv1.Application{
{
Spec: argoappv1.ApplicationSpec{
Source: argoappv1.ApplicationSource{
RepoURL: "[email protected]:owner/repo.git",
},
},
},
{
Spec: argoappv1.ApplicationSpec{
Source: argoappv1.ApplicationSource{
RepoURL: "[email protected]:owner/otherrepo.git",
},
},
},
}

t.Run("Empty filter", func(t *testing.T) {
res := FilterByRepo(apps, "")
assert.Len(t, res, 2)
})

t.Run("Match", func(t *testing.T) {
res := FilterByRepo(apps, "[email protected]:owner/repo.git")
assert.Len(t, res, 1)
})

t.Run("No match", func(t *testing.T) {
res := FilterByRepo(apps, "[email protected]:owner/willnotmatch.git")
assert.Len(t, res, 0)
})
}

func TestValidatePermissions(t *testing.T) {
t.Run("Empty Repo URL result in condition", func(t *testing.T) {
spec := argoappv1.ApplicationSpec{
Expand Down

0 comments on commit 89f5a71

Please sign in to comment.