X Tutup
Skip to content

Commit 77ed118

Browse files
author
Nate Smith
authored
Merge pull request cli#880 from cli/pr-status-crash
Fix `pr status -R` crash with closed PR on the default branch
2 parents 27de1fd + 93c61a8 commit 77ed118

8 files changed

+98
-13
lines changed

api/queries_pr.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type PullRequestsPayload struct {
2828
ViewerCreated PullRequestAndTotalCount
2929
ReviewRequested PullRequestAndTotalCount
3030
CurrentPR *PullRequest
31+
DefaultBranch string
3132
}
3233

3334
type PullRequestAndTotalCount struct {
@@ -204,6 +205,9 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
204205

205206
type response struct {
206207
Repository struct {
208+
DefaultBranchRef struct {
209+
Name string
210+
}
207211
PullRequests edges
208212
PullRequest *PullRequest
209213
}
@@ -252,6 +256,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
252256
queryPrefix := `
253257
query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
254258
repository(owner: $owner, name: $repo) {
259+
defaultBranchRef { name }
255260
pullRequests(headRefName: $headRefName, first: $per_page, orderBy: { field: CREATED_AT, direction: DESC }) {
256261
totalCount
257262
edges {
@@ -266,6 +271,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
266271
queryPrefix = `
267272
query($owner: String!, $repo: String!, $number: Int!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) {
268273
repository(owner: $owner, name: $repo) {
274+
defaultBranchRef { name }
269275
pullRequest(number: $number) {
270276
...prWithReviews
271277
}
@@ -345,7 +351,8 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu
345351
PullRequests: reviewRequested,
346352
TotalCount: resp.ReviewRequested.TotalCount,
347353
},
348-
CurrentPR: currentPR,
354+
CurrentPR: currentPR,
355+
DefaultBranch: resp.Repository.DefaultBranchRef.Name,
349356
}
350357

351358
return &payload, nil

command/pr.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,15 @@ func prStatus(cmd *cobra.Command, args []string) error {
117117
printHeader(out, "Current branch")
118118
currentPR := prPayload.CurrentPR
119119
currentBranch, _ := ctx.Branch()
120-
noPRMessage := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
120+
if currentPR != nil && currentPR.State != "OPEN" && prPayload.DefaultBranch == currentBranch {
121+
currentPR = nil
122+
}
121123
if currentPR != nil {
122-
if baseRepo.(*api.Repository).DefaultBranchRef.Name == currentBranch && currentPR.State != "OPEN" {
123-
printMessage(out, noPRMessage)
124-
} else {
125-
printPrs(out, 0, *currentPR)
126-
}
124+
printPrs(out, 1, *currentPR)
127125
} else if currentPRHeadRef == "" {
128126
printMessage(out, " There is no current branch")
129127
} else {
130-
printMessage(out, noPRMessage)
128+
printMessage(out, fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]")))
131129
}
132130
fmt.Fprintln(out)
133131

command/pr_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ func TestPRStatus_currentBranch_defaultBranch(t *testing.T) {
162162
}
163163
}
164164

165+
func TestPRStatus_currentBranch_defaultBranch_repoFlag(t *testing.T) {
166+
initBlankContext("", "OWNER/REPO", "blueberries")
167+
http := initFakeHTTP()
168+
169+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosedOnDefaultBranch.json")
170+
defer jsonFile.Close()
171+
http.StubResponse(200, jsonFile)
172+
173+
output, err := RunCommand("pr status -R OWNER/REPO")
174+
if err != nil {
175+
t.Errorf("error running command `pr status`: %v", err)
176+
}
177+
178+
expectedLine := regexp.MustCompile(`#8 Blueberries are a good fruit \[blueberries\]`)
179+
if expectedLine.MatchString(output.String()) {
180+
t.Errorf("output not expected to match regexp /%s/\n> output\n%s\n", expectedLine, output)
181+
return
182+
}
183+
}
184+
165185
func TestPRStatus_currentBranch_Closed(t *testing.T) {
166186
initBlankContext("", "OWNER/REPO", "blueberries")
167187
http := initFakeHTTP()
@@ -188,7 +208,7 @@ func TestPRStatus_currentBranch_Closed_defaultBranch(t *testing.T) {
188208
http := initFakeHTTP()
189209
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
190210

191-
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosed.json")
211+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosedOnDefaultBranch.json")
192212
defer jsonFile.Close()
193213
http.StubResponse(200, jsonFile)
194214

@@ -230,7 +250,7 @@ func TestPRStatus_currentBranch_Merged_defaultBranch(t *testing.T) {
230250
http := initFakeHTTP()
231251
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
232252

233-
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchMerged.json")
253+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchMergedOnDefaultBranch.json")
234254
defer jsonFile.Close()
235255
http.StubResponse(200, jsonFile)
236256

test/fixtures/prStatusCurrentBranch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"headRefName": "blueberries",
1414
"isDraft": false,
1515
"headRepositoryOwner": {
16-
"login": "OWNER/REPO"
16+
"login": "OWNER"
1717
},
1818
"isCrossRepository": false
1919
}
@@ -27,7 +27,7 @@
2727
"headRefName": "blueberries",
2828
"isDraft": false,
2929
"headRepositoryOwner": {
30-
"login": "OWNER/REPO"
30+
"login": "OWNER"
3131
},
3232
"isCrossRepository": false
3333
}
@@ -41,7 +41,7 @@
4141
"headRefName": "blueberries",
4242
"isDraft": false,
4343
"headRepositoryOwner": {
44-
"login": "OWNER/REPO"
44+
"login": "OWNER"
4545
},
4646
"isCrossRepository": false
4747
}

test/fixtures/prStatusCurrentBranchClosed.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"data": {
33
"repository": {
4+
"defaultBranchRef": { "name": "master" },
45
"pullRequests": {
56
"totalCount": 1,
67
"edges": [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"defaultBranchRef": { "name": "blueberries" },
5+
"pullRequests": {
6+
"totalCount": 1,
7+
"edges": [
8+
{
9+
"node": {
10+
"number": 8,
11+
"title": "Blueberries are a good fruit",
12+
"state": "CLOSED",
13+
"url": "https://github.com/cli/cli/pull/8",
14+
"headRefName": "blueberries"
15+
}
16+
}
17+
]
18+
}
19+
},
20+
"viewerCreated": {
21+
"totalCount": 0,
22+
"edges": []
23+
},
24+
"reviewRequested": {
25+
"totalCount": 0,
26+
"edges": []
27+
}
28+
}
29+
}

test/fixtures/prStatusCurrentBranchMerged.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"data": {
33
"repository": {
4+
"defaultBranchRef": { "name": "master" },
45
"pullRequests": {
56
"totalCount": 1,
67
"edges": [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"defaultBranchRef": { "name": "blueberries" },
5+
"pullRequests": {
6+
"totalCount": 1,
7+
"edges": [
8+
{
9+
"node": {
10+
"number": 8,
11+
"title": "Blueberries are a good fruit",
12+
"state": "MERGED",
13+
"url": "https://github.com/cli/cli/pull/8",
14+
"headRefName": "blueberries"
15+
}
16+
}
17+
]
18+
}
19+
},
20+
"viewerCreated": {
21+
"totalCount": 0,
22+
"edges": []
23+
},
24+
"reviewRequested": {
25+
"totalCount": 0,
26+
"edges": []
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)
X Tutup