X Tutup
Skip to content

Commit e10ccef

Browse files
authored
Merge pull request cli#667 from doi-t/view-the-current-state
Show the state (open, closed, merged) in issue view and pr view
2 parents 43b28f7 + 7eabbf5 commit e10ccef

21 files changed

+500
-159
lines changed

api/queries_issue.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Issue struct {
2424
URL string
2525
State string
2626
Body string
27+
CreatedAt time.Time
2728
UpdatedAt time.Time
2829
Comments struct {
2930
TotalCount int
@@ -278,6 +279,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
278279
hasIssuesEnabled
279280
issue(number: $issue_number) {
280281
title
282+
state
281283
body
282284
author {
283285
login
@@ -292,6 +294,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
292294
}
293295
number
294296
url
297+
createdAt
295298
}
296299
}
297300
}`

api/queries_pr.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
301301
url
302302
number
303303
title
304+
state
304305
body
305306
author {
306307
login
@@ -320,6 +321,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
320321
}
321322
}
322323
isCrossRepository
324+
isDraft
323325
maintainerCanModify
324326
}
325327
}
@@ -356,6 +358,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
356358
nodes {
357359
number
358360
title
361+
state
359362
body
360363
author {
361364
login

command/issue.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ func issueView(cmd *cobra.Command, args []string) error {
228228

229229
}
230230

231+
func issueStateTitleWithColor(state string) string {
232+
colorFunc := colorFuncForState(state)
233+
return colorFunc(strings.Title(strings.ToLower(state)))
234+
}
235+
231236
func listHeader(repoName string, itemName string, matchCount int, totalMatchCount int, hasFilters bool) string {
232237
if totalMatchCount == 0 {
233238
if hasFilters {
@@ -248,17 +253,16 @@ func listHeader(repoName string, itemName string, matchCount int, totalMatchCoun
248253
}
249254

250255
func printIssuePreview(out io.Writer, issue *api.Issue) error {
251-
coloredLabels := labelList(*issue)
252-
if coloredLabels != "" {
253-
coloredLabels = utils.Gray(fmt.Sprintf("(%s)", coloredLabels))
254-
}
256+
now := time.Now()
257+
ago := now.Sub(issue.CreatedAt)
255258

256259
fmt.Fprintln(out, utils.Bold(issue.Title))
260+
fmt.Fprintf(out, "%s", issueStateTitleWithColor(issue.State))
257261
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
258-
"opened by %s. %s. %s",
262+
" %s opened %s • %s",
259263
issue.Author.Login,
264+
utils.FuzzyAgo(ago),
260265
utils.Pluralize(issue.Comments.TotalCount, "comment"),
261-
coloredLabels,
262266
)))
263267

264268
if issue.Body != "" {

command/issue_test.go

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/cli/cli/internal/run"
1414
"github.com/cli/cli/test"
15+
"github.com/google/go-cmp/cmp"
1516
)
1617

1718
func TestIssueStatus(t *testing.T) {
@@ -284,81 +285,77 @@ func TestIssueView_web_numberArgWithHash(t *testing.T) {
284285
eq(t, url, "https://github.com/OWNER/REPO/issues/123")
285286
}
286287

287-
func TestIssueView(t *testing.T) {
288-
initBlankContext("OWNER/REPO", "master")
289-
http := initFakeHTTP()
290-
http.StubRepoResponse("OWNER", "REPO")
291-
292-
http.StubResponse(200, bytes.NewBufferString(`
293-
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
294-
"number": 123,
295-
"body": "**bold story**",
296-
"title": "ix of coins",
297-
"author": {
298-
"login": "marseilles"
288+
func TestIssueView_Preview(t *testing.T) {
289+
tests := map[string]struct {
290+
ownerRepo string
291+
command string
292+
fixture string
293+
expectedOutputs []string
294+
}{
295+
"Open issue": {
296+
ownerRepo: "master",
297+
command: "issue view 123",
298+
fixture: "../test/fixtures/issueView_preview.json",
299+
expectedOutputs: []string{
300+
"ix of coins",
301+
"Open • marseilles opened about 292 years ago • 9 comments",
302+
"bold story",
303+
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
304+
},
299305
},
300-
"labels": {
301-
"nodes": [
302-
{"name": "tarot"}
303-
]
306+
"Open issue with no label": {
307+
ownerRepo: "master",
308+
command: "issue view 123",
309+
fixture: "../test/fixtures/issueView_previewNoLabel.json",
310+
expectedOutputs: []string{
311+
"ix of coins",
312+
"Open • marseilles opened about 292 years ago • 9 comments",
313+
"bold story",
314+
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
315+
},
304316
},
305-
"comments": {
306-
"totalCount": 9
317+
"Open issue with empty body": {
318+
ownerRepo: "master",
319+
command: "issue view 123",
320+
fixture: "../test/fixtures/issueView_previewWithEmptyBody.json",
321+
expectedOutputs: []string{
322+
"ix of coins",
323+
"Open • marseilles opened about 292 years ago • 9 comments",
324+
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
325+
},
326+
},
327+
"Closed issue": {
328+
ownerRepo: "master",
329+
command: "issue view 123",
330+
fixture: "../test/fixtures/issueView_previewClosedState.json",
331+
expectedOutputs: []string{
332+
"ix of coins",
333+
"Closed • marseilles opened about 292 years ago • 9 comments",
334+
"bold story",
335+
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123",
336+
},
307337
},
308-
"url": "https://github.com/OWNER/REPO/issues/123"
309-
} } } }
310-
`))
311-
312-
output, err := RunCommand(issueViewCmd, "issue view 123")
313-
if err != nil {
314-
t.Errorf("error running command `issue view`: %v", err)
315338
}
339+
for name, tc := range tests {
340+
t.Run(name, func(t *testing.T) {
341+
initBlankContext("OWNER/REPO", tc.ownerRepo)
342+
http := initFakeHTTP()
343+
http.StubRepoResponse("OWNER", "REPO")
316344

317-
eq(t, output.Stderr(), "")
318-
319-
test.ExpectLines(t, output.String(),
320-
"ix of coins",
321-
`opened by marseilles. 9 comments. \(tarot\)`,
322-
"bold story",
323-
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
324-
}
345+
jsonFile, _ := os.Open(tc.fixture)
346+
defer jsonFile.Close()
347+
http.StubResponse(200, jsonFile)
325348

326-
func TestIssueView_WithEmptyBody(t *testing.T) {
327-
initBlankContext("OWNER/REPO", "master")
328-
http := initFakeHTTP()
329-
http.StubRepoResponse("OWNER", "REPO")
349+
output, err := RunCommand(issueViewCmd, tc.command)
350+
if err != nil {
351+
t.Errorf("error running command `%v`: %v", tc.command, err)
352+
}
330353

331-
http.StubResponse(200, bytes.NewBufferString(`
332-
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
333-
"number": 123,
334-
"body": "",
335-
"title": "ix of coins",
336-
"author": {
337-
"login": "marseilles"
338-
},
339-
"labels": {
340-
"nodes": [
341-
{"name": "tarot"}
342-
]
343-
},
344-
"comments": {
345-
"totalCount": 9
346-
},
347-
"url": "https://github.com/OWNER/REPO/issues/123"
348-
} } } }
349-
`))
354+
eq(t, output.Stderr(), "")
350355

351-
output, err := RunCommand(issueViewCmd, "issue view 123")
352-
if err != nil {
353-
t.Errorf("error running command `issue view`: %v", err)
356+
test.ExpectLines(t, output.String(), tc.expectedOutputs...)
357+
})
354358
}
355-
356-
eq(t, output.Stderr(), "")
357-
358-
test.ExpectLines(t, output.String(),
359-
"ix of coins",
360-
`opened by marseilles. 9 comments. \(tarot\)`,
361-
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
362359
}
363360

364361
func TestIssueView_web_notFound(t *testing.T) {
@@ -659,3 +656,23 @@ func Test_listHeader(t *testing.T) {
659656
})
660657
}
661658
}
659+
660+
func TestIssueStateTitleWithColor(t *testing.T) {
661+
tests := map[string]struct {
662+
state string
663+
want string
664+
}{
665+
"Open state": {state: "OPEN", want: "Open"},
666+
"Closed state": {state: "CLOSED", want: "Closed"},
667+
}
668+
669+
for name, tc := range tests {
670+
t.Run(name, func(t *testing.T) {
671+
got := issueStateTitleWithColor(tc.state)
672+
diff := cmp.Diff(tc.want, got)
673+
if diff != "" {
674+
t.Fatalf(diff)
675+
}
676+
})
677+
}
678+
}

command/pr.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,22 @@ func prList(cmd *cobra.Command, args []string) error {
226226
return nil
227227
}
228228

229+
func prStateTitleWithColor(pr api.PullRequest) string {
230+
prStateColorFunc := colorFuncForPR(pr)
231+
if pr.State == "OPEN" && pr.IsDraft {
232+
return prStateColorFunc(strings.Title(strings.ToLower("Draft")))
233+
}
234+
return prStateColorFunc(strings.Title(strings.ToLower(pr.State)))
235+
}
236+
229237
func colorFuncForPR(pr api.PullRequest) func(string) string {
230238
if pr.State == "OPEN" && pr.IsDraft {
231239
return utils.Gray
232-
} else {
233-
return colorFuncForState(pr.State)
234240
}
241+
return colorFuncForState(pr.State)
235242
}
236243

244+
// colorFuncForState returns a color function for a PR/Issue state
237245
func colorFuncForState(state string) func(string) string {
238246
switch state {
239247
case "OPEN":
@@ -320,8 +328,9 @@ func prView(cmd *cobra.Command, args []string) error {
320328

321329
func printPrPreview(out io.Writer, pr *api.PullRequest) error {
322330
fmt.Fprintln(out, utils.Bold(pr.Title))
331+
fmt.Fprintf(out, "%s", prStateTitleWithColor(*pr))
323332
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
324-
"%s wants to merge %s into %s from %s",
333+
"%s wants to merge %s into %s from %s",
325334
pr.Author.Login,
326335
utils.Pluralize(pr.Commits.TotalCount, "commit"),
327336
pr.BaseRefName,
@@ -453,8 +462,7 @@ func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
453462
fmt.Fprint(w, utils.Green("✓ Approved"))
454463
}
455464
} else {
456-
s := strings.Title(strings.ToLower(pr.State))
457-
fmt.Fprintf(w, " - %s", prStateColorFunc(s))
465+
fmt.Fprintf(w, " - %s", prStateTitleWithColor(pr))
458466
}
459467

460468
fmt.Fprint(w, "\n")

0 commit comments

Comments
 (0)
X Tutup