GitLabgitlab-org
cli
main
1//go:build !integration
3package glrepo
5import (
6 "errors"
7 "fmt"
8 "net/url"
9 "testing"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17 "gitlab.com/gitlab-org/cli/internal/api"
18 "gitlab.com/gitlab-org/cli/internal/config"
19 "gitlab.com/gitlab-org/cli/internal/git"
20 "gitlab.com/gitlab-org/cli/internal/glinstance"
21)
23func Test_RemoteURL(t *testing.T) {
24 type args struct {
25 project *gitlab.Project
26 protocol string
27 }
29 for _, tt := range []struct {
30 name string
31 args args
32 want string
33 wantErr bool
34 }{
35 {
36 name: "is_https",
37 args: args{
38 project: &gitlab.Project{
39 SSHURLToRepo: "git@gitlab.com:profclems/glab.git",
40 HTTPURLToRepo: "https://gitlab.com/profclems/glab.git",
41 },
42 protocol: "https",
43 },
44 want: "https://gitlab.com/profclems/glab.git",
45 },
46 {
47 name: "host_is_http",
48 args: args{
49 project: &gitlab.Project{
50 SSHURLToRepo: "git@gitlabedd.com:profclems/glab.git",
51 HTTPURLToRepo: "http://gitlabedd.com/profclems/glab.git",
52 },
53 protocol: "http",
54 },
55 want: "http://gitlabedd.com/profclems/glab.git",
56 },
57 {
58 name: "is_ssh",
59 args: args{
60 project: &gitlab.Project{
61 SSHURLToRepo: "git@gitlab.com:profclems/glab.git",
62 },
63 protocol: "ssh",
64 },
65 want: "git@gitlab.com:profclems/glab.git",
66 },
67 {
68 name: "malicious_https",
69 args: args{
70 project: &gitlab.Project{HTTPURLToRepo: "--upload-pack=touch x"},
71 protocol: "https",
72 },
73 wantErr: true,
74 },
75 {
76 name: "malicious_ssh",
77 args: args{
78 project: &gitlab.Project{SSHURLToRepo: "ext::sh -c touch% /tmp/x"},
79 protocol: "ssh",
80 },
81 wantErr: true,
82 },
83 } {
84 t.Run(tt.name, func(t *testing.T) {
85 got, err := RemoteURL(tt.args.project, tt.args.protocol)
86 if tt.wantErr {
87 require.Error(t, err)
88 return
89 }
90 require.NoError(t, err)
91 if got != tt.want {
92 t.Errorf("RemoteURL() got = %v, want %v", got, tt.want)
93 }
94 })
95 }
96}
98func TestWikiRemoteURL(t *testing.T) {
99 t.Parallel()
101 project := &gitlab.Project{
102 SSHURLToRepo: "git@gitlab.com:profclems/docs.wiki.git",
103 HTTPURLToRepo: "https://gitlab.com/profclems/docs.wiki.git",
104 }
106 got, err := WikiRemoteURL(project, "ssh")
107 require.NoError(t, err)
108 assert.Equal(t, "git@gitlab.com:profclems/docs.wiki.wiki.git", got)
109 got, err = WikiRemoteURL(project, "https")
110 require.NoError(t, err)
111 assert.Equal(t, "https://gitlab.com/profclems/docs.wiki.wiki.git", got)
113 malicious := &gitlab.Project{
114 SSHURLToRepo: "ext::sh -c touch% /tmp/x",
115 HTTPURLToRepo: "--upload-pack=touch x",
116 }
117 _, err = WikiRemoteURL(malicious, "ssh")
118 require.Error(t, err)
119 _, err = WikiRemoteURL(malicious, "https")
120 require.Error(t, err)
121}
123func Test_repoFromURL(t *testing.T) {
124 cfg := config.NewFromString(`---
125hosts:
126 my.host.com:
127 token: OTOKEN
128 api_host: my.host.com/git
129 gdk.test:
130 token: OTOKEN
131 api_host: gdk.test:3443
132 git.host.com:
133 token: OTOKEN
134 api_host: git-api.host.com
135`)
137 tests := []struct {
138 name string
139 input string
140 result string
141 host string
142 err error
143 }{
144 {
145 name: "gitlab.com URL",
146 input: "https://gitlab.com/monalisa/octo-cat.git",
147 result: "monalisa/octo-cat",
148 host: "gitlab.com",
149 err: nil,
150 },
151 {
152 name: "gitlab.com URL with trailing slash",
153 input: "https://gitlab.com/monalisa/octo-cat/",
154 result: "monalisa/octo-cat",
155 host: "gitlab.com",
156 err: nil,
157 },
158 {
159 name: "gitlab.com dot-git URL with trailing slash",
160 input: "https://gitlab.com/monalisa/octo-cat.git/",
161 result: "monalisa/octo-cat",
162 host: "gitlab.com",
163 err: nil,
164 },
165 {
166 name: "www.gitlab.com URL",
167 input: "http://www.GITLAB.com/monalisa/octo-cat.git",
168 result: "monalisa/octo-cat",
169 host: "gitlab.com",
170 err: nil,
171 },
172 {
173 name: "self-managed www. hostname kept",
174 input: "https://www.example.com/monalisa/octo-cat.git",
175 result: "monalisa/octo-cat",
176 host: "www.example.com",
177 err: nil,
178 },
179 {
180 name: "self-managed www. hostname with port kept",
181 input: "https://www.example.com:8443/monalisa/octo-cat.git",
182 result: "monalisa/octo-cat",
183 host: "www.example.com:8443",
184 err: nil,
185 },
186 {
187 name: "group namespacing",
188 input: "https://gitlab.com/monalisa/octo-cat/minor",
189 result: "monalisa/octo-cat/minor",
190 host: "gitlab.com",
191 err: nil,
192 },
193 {
194 name: "non-GitLab hostname",
195 input: "https://example.com/one/two",
196 result: "one/two",
197 host: "example.com",
198 err: nil,
199 },
200 {
201 name: "non-GitLab hostname with api_host",
202 input: "https://git.host.com/one/two",
203 result: "one/two",
204 host: "git.host.com",
205 err: nil,
206 },
207 {
208 name: "GDK with api_host",
209 input: "https://gdk.test:3443/one/two",
210 result: "one/two",
211 host: "gdk.test:3443",
212 err: nil,
213 },
214 {
215 name: "non-standard port without api_host",
216 input: "https://example.com:8443/owner/repo",
217 result: "owner/repo",
218 host: "example.com:8443",
219 err: nil,
220 },
221 {
222 name: "non-GitLab hostname with subdirectory and api_host",
223 input: "https://my.host.com/git/one/two",
224 result: "one/two",
225 host: "my.host.com",
226 err: nil,
227 },
228 {
229 name: "filesystem path",
230 input: "/path/to/file",
231 result: "",
232 host: "",
233 err: errors.New("no hostname detected"),
234 },
235 {
236 name: "filesystem path with scheme",
237 input: "file:///path/to/file",
238 result: "",
239 host: "",
240 err: errors.New("no hostname detected"),
241 },
242 {
243 name: "gitlab.com SSH URL",
244 input: "ssh://gitlab.com/monalisa/octo-cat.git",
245 result: "monalisa/octo-cat",
246 host: "gitlab.com",
247 err: nil,
248 },
249 {
250 name: "gitlab.com HTTPS+SSH URL",
251 input: "https+ssh://gitlab.com/monalisa/octo-cat.git",
252 result: "monalisa/octo-cat",
253 host: "gitlab.com",
254 err: nil,
255 },
256 {
257 name: "gitlab.com git URL",
258 input: "git://gitlab.com/monalisa/octo-cat.git",
259 result: "monalisa/octo-cat",
260 host: "gitlab.com",
261 err: nil,
262 },
263 {
264 name: "gitlab.com deep nested",
265 input: "git://gitlab.com/owner/subgroup/subgroup1/subgroup2/subgroup3/namespace/repo.git",
266 result: "owner/subgroup/subgroup1/subgroup2/subgroup3/namespace/repo",
267 host: "gitlab.com",
268 err: nil,
269 },
270 }
272 for _, tt := range tests {
273 t.Run(tt.name, func(t *testing.T) {
274 u, err := url.Parse(tt.input)
275 if err != nil {
276 t.Fatalf("got error %q", err)
277 }
279 repo, err := FromURL(u, glinstance.DefaultHostname, cfg)
280 if err != nil {
281 if tt.err == nil {
282 t.Fatalf("got error %q", err)
283 } else if tt.err.Error() == err.Error() {
284 return
285 }
286 t.Fatalf("got error %q", err)
287 }
289 got := repo.FullName()
290 if tt.result != got {
291 t.Errorf("expected %q, got %q", tt.result, got)
292 }
293 if tt.host != repo.RepoHost() {
294 t.Errorf("expected %q, got %q", tt.host, repo.RepoHost())
295 }
296 })
297 }
298}
300func Test_normalizeHostname(t *testing.T) {
301 tests := []struct {
302 name string
303 input string
304 want string
305 }{
306 {"gitlab.com unchanged", "gitlab.com", "gitlab.com"},
307 {"www.gitlab.com collapses", "www.gitlab.com", "gitlab.com"},
308 {"www.gitlab.com with port collapses", "www.gitlab.com:8443", "gitlab.com:8443"},
309 {"uppercase gitlab.com lowercased", "GITLAB.COM", "gitlab.com"},
310 {"uppercase www.gitlab.com collapses", "WWW.GITLAB.COM", "gitlab.com"},
311 {"self-managed www. host kept", "www.example.com", "www.example.com"},
312 {"self-managed www. host with port kept", "www.example.com:8443", "www.example.com:8443"},
313 {"self-managed host unchanged", "example.com", "example.com"},
314 }
316 for _, tt := range tests {
317 t.Run(tt.name, func(t *testing.T) {
318 assert.Equal(t, tt.want, normalizeHostname(tt.input))
319 })
320 }
321}
323func TestFromFullName(t *testing.T) {
324 cfg := config.NewFromString(`---
325hosts:
326 gitlab.com:
327 token: xxxxxxxxxxxxxxxxxxxx
328 git_protocol: ssh
329 api_protocol: https
330 example.org:
331 token: xxxxxxxxxxxxxxxxxxxxx
332 localhost:3000:
333 token: xxxxxxxxxxxxxxxxxxxxx
334`)
335 tests := []struct {
336 name string
337 input string
338 wantOwner string
339 wantName string
340 wantHost string
341 wantFullname string
342 wantGroup string
343 wantNamespace string
344 wantErr error
345 }{
346 {
347 name: "OWNER/REPO combo",
348 input: "OWNER/REPO",
349 wantHost: "gitlab.com",
350 wantOwner: "OWNER",
351 wantName: "REPO",
352 wantFullname: "OWNER/REPO",
353 wantNamespace: "OWNER",
354 wantErr: nil,
355 },
356 {
357 name: "too few elements",
358 input: "OWNER",
359 wantErr: errors.New(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got "OWNER"`),
360 },
361 {
362 name: "group namespace",
363 input: "example.org/b/c/d",
364 wantHost: "example.org",
365 wantOwner: "b/c",
366 wantName: "d",
367 wantFullname: "b/c/d",
368 wantNamespace: "c",
369 wantGroup: "b",
370 wantErr: nil,
371 },
372 {
373 name: "with group namespace",
374 input: "gitlab.com/owner/namespace/repo",
375 wantHost: "gitlab.com",
376 wantOwner: "owner/namespace",
377 wantName: "repo",
378 wantFullname: "owner/namespace/repo",
379 wantNamespace: "namespace",
380 wantGroup: "owner",
381 wantErr: nil,
382 },
383 {
384 name: "blank value",
385 input: "a/",
386 wantErr: errors.New(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got "a/"`),
387 },
388 {
389 name: "blank value inner",
390 input: "a//c",
391 wantErr: errors.New(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got "a//c"`),
392 },
393 {
394 name: "with hostname",
395 input: "example.org/OWNER/REPO",
396 wantHost: "example.org",
397 wantOwner: "OWNER",
398 wantName: "REPO",
399 wantFullname: "OWNER/REPO",
400 wantNamespace: "OWNER",
401 wantGroup: "",
402 wantErr: nil,
403 },
404 {
405 name: "authenticated hostname with port and no dot",
406 input: "localhost:3000/OWNER/REPO",
407 wantHost: "localhost:3000",
408 wantOwner: "OWNER",
409 wantName: "REPO",
410 wantFullname: "OWNER/REPO",
411 wantNamespace: "OWNER",
412 wantGroup: "",
413 wantErr: nil,
414 },
415 {
416 name: "unauthenticated hostname with port",
417 input: "localhost:4000/OWNER/REPO",
418 wantHost: "gitlab.com",
419 wantOwner: "localhost:4000/OWNER",
420 wantName: "REPO",
421 wantFullname: "localhost:4000/OWNER/REPO",
422 wantNamespace: "OWNER",
423 wantGroup: "localhost:4000",
424 wantErr: nil,
425 },
426 {
427 name: "group name has dot",
428 input: "my.group/sub.group/repo",
429 wantHost: "gitlab.com",
430 wantOwner: "my.group/sub.group",
431 wantName: "repo",
432 wantFullname: "my.group/sub.group/repo",
433 wantNamespace: "sub.group",
434 wantGroup: "my.group",
435 wantErr: nil,
436 },
437 {
438 name: "full URL",
439 input: "https://example.org/OWNER/REPO.git",
440 wantHost: "example.org",
441 wantOwner: "OWNER",
442 wantName: "REPO",
443 wantFullname: "OWNER/REPO",
444 wantNamespace: "OWNER",
445 wantGroup: "",
446 wantErr: nil,
447 },
448 {
449 name: "SSH URL",
450 input: "git@example.org:OWNER/REPO.git",
451 wantHost: "example.org",
452 wantOwner: "OWNER",
453 wantName: "REPO",
454 wantFullname: "OWNER/REPO",
455 wantNamespace: "OWNER",
456 wantGroup: "",
457 wantErr: nil,
458 },
459 {
460 name: "Deep Nested Groups",
461 input: "git@example.org:GROUP/SUBGROUP1/SUBGROUP2/SUBGROUP3/SUBGROUP4/REPO.git",
462 wantHost: "example.org",
463 wantOwner: "GROUP/SUBGROUP1/SUBGROUP2/SUBGROUP3/SUBGROUP4",
464 wantName: "REPO",
465 wantFullname: "GROUP/SUBGROUP1/SUBGROUP2/SUBGROUP3/SUBGROUP4/REPO",
466 wantNamespace: "SUBGROUP1/SUBGROUP2/SUBGROUP3/SUBGROUP4",
467 wantGroup: "GROUP",
468 wantErr: nil,
469 },
470 {
471 name: "invalid URL",
472 input: "git@example.com/%/url",
473 wantErr: errors.New(`parse "git@example.com/%/url": invalid URL escape "%/u"`),
474 },
475 }
476 for _, tt := range tests {
477 t.Run(tt.name, func(t *testing.T) {
478 r, err := FromFullName(tt.input, glinstance.DefaultHostname, cfg)
479 if tt.wantErr != nil {
480 if err == nil {
481 t.Fatalf("no error in result, expected %v", tt.wantErr)
482 } else if err.Error() != tt.wantErr.Error() {
483 t.Fatalf("expected error %q, got %q", tt.wantErr.Error(), err.Error())
484 }
485 return
486 }
487 if err != nil {
488 t.Fatalf("got error %v", err)
489 }
490 if r.RepoHost() != tt.wantHost {
491 t.Errorf("expected host %q, got %q", tt.wantHost, r.RepoHost())
492 }
493 if r.RepoOwner() != tt.wantOwner {
494 t.Errorf("expected owner %q, got %q", tt.wantOwner, r.RepoOwner())
495 }
496 if r.RepoName() != tt.wantName {
497 t.Errorf("expected name %q, got %q", tt.wantName, r.RepoName())
498 }
499 if r.RepoGroup() != tt.wantGroup {
500 t.Errorf("expected group %q, got %q", tt.wantGroup, r.RepoGroup())
501 }
502 if r.FullName() != tt.wantFullname {
503 t.Errorf("expected fullname %q, got %q", tt.wantFullname, r.FullName())
504 }
505 if r.RepoNamespace() != tt.wantNamespace {
506 t.Errorf("expected namespace %q, got %q", tt.wantNamespace, r.RepoNamespace())
507 }
508 })
509 }
510}
512func TestFullNameFromURL(t *testing.T) {
513 tests := []struct {
514 remoteURL string
515 want string
516 wantErr error
517 }{
518 {
519 remoteURL: "gitlab.com/profclems/glab.git",
520 wantErr: errors.New("cannot parse remote: gitlab.com/profclems/glab.git"),
521 },
522 {
523 remoteURL: "ssh://https://gitlab.com/owner/repo",
524 wantErr: errors.New(`cannot parse remote: ssh://https://gitlab.com/owner/repo`),
525 },
526 {
527 remoteURL: "https://gitlab.com/profclems/glab.git",
528 want: "profclems/glab",
529 wantErr: nil,
530 },
531 {
532 remoteURL: "https://gitlab.com/profclems/glab",
533 want: "profclems/glab",
534 wantErr: nil,
535 },
536 {
537 remoteURL: "https://gitlab.com/profclems/glab/",
538 want: "profclems/glab",
539 wantErr: nil,
540 },
541 {
542 remoteURL: "https://gitlab.com/profclems/glab.git/",
543 want: "profclems/glab",
544 wantErr: nil,
545 },
546 {
547 remoteURL: "https://gitlab.com/owner/namespace/repo.git",
548 want: "owner/namespace/repo",
549 wantErr: nil,
550 },
551 {
552 remoteURL: "git@gitlab.com:owner/namespace/repo.git",
553 want: "owner/namespace/repo",
554 wantErr: nil,
555 },
556 {
557 remoteURL: "git@gitlab.com:owner/subgroup/subgroup1/subgroup2/subgroup3/namespace/repo.git",
558 want: "owner/subgroup/subgroup1/subgroup2/subgroup3/namespace/repo",
559 wantErr: nil,
560 },
561 }
562 for i, tt := range tests {
563 t.Run(fmt.Sprintf("%d - %s", i, tt.remoteURL), func(t *testing.T) {
564 got, err := FullNameFromURL(tt.remoteURL)
565 if tt.wantErr != nil {
566 if err == nil {
567 t.Fatalf("no error in result, expected %v", tt.wantErr)
568 } else if err.Error() != tt.wantErr.Error() {
569 t.Fatalf("expected error %q, got %q", tt.wantErr.Error(), err.Error())
570 }
571 return
572 }
573 if got != tt.want {
574 t.Errorf("FullNameFromURL() got = %v, want %v", got, tt.want)
575 }
576 })
577 }
578}
580func Test_NewWitHost(t *testing.T) {
581 testCases := []struct {
582 name string
583 input []string
584 wantHostname string
585 wantOwner string
586 wantGroup string
587 wantNamespace string
588 wantName string
589 wantFullname string
590 }{
591 {
592 name: "Simple",
593 input: []string{"profclems", "glab", "gitlab.com"},
594 wantHostname: "gitlab.com",
595 wantNamespace: "profclems",
596 wantOwner: "profclems",
597 wantName: "glab",
598 wantFullname: "profclems/glab",
599 },
600 {
601 name: "group",
602 input: []string{"company/profclems", "glab", "gitlab.com"},
603 wantHostname: "gitlab.com",
604 wantNamespace: "profclems",
605 wantOwner: "company/profclems",
606 wantGroup: "company",
607 wantName: "glab",
608 wantFullname: "company/profclems/glab",
609 },
610 }
611 for _, tC := range testCases {
612 t.Run(tC.name, func(t *testing.T) {
613 got := NewWithHost(tC.input[0], tC.input[1], tC.input[2])
614 if tC.wantHostname != "" {
615 assert.Equal(t, tC.wantHostname, got.RepoHost())
616 }
617 if tC.wantOwner != "" {
618 assert.Equal(t, tC.wantOwner, got.RepoOwner())
619 }
620 if tC.wantGroup != "" {
621 assert.Equal(t, tC.wantGroup, got.RepoGroup())
622 }
623 if tC.wantNamespace != "" {
624 assert.Equal(t, tC.wantNamespace, got.RepoNamespace())
625 }
626 if tC.wantName != "" {
627 assert.Equal(t, tC.wantName, got.RepoName())
628 }
629 if tC.wantFullname != "" {
630 assert.Equal(t, tC.wantFullname, got.FullName())
631 }
632 })
633 }
634}
636// FromURL resolves a remote URL against a host entry in the config. The entry is
637// keyed exactly as `auth login` writes it, which is `u.Host` — the hostname plus
638// the port when one is given. This matrix covers the combinations a self-managed
639// instance can be reached on: default or custom port, root or subfolder install,
640// and the `subfolder` key or the older `api_host`-with-path spelling.
641//
642// RepoHost is asserted alongside the project path because it is the key every
643// later config lookup uses (protocol, token, API host). A resolution that gets
644// the path right but the host wrong still sends unauthenticated requests to the
645// wrong port.
646func Test_FromURL_PortAndSubfolderMatrix(t *testing.T) {
647 t.Parallel()
649 cases := []struct {
650 name string
651 cfg string
652 url string
653 wantFullName string
654 wantHost string
655 }{
656 {
657 name: "no port, no subfolder",
658 cfg: heredoc.Doc(`
659 hosts:
660 example.com:
661 token: TOKEN
662 `),
663 url: "https://example.com/group/repo.git",
664 wantFullName: "group/repo",
665 wantHost: "example.com",
666 },
667 {
668 name: "custom port, no subfolder",
669 cfg: heredoc.Doc(`
670 hosts:
671 example.com:8080:
672 token: TOKEN
673 `),
674 url: "http://example.com:8080/group/repo.git",
675 wantFullName: "group/repo",
676 wantHost: "example.com:8080",
677 },
678 {
679 name: "no port, subfolder key",
680 cfg: heredoc.Doc(`
681 hosts:
682 example.com:
683 token: TOKEN
684 subfolder: gitlab
685 `),
686 url: "https://example.com/gitlab/group/repo.git",
687 wantFullName: "group/repo",
688 wantHost: "example.com",
689 },
690 {
691 name: "custom port, subfolder key",
692 cfg: heredoc.Doc(`
693 hosts:
694 example.com:8080:
695 token: TOKEN
696 subfolder: gitlab
697 `),
698 url: "http://example.com:8080/gitlab/group/repo.git",
699 wantFullName: "group/repo",
700 wantHost: "example.com:8080",
701 },
702 {
703 name: "no port, nested subfolder key",
704 cfg: heredoc.Doc(`
705 hosts:
706 example.com:
707 token: TOKEN
708 subfolder: apps/gitlab
709 `),
710 url: "https://example.com/apps/gitlab/group/repo.git",
711 wantFullName: "group/repo",
712 wantHost: "example.com",
713 },
714 {
715 name: "custom port, nested subfolder key",
716 cfg: heredoc.Doc(`
717 hosts:
718 example.com:8080:
719 token: TOKEN
720 subfolder: apps/gitlab
721 `),
722 url: "http://example.com:8080/apps/gitlab/group/repo.git",
723 wantFullName: "group/repo",
724 wantHost: "example.com:8080",
725 },
726 {
727 name: "no port, api_host backward compatibility",
728 cfg: heredoc.Doc(`
729 hosts:
730 example.com:
731 token: TOKEN
732 api_host: example.com/gitlab
733 `),
734 url: "https://example.com/gitlab/group/repo.git",
735 wantFullName: "group/repo",
736 wantHost: "example.com",
737 },
738 {
739 name: "custom port, api_host backward compatibility",
740 cfg: heredoc.Doc(`
741 hosts:
742 example.com:8080:
743 token: TOKEN
744 api_host: example.com:8080/gitlab
745 `),
746 url: "http://example.com:8080/gitlab/group/repo.git",
747 wantFullName: "group/repo",
748 wantHost: "example.com:8080",
749 },
750 {
751 name: "custom port, subfolder key wins over api_host",
752 cfg: heredoc.Doc(`
753 hosts:
754 example.com:8080:
755 token: TOKEN
756 subfolder: gitlab
757 api_host: example.com:8080/wrong
758 `),
759 url: "http://example.com:8080/gitlab/group/repo.git",
760 wantFullName: "group/repo",
761 wantHost: "example.com:8080",
762 },
763 {
764 name: "custom port, subgroup under a subfolder",
765 cfg: heredoc.Doc(`
766 hosts:
767 example.com:8080:
768 token: TOKEN
769 subfolder: gitlab
770 `),
771 url: "http://example.com:8080/gitlab/group/subgroup/repo.git",
772 wantFullName: "group/subgroup/repo",
773 wantHost: "example.com:8080",
774 },
775 {
776 // The subfolder is only a prefix to strip. A group that happens to
777 // share its name must survive when it appears deeper in the path.
778 name: "custom port, group name repeats the subfolder",
779 cfg: heredoc.Doc(`
780 hosts:
781 example.com:8080:
782 token: TOKEN
783 subfolder: gitlab
784 `),
785 url: "http://example.com:8080/gitlab/gitlab/repo.git",
786 wantFullName: "gitlab/repo",
787 wantHost: "example.com:8080",
788 },
789 {
790 // A host entry for the portless name must not answer for a ported
791 // remote: they are different instances as far as the config is
792 // concerned, and the API client treats them that way too.
793 name: "port on the remote, config keyed without it, is not applied",
794 cfg: heredoc.Doc(`
795 hosts:
796 example.com:
797 token: TOKEN
798 subfolder: gitlab
799 `),
800 url: "http://example.com:8080/gitlab/group/repo.git",
801 wantFullName: "gitlab/group/repo",
802 wantHost: "example.com:8080",
803 },
804 {
805 name: "config for an unrelated host does not leak",
806 cfg: heredoc.Doc(`
807 hosts:
808 other.example.com:8080:
809 token: TOKEN
810 subfolder: gitlab
811 `),
812 url: "http://example.com:8080/gitlab/group/repo.git",
813 wantFullName: "gitlab/group/repo",
814 wantHost: "example.com:8080",
815 },
816 }
818 for _, tc := range cases {
819 t.Run(tc.name, func(t *testing.T) {
820 t.Parallel()
822 u, err := url.Parse(tc.url)
823 require.NoError(t, err)
825 repo, err := FromURL(u, glinstance.DefaultHostname, config.NewFromString(tc.cfg))
826 require.NoError(t, err)
827 assert.Equal(t, tc.wantFullName, repo.FullName())
828 assert.Equal(t, tc.wantHost, repo.RepoHost())
829 })
830 }
831}
833// SSH remotes reach FromURL after git.ParseURL has normalized SCP-style syntax.
834// git.ParseURL deliberately drops the SSH port (see its TestParseURL "ssh with
835// port" case), because an SSH port is not the port the API is served on: the
836// host entry a remote resolves against is keyed on the HTTP host. So an SSH
837// remote always looks up the portless entry, whatever port Git connects on.
838//
839// FromURL is scheme-agnostic, so the subfolder segment in these paths is a
840// probe: stripping it proves which host entry answered the lookup. Real SSH
841// remotes do not carry one — see Test_remoteResolver_SSHRemoteOnPortedSubfolderHost.
842func Test_FromURL_SSHSubfolderMatrix(t *testing.T) {
843 t.Parallel()
845 cases := []struct {
846 name string
847 cfg string
848 remote string
849 wantFullName string
850 wantHost string
851 }{
852 {
853 name: "scp-style, subfolder",
854 cfg: heredoc.Doc(`
855 hosts:
856 example.com:
857 token: TOKEN
858 subfolder: gitlab
859 `),
860 remote: "git@example.com:gitlab/group/repo.git",
861 wantFullName: "group/repo",
862 wantHost: "example.com",
863 },
864 {
865 name: "scp-style, nested subfolder and subgroup",
866 cfg: heredoc.Doc(`
867 hosts:
868 example.com:
869 token: TOKEN
870 subfolder: apps/gitlab
871 `),
872 remote: "git@example.com:apps/gitlab/group/subgroup/repo.git",
873 wantFullName: "group/subgroup/repo",
874 wantHost: "example.com",
875 },
876 {
877 // The SSH port is dropped, so the portless host entry applies and
878 // the subfolder is still stripped.
879 name: "ssh:// with port, subfolder",
880 cfg: heredoc.Doc(`
881 hosts:
882 example.com:
883 token: TOKEN
884 subfolder: gitlab
885 `),
886 remote: "ssh://git@example.com:2222/gitlab/group/repo.git",
887 wantFullName: "group/repo",
888 wantHost: "example.com",
889 },
890 {
891 name: "ssh:// with port, no subfolder",
892 cfg: heredoc.Doc(`
893 hosts:
894 example.com:
895 token: TOKEN
896 `),
897 remote: "ssh://git@example.com:2222/group/repo.git",
898 wantFullName: "group/repo",
899 wantHost: "example.com",
900 },
901 }
903 for _, tc := range cases {
904 t.Run(tc.name, func(t *testing.T) {
905 t.Parallel()
907 u, err := git.ParseURL(tc.remote)
908 require.NoError(t, err)
910 repo, err := FromURL(u, glinstance.DefaultHostname, config.NewFromString(tc.cfg))
911 require.NoError(t, err)
912 assert.Equal(t, tc.wantFullName, repo.FullName())
913 assert.Equal(t, tc.wantHost, repo.RepoHost())
914 })
915 }
916}
918func Test_glRepo_Project_cachesTheFetch(t *testing.T) {
919 // A value receiver on Project() made the cache write a no-op, so every
920 // caller re-fetched. Loops such as `glab ci cancel` refetched per item.
921 originalGetProject := api.GetProject
922 t.Cleanup(func() { api.GetProject = originalGetProject })
924 calls := 0
925 api.GetProject = func(_ *gitlab.Client, projectID any) (*gitlab.Project, error) {
926 calls++
927 return &gitlab.Project{ID: 42, PathWithNamespace: fmt.Sprint(projectID)}, nil
928 }
930 repo := NewWithHost("OWNER", "REPO", "gitlab.com")
932 first, err := repo.Project(nil)
933 require.NoError(t, err)
934 _, err = repo.Project(nil)
935 require.NoError(t, err)
937 assert.Equal(t, 1, calls)
938 assert.Equal(t, "OWNER/REPO", first.PathWithNamespace)
939}
Diagnostics6 git calls · 345 ms
CallStatusSizeTime0345 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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 · hit10 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=10 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:0200314 B · 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=314 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=93a3fb44940eceae81f0a58fd4df1d50fbd7ccb5 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=314 git.pkt.payload.max=287 git.sideband.band1.bytes=287 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=287 git.pack.trailer=0a0d545706824b859dbb83ba13169780ef407392Open pack map
fetch2004.5 KiB331 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=331 git.response.bytes=4656 git.response.cache=MISS git.response.duration_ms=331 git.fetch.wants=7a1662409e8051524ceaf1e9bfde97c8e2c40f8b 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=4656 git.pkt.payload.max=4629 git.sideband.band1.bytes=4629 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
- a5e2d7c7888214c2
- Request
- 6375080f…
- 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