GitLabgitlab-org
cli
main
1//go:build !integration
3package issueutils
5import (
6 "strconv"
7 "sync/atomic"
8 "testing"
9 "time"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/config"
18 "gitlab.com/gitlab-org/cli/internal/glinstance"
19 "gitlab.com/gitlab-org/cli/internal/glrepo"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23func Test_issueMetadataFromURL(t *testing.T) {
24 t.Parallel()
25 tests := []struct {
26 name string
27 str string
28 want int64
29 path string
30 }{
31 {
32 name: "valid URL",
33 str: "https://gitlab.com/namespace/repo/-/issues/1",
34 want: 1,
35 path: "https://gitlab.com/namespace/repo/",
36 },
37 {
38 name: "valid URL with nested subgroup",
39 str: "https://gitlab.com/namespace/project/subproject/repo/-/issues/100",
40 want: 100,
41 path: "https://gitlab.com/namespace/project/subproject/repo/",
42 },
43 {
44 name: "valid URL without dash",
45 str: "https://gitlab.com/namespace/project/subproject/repo/issues/1",
46 want: 1,
47 path: "https://gitlab.com/namespace/project/subproject/repo/",
48 },
49 {
50 name: "valid incident URL",
51 str: "https://gitlab.com/namespace/repo/-/issues/incident/1",
52 want: 1,
53 path: "https://gitlab.com/namespace/repo/",
54 },
55 {
56 name: "valid incident URL with nested subgroup",
57 str: "https://gitlab.com/namespace/project/subproject/repo/-/issues/incident/100",
58 want: 100,
59 path: "https://gitlab.com/namespace/project/subproject/repo/",
60 },
61 {
62 name: "valid incident URL without dash",
63 str: "https://gitlab.com/namespace/project/subproject/repo/issues/incident/1",
64 want: 1,
65 path: "https://gitlab.com/namespace/project/subproject/repo/",
66 },
67 {
68 name: "valid work item URL",
69 str: "https://gitlab.com/namespace/repo/-/work_items/1",
70 want: 1,
71 path: "https://gitlab.com/namespace/repo/",
72 },
73 {
74 name: "valid work item URL with hyphenated namespace",
75 str: "https://gitlab.com/gitlab-org/gitlab/-/work_items/337780",
76 want: 337780,
77 path: "https://gitlab.com/gitlab-org/gitlab/",
78 },
79 {
80 name: "valid work item URL without dash",
81 str: "https://gitlab.com/namespace/project/subproject/repo/work_items/1",
82 want: 1,
83 path: "https://gitlab.com/namespace/project/subproject/repo/",
84 },
85 {
86 name: "valid work item URL with trailing slash",
87 str: "https://gitlab.com/namespace/repo/-/work_items/1/",
88 want: 1,
89 path: "https://gitlab.com/namespace/repo/",
90 },
91 {
92 name: "valid work item URL with query string",
93 str: "https://gitlab.com/namespace/repo/-/work_items/1?foo=bar",
94 want: 1,
95 path: "https://gitlab.com/namespace/repo/",
96 },
97 {
98 name: "invalid URL with no issue number",
99 str: "https://gitlab.com/namespace/project/subproject/repo/issues",
100 want: 0,
101 path: "",
102 },
103 {
104 name: "invalid incident URL with no incident number",
105 str: "https://gitlab.com/namespace/project/subproject/repo/issues/incident",
106 want: 0,
107 path: "",
108 },
109 {
110 name: "invalid URL with only namespace, missing repo",
111 str: "https://gitlab.com/namespace/issues/100",
112 want: 0,
113 path: "",
114 },
115 {
116 name: "invalid incident URL with only namespace, missing repo",
117 str: "https://gitlab.com/namespace/issues/incident/100",
118 want: 0,
119 path: "",
120 },
121 {
122 name: "invalid issue URL",
123 str: "https://gitlab.com/namespace/repo",
124 want: 0,
125 path: "",
126 },
127 {
128 name: "invalid scheme-less URL",
129 str: "//gitlab.com/namespace/repo/-/issues/1",
130 want: 0,
131 path: "",
132 },
133 {
134 name: "invalid merge request URL",
135 str: "https://gitlab.com/namespace/repo/-/merge_requests/1",
136 want: 0,
137 path: "",
138 },
139 {
140 name: "valid URL with trailing merge_requests segment",
141 str: "https://gitlab.com/namespace/repo/issues/12/merge_requests/3",
142 want: 12,
143 path: "https://gitlab.com/namespace/repo/",
144 },
145 {
146 name: "invalid issue URL, missing issues path",
147 str: "https://gitlab.com/namespace/project/subproject/repo/10/",
148 want: 0,
149 path: "",
150 },
151 }
152 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154 t.Parallel()
155 id, repo := issueMetadataFromURL(tt.str, glinstance.DefaultHostname, config.NewBlankConfig())
156 require.Equal(t, tt.want, id)
158 if tt.want != 0 && tt.path != "" {
159 expectedRepo, err := glrepo.FromFullName(tt.path, glinstance.DefaultHostname, config.NewBlankConfig())
160 require.NoError(t, err)
161 require.Equal(t, expectedRepo, repo)
162 }
163 })
164 }
165}
167func Test_DisplayIssue(t *testing.T) {
168 now := time.Now()
170 tests := []struct {
171 name string
172 issue *gitlab.Issue
173 isTTY bool
174 wantTTY string
175 wantNoTTY string
176 }{
177 {
178 name: "opened issue with CreatedAt set, TTY",
179 issue: &gitlab.Issue{
180 IID: 1,
181 State: "opened",
182 Title: "Fix the bug",
183 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/1",
184 CreatedAt: &now,
185 },
186 isTTY: true,
187 },
188 {
189 name: "opened issue with nil CreatedAt, TTY - no panic and no empty parentheses",
190 issue: &gitlab.Issue{
191 IID: 2,
192 State: "opened",
193 Title: "External Jira issue",
194 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/2",
195 CreatedAt: nil,
196 },
197 isTTY: true,
198 wantTTY: "#2 External Jira issue\n https://gitlab.com/gitlab-org/cli/-/issues/2\n",
199 },
200 {
201 name: "closed issue with nil CreatedAt, TTY",
202 issue: &gitlab.Issue{
203 IID: 3,
204 State: "closed",
205 Title: "Closed external issue",
206 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/3",
207 CreatedAt: nil,
208 },
209 isTTY: true,
210 wantTTY: "#3 Closed external issue\n https://gitlab.com/gitlab-org/cli/-/issues/3\n",
211 },
212 {
213 name: "non-TTY output returns only WebURL regardless of CreatedAt",
214 issue: &gitlab.Issue{
215 IID: 4,
216 State: "opened",
217 Title: "Some issue",
218 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/4",
219 CreatedAt: nil,
220 },
221 isTTY: false,
222 wantNoTTY: "https://gitlab.com/gitlab-org/cli/-/issues/4",
223 },
224 {
225 name: "non-TTY output with CreatedAt set returns only WebURL",
226 issue: &gitlab.Issue{
227 IID: 5,
228 State: "opened",
229 Title: "Some issue with date",
230 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/5",
231 CreatedAt: &now,
232 },
233 isTTY: false,
234 wantNoTTY: "https://gitlab.com/gitlab-org/cli/-/issues/5",
235 },
236 }
238 streams, _, _, _ := cmdtest.TestIOStreams()
239 c := streams.Color()
241 for _, tt := range tests {
242 t.Run(tt.name, func(t *testing.T) {
243 // Ensure no panic occurs (covers the nil pointer dereference fix)
244 got := DisplayIssue(c, tt.issue, tt.isTTY)
246 if tt.isTTY {
247 if tt.wantTTY != "" {
248 assert.Equal(t, tt.wantTTY, got)
249 }
250 // For issues with a non-nil CreatedAt, just verify no empty parentheses
251 if tt.issue.CreatedAt != nil {
252 assert.NotContains(t, got, "()")
253 assert.Contains(t, got, tt.issue.Title)
254 assert.Contains(t, got, tt.issue.WebURL)
255 }
256 } else {
257 assert.Equal(t, tt.wantNoTTY, got)
258 }
259 })
260 }
261}
263func Test_DisplayIssueList(t *testing.T) {
264 now := time.Now()
266 tests := []struct {
267 name string
268 issues []*gitlab.Issue
269 wantEmpty bool
270 }{
271 {
272 name: "list with all CreatedAt set - no panic",
273 issues: []*gitlab.Issue{
274 {
275 IID: 1,
276 State: "opened",
277 Title: "First issue",
278 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/1",
279 CreatedAt: &now,
280 },
281 {
282 IID: 2,
283 State: "closed",
284 Title: "Second issue",
285 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/2",
286 CreatedAt: &now,
287 },
288 },
289 },
290 {
291 name: "list with nil CreatedAt - no panic, empty cell rendered",
292 issues: []*gitlab.Issue{
293 {
294 IID: 0,
295 State: "opened",
296 Title: "External Jira issue",
297 WebURL: "https://jira.example.com/browse/PROJ-1",
298 CreatedAt: nil,
299 },
300 },
301 },
302 {
303 name: "mixed list with some nil CreatedAt - no panic",
304 issues: []*gitlab.Issue{
305 {
306 IID: 1,
307 State: "opened",
308 Title: "Normal issue",
309 WebURL: "https://gitlab.com/gitlab-org/cli/-/issues/1",
310 CreatedAt: &now,
311 },
312 {
313 IID: 0,
314 State: "opened",
315 Title: "External issue without date",
316 WebURL: "https://jira.example.com/browse/PROJ-2",
317 CreatedAt: nil,
318 },
319 },
320 },
321 {
322 name: "empty list",
323 issues: []*gitlab.Issue{},
324 wantEmpty: true,
325 },
326 }
328 for _, tt := range tests {
329 t.Run(tt.name, func(t *testing.T) {
330 streams, _, _, _ := cmdtest.TestIOStreams()
332 // Ensure no panic occurs (covers the nil pointer dereference fix)
333 got := DisplayIssueList(streams, tt.issues, "gitlab-org/cli")
335 if tt.wantEmpty {
336 assert.Empty(t, got)
337 return
338 }
340 // Verify all issue titles appear in the output
341 for _, issue := range tt.issues {
342 assert.Contains(t, got, issue.Title)
343 }
344 })
345 }
346}
348func Test_IssuesFromArgs_LimitsConcurrency(t *testing.T) {
349 ios, _, _, _ := cmdtest.TestIOStreams()
350 f := cmdtest.NewTestFactory(ios, cmdtest.WithBaseRepo("foo", "bar", ""))
352 var inFlight, maxInFlight atomic.Int32
353 release := make(chan struct{})
354 origGetIssue := api.GetIssue
355 t.Cleanup(func() { api.GetIssue = origGetIssue })
356 api.GetIssue = func(_ *gitlab.Client, _ any, issueID int64) (*gitlab.Issue, error) {
357 n := inFlight.Add(1)
358 defer inFlight.Add(-1)
359 for {
360 m := maxInFlight.Load()
361 if n <= m || maxInFlight.CompareAndSwap(m, n) {
362 break
363 }
364 }
365 <-release
366 return &gitlab.Issue{IID: issueID}, nil
367 }
369 args := make([]string, 4*api.MaxConcurrentRequests)
370 for i := range args {
371 args[i] = strconv.Itoa(i + 1)
372 }
374 type result struct {
375 issues []*gitlab.Issue
376 err error
377 }
378 done := make(chan result)
379 go func() {
380 issues, _, err := IssuesFromArgs(t.Context(), nil, nil, f.BaseRepo, glinstance.DefaultHostname, nil, args)
381 done <- result{issues, err}
382 }()
384 require.Eventually(t, func() bool {
385 return inFlight.Load() == api.MaxConcurrentRequests
386 }, time.Second, time.Millisecond)
387 close(release)
388 res := <-done
390 require.NoError(t, res.err)
391 require.Len(t, res.issues, len(args))
392 assert.Equal(t, int32(api.MaxConcurrentRequests), maxInFlight.Load())
393}
Diagnostics8 git calls · 245 ms
CallStatusSizeTime0245 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 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 · hit2 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=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=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=3 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:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=3 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200492 B · hit3 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=492 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=409e7038e253494f94691a22a250deee77ba03bf 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=492 git.pkt.payload.max=465 git.sideband.band1.bytes=465 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=465 git.pack.trailer=c8a67aa26d573734cf0c8f302f8c1dd08e47d76aOpen pack map
fetchtree:0200137 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=228 git.response.bytes=137 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=1e3b6b810ea02e45ee91727cb626467da691d44a 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=137 git.pkt.payload.max=110 git.sideband.band1.bytes=110 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=110 git.pack.trailer=601fda9373d934d98367af3929640db9a95bed5aOpen pack map
fetch2002.3 KiB228 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2353 git.response.cache=MISS git.response.duration_ms=228 git.fetch.wants=db61d2c95261c3a4100803a70cbe95caae0dd067 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=2353 git.pkt.payload.max=2326 git.sideband.band1.bytes=2326 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
- a5e21695fb3e984a
- Request
- 5efe6f3d…
- 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