X Tutup
Skip to content

Commit dee7077

Browse files
committed
Extract shared comment and reaction group code
1 parent 9f101ff commit dee7077

File tree

4 files changed

+142
-245
lines changed

4 files changed

+142
-245
lines changed

pkg/cmd/issue/view/view.go

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ func viewRun(opts *ViewOptions) error {
123123
}
124124

125125
if opts.Comments {
126-
return printRawIssueComments(opts.IO.Out, issue)
126+
fmt.Fprint(opts.IO.Out, prShared.RawCommentList(issue.Comments))
127+
return nil
127128
}
128129

129130
return printRawIssuePreview(opts.IO.Out, issue)
@@ -149,26 +150,6 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
149150
return nil
150151
}
151152

152-
func printRawIssueComments(out io.Writer, issue *api.Issue) error {
153-
var b strings.Builder
154-
for _, comment := range issue.Comments.Nodes {
155-
fmt.Fprint(&b, formatRawIssueComment(comment))
156-
}
157-
fmt.Fprint(out, b.String())
158-
return nil
159-
}
160-
161-
func formatRawIssueComment(comment api.Comment) string {
162-
var b strings.Builder
163-
fmt.Fprintf(&b, "author:\t%s\n", comment.Author.Login)
164-
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.AuthorAssociation))
165-
fmt.Fprintf(&b, "edited:\t%t\n", comment.IncludesCreatedEdit)
166-
fmt.Fprintln(&b, "--")
167-
fmt.Fprintln(&b, comment.Body)
168-
fmt.Fprintln(&b, "--")
169-
return b.String()
170-
}
171-
172153
func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
173154
out := io.Out
174155
now := time.Now()
@@ -186,7 +167,7 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
186167
)
187168

188169
// Reactions
189-
if reactions := reactionGroupList(issue.ReactionGroups); reactions != "" {
170+
if reactions := prShared.ReactionGroupList(issue.ReactionGroups); reactions != "" {
190171
fmt.Fprint(out, reactions)
191172
fmt.Fprintln(out)
192173
}
@@ -224,7 +205,7 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
224205

225206
// Comments
226207
if issue.Comments.TotalCount > 0 {
227-
comments, err := issueCommentList(io, issue.Comments)
208+
comments, err := prShared.CommentList(io, issue.Comments)
228209
if err != nil {
229210
return err
230211
}
@@ -237,75 +218,6 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
237218
return nil
238219
}
239220

240-
func issueCommentList(io *iostreams.IOStreams, comments api.Comments) (string, error) {
241-
var b strings.Builder
242-
cs := io.ColorScheme()
243-
retrievedCount := len(comments.Nodes)
244-
hiddenCount := comments.TotalCount - retrievedCount
245-
246-
if hiddenCount > 0 {
247-
fmt.Fprint(&b, cs.Gray(fmt.Sprintf("———————— Not showing %s ————————", utils.Pluralize(hiddenCount, "comment"))))
248-
fmt.Fprintf(&b, "\n\n\n")
249-
}
250-
251-
for i, comment := range comments.Nodes {
252-
last := i+1 == retrievedCount
253-
cmt, err := formatIssueComment(io, comment, last)
254-
if err != nil {
255-
return "", err
256-
}
257-
fmt.Fprint(&b, cmt)
258-
if last {
259-
fmt.Fprintln(&b)
260-
}
261-
}
262-
263-
if hiddenCount > 0 {
264-
fmt.Fprint(&b, cs.Gray("Use --comments to view the full conversation"))
265-
fmt.Fprintln(&b)
266-
}
267-
268-
return b.String(), nil
269-
}
270-
271-
func formatIssueComment(io *iostreams.IOStreams, comment api.Comment, newest bool) (string, error) {
272-
var b strings.Builder
273-
cs := io.ColorScheme()
274-
275-
// Header
276-
fmt.Fprint(&b, cs.Bold(comment.Author.Login))
277-
if comment.AuthorAssociation != "NONE" {
278-
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" (%s)", strings.ToLower(comment.AuthorAssociation))))
279-
}
280-
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" • %s", utils.FuzzyAgoAbbr(time.Now(), comment.CreatedAt))))
281-
if comment.IncludesCreatedEdit {
282-
fmt.Fprint(&b, cs.Bold(" • edited"))
283-
}
284-
if newest {
285-
fmt.Fprint(&b, cs.Bold(" • "))
286-
fmt.Fprint(&b, cs.CyanBold("Newest comment"))
287-
}
288-
fmt.Fprintln(&b)
289-
290-
// Reactions
291-
if reactions := reactionGroupList(comment.ReactionGroups); reactions != "" {
292-
fmt.Fprint(&b, reactions)
293-
fmt.Fprintln(&b)
294-
}
295-
296-
// Body
297-
if comment.Body != "" {
298-
style := markdown.GetStyle(io.TerminalTheme())
299-
md, err := markdown.Render(comment.Body, style, "")
300-
if err != nil {
301-
return "", err
302-
}
303-
fmt.Fprint(&b, md)
304-
}
305-
306-
return b.String(), nil
307-
}
308-
309221
func issueStateTitleWithColor(cs *iostreams.ColorScheme, state string) string {
310222
colorFunc := cs.ColorFromString(prShared.ColorForState(state))
311223
return colorFunc(strings.Title(strings.ToLower(state)))
@@ -348,27 +260,3 @@ func issueProjectList(issue api.Issue) string {
348260
}
349261
return list
350262
}
351-
352-
func reactionGroupList(rgs api.ReactionGroups) string {
353-
var rs []string
354-
355-
for _, rg := range rgs {
356-
if r := formatReactionGroup(rg); r != "" {
357-
rs = append(rs, r)
358-
}
359-
}
360-
361-
return strings.Join(rs, " • ")
362-
}
363-
364-
func formatReactionGroup(rg api.ReactionGroup) string {
365-
c := rg.Count()
366-
if c == 0 {
367-
return ""
368-
}
369-
e := rg.Emoji()
370-
if e == "" {
371-
return ""
372-
}
373-
return fmt.Sprintf("%v %s", c, e)
374-
}

pkg/cmd/pr/shared/comments.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package shared
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"time"
7+
8+
"github.com/cli/cli/api"
9+
"github.com/cli/cli/pkg/iostreams"
10+
"github.com/cli/cli/pkg/markdown"
11+
"github.com/cli/cli/utils"
12+
)
13+
14+
func RawCommentList(comments api.Comments) string {
15+
var b strings.Builder
16+
for _, comment := range comments.Nodes {
17+
fmt.Fprint(&b, formatRawComment(comment))
18+
}
19+
return b.String()
20+
}
21+
22+
func formatRawComment(comment api.Comment) string {
23+
var b strings.Builder
24+
fmt.Fprintf(&b, "author:\t%s\n", comment.Author.Login)
25+
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.AuthorAssociation))
26+
fmt.Fprintf(&b, "edited:\t%t\n", comment.IncludesCreatedEdit)
27+
fmt.Fprintln(&b, "--")
28+
fmt.Fprintln(&b, comment.Body)
29+
fmt.Fprintln(&b, "--")
30+
return b.String()
31+
}
32+
33+
func CommentList(io *iostreams.IOStreams, comments api.Comments) (string, error) {
34+
var b strings.Builder
35+
cs := io.ColorScheme()
36+
retrievedCount := len(comments.Nodes)
37+
hiddenCount := comments.TotalCount - retrievedCount
38+
39+
if hiddenCount > 0 {
40+
fmt.Fprint(&b, cs.Gray(fmt.Sprintf("———————— Not showing %s ————————", utils.Pluralize(hiddenCount, "comment"))))
41+
fmt.Fprintf(&b, "\n\n\n")
42+
}
43+
44+
for i, comment := range comments.Nodes {
45+
last := i+1 == retrievedCount
46+
cmt, err := formatComment(io, comment, last)
47+
if err != nil {
48+
return "", err
49+
}
50+
fmt.Fprint(&b, cmt)
51+
if last {
52+
fmt.Fprintln(&b)
53+
}
54+
}
55+
56+
if hiddenCount > 0 {
57+
fmt.Fprint(&b, cs.Gray("Use --comments to view the full conversation"))
58+
fmt.Fprintln(&b)
59+
}
60+
61+
return b.String(), nil
62+
}
63+
64+
func formatComment(io *iostreams.IOStreams, comment api.Comment, newest bool) (string, error) {
65+
var b strings.Builder
66+
cs := io.ColorScheme()
67+
68+
// Header
69+
fmt.Fprint(&b, cs.Bold(comment.Author.Login))
70+
if comment.AuthorAssociation != "NONE" {
71+
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" (%s)", strings.ToLower(comment.AuthorAssociation))))
72+
}
73+
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" • %s", utils.FuzzyAgoAbbr(time.Now(), comment.CreatedAt))))
74+
if comment.IncludesCreatedEdit {
75+
fmt.Fprint(&b, cs.Bold(" • edited"))
76+
}
77+
if newest {
78+
fmt.Fprint(&b, cs.Bold(" • "))
79+
fmt.Fprint(&b, cs.CyanBold("Newest comment"))
80+
}
81+
fmt.Fprintln(&b)
82+
83+
// Reactions
84+
if reactions := ReactionGroupList(comment.ReactionGroups); reactions != "" {
85+
fmt.Fprint(&b, reactions)
86+
fmt.Fprintln(&b)
87+
}
88+
89+
// Body
90+
if comment.Body != "" {
91+
style := markdown.GetStyle(io.TerminalTheme())
92+
md, err := markdown.Render(comment.Body, style, "")
93+
if err != nil {
94+
return "", err
95+
}
96+
fmt.Fprint(&b, md)
97+
}
98+
99+
return b.String(), nil
100+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package shared
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/cli/cli/api"
8+
)
9+
10+
func ReactionGroupList(rgs api.ReactionGroups) string {
11+
var rs []string
12+
13+
for _, rg := range rgs {
14+
if r := formatReactionGroup(rg); r != "" {
15+
rs = append(rs, r)
16+
}
17+
}
18+
19+
return strings.Join(rs, " • ")
20+
}
21+
22+
func formatReactionGroup(rg api.ReactionGroup) string {
23+
c := rg.Count()
24+
if c == 0 {
25+
return ""
26+
}
27+
e := rg.Emoji()
28+
if e == "" {
29+
return ""
30+
}
31+
return fmt.Sprintf("%v %s", c, e)
32+
}

0 commit comments

Comments
 (0)
X Tutup