GitLabgitlab-org

cli

main
ply8 git calls318 ms100 KiB6 cachedprotocol v2
191 lines8.4 KiBRaw
1package mrutils
3import (
4 "cmp"
5 "fmt"
6 "io"
8 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
10 "gitlab.com/gitlab-org/cli/internal/iostreams"
11 "gitlab.com/gitlab-org/cli/internal/utils"
14type PrintDiscussionsOptions struct {
15 ShowSystemLogs bool
16 ShowSingleNoteDiscussionPrefix bool
19// noteUsername returns the note author's username, falling back to "unknown"
20// if the username is empty (e.g. redacted users or system-generated notes).
21func noteUsername(n *gitlab.Note) string {
22 if n.Author.Username != "" {
23 return n.Author.Username
24 }
25 return "unknown"
28// noteTimeAgo returns a human-readable timestamp for the note's creation time
29// combining a relative "time ago" string with the absolute time, e.g.
30// "1 day ago (2026-06-26 05:35:51)", or an empty string if CreatedAt is nil.
31func noteTimeAgo(n *gitlab.Note) string {
32 if n.CreatedAt == nil {
33 return ""
34 }
35 return fmt.Sprintf("%s (%s)",
36 utils.TimeToPrettyTimeAgo(*n.CreatedAt),
37 n.CreatedAt.Format("2006-01-02 15:04:05"),
38 )
41// renderBody renders the body as markdown for a terminal, or returns it
42// verbatim when piped so the raw output stays free of control characters.
43func renderBody(ios *iostreams.IOStreams, body string) string {
44 if !ios.IsOutputTTY() {
45 return body
46 }
47 rendered, err := utils.RenderMarkdown(body, ios.BackgroundColor())
48 if err != nil {
49 return body
50 }
51 return rendered
54// PrintDiscussions renders discussions to out.
55func PrintDiscussions(out io.Writer, ios *iostreams.IOStreams, discussions []*gitlab.Discussion, opts PrintDiscussionsOptions) {
56 c := ios.Color()
58 for _, discussion := range discussions {
59 if len(discussion.Notes) == 0 {
60 continue
61 }
63 firstNote := discussion.Notes[0]
65 // Skip system notes unless showSystemLogs is set
66 if firstNote.System && !opts.ShowSystemLogs {
67 continue
68 }
70 // Threaded discussions (not individual notes)
71 if !discussion.IndividualNote && len(discussion.Notes) > 1 {
72 fmt.Fprint(out, "Thread") //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
73 if discussion.ID != "" {
74 fmt.Fprintf(out, " [discussion: %s]", TruncateDiscussionID(discussion.ID)) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
75 }
77 // Show resolution status if resolvable
78 if firstNote.Resolvable {
79 if firstNote.Resolved {
80 fmt.Fprint(out, c.Green(" ✓ resolved")) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
81 } else {
82 fmt.Fprint(out, c.Yellow(" ⚠ unresolved")) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
83 }
84 }
85 fmt.Fprintln(out) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
87 // Print first note
88 createdAt := noteTimeAgo(firstNote)
89 fmt.Fprintf(out, " @%s commented ", noteUsername(firstNote)) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
90 fmt.Fprintf(out, "%s %s\n", c.Gray(createdAt), c.Gray(fmt.Sprintf("[note #%d]", firstNote.ID))) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
92 if firstNote.Position != nil {
93 PrintCommentFileContext(out, c, firstNote.Position)
94 }
96 body := renderBody(ios, firstNote.Body)
97 fmt.Fprintln(out, utils.Indent(body, " ")) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
98 fmt.Fprintln(out) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
100 // Print replies (indented)
101 for i, note := range discussion.Notes[1:] {
102 if note.System && !opts.ShowSystemLogs {
103 continue
104 }
105 replyTime := noteTimeAgo(note)
106 fmt.Fprintf(out, " @%s replied ", noteUsername(note)) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
107 fmt.Fprintf(out, "%s %s\n", c.Gray(replyTime), c.Gray(fmt.Sprintf("[note #%d]", note.ID))) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
109 replyBody := renderBody(ios, note.Body)
110 fmt.Fprintln(out, utils.Indent(replyBody, " ")) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
111 if i < len(discussion.Notes[1:])-1 {
112 fmt.Fprintln(out) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
113 }
114 }
115 fmt.Fprintln(out) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
116 } else {
117 // Individual note (not a thread)
118 note := firstNote
119 createdAt := noteTimeAgo(note)
120 fmt.Fprint(out, "@", noteUsername(note)) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
121 if note.System {
122 fmt.Fprintf(out, " %s ", note.Body) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
123 fmt.Fprintln(out, c.Gray(createdAt)) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
124 } else {
125 body := renderBody(ios, note.Body)
126 noteLabel := fmt.Sprintf("[note #%d]", note.ID)
127 if opts.ShowSingleNoteDiscussionPrefix && discussion.ID != "" {
128 noteLabel += fmt.Sprintf(" [discussion: %s]", TruncateDiscussionID(discussion.ID))
129 }
130 fmt.Fprint(out, " commented ") //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
131 fmt.Fprintf(out, "%s %s\n", c.Gray(createdAt), c.Gray(noteLabel)) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
133 if note.Position != nil {
134 PrintCommentFileContext(out, c, note.Position)
135 }
137 fmt.Fprintln(out, utils.Indent(body, " ")) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
138 }
139 fmt.Fprintln(out) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
140 }
141 }
144// PrintDraftNotes renders pending review comments to standard output.
145func PrintDraftNotes(ios *iostreams.IOStreams, drafts []*gitlab.DraftNote) {
146 c := ios.Color()
147 for _, d := range drafts {
148 header := c.Gray(fmt.Sprintf("[draft #%d]", d.ID))
149 if d.DiscussionID != "" {
150 header += " (reply to " + TruncateDiscussionID(d.DiscussionID) + ")"
151 }
152 ios.LogInfof("%s%s\n%s\n\n", header, commentFileContext(c, d.Position), utils.Indent(renderBody(ios, d.Note), " "))
153 }
156// PrintCommentFileContext prints file and line context for a note position.
157func PrintCommentFileContext(out io.Writer, c *iostreams.ColorPalette, pos *gitlab.NotePosition) {
158 if fileContext := commentFileContext(c, pos); fileContext != "" {
159 fmt.Fprintln(out, fileContext) //nolint:forbidigo // out is a generic io.Writer also used with non-stdout writers (strings.Builder, bytes.Buffer)
160 }
163// commentFileContext returns " on <path>:<lines>" for a position that targets
164// a file, and "" otherwise.
165func commentFileContext(c *iostreams.ColorPalette, pos *gitlab.NotePosition) string {
166 if !HasFilePosition(pos) {
167 return ""
168 }
169 path := cmp.Or(pos.NewPath, pos.OldPath)
171 if lr := pos.LineRange; lr != nil && lr.StartRange != nil && lr.EndRange != nil {
172 startLine := cmp.Or(lr.StartRange.NewLine, lr.StartRange.OldLine)
173 endLine := cmp.Or(lr.EndRange.NewLine, lr.EndRange.OldLine)
174 switch {
175 case startLine > 0 && endLine > 0 && startLine != endLine:
176 return fmt.Sprintf(" on %s:%d-%d", c.Cyan(path), startLine, endLine)
177 case startLine > 0 && endLine > 0:
178 return fmt.Sprintf(" on %s:%d", c.Cyan(path), startLine)
179 }
180 }
182 switch {
183 case pos.NewPath != "" && pos.NewLine > 0:
184 return fmt.Sprintf(" on %s:%d", c.Cyan(pos.NewPath), pos.NewLine)
185 case pos.OldPath != "" && pos.OldLine > 0:
186 return fmt.Sprintf(" on %s:%d", c.Cyan(pos.OldPath), pos.OldLine)
187 default:
188 // File-level and image comments have a path but no line.
189 return " on " + c.Cyan(path)
190 }
Diagnostics8 git calls · 318 ms
CallStatusSizeTime0318 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200746 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=746
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=746
git.pkt.payload.max=719
git.sideband.band1.bytes=719
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=719
git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790
Open pack map
fetchtree:0200469 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=469
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=6020159a68b7314d330ee4ac0f7529b651d7accc
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=469
git.pkt.payload.max=442
git.sideband.band1.bytes=442
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=442
git.pack.trailer=97b1fd715810f2e169ed93a75830c9ee2eff1fc0
Open pack map
fetch2002.1 KiB307 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=307
git.response.bytes=2153
git.response.cache=MISS
git.response.duration_ms=307
git.fetch.wants=a433cdcc35ea459849e43dc155511cbd9d6a613f
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2153
git.pkt.payload.max=2126
git.sideband.band1.bytes=2126
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2f4e1bee47089
Request
6d6cd2c1…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format