GitLabgitlab-org
cli
main
1//go:build !integration
3package cmdutils
5import (
6 "testing"
8 "github.com/stretchr/testify/require"
10 "gitlab.com/gitlab-org/cli/internal/config"
11 "gitlab.com/gitlab-org/cli/internal/glinstance"
12 "gitlab.com/gitlab-org/cli/internal/glrepo"
13)
15func TestParseGitLabURL(t *testing.T) {
16 tests := []struct {
17 name string
18 url string
19 wantID int
20 wantType GitLabResourceType
21 wantRepoURL string
22 }{
23 // Merge Request URLs
24 {
25 name: "valid MR URL",
26 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/1234",
27 wantID: 1234,
28 wantType: GitLabResourceMergeRequest,
29 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
30 },
31 {
32 name: "valid MR URL without dash",
33 url: "https://gitlab.com/gitlab-org/cli/merge_requests/1234",
34 wantID: 1234,
35 wantType: GitLabResourceMergeRequest,
36 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
37 },
38 {
39 name: "valid MR URL with nested subgroup",
40 url: "https://gitlab.com/namespace/project/subproject/repo/-/merge_requests/100",
41 wantID: 100,
42 wantType: GitLabResourceMergeRequest,
43 wantRepoURL: "https://gitlab.com/namespace/project/subproject/repo/",
44 },
45 {
46 name: "valid MR URL with very deep nested subgroups",
47 url: "https://gitlab.com/gitlab-org/subgroup/othersubgroup/oh/please/make/it/stop/-/merge_requests/2091",
48 wantID: 2091,
49 wantType: GitLabResourceMergeRequest,
50 wantRepoURL: "https://gitlab.com/gitlab-org/subgroup/othersubgroup/oh/please/make/it/stop/",
51 },
52 {
53 name: "valid MR URL with self-managed instance (invent.kde.org)",
54 url: "https://invent.kde.org/kde/krita/-/merge_requests/42",
55 wantID: 42,
56 wantType: GitLabResourceMergeRequest,
57 wantRepoURL: "https://invent.kde.org/kde/krita/",
58 },
59 {
60 name: "valid MR URL with self-managed instance",
61 url: "https://gitlab.example.com/org/project/-/merge_requests/42",
62 wantID: 42,
63 wantType: GitLabResourceMergeRequest,
64 wantRepoURL: "https://gitlab.example.com/org/project/",
65 },
66 {
67 name: "MR URL with fragment (note)",
68 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/2091#note_2510904692",
69 wantID: 2091,
70 wantType: GitLabResourceMergeRequest,
71 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
72 },
73 {
74 name: "MR URL with query parameters",
75 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/1234?tab=notes",
76 wantID: 1234,
77 wantType: GitLabResourceMergeRequest,
78 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
79 },
80 {
81 name: "MR URL with both query and fragment",
82 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/1234?tab=notes#note_123",
83 wantID: 1234,
84 wantType: GitLabResourceMergeRequest,
85 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
86 },
87 {
88 name: "MR URL with trailing slash",
89 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/1234/",
90 wantID: 1234,
91 wantType: GitLabResourceMergeRequest,
92 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
93 },
94 // Issue URLs
95 {
96 name: "valid issue URL",
97 url: "https://gitlab.com/namespace/repo/-/issues/1",
98 wantID: 1,
99 wantType: GitLabResourceIssue,
100 wantRepoURL: "https://gitlab.com/namespace/repo/",
101 },
102 {
103 name: "valid issue URL without dash",
104 url: "https://gitlab.com/namespace/repo/issues/1",
105 wantID: 1,
106 wantType: GitLabResourceIssue,
107 wantRepoURL: "https://gitlab.com/namespace/repo/",
108 },
109 {
110 name: "valid issue URL with nested subgroup",
111 url: "https://gitlab.com/namespace/project/subproject/repo/-/issues/100",
112 wantID: 100,
113 wantType: GitLabResourceIssue,
114 wantRepoURL: "https://gitlab.com/namespace/project/subproject/repo/",
115 },
116 {
117 name: "valid incident URL",
118 url: "https://gitlab.com/namespace/repo/-/issues/incident/1",
119 wantID: 1,
120 wantType: GitLabResourceIssue,
121 wantRepoURL: "https://gitlab.com/namespace/repo/",
122 },
123 {
124 name: "valid incident URL without dash",
125 url: "https://gitlab.com/namespace/repo/issues/incident/1",
126 wantID: 1,
127 wantType: GitLabResourceIssue,
128 wantRepoURL: "https://gitlab.com/namespace/repo/",
129 },
130 {
131 name: "valid work item URL",
132 url: "https://gitlab.com/gitlab-org/gitlab/-/work_items/337780",
133 wantID: 337780,
134 wantType: GitLabResourceIssue,
135 wantRepoURL: "https://gitlab.com/gitlab-org/gitlab/",
136 },
137 {
138 name: "valid work item URL without dash",
139 url: "https://gitlab.com/namespace/repo/work_items/1",
140 wantID: 1,
141 wantType: GitLabResourceIssue,
142 wantRepoURL: "https://gitlab.com/namespace/repo/",
143 },
144 {
145 name: "issue URL with trailing slash",
146 url: "https://gitlab.com/namespace/repo/-/issues/1/",
147 wantID: 1,
148 wantType: GitLabResourceIssue,
149 wantRepoURL: "https://gitlab.com/namespace/repo/",
150 },
151 {
152 name: "issue URL with query and fragment",
153 url: "https://gitlab.com/namespace/repo/-/issues/1?foo=bar#note_123",
154 wantID: 1,
155 wantType: GitLabResourceIssue,
156 wantRepoURL: "https://gitlab.com/namespace/repo/",
157 },
158 {
159 name: "issue URL over http",
160 url: "http://gitlab.example.com/namespace/repo/-/issues/1",
161 wantID: 1,
162 wantType: GitLabResourceIssue,
163 wantRepoURL: "https://gitlab.example.com/namespace/repo/",
164 },
165 {
166 name: "scheme-less issue URL",
167 url: "//gitlab.com/namespace/repo/-/issues/1",
168 wantID: 1,
169 wantType: GitLabResourceIssue,
170 wantRepoURL: "https://gitlab.com/namespace/repo/",
171 },
172 {
173 name: "issue URL with trailing merge_requests segment resolves as merge request",
174 url: "https://gitlab.com/namespace/repo/issues/12/merge_requests/3",
175 wantID: 3,
176 wantType: GitLabResourceMergeRequest,
177 wantRepoURL: "https://gitlab.com/namespace/repo/issues/12/",
178 },
179 // Invalid URLs
180 {
181 name: "invalid URL - no resource ID",
182 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/",
183 wantID: 0,
184 wantType: "",
185 },
186 {
187 name: "invalid URL - not a GitLab resource",
188 url: "https://gitlab.com/gitlab-org/cli",
189 wantID: 0,
190 wantType: "",
191 },
192 {
193 name: "invalid URL - wrong protocol",
194 url: "ftp://gitlab.com/gitlab-org/cli/-/merge_requests/123",
195 wantID: 0,
196 wantType: "",
197 },
198 {
199 name: "invalid URL - malformed",
200 url: "not-a-url",
201 wantID: 0,
202 wantType: "",
203 },
204 {
205 name: "invalid URL - missing repo name",
206 url: "https://gitlab.com/gitlab-org/-/merge_requests/123",
207 wantID: 0,
208 wantType: "",
209 },
210 {
211 name: "invalid issue URL - no issue number",
212 url: "https://gitlab.com/namespace/repo/issues",
213 wantID: 0,
214 wantType: "",
215 },
216 {
217 name: "invalid incident URL - no incident number",
218 url: "https://gitlab.com/namespace/repo/issues/incident",
219 wantID: 0,
220 wantType: "",
221 },
222 {
223 name: "invalid issue URL - missing repo name",
224 url: "https://gitlab.com/namespace/issues/100",
225 wantID: 0,
226 wantType: "",
227 },
228 {
229 name: "invalid issue URL - wrong protocol",
230 url: "ftp://gitlab.com/namespace/repo/-/issues/1",
231 wantID: 0,
232 wantType: "",
233 },
234 {
235 name: "invalid issue URL - no hostname",
236 url: "/namespace/repo/-/issues/1",
237 wantID: 0,
238 wantType: "",
239 },
240 }
242 for _, tt := range tests {
243 t.Run(tt.name, func(t *testing.T) {
244 result := ParseGitLabURL(tt.url, glinstance.DefaultHostname, config.NewBlankConfig())
246 if tt.wantID == 0 {
247 require.Nil(t, result, "Expected nil result for invalid URL")
248 return
249 }
251 require.NotNil(t, result, "Expected non-nil result for valid URL")
252 require.Equal(t, tt.wantID, result.ID)
253 require.Equal(t, tt.wantType, result.Type)
255 if tt.wantRepoURL != "" {
256 expectedRepo, err := glrepo.FromFullName(tt.wantRepoURL, glinstance.DefaultHostname, config.NewBlankConfig())
257 require.NoError(t, err)
258 require.Equal(t, expectedRepo, result.Repo)
259 }
260 })
261 }
262}
264func TestParseMergeRequestFromURL(t *testing.T) {
265 tests := []struct {
266 name string
267 url string
268 wantID int
269 wantRepoURL string
270 }{
271 {
272 name: "valid MR URL",
273 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/1234",
274 wantID: 1234,
275 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
276 },
277 {
278 name: "invalid URL should return 0",
279 url: "not-a-url",
280 wantID: 0,
281 },
282 }
284 for _, tt := range tests {
285 t.Run(tt.name, func(t *testing.T) {
286 id, repo := ParseMergeRequestFromURL(tt.url, glinstance.DefaultHostname, config.NewBlankConfig())
287 require.Equal(t, tt.wantID, id)
289 if tt.wantID != 0 && tt.wantRepoURL != "" {
290 expectedRepo, err := glrepo.FromFullName(tt.wantRepoURL, glinstance.DefaultHostname, config.NewBlankConfig())
291 require.NoError(t, err)
292 require.Equal(t, expectedRepo, repo)
293 } else {
294 require.Nil(t, repo)
295 }
296 })
297 }
298}
300func TestParseIssueFromURL(t *testing.T) {
301 tests := []struct {
302 name string
303 url string
304 wantID int
305 wantRepoURL string
306 }{
307 {
308 name: "valid issue URL",
309 url: "https://gitlab.com/gitlab-org/cli/-/issues/8398",
310 wantID: 8398,
311 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
312 },
313 {
314 name: "valid work item URL",
315 url: "https://gitlab.com/gitlab-org/cli/-/work_items/8398",
316 wantID: 8398,
317 wantRepoURL: "https://gitlab.com/gitlab-org/cli/",
318 },
319 {
320 name: "issue URL with trailing merge_requests segment",
321 url: "https://gitlab.com/namespace/repo/issues/12/merge_requests/3",
322 wantID: 12,
323 wantRepoURL: "https://gitlab.com/namespace/repo/",
324 },
325 {
326 name: "merge request URL should return 0",
327 url: "https://gitlab.com/gitlab-org/cli/-/merge_requests/1234",
328 wantID: 0,
329 },
330 {
331 name: "wrong protocol should return 0",
332 url: "ftp://gitlab.com/gitlab-org/cli/-/issues/1",
333 wantID: 0,
334 },
335 {
336 name: "URL without hostname should return 0",
337 url: "/gitlab-org/cli/-/issues/1",
338 wantID: 0,
339 },
340 {
341 name: "invalid URL should return 0",
342 url: "not-a-url",
343 wantID: 0,
344 },
345 }
347 for _, tt := range tests {
348 t.Run(tt.name, func(t *testing.T) {
349 id, repo := ParseIssueFromURL(tt.url, glinstance.DefaultHostname, config.NewBlankConfig())
350 require.Equal(t, tt.wantID, id)
352 if tt.wantID != 0 {
353 expectedRepo, err := glrepo.FromFullName(tt.wantRepoURL, glinstance.DefaultHostname, config.NewBlankConfig())
354 require.NoError(t, err)
355 require.Equal(t, expectedRepo, repo)
356 } else {
357 require.Nil(t, repo)
358 }
359 })
360 }
361}
363func TestParseGroupWorkItemFromURL(t *testing.T) {
364 tests := []struct {
365 name string
366 url string
367 wantGroup string
368 wantID int
369 }{
370 {name: "work item URL", url: "https://gitlab.com/groups/gitlab-org/-/work_items/7", wantGroup: "gitlab-org", wantID: 7},
371 {name: "epic URL in a subgroup", url: "https://gitlab.com/groups/gitlab-org/sub/-/epics/7#note_1", wantGroup: "gitlab-org/sub", wantID: 7},
372 {name: "epic URL without dash", url: "https://gitlab.com/groups/gitlab-org/epics/7", wantGroup: "gitlab-org", wantID: 7},
373 {name: "project work item URL", url: "https://gitlab.com/gitlab-org/cli/-/work_items/7"},
374 {name: "wrong protocol", url: "ftp://gitlab.com/groups/gitlab-org/-/epics/7"},
375 {name: "work item URL under a subfolder", url: "https://example.com/gitlab/groups/my-group/-/work_items/7", wantGroup: "my-group", wantID: 7},
376 {name: "epic URL under a subfolder", url: "https://example.com/gitlab/groups/my-group/-/epics/7", wantGroup: "my-group", wantID: 7},
377 }
379 cfg := config.NewBlankConfig()
380 require.NoError(t, cfg.Set("example.com", "subfolder", "gitlab"))
382 for _, tt := range tests {
383 t.Run(tt.name, func(t *testing.T) {
384 group, id := ParseGroupWorkItemFromURL(tt.url, cfg)
385 require.Equal(t, tt.wantGroup, group)
386 require.Equal(t, tt.wantID, id)
387 })
388 }
389}
Diagnostics6 git calls · 231 ms
CallStatusSizeTime0231 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 · hit9 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=9 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=3 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 · 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
fetchtree:02001.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=1238 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17 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=1238 git.pkt.payload.max=1211 git.sideband.band1.bytes=1211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1211 git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetch2001.7 KiB217 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=217 git.response.bytes=1741 git.response.cache=MISS git.response.duration_ms=217 git.fetch.wants=c49a22160ff542ebd8fb5eedd26df89796ffadaa 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=1741 git.pkt.payload.max=1714 git.sideband.band1.bytes=1714 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
- a5e1458ccd4b0bf5
- Request
- 8be6bc39…
- 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