GitLabgitlab-org
cli
main
1package list
3import (
4 "context"
5 "fmt"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
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"
18)
20var (
21 validStates = map[string]bool{"pending": true, "done": true, "all": true}
22 validActions = map[string]bool{
23 "assigned": true, "mentioned": true, "build_failed": true,
24 "marked": true, "approval_required": true, "directly_addressed": true,
25 }
26)
28type options struct {
29 apiClient func(repoHost string) (*api.Client, error)
30 io *iostreams.IOStreams
32 state string
33 action string
34 typ string
35 page int
36 perPage int
38 outputFormat string
39}
41func NewCmd(f cmdutils.Factory) *cobra.Command {
42 opts := &options{
43 io: f.IO(),
44 apiClient: f.ApiClient,
45 }
47 cmd := &cobra.Command{
48 Use: "list [flags]",
49 Short: "List your to-do items.",
50 Long: heredoc.Docf(`
51 By default, lists your pending to-do items. Use %[1]s--state%[1]s to
52 show done or all items instead.
54 Narrow the results with %[1]s--action%[1]s to filter by why an item was
55 created, or %[1]s--type%[1]s to filter by its target type.
56 `, "`"),
57 Aliases: []string{"ls"},
58 Example: heredoc.Doc(`
59 glab todo list
60 glab todo list --state=done
61 glab todo list --action=assigned
62 glab todo list --type=MergeRequest
63 glab todo list --output=json
64 `),
65 Args: cobra.NoArgs,
66 ValidArgsFunction: cobra.NoFileCompletions,
67 Annotations: map[string]string{
68 mcpannotations.Safe: "true",
69 },
70 RunE: func(cmd *cobra.Command, args []string) error {
71 if err := opts.validate(); err != nil {
72 return err
73 }
74 return opts.run(cmd.Context())
75 },
76 }
78 fl := cmd.Flags()
79 fl.StringVarP(&opts.state, "state", "s", "pending", "Filter by state: pending, done, all.")
80 fl.StringVarP(&opts.action, "action", "a", "", "Filter by action: assigned, mentioned, build_failed, marked, approval_required, directly_addressed.")
81 fl.StringVarP(&opts.typ, "type", "t", "", "Filter by target type: Issue, MergeRequest.")
82 fl.IntVarP(&opts.page, "page", "p", 1, "Page number.")
83 fl.IntVarP(&opts.perPage, "per-page", "P", 30, "Number of items to list per page.")
84 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
86 return cmd
87}
89func (o *options) validate() error {
90 if !validStates[o.state] {
91 return cmdutils.FlagError{
92 Err: fmt.Errorf("invalid --state %q: must be one of: pending, done, all", o.state),
93 }
94 }
95 if o.action != "" && !validActions[o.action] {
96 return cmdutils.FlagError{
97 Err: fmt.Errorf("invalid --action %q: must be one of: assigned, mentioned, build_failed, marked, approval_required, directly_addressed", o.action),
98 }
99 }
100 if o.page < 1 {
101 return cmdutils.FlagError{
102 Err: fmt.Errorf("--page must be >= 1"),
103 }
104 }
105 if o.perPage < 1 || o.perPage > 100 {
106 return cmdutils.FlagError{
107 Err: fmt.Errorf("--per-page must be between 1 and 100"),
108 }
109 }
110 return nil
111}
113func (o *options) run(ctx context.Context) error {
114 c, err := o.apiClient("")
115 if err != nil {
116 return err
117 }
118 client := c.Lab()
120 listOpts := &gitlab.ListTodosOptions{
121 ListOptions: gitlab.ListOptions{
122 Page: int64(o.page),
123 PerPage: int64(o.perPage),
124 },
125 }
127 if o.state != "all" {
128 listOpts.State = &o.state
129 }
130 if o.action != "" {
131 action := gitlab.TodoAction(o.action)
132 listOpts.Action = &action
133 }
134 if o.typ != "" {
135 listOpts.Type = &o.typ
136 }
138 todos, _, err := client.Todos.ListTodos(listOpts, gitlab.WithContext(ctx))
139 if err != nil {
140 return cmdutils.WrapError(err, "failed to list to-do items.")
141 }
143 if o.outputFormat == "json" {
144 return o.io.PrintJSON(todos)
145 }
147 cs := o.io.Color()
148 table := tableprinter.NewTablePrinter()
149 isTTY := o.io.IsOutputTTY()
151 if len(todos) > 0 {
152 table.AddRow("ID", "Action", "Type", "Title", "Project", "Created")
153 }
155 for _, todo := range todos {
156 createdAt := ""
157 if todo.CreatedAt != nil {
158 if isTTY {
159 createdAt = utils.TimeToPrettyTimeAgo(*todo.CreatedAt)
160 } else {
161 createdAt = todo.CreatedAt.String()
162 }
163 }
165 projectPath := ""
166 switch {
167 case todo.Project != nil:
168 projectPath = todo.Project.PathWithNamespace
169 case todo.Group != nil:
170 projectPath = todo.Group.FullPath
171 }
173 title := ""
174 if todo.Target != nil {
175 title = todo.Target.Title
176 }
178 table.AddRow(
179 todo.ID,
180 string(todo.ActionName),
181 string(todo.TargetType),
182 title,
183 projectPath,
184 cs.Gray(createdAt),
185 )
186 }
188 o.io.LogInfo(table.String())
190 return nil
191}
Diagnostics8 git calls · 229 ms
CallStatusSizeTime0229 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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=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:0200165 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=165 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=c6bf531ba0613331e49d2faa099cd3466613e7b8 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=165 git.pkt.payload.max=138 git.sideband.band1.bytes=138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=138 git.pack.trailer=a34ae15f8bc51d95314a64e29216fc88a5c857b0Open pack map
fetchtree:0200133 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=133 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=e13e74e1951904177bfc6400e39c755ab95bb9fb 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=133 git.pkt.payload.max=106 git.sideband.band1.bytes=106 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=106 git.pack.trailer=4a81e579a84fe2ad1ae3eba3e9b979d90df7fa59Open pack map
fetch2001.8 KiB215 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=215 git.response.bytes=1867 git.response.cache=MISS git.response.duration_ms=215 git.fetch.wants=36fd5f86ae61663d07346d56494cef01726809e7 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
- a5e2e03a7b972308
- Request
- 77448b7e…
- 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