GitLabgitlab-org
cli
main
1package tokenutils
3import (
4 "context"
5 "strconv"
7 "github.com/spf13/cobra"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/api"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/dbg"
14)
16func GroupCompletions(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
17 return cobra.NoFileCompletions(cmd, args, toComplete)
18}
20func UserCompletions(_ *cobra.Command, _ []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
21 return []cobra.Completion{cobra.CompletionWithDesc("@me", "Current user")}, cobra.ShellCompDirectiveNoFileComp
22}
24func NameOrIDCompletions(ctx context.Context, f cmdutils.Factory, cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
25 if len(args) != 0 {
26 return nil, cobra.ShellCompDirectiveNoFileComp
27 }
29 user, err := cmd.Flags().GetString("user")
30 if err != nil {
31 dbg.Debugf("error getting 'user' flag: %v", err)
32 return nil, cobra.ShellCompDirectiveNoFileComp
33 }
35 if err := cmdutils.ApplyRepoOverride(cmd, f); err != nil {
36 dbg.Debugf("error applying repo override: %v", err)
37 return nil, cobra.ShellCompDirectiveNoFileComp
38 }
40 group, err := cmdutils.GroupOverride(cmd)
41 if err != nil {
42 dbg.Debugf("error getting group: %v", err)
43 return nil, cobra.ShellCompDirectiveNoFileComp
44 }
46 // NOTE: like the token commands themselves, this completion also works
47 // outside of a repository, so we fall back to the default hostname.
48 repoHost := ""
49 if repo, err := f.BaseRepo(); err == nil {
50 repoHost = repo.RepoHost()
51 }
52 apiClient, err := f.ApiClient(repoHost)
53 if err != nil {
54 dbg.Debugf("error obtaining API client: %v", err)
55 return nil, cobra.ShellCompDirectiveNoFileComp
56 }
57 client := apiClient.Lab()
59 completions := []cobra.Completion{}
61 switch {
62 case user != "":
63 u, err := api.UserByName(client, user)
64 if err != nil {
65 dbg.Debugf("error finding user %q: %v", user, err)
66 return nil, cobra.ShellCompDirectiveNoFileComp
67 }
68 options := &gitlab.ListPersonalAccessTokensOptions{
69 PerPage: api.MaxPerPage,
70 UserID: &u.ID,
71 }
72 tokens, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.PersonalAccessToken, *gitlab.Response, error) {
73 return client.PersonalAccessTokens.ListPersonalAccessTokens(options, p, gitlab.WithContext(ctx))
74 })
75 if err != nil {
76 dbg.Debugf("error collecting personal access tokens: %v", err)
77 return nil, cobra.ShellCompDirectiveNoFileComp
78 }
79 for _, token := range tokens {
80 completions = appendTokenCompletions(completions, token.Active, token.ID, token.Name)
81 }
82 case group != "":
83 options := &gitlab.ListGroupAccessTokensOptions{ListOptions: gitlab.ListOptions{PerPage: api.MaxPerPage}}
84 tokens, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.GroupAccessToken, *gitlab.Response, error) {
85 return client.GroupAccessTokens.ListGroupAccessTokens(group, options, p, gitlab.WithContext(ctx))
86 })
87 if err != nil {
88 dbg.Debugf("error collecting group access tokens: %v", err)
89 return nil, cobra.ShellCompDirectiveNoFileComp
90 }
91 for _, token := range tokens {
92 completions = appendTokenCompletions(completions, token.Active, token.ID, token.Name)
93 }
94 default:
95 repo, err := f.BaseRepo()
96 if err != nil {
97 dbg.Debugf("error resolving base repository: %v", err)
98 return nil, cobra.ShellCompDirectiveNoFileComp
99 }
100 options := &gitlab.ListProjectAccessTokensOptions{ListOptions: gitlab.ListOptions{PerPage: api.MaxPerPage}}
101 tokens, err := gitlab.ScanAndCollect(func(p gitlab.PaginationOptionFunc) ([]*gitlab.ProjectAccessToken, *gitlab.Response, error) {
102 return client.ProjectAccessTokens.ListProjectAccessTokens(repo.FullName(), options, p, gitlab.WithContext(ctx))
103 })
104 if err != nil {
105 dbg.Debugf("error collecting project access tokens: %v", err)
106 return nil, cobra.ShellCompDirectiveNoFileComp
107 }
108 for _, token := range tokens {
109 completions = appendTokenCompletions(completions, token.Active, token.ID, token.Name)
110 }
111 }
113 return completions, cobra.ShellCompDirectiveNoFileComp
114}
116func appendTokenCompletions(completions []cobra.Completion, active bool, id int64, name string) []cobra.Completion {
117 if !active {
118 return completions
119 }
120 strID := strconv.FormatInt(id, 10)
121 return append(completions,
122 cobra.CompletionWithDesc(strID, name),
123 cobra.CompletionWithDesc(name, strID),
124 )
125}
Diagnostics8 git calls · 341 ms
CallStatusSizeTime0341 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 · 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=44Open pack map
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: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:0200358 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=358 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=218b543e075f31e84c52193d193103408c52cb34 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=358 git.pkt.payload.max=331 git.sideband.band1.bytes=331 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=331 git.pack.trailer=fbb77d44047cd3e1ee682eb9483227c59489e50fOpen pack map
fetchtree:0200143 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=143 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=92a7d66497c7f31e5826eef28aeb6841773b0b04 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=143 git.pkt.payload.max=116 git.sideband.band1.bytes=116 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=116 git.pack.trailer=feb17a6171e412d7f30efc3ef7678e87ec969eccOpen pack map
fetch2001.2 KiB326 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=326 git.response.bytes=1208 git.response.cache=MISS git.response.duration_ms=326 git.fetch.wants=caa64c99f1a8fe6e1579bdd7cc49586a0d2c3c18 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=1208 git.pkt.payload.max=1181 git.sideband.band1.bytes=1181 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
- a5e2d6bb1dc3689d
- Request
- 0fcac08d…
- 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