diff --git a/api/queries_pr.go b/api/queries_pr.go index 34884d16f..ee80c2f7f 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -10,7 +10,7 @@ import ( type PullRequestsPayload struct { ViewerCreated PullRequestAndTotalCount ReviewRequested PullRequestAndTotalCount - CurrentPRs []PullRequest + CurrentPR *PullRequest } type PullRequestAndTotalCount struct { @@ -262,13 +262,12 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu reviewRequested = append(reviewRequested, edge.Node) } - var currentPRs []PullRequest - if resp.Repository.PullRequest != nil { - currentPRs = append(currentPRs, *resp.Repository.PullRequest) - } else { + var currentPR = resp.Repository.PullRequest + if currentPR == nil { for _, edge := range resp.Repository.PullRequests.Edges { if edge.Node.HeadLabel() == currentPRHeadRef { - currentPRs = append(currentPRs, edge.Node) + currentPR = &edge.Node + break // Take the most recent PR for the current branch } } } @@ -282,7 +281,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu PullRequests: reviewRequested, TotalCount: resp.ReviewRequested.TotalCount, }, - CurrentPRs: currentPRs, + CurrentPR: currentPR, } return &payload, nil diff --git a/command/pr.go b/command/pr.go index 7a1b00047..e1c8e796b 100644 --- a/command/pr.go +++ b/command/pr.go @@ -98,8 +98,8 @@ func prStatus(cmd *cobra.Command, args []string) error { fmt.Fprintln(out, "") printHeader(out, "Current branch") - if prPayload.CurrentPRs != nil { - printPrs(out, 0, prPayload.CurrentPRs...) + if prPayload.CurrentPR != nil { + printPrs(out, 0, *prPayload.CurrentPR) } else { message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]")) printMessage(out, message) diff --git a/command/pr_test.go b/command/pr_test.go index e72a7d64e..f024792e3 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -182,6 +182,38 @@ func TestPRStatus_closedMerged(t *testing.T) { } } +func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) { + initBlankContext("OWNER/REPO", "blueberries") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prStatusCmd, "pr status") + if err != nil { + t.Errorf("error running command `pr status`: %v", err) + } + + expectedLine := regexp.MustCompile(`#10 Blueberries are certainly a good fruit \[blueberries\]`) + if !expectedLine.MatchString(output.String()) { + t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output) + return + } + + unexpectedLines := []*regexp.Regexp{ + regexp.MustCompile(`#9 Blueberries are a good fruit \[blueberries\] - Merged`), + regexp.MustCompile(`#8 Blueberries are probably a good fruit \[blueberries\] - Closed`), + } + for _, r := range unexpectedLines { + if r.MatchString(output.String()) { + t.Errorf("output unexpectedly match regexp /%s/\n> output\n%s\n", r, output) + return + } + } +} + func TestPRStatus_blankSlate(t *testing.T) { initBlankContext("OWNER/REPO", "blueberries") http := initFakeHTTP() diff --git a/test/fixtures/prStatusClosedMerged.json b/test/fixtures/prStatusClosedMerged.json index 25b4391dc..03e31637e 100644 --- a/test/fixtures/prStatusClosedMerged.json +++ b/test/fixtures/prStatusClosedMerged.json @@ -1,66 +1,65 @@ { - "data": { - "repository": { - "pullRequests": { - "totalCount": 1, - "edges": [ - { - "node": { - "number": 8, - "title": "Blueberries are a good fruit", - "state": "OPEN", - "url": "https://github.com/cli/cli/pull/8", - "headRefName": "blueberries", - "reviewDecision": "CHANGES_REQUESTED", - "commits": { - "nodes": [ - { - "commit": { - "statusCheckRollup": { - "contexts": { - "nodes": [ - { - "state": "SUCCESS" - } - ] - } - } - } - } - ] - } - } - } - ] - } - }, - "viewerCreated": { - "totalCount": 1, - "edges": [ - { - "node": { - "number": 8, - "state": "CLOSED", - "title": "Strawberries are not actually berries", - "url": "https://github.com/cli/cli/pull/8", - "headRefName": "strawberries" - } - }, - { - "node": { - "number": 8, - "state": "MERGED", - "title": "Bananas are berries", - "url": "https://github.com/cli/cli/pull/8", - "headRefName": "banananana" - } - } - ] - }, - "reviewRequested": { - "totalCount": 0, - "edges": [] - } - } + "data": { + "repository": { + "pullRequests": { + "totalCount": 1, + "edges": [ + { + "node": { + "number": 8, + "title": "Blueberries are a good fruit", + "state": "OPEN", + "url": "https://github.com/cli/cli/pull/8", + "headRefName": "blueberries", + "reviewDecision": "CHANGES_REQUESTED", + "commits": { + "nodes": [ + { + "commit": { + "statusCheckRollup": { + "contexts": { + "nodes": [ + { + "state": "SUCCESS" + } + ] + } + } + } + } + ] + } + } + } + ] + } + }, + "viewerCreated": { + "totalCount": 1, + "edges": [ + { + "node": { + "number": 10, + "state": "CLOSED", + "title": "Strawberries are not actually berries", + "url": "https://github.com/cli/cli/pull/10", + "headRefName": "strawberries" + } + }, + { + "node": { + "number": 9, + "state": "MERGED", + "title": "Bananas are berries", + "url": "https://github.com/cli/cli/pull/9", + "headRefName": "banananana" + } + } + ] + }, + "reviewRequested": { + "totalCount": 0, + "edges": [] + } } - \ No newline at end of file +} diff --git a/test/fixtures/prStatusCurrentBranch.json b/test/fixtures/prStatusCurrentBranch.json new file mode 100644 index 000000000..d024d48c3 --- /dev/null +++ b/test/fixtures/prStatusCurrentBranch.json @@ -0,0 +1,61 @@ +{ + "data": { + "repository": { + "pullRequests": { + "totalCount": 3, + "edges": [ + { + "node": { + "number": 10, + "title": "Blueberries are certainly a good fruit", + "state": "OPEN", + "url": "https://github.com/PARENT/REPO/pull/10", + "headRefName": "blueberries", + "isDraft": false, + "headRepositoryOwner": { + "login": "OWNER/REPO" + }, + "isCrossRepository": false + } + }, + { + "node": { + "number": 9, + "title": "Blueberries are a good fruit", + "state": "MERGED", + "url": "https://github.com/PARENT/REPO/pull/9", + "headRefName": "blueberries", + "isDraft": false, + "headRepositoryOwner": { + "login": "OWNER/REPO" + }, + "isCrossRepository": false + } + }, + { + "node": { + "number": 8, + "title": "Blueberries are probably a good fruit", + "state": "CLOSED", + "url": "https://github.com/PARENT/REPO/pull/8", + "headRefName": "blueberries", + "isDraft": false, + "headRepositoryOwner": { + "login": "OWNER/REPO" + }, + "isCrossRepository": false + } + } + ] + } + }, + "viewerCreated": { + "totalCount": 0, + "edges": [] + }, + "reviewRequested": { + "totalCount": 0, + "edges": [] + } + } +}