Skip to content

Commit 1a1fc64

Browse files
author
Nate Smith
authored
Merge pull request cli#842 from foad/df-805-hide-closed-default
Hide closed/merged PRs from default branch
2 parents e172f31 + e7e3a4f commit 1a1fc64

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

api/fake_http.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ func (f *FakeHTTP) StubRepoResponseWithPermission(owner, repo, permission string
7070
f.responseStubs = append(f.responseStubs, resp)
7171
}
7272

73+
func (f *FakeHTTP) StubRepoResponseWithDefaultBranch(owner, repo, defaultBranch string) {
74+
body := bytes.NewBufferString(fmt.Sprintf(`
75+
{ "data": { "repo_000": {
76+
"id": "REPOID",
77+
"name": "%s",
78+
"owner": {"login": "%s"},
79+
"defaultBranchRef": {
80+
"name": "%s"
81+
},
82+
"viewerPermission": "READ"
83+
} } }
84+
`, repo, owner, defaultBranch))
85+
resp := &http.Response{
86+
StatusCode: 200,
87+
Body: ioutil.NopCloser(body),
88+
}
89+
f.responseStubs = append(f.responseStubs, resp)
90+
}
91+
7392
func (f *FakeHTTP) StubForkedRepoResponse(forkFullName, parentFullName string) {
7493
forkRepo := strings.SplitN(forkFullName, "/", 2)
7594
parentRepo := strings.SplitN(parentFullName, "/", 2)

command/pr.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,19 @@ func prStatus(cmd *cobra.Command, args []string) error {
115115
fmt.Fprintln(out, "")
116116

117117
printHeader(out, "Current branch")
118-
if prPayload.CurrentPR != nil {
119-
printPrs(out, 0, *prPayload.CurrentPR)
118+
currentPR := prPayload.CurrentPR
119+
currentBranch, _ := ctx.Branch()
120+
noPRMessage := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
121+
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+
}
120127
} else if currentPRHeadRef == "" {
121128
printMessage(out, " There is no current branch")
122129
} else {
123-
message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
124-
printMessage(out, message)
130+
printMessage(out, noPRMessage)
125131
}
126132
fmt.Fprintln(out)
127133

command/pr_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
189189
}
190190
}
191191

192+
func TestPRStatus_currentBranch_defaultBranch(t *testing.T) {
193+
initBlankContext("", "OWNER/REPO", "blueberries")
194+
http := initFakeHTTP()
195+
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
196+
197+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json")
198+
defer jsonFile.Close()
199+
http.StubResponse(200, jsonFile)
200+
201+
output, err := RunCommand(prStatusCmd, "pr status")
202+
if err != nil {
203+
t.Errorf("error running command `pr status`: %v", err)
204+
}
205+
206+
expectedLine := regexp.MustCompile(`#10 Blueberries are certainly a good fruit \[blueberries\]`)
207+
if !expectedLine.MatchString(output.String()) {
208+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
209+
return
210+
}
211+
}
212+
192213
func TestPRStatus_currentBranch_Closed(t *testing.T) {
193214
initBlankContext("", "OWNER/REPO", "blueberries")
194215
http := initFakeHTTP()
@@ -210,6 +231,27 @@ func TestPRStatus_currentBranch_Closed(t *testing.T) {
210231
}
211232
}
212233

234+
func TestPRStatus_currentBranch_Closed_defaultBranch(t *testing.T) {
235+
initBlankContext("", "OWNER/REPO", "blueberries")
236+
http := initFakeHTTP()
237+
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
238+
239+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosed.json")
240+
defer jsonFile.Close()
241+
http.StubResponse(200, jsonFile)
242+
243+
output, err := RunCommand(prStatusCmd, "pr status")
244+
if err != nil {
245+
t.Errorf("error running command `pr status`: %v", err)
246+
}
247+
248+
expectedLine := regexp.MustCompile(`There is no pull request associated with \[blueberries\]`)
249+
if !expectedLine.MatchString(output.String()) {
250+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
251+
return
252+
}
253+
}
254+
213255
func TestPRStatus_currentBranch_Merged(t *testing.T) {
214256
initBlankContext("", "OWNER/REPO", "blueberries")
215257
http := initFakeHTTP()
@@ -231,6 +273,27 @@ func TestPRStatus_currentBranch_Merged(t *testing.T) {
231273
}
232274
}
233275

276+
func TestPRStatus_currentBranch_Merged_defaultBranch(t *testing.T) {
277+
initBlankContext("", "OWNER/REPO", "blueberries")
278+
http := initFakeHTTP()
279+
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
280+
281+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchMerged.json")
282+
defer jsonFile.Close()
283+
http.StubResponse(200, jsonFile)
284+
285+
output, err := RunCommand(prStatusCmd, "pr status")
286+
if err != nil {
287+
t.Errorf("error running command `pr status`: %v", err)
288+
}
289+
290+
expectedLine := regexp.MustCompile(`There is no pull request associated with \[blueberries\]`)
291+
if !expectedLine.MatchString(output.String()) {
292+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
293+
return
294+
}
295+
}
296+
234297
func TestPRStatus_blankSlate(t *testing.T) {
235298
initBlankContext("", "OWNER/REPO", "blueberries")
236299
http := initFakeHTTP()

0 commit comments

Comments
 (0)