Skip to content

Commit 45b358b

Browse files
authored
Merge pull request cli#4136 from lepasq/filter-by-topic
Add topic filter to repository listing
2 parents 8fb6bb6 + 998a29d commit 45b358b

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

pkg/cmd/repo/list/http.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ type FilterOptions struct {
2222
Fork bool
2323
Source bool
2424
Language string
25+
Topic string
2526
Archived bool
2627
NonArchived bool
2728
Fields []string
2829
}
2930

3031
func listRepos(client *http.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) {
31-
if filter.Language != "" || filter.Archived || filter.NonArchived {
32+
if filter.Language != "" || filter.Archived || filter.NonArchived || filter.Topic != "" {
3233
return searchRepos(client, hostname, limit, owner, filter)
3334
}
3435

@@ -197,6 +198,10 @@ func searchQuery(owner string, filter FilterOptions) string {
197198
q.SetLanguage(filter.Language)
198199
}
199200

201+
if filter.Topic != "" {
202+
q.SetTopic(filter.Topic)
203+
}
204+
200205
switch filter.Visibility {
201206
case "public":
202207
q.SetVisibility(githubsearch.Public)

pkg/cmd/repo/list/list.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ListOptions struct {
2828
Fork bool
2929
Source bool
3030
Language string
31+
Topic string
3132
Archived bool
3233
NonArchived bool
3334

@@ -89,6 +90,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
8990
cmd.Flags().BoolVar(&opts.Source, "source", false, "Show only non-forks")
9091
cmd.Flags().BoolVar(&opts.Fork, "fork", false, "Show only forks")
9192
cmd.Flags().StringVarP(&opts.Language, "language", "l", "", "Filter by primary coding language")
93+
cmd.Flags().StringVar(&opts.Topic, "topic", "", "Filter by topic")
9294
cmd.Flags().BoolVar(&opts.Archived, "archived", false, "Show only archived repositories")
9395
cmd.Flags().BoolVar(&opts.NonArchived, "no-archived", false, "Omit archived repositories")
9496
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.RepositoryFields)
@@ -109,6 +111,7 @@ func listRun(opts *ListOptions) error {
109111
Fork: opts.Fork,
110112
Source: opts.Source,
111113
Language: opts.Language,
114+
Topic: opts.Topic,
112115
Archived: opts.Archived,
113116
NonArchived: opts.NonArchived,
114117
Fields: defaultFields,
@@ -169,7 +172,7 @@ func listRun(opts *ListOptions) error {
169172
}
170173

171174
if opts.IO.IsStdoutTTY() {
172-
hasFilters := filter.Visibility != "" || filter.Fork || filter.Source || filter.Language != ""
175+
hasFilters := filter.Visibility != "" || filter.Fork || filter.Source || filter.Language != "" || filter.Topic != ""
173176
title := listHeader(listResult.Owner, len(listResult.Repositories), listResult.TotalCount, hasFilters)
174177
fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title)
175178
}

pkg/cmd/repo/list/list_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestNewCmdList(t *testing.T) {
3535
Fork: false,
3636
Source: false,
3737
Language: "",
38+
Topic: "",
3839
Archived: false,
3940
NonArchived: false,
4041
},
@@ -49,6 +50,7 @@ func TestNewCmdList(t *testing.T) {
4950
Fork: false,
5051
Source: false,
5152
Language: "",
53+
Topic: "",
5254
Archived: false,
5355
NonArchived: false,
5456
},
@@ -63,6 +65,7 @@ func TestNewCmdList(t *testing.T) {
6365
Fork: false,
6466
Source: false,
6567
Language: "",
68+
Topic: "",
6669
Archived: false,
6770
NonArchived: false,
6871
},
@@ -77,6 +80,7 @@ func TestNewCmdList(t *testing.T) {
7780
Fork: false,
7881
Source: false,
7982
Language: "",
83+
Topic: "",
8084
Archived: false,
8185
NonArchived: false,
8286
},
@@ -91,6 +95,7 @@ func TestNewCmdList(t *testing.T) {
9195
Fork: false,
9296
Source: false,
9397
Language: "",
98+
Topic: "",
9499
Archived: false,
95100
NonArchived: false,
96101
},
@@ -105,6 +110,7 @@ func TestNewCmdList(t *testing.T) {
105110
Fork: true,
106111
Source: false,
107112
Language: "",
113+
Topic: "",
108114
Archived: false,
109115
NonArchived: false,
110116
},
@@ -119,6 +125,7 @@ func TestNewCmdList(t *testing.T) {
119125
Fork: false,
120126
Source: true,
121127
Language: "",
128+
Topic: "",
122129
Archived: false,
123130
NonArchived: false,
124131
},
@@ -133,6 +140,7 @@ func TestNewCmdList(t *testing.T) {
133140
Fork: false,
134141
Source: false,
135142
Language: "go",
143+
Topic: "",
136144
Archived: false,
137145
NonArchived: false,
138146
},
@@ -147,6 +155,7 @@ func TestNewCmdList(t *testing.T) {
147155
Fork: false,
148156
Source: false,
149157
Language: "",
158+
Topic: "",
150159
Archived: true,
151160
NonArchived: false,
152161
},
@@ -161,10 +170,26 @@ func TestNewCmdList(t *testing.T) {
161170
Fork: false,
162171
Source: false,
163172
Language: "",
173+
Topic: "",
164174
Archived: false,
165175
NonArchived: true,
166176
},
167177
},
178+
{
179+
name: "with topic",
180+
cli: "--topic cli",
181+
wants: ListOptions{
182+
Limit: 30,
183+
Owner: "",
184+
Visibility: "",
185+
Fork: false,
186+
Source: false,
187+
Language: "",
188+
Topic: "cli",
189+
Archived: false,
190+
NonArchived: false,
191+
},
192+
},
168193
{
169194
name: "no public and private",
170195
cli: "--public --private",
@@ -220,6 +245,7 @@ func TestNewCmdList(t *testing.T) {
220245
assert.Equal(t, tt.wants.Owner, gotOpts.Owner)
221246
assert.Equal(t, tt.wants.Visibility, gotOpts.Visibility)
222247
assert.Equal(t, tt.wants.Fork, gotOpts.Fork)
248+
assert.Equal(t, tt.wants.Topic, gotOpts.Topic)
223249
assert.Equal(t, tt.wants.Source, gotOpts.Source)
224250
assert.Equal(t, tt.wants.Archived, gotOpts.Archived)
225251
assert.Equal(t, tt.wants.NonArchived, gotOpts.NonArchived)

pkg/githubsearch/query.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Query struct {
5050
milestone string
5151

5252
language string
53+
topic string
5354
forkState string
5455
visibility string
5556
isArchived *bool
@@ -118,6 +119,10 @@ func (q *Query) SetLanguage(name string) {
118119
q.language = name
119120
}
120121

122+
func (q *Query) SetTopic(name string) {
123+
q.topic = name
124+
}
125+
121126
func (q *Query) SetVisibility(visibility RepoVisibility) {
122127
q.visibility = string(visibility)
123128
}
@@ -159,6 +164,9 @@ func (q *Query) String() string {
159164
if q.language != "" {
160165
qs += fmt.Sprintf("language:%s ", quote(q.language))
161166
}
167+
if q.topic != "" {
168+
qs += fmt.Sprintf("topic:%s ", quote(q.topic))
169+
}
162170
if q.forkState != "" {
163171
qs += fmt.Sprintf("fork:%s ", q.forkState)
164172
}

0 commit comments

Comments
 (0)