Skip to content

Commit 0f61272

Browse files
Ian Billettmislav
authored andcommitted
Allow PullRequestForBranch to search in closed pull requests
1 parent c77e6af commit 0f61272

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

api/queries_pr.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"sort"
910
"strings"
1011
"time"
1112

@@ -621,7 +622,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
621622
return &resp.Repository.PullRequest, nil
622623
}
623624

624-
func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, headBranch string) (*PullRequest, error) {
625+
func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, headBranch string, stateFilters []string) (*PullRequest, error) {
625626
type response struct {
626627
Repository struct {
627628
PullRequests struct {
@@ -637,9 +638,9 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
637638
}
638639

639640
query := `
640-
query PullRequestForBranch($owner: String!, $repo: String!, $headRefName: String!) {
641+
query PullRequestForBranch($owner: String!, $repo: String!, $headRefName: String!, $states: [PullRequestState!]) {
641642
repository(owner: $owner, name: $repo) {
642-
pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
643+
pullRequests(headRefName: $headRefName, states: $states, first: 30) {
643644
nodes {
644645
id
645646
number
@@ -726,6 +727,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
726727
"owner": repo.RepoOwner(),
727728
"repo": repo.RepoName(),
728729
"headRefName": branchWithoutOwner,
730+
"states": stateFilters,
729731
}
730732

731733
var resp response
@@ -734,7 +736,8 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
734736
return nil, err
735737
}
736738

737-
for _, pr := range resp.Repository.PullRequests.Nodes {
739+
prs := sortPullRequestsByState(resp.Repository.PullRequests.Nodes)
740+
for _, pr := range prs {
738741
if pr.HeadLabel() == headBranch {
739742
if baseBranch != "" {
740743
if pr.BaseRefName != baseBranch {
@@ -1149,6 +1152,14 @@ func BranchDeleteRemote(client *Client, repo ghrepo.Interface, branch string) er
11491152
return client.REST(repo.RepoHost(), "DELETE", path, nil, nil)
11501153
}
11511154

1155+
// sortPullRequestsByState ensures that OPEN PRs precede non-open states (MERGED, CLOSED)
1156+
func sortPullRequestsByState(prs []PullRequest) []PullRequest {
1157+
sort.SliceStable(prs, func(a, b int) bool {
1158+
return prs[a].State == "OPEN"
1159+
})
1160+
return prs
1161+
}
1162+
11521163
func min(a, b int) int {
11531164
if a < b {
11541165
return a

api/queries_pr_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,23 @@ func Test_determinePullRequestFeatures(t *testing.T) {
138138
})
139139
}
140140
}
141+
142+
func Test_sortPullRequestsByPrecedence(t *testing.T) {
143+
prs := sortPullRequestsByState([]PullRequest{
144+
{
145+
BaseRefName: "test-PR",
146+
State: "MERGED",
147+
},
148+
{
149+
BaseRefName: "test-PR",
150+
State: "CLOSED",
151+
},
152+
{
153+
BaseRefName: "test-PR",
154+
State: "OPEN",
155+
},
156+
})
157+
if prs[0].State != "OPEN" {
158+
t.Errorf("sortPullRequestsByPrecedence() = got %s, want \"OPEN\"", prs[0].State)
159+
}
160+
}

pkg/cmd/pr/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func createRun(opts *CreateOptions) error {
303303
}
304304

305305
if !opts.WebMode {
306-
existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchLabel)
306+
existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchLabel, []string{"OPEN"})
307307
var notFound *api.NotFoundError
308308
if err != nil && !errors.As(err, &notFound) {
309309
return fmt.Errorf("error checking for existing pull request: %w", err)

pkg/cmd/pr/shared/lookup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func PRFromArgs(apiClient *api.Client, baseRepoFn func() (ghrepo.Interface, erro
4343
}
4444

4545
// Last see if it is a branch name
46-
pr, err = api.PullRequestForBranch(apiClient, repo, "", arg)
46+
pr, err = api.PullRequestForBranch(apiClient, repo, "", arg, nil)
4747
return pr, repo, err
4848
}
4949
}
@@ -117,5 +117,5 @@ func prForCurrentBranch(apiClient *api.Client, repo ghrepo.Interface, branchFn f
117117
}
118118
}
119119

120-
return api.PullRequestForBranch(apiClient, repo, "", prHeadRef)
120+
return api.PullRequestForBranch(apiClient, repo, "", prHeadRef, nil)
121121
}

0 commit comments

Comments
 (0)