From 87e5e6f2e3be02d78610af8ead3a191fba8ee139 Mon Sep 17 00:00:00 2001 From: lepasq Date: Sat, 14 Aug 2021 19:12:59 +0200 Subject: [PATCH 1/2] Add topic filter to repository listing --- pkg/cmd/repo/list/http.go | 7 ++++++- pkg/cmd/repo/list/list.go | 5 ++++- pkg/githubsearch/query.go | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/repo/list/http.go b/pkg/cmd/repo/list/http.go index ef574b43c..4c2ca50b3 100644 --- a/pkg/cmd/repo/list/http.go +++ b/pkg/cmd/repo/list/http.go @@ -22,13 +22,14 @@ type FilterOptions struct { Fork bool Source bool Language string + Topic string Archived bool NonArchived bool Fields []string } func listRepos(client *http.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) { - if filter.Language != "" || filter.Archived || filter.NonArchived { + if filter.Language != "" || filter.Archived || filter.NonArchived || filter.Topic != "" { return searchRepos(client, hostname, limit, owner, filter) } @@ -197,6 +198,10 @@ func searchQuery(owner string, filter FilterOptions) string { q.SetLanguage(filter.Language) } + if filter.Topic != "" { + q.SetTopic(filter.Topic) + } + switch filter.Visibility { case "public": q.SetVisibility(githubsearch.Public) diff --git a/pkg/cmd/repo/list/list.go b/pkg/cmd/repo/list/list.go index 3f1bf64f3..bb4ad3d53 100644 --- a/pkg/cmd/repo/list/list.go +++ b/pkg/cmd/repo/list/list.go @@ -28,6 +28,7 @@ type ListOptions struct { Fork bool Source bool Language string + Topic string Archived bool NonArchived bool @@ -89,6 +90,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().BoolVar(&opts.Source, "source", false, "Show only non-forks") cmd.Flags().BoolVar(&opts.Fork, "fork", false, "Show only forks") cmd.Flags().StringVarP(&opts.Language, "language", "l", "", "Filter by primary coding language") + cmd.Flags().StringVar(&opts.Topic, "topic", "", "Filter by topic") cmd.Flags().BoolVar(&opts.Archived, "archived", false, "Show only archived repositories") cmd.Flags().BoolVar(&opts.NonArchived, "no-archived", false, "Omit archived repositories") cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.RepositoryFields) @@ -109,6 +111,7 @@ func listRun(opts *ListOptions) error { Fork: opts.Fork, Source: opts.Source, Language: opts.Language, + Topic: opts.Topic, Archived: opts.Archived, NonArchived: opts.NonArchived, Fields: defaultFields, @@ -169,7 +172,7 @@ func listRun(opts *ListOptions) error { } if opts.IO.IsStdoutTTY() { - hasFilters := filter.Visibility != "" || filter.Fork || filter.Source || filter.Language != "" + hasFilters := filter.Visibility != "" || filter.Fork || filter.Source || filter.Language != "" || filter.Topic != "" title := listHeader(listResult.Owner, len(listResult.Repositories), listResult.TotalCount, hasFilters) fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title) } diff --git a/pkg/githubsearch/query.go b/pkg/githubsearch/query.go index 3ba64fd68..f25707710 100644 --- a/pkg/githubsearch/query.go +++ b/pkg/githubsearch/query.go @@ -50,6 +50,7 @@ type Query struct { milestone string language string + topic string forkState string visibility string isArchived *bool @@ -118,6 +119,10 @@ func (q *Query) SetLanguage(name string) { q.language = name } +func (q *Query) SetTopic(name string) { + q.topic = name +} + func (q *Query) SetVisibility(visibility RepoVisibility) { q.visibility = string(visibility) } @@ -159,6 +164,9 @@ func (q *Query) String() string { if q.language != "" { qs += fmt.Sprintf("language:%s ", quote(q.language)) } + if q.topic != "" { + qs += fmt.Sprintf("topic:%s ", quote(q.topic)) + } if q.forkState != "" { qs += fmt.Sprintf("fork:%s ", q.forkState) } From 998a29d391d342dcff470442baf34a23f834af6c Mon Sep 17 00:00:00 2001 From: lepasq Date: Sun, 15 Aug 2021 14:31:24 +0200 Subject: [PATCH 2/2] Update list_test.go to include topics as well --- pkg/cmd/repo/list/list_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/cmd/repo/list/list_test.go b/pkg/cmd/repo/list/list_test.go index 4c7c5afb1..e40646aeb 100644 --- a/pkg/cmd/repo/list/list_test.go +++ b/pkg/cmd/repo/list/list_test.go @@ -35,6 +35,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -49,6 +50,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -63,6 +65,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -77,6 +80,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -91,6 +95,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -105,6 +110,7 @@ func TestNewCmdList(t *testing.T) { Fork: true, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -119,6 +125,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: true, Language: "", + Topic: "", Archived: false, NonArchived: false, }, @@ -133,6 +140,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "go", + Topic: "", Archived: false, NonArchived: false, }, @@ -147,6 +155,7 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: true, NonArchived: false, }, @@ -161,10 +170,26 @@ func TestNewCmdList(t *testing.T) { Fork: false, Source: false, Language: "", + Topic: "", Archived: false, NonArchived: true, }, }, + { + name: "with topic", + cli: "--topic cli", + wants: ListOptions{ + Limit: 30, + Owner: "", + Visibility: "", + Fork: false, + Source: false, + Language: "", + Topic: "cli", + Archived: false, + NonArchived: false, + }, + }, { name: "no public and private", cli: "--public --private", @@ -220,6 +245,7 @@ func TestNewCmdList(t *testing.T) { assert.Equal(t, tt.wants.Owner, gotOpts.Owner) assert.Equal(t, tt.wants.Visibility, gotOpts.Visibility) assert.Equal(t, tt.wants.Fork, gotOpts.Fork) + assert.Equal(t, tt.wants.Topic, gotOpts.Topic) assert.Equal(t, tt.wants.Source, gotOpts.Source) assert.Equal(t, tt.wants.Archived, gotOpts.Archived) assert.Equal(t, tt.wants.NonArchived, gotOpts.NonArchived)