Skip to content

Commit f6b3357

Browse files
authored
Merge pull request cli#4279 from SiarheiFedartsou/sf-pr-list-head-filter
Add `--head` filter to `gh pr list`
2 parents cb89b8c + 8f581fa commit f6b3357

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

pkg/cmd/pr/list/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters pr
4040
$limit: Int!,
4141
$endCursor: String,
4242
$baseBranch: String,
43+
$headBranch: String,
4344
$state: [PullRequestState!] = OPEN
4445
) {
4546
repository(owner: $owner, name: $repo) {
4647
pullRequests(
4748
states: $state,
4849
baseRefName: $baseBranch,
50+
headRefName: $headBranch,
4951
first: $limit,
5052
after: $endCursor,
5153
orderBy: {field: CREATED_AT, direction: DESC}
@@ -84,6 +86,9 @@ func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters pr
8486
if filters.BaseBranch != "" {
8587
variables["baseBranch"] = filters.BaseBranch
8688
}
89+
if filters.HeadBranch != "" {
90+
variables["headBranch"] = filters.HeadBranch
91+
}
8792

8893
res := api.PullRequestAndTotalCount{}
8994
var check = make(map[int]struct{})

pkg/cmd/pr/list/list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ListOptions struct {
3333

3434
State string
3535
BaseBranch string
36+
HeadBranch string
3637
Labels []string
3738
Author string
3839
Assignee string
@@ -95,6 +96,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
9596
return []string{"open", "closed", "merged", "all"}, cobra.ShellCompDirectiveNoFileComp
9697
})
9798
cmd.Flags().StringVarP(&opts.BaseBranch, "base", "B", "", "Filter by base branch")
99+
cmd.Flags().StringVarP(&opts.HeadBranch, "head", "H", "", "Filter by head branch")
98100
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
99101
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
100102
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
@@ -140,6 +142,7 @@ func listRun(opts *ListOptions) error {
140142
Assignee: opts.Assignee,
141143
Labels: opts.Labels,
142144
BaseBranch: opts.BaseBranch,
145+
HeadBranch: opts.HeadBranch,
143146
Search: opts.Search,
144147
Draft: opts.Draft,
145148
Fields: defaultFields,

pkg/cmd/pr/list/list_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ func TestPRList_filteringClosed(t *testing.T) {
160160
}
161161
}
162162

163+
func TestPRList_filteringHeadBranch(t *testing.T) {
164+
http := initFakeHTTP()
165+
defer http.Verify(t)
166+
167+
http.Register(
168+
httpmock.GraphQL(`query PullRequestList\b`),
169+
httpmock.GraphQLQuery(`{}`, func(_ string, params map[string]interface{}) {
170+
assert.Equal(t, interface{}("bug-fix"), params["headBranch"])
171+
}))
172+
173+
_, err := runCommand(http, true, `-H bug-fix`)
174+
if err != nil {
175+
t.Fatal(err)
176+
}
177+
}
178+
163179
func TestPRList_filteringAssignee(t *testing.T) {
164180
http := initFakeHTTP()
165181
defer http.Verify(t)

pkg/cmd/pr/shared/params.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ type FilterOptions struct {
154154
Labels []string
155155
Author string
156156
BaseBranch string
157+
HeadBranch string
157158
Mention string
158159
Milestone string
159160
Search string
@@ -177,6 +178,9 @@ func (opts *FilterOptions) IsDefault() bool {
177178
if opts.BaseBranch != "" {
178179
return false
179180
}
181+
if opts.HeadBranch != "" {
182+
return false
183+
}
180184
if opts.Mention != "" {
181185
return false
182186
}
@@ -232,6 +236,9 @@ func SearchQueryBuild(options FilterOptions) string {
232236
if options.BaseBranch != "" {
233237
q.SetBaseBranch(options.BaseBranch)
234238
}
239+
if options.HeadBranch != "" {
240+
q.SetHeadBranch(options.HeadBranch)
241+
}
235242
if options.Mention != "" {
236243
q.Mentions(options.Mention)
237244
}

pkg/cmd/pr/shared/params_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ func Test_listURLWithQuery(t *testing.T) {
7171
Assignee: "bo",
7272
Author: "ka",
7373
BaseBranch: "trunk",
74+
HeadBranch: "bug-fix",
7475
Mention: "nu",
7576
},
7677
},
77-
want: "https://example.com/path?q=is%3Aissue+is%3Aopen+assignee%3Abo+author%3Aka+mentions%3Anu+base%3Atrunk",
78+
want: "https://example.com/path?q=is%3Aissue+is%3Aopen+assignee%3Abo+author%3Aka+mentions%3Anu+base%3Atrunk+head%3Abug-fix",
7879
wantErr: false,
7980
},
8081
{

0 commit comments

Comments
 (0)