Merge pull request #638 from doi-t/show-the-most-recent-pr

Only show the most recent PR for the current branch
This commit is contained in:
Mislav Marohnić 2020-03-13 10:07:12 +01:00 committed by GitHub
commit 8a4fd432ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 164 additions and 73 deletions

View file

@ -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

View file

@ -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)

View file

@ -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()

View file

@ -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": []
}
}
}

View file

@ -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": []
}
}
}