This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
mr_view.go
263 lines (227 loc) · 7.52 KB
/
mr_view.go
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package view
import (
"fmt"
"strings"
"github.com/profclems/glab/pkg/iostreams"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/commands/mr/mrutils"
"github.com/profclems/glab/pkg/utils"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
type ViewOpts struct {
ShowComments bool
ShowSystemLogs bool
OpenInBrowser bool
CommentPageNumber int
CommentLimit int
IO *iostreams.IOStreams
}
func NewCmdView(f *cmdutils.Factory) *cobra.Command {
opts := &ViewOpts{
IO: f.IO,
}
var mrViewCmd = &cobra.Command{
Use: "view {<id> | <branch>}",
Short: `Display the title, body, and other information about a merge request.`,
Long: ``,
Aliases: []string{"show"},
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
apiClient, err := f.HttpClient()
if err != nil {
return err
}
mr, baseRepo, err := mrutils.MRFromArgsWithOpts(f, args, &gitlab.GetMergeRequestsOptions{
IncludeDivergedCommitsCount: gitlab.Bool(true),
RenderHTML: gitlab.Bool(true),
IncludeRebaseInProgress: gitlab.Bool(true),
}, "any")
if err != nil {
return err
}
cfg, _ := f.Config()
if opts.OpenInBrowser { //open in browser if --web flag is specified
if f.IO.IsOutputTTY() {
fmt.Fprintf(f.IO.StdErr, "Opening %s in your browser.\n", utils.DisplayURL(mr.WebURL))
}
browser, _ := cfg.Get(baseRepo.RepoHost(), "browser")
return utils.OpenInBrowser(mr.WebURL, browser)
}
notes := []*gitlab.Note{}
if opts.ShowComments {
l := &gitlab.ListMergeRequestNotesOptions{
Sort: gitlab.String("asc"),
}
l.Page = opts.CommentPageNumber
l.PerPage = opts.CommentLimit
notes, err = api.ListMRNotes(apiClient, baseRepo.FullName(), mr.IID, l)
if err != nil {
return err
}
}
glamourStyle, _ := cfg.Get(baseRepo.RepoHost(), "glamour_style")
f.IO.ResolveBackgroundColor(glamourStyle)
if err := f.IO.StartPager(); err != nil {
return err
}
defer f.IO.StopPager()
if f.IO.IsOutputTTY() {
return printTTYMRPreview(opts, mr, notes)
}
return printRawMRPreview(opts, mr)
},
}
mrViewCmd.Flags().BoolVarP(&opts.ShowComments, "comments", "c", false, "Show mr comments and activities")
mrViewCmd.Flags().BoolVarP(&opts.ShowSystemLogs, "system-logs", "s", false, "Show system activities / logs")
mrViewCmd.Flags().BoolVarP(&opts.OpenInBrowser, "web", "w", false, "Open mr in a browser. Uses default browser or browser specified in BROWSER variable")
mrViewCmd.Flags().IntVarP(&opts.CommentPageNumber, "page", "p", 0, "Page number")
mrViewCmd.Flags().IntVarP(&opts.CommentLimit, "per-page", "P", 20, "Number of items to list per page")
return mrViewCmd
}
func labelsList(mr *gitlab.MergeRequest) string {
var labels string
for _, l := range mr.Labels {
labels += " " + l + ","
}
return strings.Trim(labels, ", ")
}
func assigneesList(mr *gitlab.MergeRequest) string {
var assignees string
for _, a := range mr.Assignees {
assignees += " " + a.Username + ","
}
return strings.Trim(assignees, ", ")
}
func reviewersList(mr *gitlab.MergeRequest) string {
var reviewers string
for _, a := range mr.Reviewers {
reviewers += " " + a.Username + ","
}
return strings.Trim(reviewers, ", ")
}
func mrState(c *iostreams.ColorPalette, mr *gitlab.MergeRequest) (mrState string) {
if mr.State == "opened" {
mrState = c.Green("open")
} else if mr.State == "merged" {
mrState = c.Blue(mr.State)
} else {
mrState = c.Red(mr.State)
}
return mrState
}
func printTTYMRPreview(opts *ViewOpts, mr *gitlab.MergeRequest, notes []*gitlab.Note) error {
c := opts.IO.Color()
out := opts.IO.StdOut
mrTimeAgo := utils.TimeToPrettyTimeAgo(*mr.CreatedAt)
// Header
fmt.Fprint(out, mrState(c, mr))
fmt.Fprintf(out, c.Gray(" • opened by %s %s\n"), mr.Author.Username, mrTimeAgo)
fmt.Fprint(out, mr.Title)
fmt.Fprintf(out, c.Gray(" !%d"), mr.IID)
fmt.Fprintln(out)
// Description
if mr.Description != "" {
mr.Description, _ = utils.RenderMarkdown(mr.Description, opts.IO.BackgroundColor())
fmt.Fprintln(out, mr.Description)
}
fmt.Fprintf(out, c.Gray("\n%d upvotes • %d downvotes • %d comments\n"), mr.Upvotes, mr.Downvotes, mr.UserNotesCount)
// Meta information
if labels := labelsList(mr); labels != "" {
fmt.Fprint(out, c.Bold("Labels: "))
fmt.Fprintln(out, labels)
}
if assignees := assigneesList(mr); assignees != "" {
fmt.Fprint(out, c.Bold("Assignees: "))
fmt.Fprintln(out, assignees)
}
if reviewers := reviewersList(mr); reviewers != "" {
fmt.Fprint(out, c.Bold("Reviewers: "))
fmt.Fprintln(out, reviewers)
}
if mr.Milestone != nil {
fmt.Fprint(out, c.Bold("Milestone: "))
fmt.Fprintln(out, mr.Milestone.Title)
}
if mr.State == "closed" {
fmt.Fprintf(out, "Closed By: %s %s\n", mr.ClosedBy.Username, mrTimeAgo)
}
if mr.Pipeline != nil {
fmt.Fprint(out, c.Bold("Pipeline Status: "))
var status string
switch s := mr.Pipeline.Status; s {
case "failed":
status = c.Red(s)
case "success":
status = c.Green(s)
default:
status = c.Gray(s)
}
fmt.Fprintf(out, "%s (View pipeline with `%s`)\n", status, c.Bold("glab ci view "+mr.SourceBranch))
if mr.MergeWhenPipelineSucceeds && mr.Pipeline.Status != "success" {
fmt.Fprintf(out, "%s Requires pipeline to succeed before merging\n", c.WarnIcon())
}
}
fmt.Fprintf(out, "%s This merge request has %s changes\n", c.GreenCheck(), c.Yellow(mr.ChangesCount))
if mr.State == "merged" && mr.MergedBy != nil {
fmt.Fprintf(out, "%s The changes were merged into %s by %s %s\n", c.GreenCheck(), mr.TargetBranch, mr.MergedBy.Name, utils.TimeToPrettyTimeAgo(*mr.MergedAt))
}
if mr.HasConflicts {
fmt.Fprintf(out, c.Red("%s This branch has conflicts that must be resolved\n"), c.FailedIcon())
}
// Comments
if opts.ShowComments {
fmt.Fprintln(out, heredoc.Doc(`
--------------------------------------------
Comments / Notes
--------------------------------------------
`))
if len(notes) > 0 {
for _, note := range notes {
if note.System && !opts.ShowSystemLogs {
continue
}
createdAt := utils.TimeToPrettyTimeAgo(*note.CreatedAt)
fmt.Fprint(out, note.Author.Username)
if note.System {
fmt.Fprintf(out, " %s ", note.Body)
fmt.Fprintln(out, c.Gray(createdAt))
} else {
body, _ := utils.RenderMarkdown(note.Body, opts.IO.BackgroundColor())
fmt.Fprint(out, " commented ")
fmt.Fprintf(out, c.Gray("%s\n"), createdAt)
fmt.Fprintln(out, utils.Indent(body, " "))
}
fmt.Fprintln(out)
}
} else {
fmt.Fprintln(out, "There are no comments on this merge request")
}
}
fmt.Fprintln(out)
fmt.Fprintf(out, c.Gray("View this merge request on GitLab: %s\n"), mr.WebURL)
return nil
}
func printRawMRPreview(opts *ViewOpts, mr *gitlab.MergeRequest) error {
out := opts.IO.StdOut
assignees := assigneesList(mr)
reviewers := reviewersList(mr)
labels := labelsList(mr)
fmt.Fprintf(out, "title:\t%s\n", mr.Title)
fmt.Fprintf(out, "state:\t%s\n", mrState(opts.IO.Color(), mr))
fmt.Fprintf(out, "author:\t%s\n", mr.Author.Username)
fmt.Fprintf(out, "labels:\t%s\n", labels)
fmt.Fprintf(out, "assignees:\t%s\n", assignees)
fmt.Fprintf(out, "reviewers:\t%s\n", reviewers)
fmt.Fprintf(out, "comments:\t%d\n", mr.UserNotesCount)
if mr.Milestone != nil {
fmt.Fprintf(out, "milestone:\t%s\n", mr.Milestone.Title)
}
fmt.Fprintf(out, "number:\t%d\n", mr.IID)
fmt.Fprintf(out, "url:\t%s\n", mr.WebURL)
fmt.Fprintln(out, "--")
fmt.Fprintln(out, mr.Description)
return nil
}