GitLabgitlab-org

cli

main
ply8 git calls316 ms99 KiB5 cachedprotocol v2
207 lines5.9 KiBRaw
1package list
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strings"
8 "time"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 timelogapi "gitlab.com/gitlab-org/cli/internal/commands/timelog/api"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/tableprinter"
21 "gitlab.com/gitlab-org/cli/internal/utils"
24type options struct {
25 io *iostreams.IOStreams
26 baseRepo func() (glrepo.Interface, error)
27 gitlabClient func() (*gitlab.Client, error)
29 group string
30 username string
31 startDate time.Time
32 endDate time.Time
33 outputFormat string
36func NewCmd(f cmdutils.Factory) *cobra.Command {
37 opts := &options{
38 io: f.IO(),
39 baseRepo: f.BaseRepo,
40 gitlabClient: f.GitLabClient,
41 }
43 cmd := &cobra.Command{
44 Use: "list [flags]",
45 Short: "List time logged on issues and merge requests.",
46 Long: heredoc.Docf(`
47 By default, reports on the current project. Use %[1]s--group%[1]s to report on a group and its subgroups, or %[1]s--repo%[1]s to report on another project. Entries are listed newest first, followed by the total time spent.
49 Dates appear in UTC. Durations use time tracking units: 1 day is 8 hours, 1 week is 5 days, and 1 month is 4 weeks. Subtracted time appears as a negative value and reduces the total.
51 Omits time logged on issues and merge requests you cannot view.
52 `, "`"),
53 Aliases: []string{"ls"},
54 Example: heredoc.Doc(`
55 # List all time logged in the current project
56 glab timelog list
58 # List the time you logged in a group and its subgroups since the start of the month
59 glab timelog list --group gitlab-org --user @me --start-date 2026-10-01
61 # List the time logged in another project on a single day
62 glab timelog list -R gitlab-org/cli --start-date 2026-10-14 --end-date 2026-10-14
64 # Total the seconds logged per user
65 glab timelog list --output json --jq 'group_by(.user.username) | map({user: .[0].user.username, seconds: map(.timeSpent) | add})'`),
66 Args: cobra.NoArgs,
67 ValidArgsFunction: cobra.NoFileCompletions,
68 Annotations: map[string]string{
69 mcpannotations.Safe: "true",
70 },
71 RunE: func(cmd *cobra.Command, args []string) error {
72 if err := opts.complete(cmd); err != nil {
73 return err
74 }
75 if err := opts.validate(); err != nil {
76 return err
77 }
78 return opts.run(cmd.Context())
79 },
80 }
82 cmdutils.EnableRepoOverride(cmd, f)
83 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
85 fl := cmd.Flags()
86 fl.StringVarP(&opts.group, "group", "g", "", "List time logged in a group and its subgroups.")
87 fl.StringVar(&opts.username, "user", "", "List only the time logged by this `username`. Use @me for yourself.")
88 fl.TimeVar(&opts.startDate, "start-date", time.Time{}, []string{time.DateOnly}, "List time logged on or after this date, in `YYYY-MM-DD` format.")
89 fl.TimeVar(&opts.endDate, "end-date", time.Time{}, []string{time.DateOnly}, "List time logged on or before this date, in `YYYY-MM-DD` format.")
91 cmd.MarkFlagsMutuallyExclusive("group", "repo")
93 return cmd
96func (o *options) complete(cmd *cobra.Command) error {
97 group, err := cmdutils.GroupOverride(cmd)
98 if err != nil {
99 return err
100 }
101 o.group = group
102 return nil
105func (o *options) validate() error {
106 if !o.startDate.IsZero() && !o.endDate.IsZero() && o.endDate.Before(o.startDate) {
107 return cmdutils.FlagError{Err: errors.New("invalid date range: --end-date is before --start-date")}
108 }
109 return nil
112func (o *options) run(ctx context.Context) error {
113 listOpts := timelogapi.ListOptions{
114 Group: o.group,
115 StartDate: o.startDate,
116 EndDate: o.endDate,
117 Username: strings.TrimPrefix(o.username, "@"),
118 }
119 if o.group == "" {
120 repo, err := o.baseRepo()
121 if err != nil {
122 return err
123 }
124 listOpts.Project = repo.FullName()
125 }
127 client, err := o.gitlabClient()
128 if err != nil {
129 return err
130 }
132 if o.username == "@me" {
133 user, _, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
134 if err != nil {
135 return fmt.Errorf("failed to get the current user: %w", err)
136 }
137 listOpts.Username = user.Username
138 }
140 var timelogs []timelogapi.Timelog
141 for timelog, err := range timelogapi.ListTimelogs(ctx, client, listOpts) {
142 if err != nil {
143 return err
144 }
145 timelogs = append(timelogs, timelog)
146 }
148 if o.outputFormat == "json" {
149 return o.io.PrintJSON(timelogs)
150 }
152 scope := listOpts.Project
153 if o.group != "" {
154 scope = o.group
155 }
156 if len(timelogs) == 0 {
157 o.io.LogInfof("No time logged in %s.\n", scope)
158 return nil
159 }
161 var total int64
162 table := tableprinter.NewTablePrinter()
163 table.SetIsTTY(o.io.IsOutputTTY())
164 table.AddRow("DATE", "USER", "TIME", "ITEM", "TITLE", "SUMMARY")
165 for _, timelog := range timelogs {
166 total += timelog.TimeSpent
167 ref, item := reference(timelog, o.group != "")
168 title, webURL := "", ""
169 if item != nil {
170 title, webURL = item.Title, item.WebURL
171 }
172 summary := timelog.Summary
173 if summary == "" {
174 summary = "-"
175 }
176 table.AddRow(
177 timelog.SpentAt.UTC().Format(time.DateOnly),
178 timelog.User.Username,
179 utils.FormatTimeSpent(timelog.TimeSpent),
180 o.io.Hyperlink(ref, webURL),
181 title,
182 summary,
183 )
184 }
186 o.io.LogInfo(table.Render())
187 o.io.LogInfof("Total: %s (%s)\n", utils.FormatTimeSpent(total), utils.Pluralize(len(timelogs), "time log"))
188 return nil
191// reference returns the short reference of the issue or merge request a
192// timelog was logged on. Group reports span projects, so they prefix the
193// project path.
194func reference(timelog timelogapi.Timelog, withProject bool) (string, *timelogapi.Issuable) {
195 prefix := ""
196 if withProject {
197 prefix = timelog.Project.FullPath
198 }
199 switch {
200 case timelog.Issue != nil:
201 return fmt.Sprintf("%s#%s", prefix, timelog.Issue.IID), timelog.Issue
202 case timelog.MergeRequest != nil:
203 return fmt.Sprintf("%s!%s", prefix, timelog.MergeRequest.IID), timelog.MergeRequest
204 default:
205 return "", nil
206 }
Diagnostics8 git calls · 316 ms
CallStatusSizeTime0316 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 KiB2 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=UPDATING
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 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=4
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:0200166 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=166
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=dc4c26b053ce2f4849dff66c00cba032cb3cfda1
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=166
git.pkt.payload.max=139
git.sideband.band1.bytes=139
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=139
git.pack.trailer=1e354378f0db79627a255e16548c215c78a4f110
Open pack map
fetchtree:0200135 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=299
git.response.bytes=135
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=05e890f0025d40f27397e3c42c53d2b815f880e2
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=135
git.pkt.payload.max=108
git.sideband.band1.bytes=108
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=108
git.pack.trailer=b7af15d6bdffabfaf03a420343008bd896eb646f
Open pack map
fetch2002.3 KiB299 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2379
git.response.cache=MISS
git.response.duration_ms=299
git.fetch.wants=fddc47aad69e5f19c11347e3f4057456f33d3cb5
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=2379
git.pkt.payload.max=2352
git.sideband.band1.bytes=2352
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
a5e2e425ef282947
Request
f4130cb5…
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