X Tutup
Skip to content

Commit bad5a59

Browse files
committed
Update non-tty comment output
1 parent 8c5e5a3 commit bad5a59

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

pkg/cmd/issue/view/fixtures/issueView_previewFullComments.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"authorAssociation": "NONE",
8686
"body": "Comment 1",
8787
"createdAt": "2020-01-01T12:00:00Z",
88-
"includesCreatedEdit": false,
88+
"includesCreatedEdit": true,
8989
"reactionGroups": [
9090
{
9191
"content": "CONFUSED",

pkg/cmd/issue/view/view.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ type ViewOptions struct {
2727
IO *iostreams.IOStreams
2828
BaseRepo func() (ghrepo.Interface, error)
2929

30-
SelectorArg string
31-
WebMode bool
32-
Comments int
30+
SelectorArg string
31+
WebMode bool
32+
Comments int
33+
CommentsProvided bool
3334
}
3435

3536
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
@@ -54,6 +55,8 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
5455
// support `-R, --repo` override
5556
opts.BaseRepo = f.BaseRepo
5657

58+
opts.CommentsProvided = cmd.Flags().Changed("comments")
59+
5760
if len(args) > 0 {
5861
opts.SelectorArg = args[0]
5962
}
@@ -66,7 +69,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
6669
}
6770

6871
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open an issue in the browser")
69-
cmd.Flags().IntVarP(&opts.Comments, "comments", "c", 1, "View issue comments")
72+
cmd.Flags().IntVarP(&opts.Comments, "comments", "c", 1, "View issue comments sorted by most recent")
7073
cmd.Flags().Lookup("comments").NoOptDefVal = "30"
7174

7275
return cmd
@@ -104,6 +107,11 @@ func viewRun(opts *ViewOptions) error {
104107
if opts.IO.IsStdoutTTY() {
105108
return printHumanIssuePreview(opts.IO, issue)
106109
}
110+
111+
if opts.CommentsProvided {
112+
return printRawIssueComments(opts.IO.Out, issue)
113+
}
114+
107115
return printRawIssuePreview(opts.IO.Out, issue)
108116
}
109117

@@ -122,30 +130,25 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
122130
fmt.Fprintf(out, "assignees:\t%s\n", assignees)
123131
fmt.Fprintf(out, "projects:\t%s\n", projects)
124132
fmt.Fprintf(out, "milestone:\t%s\n", issue.Milestone.Title)
125-
126133
fmt.Fprintln(out, "--")
127134
fmt.Fprintln(out, issue.Body)
128-
fmt.Fprintln(out, "--")
129-
130-
if len(issue.Comments.Nodes) > 0 {
131-
fmt.Fprint(out, rawIssueComments(issue.Comments))
132-
}
133-
134135
return nil
135136
}
136137

137-
func rawIssueComments(comments api.IssueComments) string {
138+
func printRawIssueComments(out io.Writer, issue *api.Issue) error {
138139
var b strings.Builder
139-
for _, comment := range comments.Nodes {
140+
for _, comment := range issue.Comments.Nodes {
140141
fmt.Fprint(&b, rawIssueComment(comment))
141142
}
142-
return b.String()
143+
fmt.Fprint(out, b.String())
144+
return nil
143145
}
144146

145147
func rawIssueComment(comment api.IssueComment) string {
146148
var b strings.Builder
147149
fmt.Fprintf(&b, "author:\t%s\n", comment.Author.Login)
148150
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.AuthorAssociation))
151+
fmt.Fprintf(&b, "edited:\t%t\n", comment.IncludesCreatedEdit)
149152
fmt.Fprintln(&b, "--")
150153
fmt.Fprintln(&b, comment.Body)
151154
fmt.Fprintln(&b, "--")

pkg/cmd/issue/view/view_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func TestIssueView_tty_Comments(t *testing.T) {
360360
expectedOutputs: []string{
361361
`some title`,
362362
`some body`,
363-
`monalisa • Jan 1, 2020`,
363+
`monalisa • Jan 1, 2020 • edited`,
364364
`1 \x{1f615} • 2 \x{1f440} • 3 \x{2764}\x{fe0f} • 4 \x{1f389} • 5 \x{1f604} • 6 \x{1f680} • 7 \x{1f44e} • 8 \x{1f44d}`,
365365
`Comment 1`,
366366
`johnnytest \(contributor\) • Jan 1, 2020`,
@@ -428,55 +428,53 @@ func TestIssueView_nontty_Comments(t *testing.T) {
428428
fixture: "./fixtures/issueView_previewSingleComment.json",
429429
expectedOutputs: []string{
430430
`title:\tsome title`,
431+
`state:\tOPEN`,
431432
`author:\tmarseilles`,
432433
`comments:\t5`,
433434
`some body`,
434-
`author:\tmarseilles`,
435-
`association:\tcollaborator`,
436-
`Comment 5`,
437435
},
438436
},
439437
"with default comments flag": {
440438
cli: "123 --comments",
441439
fixture: "./fixtures/issueView_previewFullComments.json",
442440
expectedOutputs: []string{
443-
`title:\tsome title`,
444-
`author:\tmarseilles`,
445-
`comments:\t5`,
446-
`some body`,
447441
`author:\tmonalisa`,
448442
`association:\t`,
443+
`edited:\ttrue`,
449444
`Comment 1`,
450445
`author:\tjohnnytest`,
451446
`association:\tcontributor`,
447+
`edited:\tfalse`,
452448
`Comment 2`,
453449
`author:\telvisp`,
454450
`association:\tmember`,
451+
`edited:\tfalse`,
455452
`Comment 3`,
456453
`author:\tloislane`,
457454
`association:\towner`,
455+
`edited:\tfalse`,
458456
`Comment 4`,
459457
`author:\tmarseilles`,
460458
`association:\tcollaborator`,
459+
`edited:\tfalse`,
461460
`Comment 5`,
462461
},
463462
},
464463
"with specified comments flag": {
465464
cli: "123 --comments=3",
466465
fixture: "./fixtures/issueView_previewThreeComments.json",
467466
expectedOutputs: []string{
468-
`title:\tsome title`,
469-
`author:\tmarseilles`,
470-
`comments:\t5`,
471-
`some body`,
472467
`author:\telvisp`,
473468
`association:\tmember`,
469+
`edited:\tfalse`,
474470
`Comment 3`,
475471
`author:\tloislane`,
476472
`association:\towner`,
473+
`edited:\tfalse`,
477474
`Comment 4`,
478475
`author:\tmarseilles`,
479476
`association:\tcollaborator`,
477+
`edited:\tfalse`,
480478
`Comment 5`,
481479
},
482480
},

0 commit comments

Comments
 (0)
X Tutup