Skip to content

Commit d09561d

Browse files
committed
💅 simplify sortPullRequestsByState
1 parent 2ac68cc commit d09561d

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

api/queries_pr.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
736736
return nil, err
737737
}
738738

739-
prs := sortPullRequestsByState(resp.Repository.PullRequests.Nodes)
739+
prs := resp.Repository.PullRequests.Nodes
740+
sortPullRequestsByState(prs)
741+
740742
for _, pr := range prs {
741743
if pr.HeadLabel() == headBranch {
742744
if baseBranch != "" {
@@ -751,6 +753,13 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
751753
return nil, &NotFoundError{fmt.Errorf("no pull requests found for branch %q", headBranch)}
752754
}
753755

756+
// sortPullRequestsByState sorts a PullRequest slice by open-first
757+
func sortPullRequestsByState(prs []PullRequest) {
758+
sort.SliceStable(prs, func(a, b int) bool {
759+
return prs[a].State == "OPEN"
760+
})
761+
}
762+
754763
// CreatePullRequest creates a pull request in a GitHub repository
755764
func CreatePullRequest(client *Client, repo *Repository, params map[string]interface{}) (*PullRequest, error) {
756765
query := `
@@ -1152,14 +1161,6 @@ func BranchDeleteRemote(client *Client, repo ghrepo.Interface, branch string) er
11521161
return client.REST(repo.RepoHost(), "DELETE", path, nil, nil)
11531162
}
11541163

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-
11631164
func min(a, b int) int {
11641165
if a < b {
11651166
return a

api/queries_pr_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,31 @@ func Test_determinePullRequestFeatures(t *testing.T) {
139139
}
140140
}
141141

142-
func Test_sortPullRequestsByPrecedence(t *testing.T) {
143-
prs := sortPullRequestsByState([]PullRequest{
142+
func Test_sortPullRequestsByState(t *testing.T) {
143+
prs := []PullRequest{
144144
{
145-
BaseRefName: "test-PR",
145+
BaseRefName: "test1",
146146
State: "MERGED",
147147
},
148148
{
149-
BaseRefName: "test-PR",
149+
BaseRefName: "test2",
150150
State: "CLOSED",
151151
},
152152
{
153-
BaseRefName: "test-PR",
153+
BaseRefName: "test3",
154154
State: "OPEN",
155155
},
156-
})
157-
if prs[0].State != "OPEN" {
158-
t.Errorf("sortPullRequestsByPrecedence() = got %s, want \"OPEN\"", prs[0].State)
156+
}
157+
158+
sortPullRequestsByState(prs)
159+
160+
if prs[0].BaseRefName != "test3" {
161+
t.Errorf("prs[0]: got %s, want %q", prs[0].BaseRefName, "test3")
162+
}
163+
if prs[1].BaseRefName != "test1" {
164+
t.Errorf("prs[1]: got %s, want %q", prs[1].BaseRefName, "test1")
165+
}
166+
if prs[2].BaseRefName != "test2" {
167+
t.Errorf("prs[2]: got %s, want %q", prs[2].BaseRefName, "test2")
159168
}
160169
}

0 commit comments

Comments
 (0)