GitLabgitlab-org

cli

main
ply8 git calls232 ms99 KiB6 cachedprotocol v2
203 lines5.3 KiBRaw
1package list
3import (
4 "strconv"
6 "github.com/MakeNowJust/heredoc/v2"
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/glrepo"
14 "gitlab.com/gitlab-org/cli/internal/iostreams"
15 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
16 "gitlab.com/gitlab-org/cli/internal/tableprinter"
17 "gitlab.com/gitlab-org/cli/internal/utils"
20type printLabel struct {
21 ID string
22 Name string
23 Description string
24 Color string
27type options struct {
28 io *iostreams.IOStreams
29 apiClient func(repoHost string) (*api.Client, error)
30 baseRepo func() (glrepo.Interface, error)
31 group string
32 page int
33 perPage int
34 outputFormat string
37func NewCmdList(f cmdutils.Factory) *cobra.Command {
38 opts := &options{
39 io: f.IO(),
40 apiClient: f.ApiClient,
41 baseRepo: f.BaseRepo,
42 }
44 labelListCmd := &cobra.Command{
45 Use: "list [flags]",
46 Short: `List labels in a project or group.`,
47 Long: heredoc.Docf(`
48 By default, labels are listed for the current repository. Use
49 %[1]s--group%[1]s to list labels for a group or subgroup, or
50 %[1]s--repo%[1]s to target another project.
52 Use %[1]s--output json%[1]s to format the result as JSON for use with other tools.
53 `, "`"),
54 Aliases: []string{"ls"},
55 Example: heredoc.Doc(`
56 # List labels in the current repository
57 glab label list
59 # List labels in another project
60 glab label list -R owner/repository
62 # List labels in a group
63 glab label list -g mygroup
65 # List labels as JSON
66 glab label list --output json
67 `),
68 Args: cobra.NoArgs,
69 ValidArgsFunction: cobra.NoFileCompletions,
70 Annotations: map[string]string{
71 mcpannotations.Safe: "true",
72 },
73 RunE: func(cmd *cobra.Command, args []string) error {
74 return opts.run()
75 },
76 }
78 labelListCmd.Flags().IntVarP(&opts.page, "page", "p", 1, "Page number.")
79 labelListCmd.Flags().IntVarP(&opts.perPage, "per-page", "P", 30, "Number of items to list per page.")
80 cmdutils.EnableJSONOutput(labelListCmd, opts.io, &opts.outputFormat)
81 labelListCmd.Flags().StringVarP(&opts.group, "group", "g", "", "List labels for a group.")
83 return labelListCmd
86type listLabelsOptions struct {
87 withCounts *bool
88 perPage int64
89 page int64
92func (opts *listLabelsOptions) listLabelsOptions() *gitlab.ListLabelsOptions {
93 projectOpts := &gitlab.ListLabelsOptions{}
94 projectOpts.WithCounts = opts.withCounts
95 projectOpts.PerPage = opts.perPage
96 projectOpts.Page = opts.page
97 return projectOpts
100func (opts *listLabelsOptions) listGroupLabelsOptions() *gitlab.ListGroupLabelsOptions {
101 groupOpts := &gitlab.ListGroupLabelsOptions{}
102 groupOpts.WithCounts = opts.withCounts
103 groupOpts.PerPage = opts.perPage
104 groupOpts.Page = opts.page
105 return groupOpts
108func (o *options) run() error {
109 var err error
110 var pl []printLabel
112 // NOTE: this command can not only be used for projects,
113 // so we have to manually check for the base repo, if it doesn't exist,
114 // we bootstrap the client with the default hostname.
115 var repoHost string
116 if baseRepo, err := o.baseRepo(); err == nil {
117 repoHost = baseRepo.RepoHost()
118 }
119 apiClient, err := o.apiClient(repoHost)
120 if err != nil {
121 return err
122 }
123 client := apiClient.Lab()
125 labelApiOpts := &listLabelsOptions{}
126 labelApiOpts.withCounts = new(true)
128 if o.page != 0 {
129 labelApiOpts.page = int64(o.page)
130 }
131 if o.perPage != 0 {
132 labelApiOpts.perPage = int64(o.perPage)
133 } else {
134 labelApiOpts.perPage = api.DefaultListLimit
135 }
137 if o.group != "" {
138 labels, resp, err := client.GroupLabels.ListGroupLabels(o.group, labelApiOpts.listGroupLabelsOptions())
139 if err != nil {
140 return err
141 }
142 if o.outputFormat == "json" {
143 if err := o.io.PrintJSON(labels); err != nil {
144 return err
145 }
146 } else {
147 o.io.LogInfof("%s\n", o.listTitle(o.group, len(labels), resp))
148 for _, label := range labels {
149 pl = append(pl, printLabel{ID: strconv.FormatInt(label.ID, 10), Name: label.Name, Description: label.Description, Color: label.Color})
150 }
151 printLabels(pl, o.io)
152 }
153 } else {
154 repo, err := o.baseRepo()
155 if err != nil {
156 return err
157 }
159 labels, resp, err := client.Labels.ListLabels(repo.FullName(), labelApiOpts.listLabelsOptions())
160 if err != nil {
161 return err
162 }
163 if o.outputFormat == "json" {
164 if err := o.io.PrintJSON(labels); err != nil {
165 return err
166 }
167 } else {
168 o.io.LogInfof("%s\n", o.listTitle(repo.FullName(), len(labels), resp))
169 for _, label := range labels {
170 pl = append(pl, printLabel{ID: strconv.FormatInt(label.ID, 10), Name: label.Name, Description: label.Description, Color: label.Color})
171 }
172 printLabels(pl, o.io)
173 }
175 }
177 return nil
180func printLabels(label []printLabel, io *iostreams.IOStreams) {
181 table := tableprinter.NewTablePrinter()
183 if len(label) > 0 {
184 table.AddRow("ID", "Name", "Description", "Color")
185 }
187 for _, l := range label {
188 table.AddRow(l.ID, l.Name, l.Description, l.Color)
189 }
191 io.LogInfo(table.String())
194func (o *options) listTitle(repoName string, count int, resp *gitlab.Response) string {
195 title := utils.NewListTitle("label")
196 title.RepoName = repoName
197 title.Page = o.page
198 title.CurrentPageTotal = count
199 if resp != nil {
200 title.Total = int(resp.TotalItems)
201 }
202 return title.Describe()
Diagnostics8 git calls · 232 ms
CallStatusSizeTime0232 ms
discover200185 B2 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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
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=5
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 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=5
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200283 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=283
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=6452924dd420a333df8d553dd2f7746e42cef3dd
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=283
git.pkt.payload.max=256
git.sideband.band1.bytes=256
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=256
git.pack.trailer=ccc783374d764244fbd131e1e835e875879f31cf
Open pack map
fetchtree:0200140 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=216
git.response.bytes=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a567eaeca7927e635db75d869867fc66591cf442
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=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=4d9b848d2075d5422e1c74048f9c602a9372c470
Open pack map
fetch2001.8 KiB216 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1867
git.response.cache=MISS
git.response.duration_ms=216
git.fetch.wants=0d33413110c480a59d1e15fa786aaebc8c6e42a7
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=1867
git.pkt.payload.max=1840
git.sideband.band1.bytes=1840
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
a5e2e5879da12947
Request
71f8a8da…
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