GitLabgitlab-org

cli

main
ply8 git calls257 ms100 KiB5 cachedprotocol v2
310 lines8.1 KiBRaw
1package api
3import (
4 "context"
5 "fmt"
6 "maps"
7 "regexp"
8 "slices"
9 "strconv"
10 "strings"
11 "time"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 glabapi "gitlab.com/gitlab-org/cli/internal/api"
18// scope type constants
19const (
20 ScopeTypeGroup = "group"
21 ScopeTypeProject = "project"
24// ScopeInfo contains detected scope information for work items queries
25type ScopeInfo struct {
26 Type string
27 Path string
30// WorkItem represents a work item from the GraphQL API
31type WorkItem struct {
32 IID string `json:"iid"`
33 Title string `json:"title"`
34 State string `json:"state"`
35 WorkItemType struct {
36 Name string `json:"name"`
37 } `json:"workItemType"`
38 Author struct {
39 Username string `json:"username"`
40 } `json:"author"`
41 WebURL string `json:"webUrl"`
44// DateField is a work item date filter argument, named as in the GraphQL API.
45type DateField string
47const (
48 CreatedAfter DateField = "createdAfter"
49 CreatedBefore DateField = "createdBefore"
50 UpdatedAfter DateField = "updatedAfter"
51 UpdatedBefore DateField = "updatedBefore"
52 ClosedAfter DateField = "closedAfter"
53 ClosedBefore DateField = "closedBefore"
54 DueAfter DateField = "dueAfter"
55 DueBefore DateField = "dueBefore"
58// RelativeDateUnit is a WorkItemRelativeDateUnit enum value.
59type RelativeDateUnit string
61const (
62 RelativeDateUnitDay RelativeDateUnit = "DAY"
63 RelativeDateUnitWeek RelativeDateUnit = "WEEK"
64 RelativeDateUnitMonth RelativeDateUnit = "MONTH"
65 RelativeDateUnitYear RelativeDateUnit = "YEAR"
68// RelativeDate is an offset from today. Negative amounts are in the past.
69type RelativeDate struct {
70 Amount int `json:"amount"`
71 Unit RelativeDateUnit `json:"unit"`
74// DateFilter holds exactly one of an absolute or a relative date. Absolute is
75// sent as typed: the Time scalar parses it with Ruby's Time.parse, so a date
76// without an offset is read in the system time zone of the server.
77type DateFilter struct {
78 Absolute string
79 Relative *RelativeDate
82const maxRelativeDateAmount = 9999
84var (
85 relativeDateRe = regexp.MustCompile(`^([+-]?\d+)([dwmy])$`)
86 relativeDateUnits = map[string]RelativeDateUnit{
87 "d": RelativeDateUnitDay,
88 "w": RelativeDateUnitWeek,
89 "m": RelativeDateUnitMonth,
90 "y": RelativeDateUnitYear,
91 }
94// ParseDateFilter parses an absolute date (2006-01-02 or RFC 3339) or a
95// relative date such as -7d, 2w, -1m, or 1y.
96func ParseDateFilter(s string) (DateFilter, error) {
97 if m := relativeDateRe.FindStringSubmatch(s); m != nil {
98 amount, err := strconv.Atoi(m[1])
99 if err != nil || amount < -maxRelativeDateAmount || amount > maxRelativeDateAmount {
100 return DateFilter{}, fmt.Errorf("relative date amount must be between -%[1]d and %[1]d", maxRelativeDateAmount)
101 }
102 return DateFilter{Relative: &RelativeDate{Amount: amount, Unit: relativeDateUnits[m[2]]}}, nil
103 }
105 for _, layout := range []string{time.DateOnly, time.RFC3339} {
106 if _, err := time.Parse(layout, s); err == nil {
107 return DateFilter{Absolute: s}, nil
108 }
109 }
111 return DateFilter{}, fmt.Errorf("must be a date (YYYY-MM-DD), an RFC 3339 timestamp, or a relative date such as -7d, 2w, -1m, or 1y")
114// ListOptions are the filters and pagination for FetchWorkItems.
115type ListOptions struct {
116 Types []string
117 State string
118 After string
119 PerPage int64
120 Dates map[DateField]DateFilter
123// GraphQL query templates. Date filter arguments are added only when set,
124// so that instances without them still accept the query.
125const (
126 groupWorkItemsQuery = `
127 query ListGroupWorkItems($groupPath: ID!, $types: [IssueType!], $state: IssuableState, $first: Int, $after: String%[1]s) {
128 group(fullPath: $groupPath) {
129 workItems(types: $types, state: $state, first: $first, after: $after%[2]s) {
130 nodes {
131 iid
132 title
133 state
134 workItemType {
135 name
136 }
137 author {
138 username
139 }
140 webUrl
141 }
142 pageInfo {
143 endCursor
144 hasNextPage
145 }
146 }
147 }
151 projectWorkItemsQuery = `
152 query ListProjectWorkItems($projectPath: ID!, $types: [IssueType!], $state: IssuableState, $first: Int, $after: String%[1]s) {
153 project(fullPath: $projectPath) {
154 workItems(types: $types, state: $state, first: $first, after: $after%[2]s) {
155 nodes {
156 iid
157 title
158 state
159 workItemType {
160 name
161 }
162 author {
163 username
164 }
165 webUrl
166 }
167 pageInfo {
168 endCursor
169 hasNextPage
170 }
171 }
172 }
177// FetchWorkItems retrieves all work items using cursor-based pagination
178func FetchWorkItems(ctx context.Context, client *gitlab.Client, scope *ScopeInfo, opts ListOptions) ([]WorkItem, *PageInfo, error) {
179 var queryTemplate string
180 var pathKey string
182 switch scope.Type {
183 case ScopeTypeGroup:
184 queryTemplate = groupWorkItemsQuery
185 pathKey = "groupPath"
186 case ScopeTypeProject:
187 queryTemplate = projectWorkItemsQuery
188 pathKey = "projectPath"
189 default:
190 return nil, nil, fmt.Errorf("invalid scope type: %s", scope.Type)
191 }
193 // uppercase types for GraphQL API (API exepcts EPIC not epic)
194 var uppercaseTypes []string
195 if len(opts.Types) > 0 {
196 uppercaseTypes = make([]string, len(opts.Types))
197 for i, t := range opts.Types {
198 uppercaseTypes[i] = strings.ToUpper(strings.TrimSpace(t))
199 }
200 }
202 // Build query vars
203 variables := map[string]any{
204 pathKey: scope.Path,
205 "first": opts.PerPage, // user-specified page size (max 100)
206 }
208 if opts.After != "" {
209 variables["after"] = opts.After
210 }
212 // add types filter if specified
213 if len(uppercaseTypes) > 0 {
214 variables["types"] = uppercaseTypes
215 }
217 // add state filter
218 if opts.State != "all" {
219 variables["state"] = opts.State
220 }
222 decls, args := addDateFilterVariables(variables, opts.Dates)
224 query := gitlab.GraphQLQuery{
225 Query: fmt.Sprintf(queryTemplate, decls, args),
226 Variables: variables,
227 }
229 var response WorkItemsResponse
231 // execute query
232 _, err := client.GraphQL.Do(query, &response, gitlab.WithContext(ctx), glabapi.WithSafeRetries())
233 if err != nil {
234 return nil, nil, fmt.Errorf("GraphQL query failed: %w", err)
235 }
237 // Extract work items based on scope
238 var nodes []WorkItem
239 var pageInfo PageInfo
241 if scope.Type == ScopeTypeGroup {
242 if response.Data.Group == nil {
243 return nil, nil, fmt.Errorf("group not found: %s", scope.Path)
244 }
245 nodes = response.Data.Group.WorkItems.Nodes
246 pageInfo = response.Data.Group.WorkItems.PageInfo
247 } else {
248 if response.Data.Project == nil {
249 return nil, nil, fmt.Errorf("project not found: %s", scope.Path)
250 }
251 nodes = response.Data.Project.WorkItems.Nodes
252 pageInfo = response.Data.Project.WorkItems.PageInfo
253 }
255 return nodes, &pageInfo, nil
258// addDateFilterVariables adds the date filters to variables and returns the
259// matching variable declarations and field arguments for the query.
260func addDateFilterVariables(variables map[string]any, dates map[DateField]DateFilter) (string, string) {
261 var decls, args strings.Builder
262 relative := map[DateField]RelativeDate{}
264 for _, field := range slices.Sorted(maps.Keys(dates)) {
265 filter := dates[field]
266 switch {
267 case filter.Relative != nil:
268 relative[field] = *filter.Relative
269 case filter.Absolute != "":
270 variables[string(field)] = filter.Absolute
271 fmt.Fprintf(&decls, ", $%s: Time", field) //nolint:forbidigo // writing to strings.Builder, not stdout/stderr
272 fmt.Fprintf(&args, ", %[1]s: $%[1]s", field) //nolint:forbidigo // writing to strings.Builder, not stdout/stderr
273 }
274 }
276 if len(relative) > 0 {
277 variables["relativeDateFilters"] = relative
278 decls.WriteString(", $relativeDateFilters: WorkItemRelativeDateFiltersInput")
279 args.WriteString(", relativeDateFilters: $relativeDateFilters")
280 }
282 return decls.String(), args.String()
285// WorkItemsResponse represents the GraphQL response structure for work items queries
286type WorkItemsResponse struct {
287 Data struct {
288 Group *GroupWorkItems `json:"group,omitempty"`
289 Project *ProjectWorkItems `json:"project,omitempty"`
290 } `json:"data"`
293// helper structs for GraphQL response parsing
294type GroupWorkItems struct {
295 WorkItems WorkItemsConnection `json:"workItems"`
298type ProjectWorkItems struct {
299 WorkItems WorkItemsConnection `json:"workItems"`
302type WorkItemsConnection struct {
303 Nodes []WorkItem `json:"nodes"`
304 PageInfo PageInfo `json:"pageInfo"`
307type PageInfo struct {
308 EndCursor string `json:"endCursor"`
309 HasNextPage bool `json:"hasNextPage"`
Diagnostics8 git calls · 257 ms
CallStatusSizeTime0257 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 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 · 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=44
Open 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 · 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=1857
git.response.cache=HIT
git.response.duration_ms=4
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:0200317 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=317
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=33cd62a3f1bc55e6bd56a9b96f181ff12c30955a
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=317
git.pkt.payload.max=290
git.sideband.band1.bytes=290
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=290
git.pack.trailer=5711d3cc007ef3bd902f87854e977c46d0913d04
Open pack map
fetchtree:0200135 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=135
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=32e97cf7502abfc8881abdb7d7f4a44e6e5831a4
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=9a9d7043aeb6851dad13d395133168d12d6a624b
Open pack map
fetch2002.8 KiB242 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=242
git.response.bytes=2896
git.response.cache=MISS
git.response.duration_ms=242
git.fetch.wants=5e703535e6b7f5fc5e1e175782dfc62c320ab7df
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=2896
git.pkt.payload.max=2869
git.sideband.band1.bytes=2869
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
a5e20ede683e7f3b
Request
78533566…
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