1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
package shared
import (
"fmt"
"sort"
"strings"
"time"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/text"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/markdown"
)
type Comment interface {
Identifier() string
AuthorLogin() string
Association() string
Content() string
Created() time.Time
HiddenReason() string
IsEdited() bool
IsHidden() bool
Link() string
Reactions() api.ReactionGroups
Status() string
}
func RawCommentList(comments api.Comments, reviews api.PullRequestReviews) string {
sortedComments := sortComments(comments, reviews)
var b strings.Builder
for _, comment := range sortedComments {
fmt.Fprint(&b, formatRawComment(comment))
}
return b.String()
}
func formatRawComment(comment Comment) string {
if comment.IsHidden() {
return ""
}
var b strings.Builder
fmt.Fprintf(&b, "author:\t%s\n", comment.AuthorLogin())
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.Association()))
fmt.Fprintf(&b, "edited:\t%t\n", comment.IsEdited())
fmt.Fprintf(&b, "status:\t%s\n", formatRawCommentStatus(comment.Status()))
fmt.Fprintln(&b, "--")
fmt.Fprintln(&b, comment.Content())
fmt.Fprintln(&b, "--")
return b.String()
}
func CommentList(io *iostreams.IOStreams, comments api.Comments, reviews api.PullRequestReviews, preview bool) (string, error) {
sortedComments := sortComments(comments, reviews)
if preview && len(sortedComments) > 0 {
sortedComments = sortedComments[len(sortedComments)-1:]
}
var b strings.Builder
cs := io.ColorScheme()
totalCount := comments.TotalCount + reviews.TotalCount
retrievedCount := len(sortedComments)
hiddenCount := totalCount - retrievedCount
if preview && hiddenCount > 0 {
fmt.Fprint(&b, cs.Gray(fmt.Sprintf("———————— Not showing %s ————————", text.Pluralize(hiddenCount, "comment"))))
fmt.Fprintf(&b, "\n\n\n")
}
for i, comment := range sortedComments {
last := i+1 == retrievedCount
cmt, err := formatComment(io, comment, last)
if err != nil {
return "", err
}
fmt.Fprint(&b, cmt)
if last {
fmt.Fprintln(&b)
}
}
if preview && hiddenCount > 0 {
fmt.Fprint(&b, cs.Gray("Use --comments to view the full conversation"))
fmt.Fprintln(&b)
}
return b.String(), nil
}
func formatComment(io *iostreams.IOStreams, comment Comment, newest bool) (string, error) {
var b strings.Builder
cs := io.ColorScheme()
if comment.IsHidden() {
return cs.Bold(formatHiddenComment(comment)), nil
}
// Header
fmt.Fprint(&b, cs.Bold(comment.AuthorLogin()))
if comment.Status() != "" {
fmt.Fprint(&b, formatCommentStatus(cs, comment.Status()))
}
if comment.Association() != "NONE" {
fmt.Fprint(&b, cs.Boldf(" (%s)", text.Title(comment.Association())))
}
fmt.Fprint(&b, cs.Boldf(" • %s", text.FuzzyAgoAbbr(time.Now(), comment.Created())))
if comment.IsEdited() {
fmt.Fprint(&b, cs.Bold(" • Edited"))
}
if newest {
fmt.Fprint(&b, cs.Bold(" • "))
fmt.Fprint(&b, cs.CyanBold("Newest comment"))
}
fmt.Fprintln(&b)
// Reactions
if reactions := ReactionGroupList(comment.Reactions()); reactions != "" {
fmt.Fprint(&b, reactions)
fmt.Fprintln(&b)
}
// Body
var md string
var err error
if comment.Content() == "" {
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No body provided"))
} else {
md, err = markdown.Render(comment.Content(),
markdown.WithTheme(io.TerminalTheme()),
markdown.WithWrap(io.TerminalWidth()))
if err != nil {
return "", err
}
}
fmt.Fprint(&b, md)
// Footer
if comment.Link() != "" {
fmt.Fprintf(&b, cs.Gray("View the full review: %s\n\n"), comment.Link())
}
return b.String(), nil
}
func sortComments(cs api.Comments, rs api.PullRequestReviews) []Comment {
comments := cs.Nodes
reviews := rs.Nodes
var sorted []Comment = make([]Comment, len(comments)+len(reviews))
var i int
for _, c := range comments {
sorted[i] = c
i++
}
for _, r := range reviews {
sorted[i] = r
i++
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Created().Before(sorted[j].Created())
})
return sorted
}
const (
approvedStatus = "APPROVED"
changesRequestedStatus = "CHANGES_REQUESTED"
commentedStatus = "COMMENTED"
dismissedStatus = "DISMISSED"
)
func formatCommentStatus(cs *iostreams.ColorScheme, status string) string {
switch status {
case approvedStatus:
return fmt.Sprintf(" %s", cs.Green("approved"))
case changesRequestedStatus:
return fmt.Sprintf(" %s", cs.Red("requested changes"))
case commentedStatus, dismissedStatus:
return fmt.Sprintf(" %s", strings.ToLower(status))
}
return ""
}
func formatRawCommentStatus(status string) string {
if status == approvedStatus ||
status == changesRequestedStatus ||
status == commentedStatus ||
status == dismissedStatus {
return strings.ReplaceAll(strings.ToLower(status), "_", " ")
}
return "none"
}
func formatHiddenComment(comment Comment) string {
var b strings.Builder
fmt.Fprint(&b, comment.AuthorLogin())
if comment.Association() != "NONE" {
fmt.Fprintf(&b, " (%s)", text.Title(comment.Association()))
}
fmt.Fprintf(&b, " • This comment has been marked as %s\n\n", comment.HiddenReason())
return b.String()
}
|