X Tutup
Skip to content

Commit 7f63c0b

Browse files
committed
Replace old search utility with new search
1 parent ee6fc0f commit 7f63c0b

File tree

11 files changed

+100
-369
lines changed

11 files changed

+100
-369
lines changed

pkg/cmd/issue/list/list_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestIssueList_web(t *testing.T) {
201201

202202
assert.Equal(t, "", stdout.String())
203203
assert.Equal(t, "Opening github.com/OWNER/REPO/issues in your browser.\n", stderr.String())
204-
browser.Verify(t, "https://github.com/OWNER/REPO/issues?q=is%3Aissue+assignee%3Apeter+label%3Abug+label%3Adocs+author%3Ajohn+mentions%3Afrank+milestone%3Av1.1")
204+
browser.Verify(t, "https://github.com/OWNER/REPO/issues?q=assignee%3Apeter+author%3Ajohn+label%3Abug+label%3Adocs+mentions%3Afrank+milestone%3Av1.1+state%3Aall+type%3Aissue")
205205
}
206206

207207
func Test_issueList(t *testing.T) {
@@ -276,7 +276,7 @@ func Test_issueList(t *testing.T) {
276276
"owner": "OWNER",
277277
"repo": "REPO",
278278
"limit": float64(30),
279-
"query": "repo:OWNER/REPO is:issue is:open milestone:1.x",
279+
"query": "repo:OWNER/REPO milestone:1.x state:open type:issue",
280280
"type": "ISSUE",
281281
}, params)
282282
}))
@@ -308,7 +308,7 @@ func Test_issueList(t *testing.T) {
308308
"owner": "OWNER",
309309
"repo": "REPO",
310310
"limit": float64(30),
311-
"query": "repo:OWNER/REPO is:issue is:open milestone:1.x",
311+
"query": "repo:OWNER/REPO milestone:1.x state:open type:issue",
312312
"type": "ISSUE",
313313
}, params)
314314
}))
@@ -379,7 +379,7 @@ func Test_issueList(t *testing.T) {
379379
"owner": "OWNER",
380380
"repo": "REPO",
381381
"limit": float64(30),
382-
"query": "repo:OWNER/REPO is:issue is:open assignee:@me author:@me mentions:@me auth bug",
382+
"query": "repo:OWNER/REPO auth bug assignee:@me author:@me mentions:@me state:open type:issue",
383383
"type": "ISSUE",
384384
}, params)
385385
}))
@@ -411,7 +411,7 @@ func Test_issueList(t *testing.T) {
411411
"owner": "OWNER",
412412
"repo": "REPO",
413413
"limit": float64(30),
414-
"query": `repo:OWNER/REPO is:issue is:open label:hello label:"one world"`,
414+
"query": `repo:OWNER/REPO label:"one world" label:hello state:open type:issue`,
415415
"type": "ISSUE",
416416
}, params)
417417
}))

pkg/cmd/pr/list/http.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package list
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67

78
"github.com/cli/cli/v2/api"
89
"github.com/cli/cli/v2/internal/ghrepo"
910
prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
10-
"github.com/cli/cli/v2/pkg/githubsearch"
11+
"github.com/cli/cli/v2/pkg/search"
1112
)
1213

1314
func shouldUseSearch(filters prShared.FilterOptions) bool {
14-
return filters.Draft != "" || filters.Author != "" || filters.Assignee != "" || filters.Search != "" || len(filters.Labels) > 0
15+
return filters.Draft != nil || filters.Author != "" || filters.Assignee != "" || filters.Search != "" || len(filters.Labels) > 0
1516
}
1617

1718
func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) {
@@ -159,35 +160,18 @@ func searchPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters
159160
}
160161
}`
161162

162-
q := githubsearch.NewQuery()
163-
q.SetType(githubsearch.PullRequest)
164-
q.InRepository(ghrepo.FullName(repo))
165-
q.AddQuery(filters.Search)
166-
167-
switch filters.State {
168-
case "open":
169-
q.SetState(githubsearch.Open)
170-
case "closed":
171-
q.SetState(githubsearch.Closed)
172-
case "merged":
173-
q.SetState(githubsearch.Merged)
174-
}
175-
176-
if filters.Author != "" {
177-
q.AuthoredBy(filters.Author)
178-
}
179-
if filters.Assignee != "" {
180-
q.AssignedTo(filters.Assignee)
181-
}
182-
for _, label := range filters.Labels {
183-
q.AddLabel(label)
184-
}
185-
if filters.BaseBranch != "" {
186-
q.SetBaseBranch(filters.BaseBranch)
187-
}
188-
189-
if filters.Draft != "" {
190-
q.SetDraft(filters.Draft)
163+
q := search.Query{
164+
Keywords: strings.Split(filters.Search, " "),
165+
Qualifiers: search.Qualifiers{
166+
Assignee: filters.Assignee,
167+
Author: filters.Author,
168+
Base: filters.BaseBranch,
169+
Draft: filters.Draft,
170+
Label: filters.Labels,
171+
Repo: []string{ghrepo.FullName(repo)},
172+
State: filters.State,
173+
Type: "pr",
174+
},
191175
}
192176

193177
pageLimit := min(limit, 100)

pkg/cmd/pr/list/http_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func Test_listPullRequests(t *testing.T) {
8787
httpmock.GraphQL(`query PullRequestSearch\b`),
8888
httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) {
8989
want := map[string]interface{}{
90-
"q": `repo:OWNER/REPO is:pr is:open label:hello label:"one world"`,
90+
"q": `label:"one world" label:hello repo:OWNER/REPO state:open type:pr`,
9191
"limit": float64(30),
9292
}
9393
if !reflect.DeepEqual(vars, want) {
@@ -111,7 +111,7 @@ func Test_listPullRequests(t *testing.T) {
111111
httpmock.GraphQL(`query PullRequestSearch\b`),
112112
httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) {
113113
want := map[string]interface{}{
114-
"q": "repo:OWNER/REPO is:pr is:open author:monalisa",
114+
"q": "author:monalisa repo:OWNER/REPO state:open type:pr",
115115
"limit": float64(30),
116116
}
117117
if !reflect.DeepEqual(vars, want) {
@@ -135,7 +135,7 @@ func Test_listPullRequests(t *testing.T) {
135135
httpmock.GraphQL(`query PullRequestSearch\b`),
136136
httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) {
137137
want := map[string]interface{}{
138-
"q": "repo:OWNER/REPO is:pr is:open one world in:title",
138+
"q": "one world in:title repo:OWNER/REPO state:open type:pr",
139139
"limit": float64(30),
140140
}
141141
if !reflect.DeepEqual(vars, want) {

pkg/cmd/pr/list/list.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ListOptions struct {
3939
AppAuthor string
4040
Assignee string
4141
Search string
42-
Draft string
42+
Draft *bool
4343
}
4444

4545
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
@@ -49,8 +49,6 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
4949
Browser: f.Browser,
5050
}
5151

52-
var draft bool
53-
5452
cmd := &cobra.Command{
5553
Use: "list",
5654
Short: "List pull requests in a repository",
@@ -83,10 +81,6 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
8381
return cmdutil.FlagErrorf("invalid value for --limit: %v", opts.LimitResults)
8482
}
8583

86-
if cmd.Flags().Changed("draft") {
87-
opts.Draft = strconv.FormatBool(draft)
88-
}
89-
9084
if err := cmdutil.MutuallyExclusive(
9185
"specify only `--author` or `--app`",
9286
opts.Author != "",
@@ -112,7 +106,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
112106
cmd.Flags().StringVar(&opts.AppAuthor, "app", "", "Filter by GitHub App author")
113107
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
114108
cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search pull requests with `query`")
115-
cmd.Flags().BoolVarP(&draft, "draft", "d", false, "Filter by draft state")
109+
cmdutil.NilBoolFlag(cmd, &opts.Draft, "draft", "d", "Filter by draft state")
116110

117111
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields)
118112

pkg/cmd/pr/list/list_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestPRList_filteringAssignee(t *testing.T) {
183183
http.Register(
184184
httpmock.GraphQL(`query PullRequestSearch\b`),
185185
httpmock.GraphQLQuery(`{}`, func(_ string, params map[string]interface{}) {
186-
assert.Equal(t, `repo:OWNER/REPO is:pr is:merged assignee:hubot label:"needs tests" base:develop`, params["q"].(string))
186+
assert.Equal(t, `assignee:hubot base:develop label:"needs tests" repo:OWNER/REPO state:merged type:pr`, params["q"].(string))
187187
}))
188188

189189
_, err := runCommand(http, true, `-s merged -l "needs tests" -a hubot -B develop`)
@@ -201,12 +201,12 @@ func TestPRList_filteringDraft(t *testing.T) {
201201
{
202202
name: "draft",
203203
cli: "--draft",
204-
expectedQuery: `repo:OWNER/REPO is:pr is:open draft:true`,
204+
expectedQuery: `draft:true repo:OWNER/REPO state:open type:pr`,
205205
},
206206
{
207207
name: "non-draft",
208208
cli: "--draft=false",
209-
expectedQuery: `repo:OWNER/REPO is:pr is:open draft:false`,
209+
expectedQuery: `draft:false repo:OWNER/REPO state:open type:pr`,
210210
},
211211
}
212212

@@ -238,22 +238,22 @@ func TestPRList_filteringAuthor(t *testing.T) {
238238
{
239239
name: "author @me",
240240
cli: `--author "@me"`,
241-
expectedQuery: `repo:OWNER/REPO is:pr is:open author:@me`,
241+
expectedQuery: `author:@me repo:OWNER/REPO state:open type:pr`,
242242
},
243243
{
244244
name: "author user",
245245
cli: `--author "monalisa"`,
246-
expectedQuery: `repo:OWNER/REPO is:pr is:open author:monalisa`,
246+
expectedQuery: `author:monalisa repo:OWNER/REPO state:open type:pr`,
247247
},
248248
{
249249
name: "app author",
250250
cli: `--author "app/dependabot"`,
251-
expectedQuery: `repo:OWNER/REPO is:pr is:open author:app/dependabot`,
251+
expectedQuery: `author:app/dependabot repo:OWNER/REPO state:open type:pr`,
252252
},
253253
{
254254
name: "app author with app option",
255255
cli: `--app "dependabot"`,
256-
expectedQuery: `repo:OWNER/REPO is:pr is:open author:app/dependabot`,
256+
expectedQuery: `author:app/dependabot repo:OWNER/REPO state:open type:pr`,
257257
},
258258
}
259259

@@ -292,17 +292,17 @@ func TestPRList_web(t *testing.T) {
292292
{
293293
name: "filters",
294294
cli: "-a peter -l bug -l docs -L 10 -s merged -B trunk",
295-
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Amerged+assignee%3Apeter+label%3Abug+label%3Adocs+base%3Atrunk",
295+
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=assignee%3Apeter+base%3Atrunk+label%3Abug+label%3Adocs+state%3Amerged+type%3Apr",
296296
},
297297
{
298298
name: "draft",
299299
cli: "--draft=true",
300-
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Aopen+draft%3Atrue",
300+
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=draft%3Atrue+state%3Aopen+type%3Apr",
301301
},
302302
{
303303
name: "non-draft",
304304
cli: "--draft=0",
305-
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Aopen+draft%3Afalse",
305+
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=draft%3Afalse+state%3Aopen+type%3Apr",
306306
},
307307
}
308308

pkg/cmd/pr/shared/params.go

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/cli/cli/v2/api"
99
"github.com/cli/cli/v2/internal/ghrepo"
10-
"github.com/cli/cli/v2/pkg/githubsearch"
10+
"github.com/cli/cli/v2/pkg/search"
1111
"github.com/google/shlex"
1212
)
1313

@@ -158,7 +158,7 @@ type FilterOptions struct {
158158
Mention string
159159
Milestone string
160160
Search string
161-
Draft string
161+
Draft *bool
162162
Fields []string
163163
}
164164

@@ -207,49 +207,20 @@ func ListURLWithQuery(listURL string, options FilterOptions) (string, error) {
207207
}
208208

209209
func SearchQueryBuild(options FilterOptions) string {
210-
q := githubsearch.NewQuery()
211-
switch options.Entity {
212-
case "issue":
213-
q.SetType(githubsearch.Issue)
214-
case "pr":
215-
q.SetType(githubsearch.PullRequest)
216-
}
217-
218-
switch options.State {
219-
case "open":
220-
q.SetState(githubsearch.Open)
221-
case "closed":
222-
q.SetState(githubsearch.Closed)
223-
case "merged":
224-
q.SetState(githubsearch.Merged)
225-
}
226-
227-
if options.Assignee != "" {
228-
q.AssignedTo(options.Assignee)
229-
}
230-
for _, label := range options.Labels {
231-
q.AddLabel(label)
232-
}
233-
if options.Author != "" {
234-
q.AuthoredBy(options.Author)
235-
}
236-
if options.BaseBranch != "" {
237-
q.SetBaseBranch(options.BaseBranch)
238-
}
239-
if options.HeadBranch != "" {
240-
q.SetHeadBranch(options.HeadBranch)
241-
}
242-
if options.Mention != "" {
243-
q.Mentions(options.Mention)
244-
}
245-
if options.Milestone != "" {
246-
q.InMilestone(options.Milestone)
247-
}
248-
if options.Search != "" {
249-
q.AddQuery(options.Search)
250-
}
251-
if options.Draft != "" {
252-
q.SetDraft(options.Draft)
210+
q := search.Query{
211+
Keywords: strings.Split(options.Search, " "),
212+
Qualifiers: search.Qualifiers{
213+
Assignee: options.Assignee,
214+
Author: options.Author,
215+
Base: options.BaseBranch,
216+
Draft: options.Draft,
217+
Head: options.HeadBranch,
218+
Label: options.Labels,
219+
Mentions: options.Mention,
220+
Milestone: options.Milestone,
221+
State: options.State,
222+
Type: options.Entity,
223+
},
253224
}
254225
return q.String()
255226
}

pkg/cmd/pr/shared/params_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
)
1313

1414
func Test_listURLWithQuery(t *testing.T) {
15+
trueBool := true
16+
falseBool := false
17+
1518
type args struct {
1619
listURL string
1720
options FilterOptions
@@ -32,7 +35,7 @@ func Test_listURLWithQuery(t *testing.T) {
3235
State: "open",
3336
},
3437
},
35-
want: "https://example.com/path?a=b&q=is%3Aissue+is%3Aopen",
38+
want: "https://example.com/path?a=b&q=state%3Aopen+type%3Aissue",
3639
wantErr: false,
3740
},
3841
{
@@ -42,10 +45,10 @@ func Test_listURLWithQuery(t *testing.T) {
4245
options: FilterOptions{
4346
Entity: "pr",
4447
State: "open",
45-
Draft: "true",
48+
Draft: &trueBool,
4649
},
4750
},
48-
want: "https://example.com/path?q=is%3Apr+is%3Aopen+draft%3Atrue",
51+
want: "https://example.com/path?q=draft%3Atrue+state%3Aopen+type%3Apr",
4952
wantErr: false,
5053
},
5154
{
@@ -55,10 +58,10 @@ func Test_listURLWithQuery(t *testing.T) {
5558
options: FilterOptions{
5659
Entity: "pr",
5760
State: "open",
58-
Draft: "false",
61+
Draft: &falseBool,
5962
},
6063
},
61-
want: "https://example.com/path?q=is%3Apr+is%3Aopen+draft%3Afalse",
64+
want: "https://example.com/path?q=draft%3Afalse+state%3Aopen+type%3Apr",
6265
wantErr: false,
6366
},
6467
{
@@ -75,7 +78,7 @@ func Test_listURLWithQuery(t *testing.T) {
7578
Mention: "nu",
7679
},
7780
},
78-
want: "https://example.com/path?q=is%3Aissue+is%3Aopen+assignee%3Abo+author%3Aka+mentions%3Anu+base%3Atrunk+head%3Abug-fix",
81+
want: "https://example.com/path?q=assignee%3Abo+author%3Aka+base%3Atrunk+head%3Abug-fix+mentions%3Anu+state%3Aopen+type%3Aissue",
7982
wantErr: false,
8083
},
8184
{
@@ -89,7 +92,7 @@ func Test_listURLWithQuery(t *testing.T) {
8992
Milestone: `Codename "What Was Missing"`,
9093
},
9194
},
92-
want: "https://example.com/path?q=is%3Apr+is%3Aopen+label%3Adocs+label%3A%22help+wanted%22+milestone%3A%22Codename+%5C%22What+Was+Missing%5C%22%22",
95+
want: "https://example.com/path?q=label%3A%22help+wanted%22+label%3Adocs+milestone%3A%22Codename+%5C%22What+Was+Missing%5C%22%22+state%3Aopen+type%3Apr",
9396
wantErr: false,
9497
},
9598
}

0 commit comments

Comments
 (0)
X Tutup