Skip to content

Commit 8a4fd43

Browse files
authored
Merge pull request cli#638 from doi-t/show-the-most-recent-pr
Only show the most recent PR for the current branch
2 parents c940c4c + 4c4aeb5 commit 8a4fd43

File tree

5 files changed

+164
-73
lines changed

5 files changed

+164
-73
lines changed

api/queries_pr.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type PullRequestsPayload struct {
1111
ViewerCreated PullRequestAndTotalCount
1212
ReviewRequested PullRequestAndTotalCount
13-
CurrentPRs []PullRequest
13+
CurrentPR *PullRequest
1414
}
1515

1616
type PullRequestAndTotalCount struct {
@@ -262,13 +262,12 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
262262
reviewRequested = append(reviewRequested, edge.Node)
263263
}
264264

265-
var currentPRs []PullRequest
266-
if resp.Repository.PullRequest != nil {
267-
currentPRs = append(currentPRs, *resp.Repository.PullRequest)
268-
} else {
265+
var currentPR = resp.Repository.PullRequest
266+
if currentPR == nil {
269267
for _, edge := range resp.Repository.PullRequests.Edges {
270268
if edge.Node.HeadLabel() == currentPRHeadRef {
271-
currentPRs = append(currentPRs, edge.Node)
269+
currentPR = &edge.Node
270+
break // Take the most recent PR for the current branch
272271
}
273272
}
274273
}
@@ -282,7 +281,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
282281
PullRequests: reviewRequested,
283282
TotalCount: resp.ReviewRequested.TotalCount,
284283
},
285-
CurrentPRs: currentPRs,
284+
CurrentPR: currentPR,
286285
}
287286

288287
return &payload, nil

command/pr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func prStatus(cmd *cobra.Command, args []string) error {
9898
fmt.Fprintln(out, "")
9999

100100
printHeader(out, "Current branch")
101-
if prPayload.CurrentPRs != nil {
102-
printPrs(out, 0, prPayload.CurrentPRs...)
101+
if prPayload.CurrentPR != nil {
102+
printPrs(out, 0, *prPayload.CurrentPR)
103103
} else {
104104
message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
105105
printMessage(out, message)

command/pr_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,38 @@ func TestPRStatus_closedMerged(t *testing.T) {
182182
}
183183
}
184184

185+
func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
186+
initBlankContext("OWNER/REPO", "blueberries")
187+
http := initFakeHTTP()
188+
http.StubRepoResponse("OWNER", "REPO")
189+
190+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json")
191+
defer jsonFile.Close()
192+
http.StubResponse(200, jsonFile)
193+
194+
output, err := RunCommand(prStatusCmd, "pr status")
195+
if err != nil {
196+
t.Errorf("error running command `pr status`: %v", err)
197+
}
198+
199+
expectedLine := regexp.MustCompile(`#10 Blueberries are certainly a good fruit \[blueberries\]`)
200+
if !expectedLine.MatchString(output.String()) {
201+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
202+
return
203+
}
204+
205+
unexpectedLines := []*regexp.Regexp{
206+
regexp.MustCompile(`#9 Blueberries are a good fruit \[blueberries\] - Merged`),
207+
regexp.MustCompile(`#8 Blueberries are probably a good fruit \[blueberries\] - Closed`),
208+
}
209+
for _, r := range unexpectedLines {
210+
if r.MatchString(output.String()) {
211+
t.Errorf("output unexpectedly match regexp /%s/\n> output\n%s\n", r, output)
212+
return
213+
}
214+
}
215+
}
216+
185217
func TestPRStatus_blankSlate(t *testing.T) {
186218
initBlankContext("OWNER/REPO", "blueberries")
187219
http := initFakeHTTP()
Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,65 @@
11
{
2-
"data": {
3-
"repository": {
4-
"pullRequests": {
5-
"totalCount": 1,
6-
"edges": [
7-
{
8-
"node": {
9-
"number": 8,
10-
"title": "Blueberries are a good fruit",
11-
"state": "OPEN",
12-
"url": "https://github.com/cli/cli/pull/8",
13-
"headRefName": "blueberries",
14-
"reviewDecision": "CHANGES_REQUESTED",
15-
"commits": {
16-
"nodes": [
17-
{
18-
"commit": {
19-
"statusCheckRollup": {
20-
"contexts": {
21-
"nodes": [
22-
{
23-
"state": "SUCCESS"
24-
}
25-
]
26-
}
27-
}
28-
}
29-
}
30-
]
31-
}
32-
}
33-
}
34-
]
35-
}
36-
},
37-
"viewerCreated": {
38-
"totalCount": 1,
39-
"edges": [
40-
{
41-
"node": {
42-
"number": 8,
43-
"state": "CLOSED",
44-
"title": "Strawberries are not actually berries",
45-
"url": "https://github.com/cli/cli/pull/8",
46-
"headRefName": "strawberries"
47-
}
48-
},
49-
{
50-
"node": {
51-
"number": 8,
52-
"state": "MERGED",
53-
"title": "Bananas are berries",
54-
"url": "https://github.com/cli/cli/pull/8",
55-
"headRefName": "banananana"
56-
}
57-
}
58-
]
59-
},
60-
"reviewRequested": {
61-
"totalCount": 0,
62-
"edges": []
63-
}
64-
}
2+
"data": {
3+
"repository": {
4+
"pullRequests": {
5+
"totalCount": 1,
6+
"edges": [
7+
{
8+
"node": {
9+
"number": 8,
10+
"title": "Blueberries are a good fruit",
11+
"state": "OPEN",
12+
"url": "https://github.com/cli/cli/pull/8",
13+
"headRefName": "blueberries",
14+
"reviewDecision": "CHANGES_REQUESTED",
15+
"commits": {
16+
"nodes": [
17+
{
18+
"commit": {
19+
"statusCheckRollup": {
20+
"contexts": {
21+
"nodes": [
22+
{
23+
"state": "SUCCESS"
24+
}
25+
]
26+
}
27+
}
28+
}
29+
}
30+
]
31+
}
32+
}
33+
}
34+
]
35+
}
36+
},
37+
"viewerCreated": {
38+
"totalCount": 1,
39+
"edges": [
40+
{
41+
"node": {
42+
"number": 10,
43+
"state": "CLOSED",
44+
"title": "Strawberries are not actually berries",
45+
"url": "https://github.com/cli/cli/pull/10",
46+
"headRefName": "strawberries"
47+
}
48+
},
49+
{
50+
"node": {
51+
"number": 9,
52+
"state": "MERGED",
53+
"title": "Bananas are berries",
54+
"url": "https://github.com/cli/cli/pull/9",
55+
"headRefName": "banananana"
56+
}
57+
}
58+
]
59+
},
60+
"reviewRequested": {
61+
"totalCount": 0,
62+
"edges": []
63+
}
6564
}
66-
65+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"pullRequests": {
5+
"totalCount": 3,
6+
"edges": [
7+
{
8+
"node": {
9+
"number": 10,
10+
"title": "Blueberries are certainly a good fruit",
11+
"state": "OPEN",
12+
"url": "https://github.com/PARENT/REPO/pull/10",
13+
"headRefName": "blueberries",
14+
"isDraft": false,
15+
"headRepositoryOwner": {
16+
"login": "OWNER/REPO"
17+
},
18+
"isCrossRepository": false
19+
}
20+
},
21+
{
22+
"node": {
23+
"number": 9,
24+
"title": "Blueberries are a good fruit",
25+
"state": "MERGED",
26+
"url": "https://github.com/PARENT/REPO/pull/9",
27+
"headRefName": "blueberries",
28+
"isDraft": false,
29+
"headRepositoryOwner": {
30+
"login": "OWNER/REPO"
31+
},
32+
"isCrossRepository": false
33+
}
34+
},
35+
{
36+
"node": {
37+
"number": 8,
38+
"title": "Blueberries are probably a good fruit",
39+
"state": "CLOSED",
40+
"url": "https://github.com/PARENT/REPO/pull/8",
41+
"headRefName": "blueberries",
42+
"isDraft": false,
43+
"headRepositoryOwner": {
44+
"login": "OWNER/REPO"
45+
},
46+
"isCrossRepository": false
47+
}
48+
}
49+
]
50+
}
51+
},
52+
"viewerCreated": {
53+
"totalCount": 0,
54+
"edges": []
55+
},
56+
"reviewRequested": {
57+
"totalCount": 0,
58+
"edges": []
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)