GitLabgitlab-org
cli
main
1package summary
3import (
4 "fmt"
5 "strings"
7 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
8 "gitlab.com/gitlab-org/cli/internal/iostreams"
9 "gitlab.com/gitlab-org/cli/internal/text"
10)
12const title = "GitLab Dependency Firewall"
14// Status icons. Kept as constants so the rendered glyphs and the test
15// assertions reference the same source.
16const (
17 blockedIcon = "✖"
18 warningIcon = "▲"
19 allowedIcon = "✔"
20 emptyReason = "—"
21)
23// Render writes a summary of Dependency Firewall verdicts inside a rounded
24// Unicode box tinted by the worst verdict in the run. Blocked runs go to
25// stderr, warning/allow-only runs go to stdout. A blank line is emitted both
26// before and after the box so it stands apart from surrounding wrapper or CI
27// output.
28func Render(io *iostreams.IOStreams, entries []verdict.Entry) {
29 if len(entries) == 0 {
30 io.LogInfo("no Dependency Firewall activity recorded")
31 return
32 }
34 var blocked, warned int
35 for _, e := range entries {
36 switch e.Verdict {
37 case verdict.Blocked:
38 blocked++
39 case verdict.Warning:
40 warned++
41 }
42 }
44 body := renderBody(newPalette(io), entries, blocked, warned)
45 if blocked > 0 {
46 io.LogError(body)
47 } else {
48 io.LogInfo(body)
49 }
50}
52// renderBody builds the boxed summary. The palette is injected so tests can
53// exercise the colored and plain layouts deterministically, without depending
54// on ambient color/TTY/CI environment.
55func renderBody(p palette, entries []verdict.Entry, blocked, warned int) string {
56 worst := worstVerdict(blocked, warned)
57 accent := p.accent(worst)
59 // Sanitize every cell before it touches the layout or the terminal.
60 // Package and version are derived from network/PURL input and the reason
61 // is a server response, so all three are untrusted: stripping escapes and
62 // collapsing whitespace stops a crafted value from tearing the box or
63 // emitting control characters.
64 type cells struct {
65 verdict verdict.Verdict
66 pkg, ver, rsn string
67 }
68 rows := make([]cells, 0, len(entries))
69 for _, e := range entries {
70 rows = append(rows, cells{
71 verdict: e.Verdict,
72 pkg: sanitizeCell(e.Package),
73 ver: sanitizeCell(e.Version),
74 rsn: reasonOrDash(e.Reason),
75 })
76 }
78 // Column widths measured with text.StringWidth so multi-cell glyphs and
79 // any ANSI escapes are accounted for.
80 wPkg, wVer := text.StringWidth("PACKAGE"), text.StringWidth("VERSION")
81 for _, r := range rows {
82 wPkg = max(wPkg, text.StringWidth(r.pkg))
83 wVer = max(wVer, text.StringWidth(r.ver))
84 }
86 // One row-formatter builds every content line so the icon column, the
87 // column widths, and the two-space gutters are defined in a single place
88 // (rather than repeated across format strings that could drift).
89 row := func(icon, pkg, ver, reason string) string {
90 return fmt.Sprintf("%s %s %s %s",
91 icon, text.PadRight(pkg, wPkg, ' '), text.PadLeft(ver, wVer, ' '), reason)
92 }
94 header := row(" ", p.c.Bold("PACKAGE"), p.c.Bold("VERSION"), p.c.Bold("REASON"))
96 bodyRows := make([]string, 0, len(rows))
97 for _, r := range rows {
98 bodyRows = append(bodyRows, row(p.icon(r.verdict), r.pkg, r.ver, p.c.Gray(r.rsn)))
99 }
101 titleLine := accent(iconFor(worst)) + " " + p.c.Bold(title)
102 summaryLine := p.summary(len(entries), blocked, warned)
104 // inner is the widest content line; every line is left-margined and
105 // right-padded to it by box() below, so alignment is structural.
106 inner := text.StringWidth(header)
107 for _, l := range bodyRows {
108 inner = max(inner, text.StringWidth(l))
109 }
110 inner = max(inner, text.StringWidth(titleLine))
111 inner = max(inner, text.StringWidth(summaryLine))
113 var b strings.Builder
114 box := newBoxWriter(&b, accent, inner)
115 b.WriteByte('\n')
116 box.top()
117 box.row(titleLine)
118 box.row(summaryLine)
119 box.divider()
120 box.row(header)
121 for _, l := range bodyRows {
122 box.row(l)
123 }
124 box.bottom()
125 return b.String()
126}
128// boxWriter draws a rounded box of a fixed interior width. It owns the left
129// margin (one space) and right padding for every content row, so callers pass
130// only the content and can't drift the alignment.
131type boxWriter struct {
132 b *strings.Builder
133 accent func(string) string
134 inner int
135}
137func newBoxWriter(b *strings.Builder, accent func(string) string, inner int) boxWriter {
138 // +2 for the one-space margin on each side of the content.
139 return boxWriter{b: b, accent: accent, inner: inner + 2}
140}
142func (w boxWriter) top() {
143 w.b.WriteString(w.accent("╭"+strings.Repeat("─", w.inner)+"╮") + "\n")
144}
146func (w boxWriter) divider() {
147 w.b.WriteString(w.accent("├"+strings.Repeat("─", w.inner)+"┤") + "\n")
148}
150func (w boxWriter) bottom() {
151 w.b.WriteString(w.accent("╰"+strings.Repeat("─", w.inner)+"╯") + "\n")
152}
154func (w boxWriter) row(content string) {
155 // One leading space, content padded to the interior width, one trailing
156 // space, wrapped in borders.
157 padded := " " + content + strings.Repeat(" ", w.inner-1-text.StringWidth(content))
158 w.b.WriteString(w.accent("│") + padded + w.accent("│") + "\n")
159}
161// iconFor returns the plain status glyph for a verdict. It is the single source
162// of the verdict→glyph mapping; palette.icon colors this result.
163func iconFor(v verdict.Verdict) string {
164 switch v {
165 case verdict.Blocked:
166 return blockedIcon
167 case verdict.Warning:
168 return warningIcon
169 default:
170 return allowedIcon
171 }
172}
174// worstVerdict returns the most severe verdict present, so the title icon and
175// the box accent both key off the same severity.
176func worstVerdict(blocked, warned int) verdict.Verdict {
177 switch {
178 case blocked > 0:
179 return verdict.Blocked
180 case warned > 0:
181 return verdict.Warning
182 default:
183 return verdict.Allowed
184 }
185}
187// sanitizeCell makes an untrusted cell value safe to render inside the box so a
188// crafted value can't tear the box or write control characters to the terminal.
189func sanitizeCell(s string) string {
190 return text.SanitizeInline(s)
191}
193// reasonOrDash sanitizes a reason and falls back to a dash when it is empty.
194// Blocked and warned entries always carry a reason from the policy response,
195// so the dash is only ever seen for allowed/no-violation entries.
196func reasonOrDash(reason string) string {
197 if cleaned := sanitizeCell(reason); cleaned != "" {
198 return cleaned
199 }
200 return emptyReason
201}
Diagnostics7 git calls · 38 ms
CallStatusSizeTime038 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
fetchtree:0 · deepen 1020032 KiB · 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=32926 git.response.cache=HIT git.response.duration_ms=3 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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 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:0200361 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=361 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 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=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200171 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=171 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=49b5e8f39e60dbc0d93ae2ad5e42977c52eb59ee 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=171 git.pkt.payload.max=144 git.sideband.band1.bytes=144 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=144 git.pack.trailer=4cb83a763321d2956623f250a565d976e7941ae0Open pack map
fetch2002.5 KiB · hit25 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=25 git.response.bytes=2567 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=ea14cd5c4c6a3376c3cda7ec77a4c5d0a5531ae5 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=2567 git.pkt.payload.max=2540 git.sideband.band1.bytes=2540 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
- a5e27e8d1aeb0555
- Request
- 0723e14f…
- 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