Skip to content

Commit a0bac16

Browse files
committed
Do case-insensitive lookup of labels for issues
Fixes cli#1819
1 parent e930122 commit a0bac16

5 files changed

Lines changed: 233 additions & 0 deletions

File tree

‎api/queries_issue.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,18 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
237237
"repo": repo.RepoName(),
238238
"states": states,
239239
}
240+
240241
if len(labels) > 0 {
242+
matchedLabels, err := LabelsByNames(client, repo, labels)
243+
if err != nil {
244+
return nil, err
245+
}
246+
for i := range matchedLabels {
247+
labels[i] = matchedLabels[i].Name
248+
}
241249
variables["labels"] = labels
242250
}
251+
243252
if assigneeString != "" {
244253
variables["assignee"] = assigneeString
245254
}

‎api/queries_issue_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,86 @@ func TestIssueList(t *testing.T) {
6868
t.Errorf("expected %q, got %q", "ENDCURSOR", endCursor)
6969
}
7070
}
71+
72+
func TestIssueListWithLabels(t *testing.T) {
73+
http := &httpmock.Registry{}
74+
client := NewClient(ReplaceTripper(http))
75+
76+
http.StubResponse(200, bytes.NewBufferString(`
77+
{ "data": { "repository": {
78+
"labels": {
79+
"nodes": [
80+
{ "id": "1", "name": "L1" },
81+
{ "id": "2", "name": "L2" }
82+
],
83+
"pageInfo": {
84+
"hasNextPage": false,
85+
"endCursor": "ENDCURSOR"
86+
}
87+
}
88+
} } }
89+
`))
90+
http.StubResponse(200, bytes.NewBufferString(`
91+
{ "data": { "repository": {
92+
"hasIssuesEnabled": true,
93+
"issues": {
94+
"nodes": [],
95+
"pageInfo": {
96+
"hasNextPage": false,
97+
"endCursor": "ENDCURSOR"
98+
}
99+
}
100+
} } }
101+
`))
102+
103+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
104+
_, err := IssueList(client, repo, "open", []string{"l1"}, "", 251, "", "", "")
105+
if err != nil {
106+
t.Fatalf("unexpected error: %v", err)
107+
}
108+
109+
if len(http.Requests) != 2 {
110+
t.Fatalf("expected 2 HTTP requests, seen %d", len(http.Requests))
111+
}
112+
}
113+
114+
func TestIssueListWithMissingLabels(t *testing.T) {
115+
http := &httpmock.Registry{}
116+
client := NewClient(ReplaceTripper(http))
117+
118+
http.StubResponse(200, bytes.NewBufferString(`
119+
{ "data": { "repository": {
120+
"labels": {
121+
"nodes": [
122+
{ "id": "1", "name": "L1" },
123+
{ "id": "2", "name": "L2" }
124+
],
125+
"pageInfo": {
126+
"hasNextPage": false,
127+
"endCursor": "ENDCURSOR"
128+
}
129+
}
130+
} } }
131+
`))
132+
http.StubResponse(200, bytes.NewBufferString(`
133+
{ "data": { "repository": {
134+
"hasIssuesEnabled": true,
135+
"issues": {
136+
"nodes": [],
137+
"pageInfo": {
138+
"hasNextPage": false,
139+
"endCursor": "ENDCURSOR"
140+
}
141+
}
142+
} } }
143+
`))
144+
145+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
146+
_, err := IssueList(client, repo, "open", []string{"l1", "l3"}, "", 251, "", "", "")
147+
148+
eq(t, "no label found with name: l3", err.Error())
149+
150+
if len(http.Requests) != 1 {
151+
t.Fatalf("expected 1 HTTP request, seen %d", len(http.Requests))
152+
}
153+
}

‎api/queries_repo.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,39 @@ func RepoLabels(client *Client, repo ghrepo.Interface) ([]RepoLabel, error) {
782782
return labels, nil
783783
}
784784

785+
// LabelsByNames performs a case-insensitive look up of a label
786+
func LabelsByNames(client *Client, repo ghrepo.Interface, names []string) ([]RepoLabel, error) {
787+
labels, err := RepoLabels(client, repo)
788+
if err != nil {
789+
return nil, err
790+
}
791+
792+
matches := make([]RepoLabel, 0, len(names))
793+
missing := make([]string, 0, len(names))
794+
795+
for _, name := range names {
796+
found := false
797+
for _, label := range labels {
798+
if strings.EqualFold(name, label.Name) {
799+
matches = append(matches, label)
800+
found = true
801+
break
802+
}
803+
}
804+
805+
if !found {
806+
missing = append(missing, name)
807+
}
808+
}
809+
810+
if len(missing) > 1 {
811+
return nil, fmt.Errorf("no labels found with names: %s", strings.Join(missing, ", "))
812+
} else if len(missing) == 1 {
813+
return nil, fmt.Errorf("no label found with name: %s", missing[0])
814+
}
815+
return matches, nil
816+
}
817+
785818
type RepoMilestone struct {
786819
ID string
787820
Title string

‎api/queries_repo_test.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"bytes"
45
"testing"
56

67
"github.com/cli/cli/internal/ghrepo"
@@ -220,6 +221,101 @@ t001: team(slug:"robots"){id,slug}
220221
}
221222
}
222223

224+
func Test_LabelsByNames(t *testing.T) {
225+
http := &httpmock.Registry{}
226+
client := NewClient(ReplaceTripper(http))
227+
228+
http.StubResponse(200, bytes.NewBufferString(`
229+
{ "data": { "repository": {
230+
"labels": {
231+
"nodes": [
232+
{ "id": "1", "name": "L1" },
233+
{ "id": "2", "name": "L2" }
234+
],
235+
"pageInfo": {
236+
"hasNextPage": true,
237+
"endCursor": "ENDCURSOR"
238+
}
239+
}
240+
} } }
241+
`))
242+
http.StubResponse(200, bytes.NewBufferString(`
243+
{ "data": { "repository": {
244+
"labels": {
245+
"nodes": [
246+
{ "id": "3", "name": "L3"}
247+
],
248+
"pageInfo": {
249+
"hasNextPage": false,
250+
"endCursor": "ENDCURSOR"
251+
}
252+
}
253+
} } }
254+
`))
255+
256+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
257+
labels, err := LabelsByNames(client, repo, []string{"l1", "L3"})
258+
if err != nil {
259+
t.Fatalf("unexpected error: %v", err)
260+
}
261+
262+
eq(t, len(labels), 2)
263+
eq(t, labels[0].ID, "1")
264+
eq(t, labels[0].Name, "L1")
265+
eq(t, labels[1].ID, "3")
266+
eq(t, labels[1].Name, "L3")
267+
}
268+
269+
func Test_LabelsByNames_Missing_Name(t *testing.T) {
270+
http := &httpmock.Registry{}
271+
client := NewClient(ReplaceTripper(http))
272+
273+
http.StubResponse(200, bytes.NewBufferString(`
274+
{ "data": { "repository": {
275+
"labels": {
276+
"nodes": [
277+
{ "id": "1", "name": "L1" },
278+
{ "id": "2", "name": "L2" }
279+
],
280+
"pageInfo": {
281+
"hasNextPage": false,
282+
"endCursor": "ENDCURSOR"
283+
}
284+
}
285+
} } }
286+
`))
287+
288+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
289+
_, err := LabelsByNames(client, repo, []string{"l1", "l4"})
290+
291+
eq(t, err.Error(), "no label found with name: l4")
292+
}
293+
294+
func Test_LabelsByNames_Missing_Names(t *testing.T) {
295+
http := &httpmock.Registry{}
296+
client := NewClient(ReplaceTripper(http))
297+
298+
http.StubResponse(200, bytes.NewBufferString(`
299+
{ "data": { "repository": {
300+
"labels": {
301+
"nodes": [
302+
{ "id": "1", "name": "L1" },
303+
{ "id": "2", "name": "L2" }
304+
],
305+
"pageInfo": {
306+
"hasNextPage": false,
307+
"endCursor": "ENDCURSOR"
308+
}
309+
}
310+
} } }
311+
`))
312+
313+
repo, _ := ghrepo.FromFullName("OWNER/REPO")
314+
_, err := LabelsByNames(client, repo, []string{"l1", "l4", "L5"})
315+
316+
eq(t, err.Error(), "no labels found with names: l4, L5")
317+
}
318+
223319
func sliceEqual(a, b []string) bool {
224320
if len(a) != len(b) {
225321
return false

‎pkg/cmd/issue/list/list_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ func TestIssueList_tty_withFlags(t *testing.T) {
133133
assert.Equal(t, []interface{}{"OPEN"}, params["states"].([]interface{}))
134134
}))
135135

136+
http.Register(
137+
httpmock.GraphQL(`query RepositoryLabelList\b`),
138+
httpmock.StringResponse(`
139+
{ "data": { "repository": { "labels": {
140+
"nodes": [
141+
{ "id": "1", "name": "bug" },
142+
{ "id": "2", "name": "web" }
143+
],
144+
"pageInfo": { "hasNextPage": false }
145+
} } } }
146+
`))
147+
136148
http.Register(
137149
httpmock.GraphQL(`query RepositoryMilestoneList\b`),
138150
httpmock.StringResponse(`

0 commit comments

Comments
 (0)