GitLabgitlab-org
cli
main
1package api
3import (
4 "context"
5 "errors"
6 "fmt"
7 "iter"
8 "time"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11)
13// ListOptions selects the timelogs to list. Set exactly one of Group and
14// Project. Zero dates and an empty Username leave that filter off.
15type ListOptions struct {
16 Group string
17 Project string
18 StartDate time.Time
19 EndDate time.Time
20 Username string
21}
23// Timelog is one entry of time logged on an issue or a merge request.
24// TimeSpent is in seconds, and is negative for time that was subtracted.
25type Timelog struct {
26 SpentAt time.Time `json:"spentAt"`
27 TimeSpent int64 `json:"timeSpent"`
28 Summary string `json:"summary"`
29 User User `json:"user"`
30 Project Project `json:"project"`
31 Issue *Issuable `json:"issue"`
32 MergeRequest *Issuable `json:"mergeRequest"`
33}
35type User struct {
36 Username string `json:"username"`
37}
39type Project struct {
40 FullPath string `json:"fullPath"`
41}
43// Issuable is the issue or merge request a timelog was logged on.
44type Issuable struct {
45 IID string `json:"iid"`
46 Title string `json:"title"`
47 WebURL string `json:"webUrl"`
48}
50type PageInfo struct {
51 EndCursor string `json:"endCursor"`
52 HasNextPage bool `json:"hasNextPage"`
53}
55type TimelogConnection struct {
56 Nodes []Timelog `json:"nodes"`
57 PageInfo PageInfo `json:"pageInfo"`
58}
60type Namespace struct {
61 Timelogs *TimelogConnection `json:"timelogs"`
62}
64// TimelogsResponse is the GraphQL response for one page of timelogs. Argument
65// errors arrive in Errors with an HTTP 200, so GraphQL.Do does not report them.
66type TimelogsResponse struct {
67 Data struct {
68 Group *Namespace `json:"group"`
69 Project *Namespace `json:"project"`
70 } `json:"data"`
71 Errors []struct {
72 Message string `json:"message"`
73 } `json:"errors"`
74}
76const timelogsQuery = `
77query ListTimelogs($fullPath: ID!, $startDate: Time, $endDate: Time, $username: String, $after: String) {
78 %s(fullPath: $fullPath) {
79 timelogs(startDate: $startDate, endDate: $endDate, username: $username, sort: SPENT_AT_DESC, first: 100, after: $after) {
80 nodes {
81 spentAt
82 timeSpent
83 summary
84 user {
85 username
86 }
87 project {
88 fullPath
89 }
90 issue {
91 iid
92 title
93 webUrl
94 }
95 mergeRequest {
96 iid
97 title
98 webUrl
99 }
100 }
101 pageInfo {
102 endCursor
103 hasNextPage
104 }
105 }
106 }
107}
108`
110// ListTimelogs yields every timelog matching opts, newest first, fetching
111// further pages as the caller ranges over it.
112//
113// GitLab drops timelogs the user cannot see from a page, but not from the
114// page count, so a page can be empty and still have a next page.
115func ListTimelogs(ctx context.Context, client *gitlab.Client, opts ListOptions) iter.Seq2[Timelog, error] {
116 return func(yield func(Timelog, error) bool) {
117 after := ""
118 for {
119 conn, err := listTimelogsPage(ctx, client, opts, after)
120 if err != nil {
121 yield(Timelog{}, err)
122 return
123 }
124 for _, timelog := range conn.Nodes {
125 if !yield(timelog, nil) {
126 return
127 }
128 }
129 if !conn.PageInfo.HasNextPage || conn.PageInfo.EndCursor == "" {
130 return
131 }
132 after = conn.PageInfo.EndCursor
133 }
134 }
135}
137func listTimelogsPage(ctx context.Context, client *gitlab.Client, opts ListOptions, after string) (*TimelogConnection, error) {
138 field, fullPath := "project", opts.Project
139 if opts.Group != "" {
140 field, fullPath = "group", opts.Group
141 }
143 variables := map[string]any{"fullPath": fullPath}
144 if !opts.StartDate.IsZero() {
145 variables["startDate"] = opts.StartDate.Format(time.DateOnly)
146 }
147 if !opts.EndDate.IsZero() {
148 variables["endDate"] = opts.EndDate.Format(time.DateOnly)
149 }
150 if opts.Username != "" {
151 variables["username"] = opts.Username
152 }
153 if after != "" {
154 variables["after"] = after
155 }
157 query := gitlab.GraphQLQuery{
158 Query: fmt.Sprintf(timelogsQuery, field),
159 Variables: variables,
160 }
162 var response TimelogsResponse
163 if _, err := client.GraphQL.Do(query, &response, gitlab.WithContext(ctx)); err != nil {
164 return nil, fmt.Errorf("failed to list timelogs: %w", err)
165 }
166 if len(response.Errors) > 0 {
167 errs := make([]error, 0, len(response.Errors))
168 for _, e := range response.Errors {
169 errs = append(errs, errors.New(e.Message))
170 }
171 return nil, fmt.Errorf("failed to list timelogs: %w", errors.Join(errs...))
172 }
174 namespace := response.Data.Project
175 if opts.Group != "" {
176 namespace = response.Data.Group
177 }
178 if namespace == nil || namespace.Timelogs == nil {
179 return nil, fmt.Errorf("%s not found: %s", field, fullPath)
180 }
181 return namespace.Timelogs, nil
182}
Diagnostics8 git calls · 227 ms
CallStatusSizeTime0227 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=2 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 · 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
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:0200166 B · hit4 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=4 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=1e354378f0db79627a255e16548c215c78a4f110Open pack map
fetchtree:0200104 B · 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=104 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=3e5123833a2b35e978eaf973a685496347f44cbe 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=104 git.pkt.payload.max=77 git.sideband.band1.bytes=77 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=77 git.pack.trailer=38ac35bac5e9fecceb7f9a107f6baeb4c003fab1Open pack map
fetch2001.7 KiB213 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=213 git.response.bytes=1776 git.response.cache=MISS git.response.duration_ms=213 git.fetch.wants=4e067e46b7b170fc9c36002ee49b41e954de7c15 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=1776 git.pkt.payload.max=1749 git.sideband.band1.bytes=1749 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
- a5e23b2cce1e4bc2
- Request
- 84ff41a9…
- 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