GitLabgitlab-org

cli

main
ply6 git calls29 ms98 KiB4 cachedprotocol v2
253 lines7.8 KiBRaw
1package api
3import (
4 "sort"
6 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
7)
9// GetMR returns an MR
10// Attention: this is a global variable and may be overridden in tests.
11var GetMR = func(client *gitlab.Client, projectID any, mrID int64, opts *gitlab.GetMergeRequestsOptions) (*gitlab.MergeRequest, error) {
12 mr, _, err := client.MergeRequests.GetMergeRequest(projectID, mrID, opts)
13 if err != nil {
14 return nil, err
15 }
17 return mr, nil
20// ListGroupMRs retrieves merge requests for a given group with optional filtering by assignees or reviewers.
21//
22// Parameters:
23// - client: A GitLab client instance.
24// - groupID: The ID or name of the group.
25// - opts: GitLab-specific options for listing group merge requests.
26// - listOpts: Optional list of arguments to filter by assignees or reviewers.
27// May be any combination of api.WithMRAssignees and api.WithMRReviewers.
28//
29// Returns:
30// - A slice of GitLab merge request objects and an error, if any.
31//
32// Example usage:
33//
34// groupMRs, err := api.ListGroupMRs(client, "my-group", &gitlab.ListGroupMergeRequestsOptions{},
35// api.WithMRAssignees([]int{123}),
36// api.WithMRReviewers([]int{456, 789}))
37func ListGroupMRs(client *gitlab.Client, projectID any, opts *gitlab.ListGroupMergeRequestsOptions, listOpts ...CliListMROption) ([]*gitlab.BasicMergeRequest, error) {
38 composedListOpts := composeCliListMROptions(listOpts...)
39 assigneeIds, reviewerIds := composedListOpts.assigneeIds, composedListOpts.reviewerIds
41 if len(assigneeIds) == 1 && len(reviewerIds) == 0 {
42 opts.AssigneeID = gitlab.AssigneeID(assigneeIds[0])
43 return listGroupMRsBase(client, projectID, opts)
44 }
45 if len(reviewerIds) == 1 && len(assigneeIds) == 0 {
46 opts.ReviewerID = gitlab.ReviewerID(reviewerIds[0])
47 return listGroupMRsBase(client, projectID, opts)
48 }
50 if len(assigneeIds) > 0 || len(reviewerIds) > 0 {
51 return listGroupMRsWithAssigneesOrReviewers(client, projectID, opts, assigneeIds, reviewerIds)
52 } else {
53 return listGroupMRsBase(client, projectID, opts)
54 }
57func listGroupMRsBase(client *gitlab.Client, groupID any, opts *gitlab.ListGroupMergeRequestsOptions) ([]*gitlab.BasicMergeRequest, error) {
58 if opts.PerPage == 0 {
59 opts.PerPage = DefaultListLimit
60 }
62 mrs, _, err := client.MergeRequests.ListGroupMergeRequests(groupID, opts)
63 if err != nil {
64 return nil, err
65 }
66 return mrs, nil
69func listGroupMRsWithAssigneesOrReviewers(client *gitlab.Client, projectID any, opts *gitlab.ListGroupMergeRequestsOptions, assigneeIds []int, reviewerIds []int) ([]*gitlab.BasicMergeRequest, error) {
70 if opts.PerPage == 0 {
71 opts.PerPage = DefaultListLimit
72 }
74 mrMap := make(map[int64]*gitlab.BasicMergeRequest)
75 for _, id := range assigneeIds {
76 opts.AssigneeID = gitlab.AssigneeID(id)
77 assigneeMrs, err := listGroupMRsBase(client, projectID, opts)
78 if err != nil {
79 return nil, err
80 }
81 for _, mr := range assigneeMrs {
82 mrMap[mr.ID] = mr
83 }
84 }
85 opts.AssigneeID = nil // reset because it's Assignee OR Reviewer
86 for _, id := range reviewerIds {
87 opts.ReviewerID = gitlab.ReviewerID(id)
88 reviewerMrs, err := listGroupMRsBase(client, projectID, opts)
89 if err != nil {
90 return nil, err
91 }
92 for _, mr := range reviewerMrs {
93 mrMap[mr.ID] = mr
94 }
95 }
97 mrs := make([]*gitlab.BasicMergeRequest, 0, len(mrMap))
98 for _, mr := range mrMap {
99 mrs = append(mrs, mr)
100 }
102 // Sort by CreatedAt if no custom sort is specified, otherwise let API sorting take precedence
103 if opts.OrderBy == nil {
104 sort.Slice(mrs, func(i, j int) bool {
105 return mrs[i].CreatedAt.After(*mrs[j].CreatedAt)
106 })
107 }
109 return mrs, nil
112// ListMRs retrieves merge requests for a given project with optional filtering by assignees or reviewers.
113//
114// Parameters:
115// - client: A GitLab client instance.
116// - projectID: The ID or name of the project.
117// - opts: GitLab-specific options for listing merge requests.
118// - listOpts: Optional list of arguments to filter by assignees or reviewers.
119// May be any combination of api.WithMRAssignees and api.WithMRReviewers.
120//
121// Returns:
122// - A slice of GitLab merge request objects and an error, if any.
123//
124// Example usage:
125//
126// mrs, err := api.ListMRs(client, "my-group", &gitlab.ListProjectMergeRequestsOptions{},
127// api.WithMRAssignees([]int{123, 456}),
128// api.WithMRReviewers([]int{789}))
129//
130// Attention: this is a global variable and may be overridden in tests.
131var ListMRs = func(client *gitlab.Client, projectID any, opts *gitlab.ListProjectMergeRequestsOptions, listOpts ...CliListMROption) ([]*gitlab.BasicMergeRequest, error) {
132 composedListOpts := composeCliListMROptions(listOpts...)
133 assigneeIds, reviewerIds := composedListOpts.assigneeIds, composedListOpts.reviewerIds
135 if len(assigneeIds) == 1 && len(reviewerIds) == 0 {
136 opts.AssigneeID = gitlab.AssigneeID(assigneeIds[0])
137 return listMRsBase(client, projectID, opts)
138 }
139 if len(reviewerIds) == 1 && len(assigneeIds) == 0 {
140 opts.ReviewerID = gitlab.ReviewerID(reviewerIds[0])
141 return listMRsBase(client, projectID, opts)
142 }
144 if len(assigneeIds) > 0 || len(reviewerIds) > 0 {
145 return listMRsWithAssigneesOrReviewers(client, projectID, opts, assigneeIds, reviewerIds)
146 } else {
147 return listMRsBase(client, projectID, opts)
148 }
151func listMRsBase(client *gitlab.Client, projectID any, opts *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.BasicMergeRequest, error) {
152 if opts.PerPage == 0 {
153 opts.PerPage = DefaultListLimit
154 }
156 mrs, _, err := client.MergeRequests.ListProjectMergeRequests(projectID, opts)
157 if err != nil {
158 return nil, err
159 }
160 return mrs, nil
163func listMRsWithAssigneesOrReviewers(client *gitlab.Client, projectID any, opts *gitlab.ListProjectMergeRequestsOptions, assigneeIds []int, reviewerIds []int) ([]*gitlab.BasicMergeRequest, error) {
164 if opts.PerPage == 0 {
165 opts.PerPage = DefaultListLimit
166 }
168 mrMap := make(map[int64]*gitlab.BasicMergeRequest)
169 for _, id := range assigneeIds {
170 opts.AssigneeID = gitlab.AssigneeID(id)
171 assigneeMrs, err := listMRsBase(client, projectID, opts)
172 if err != nil {
173 return nil, err
174 }
175 for _, mr := range assigneeMrs {
176 mrMap[mr.ID] = mr
177 }
178 }
179 opts.AssigneeID = nil // reset because it's Assignee OR Reviewer
180 for _, id := range reviewerIds {
181 opts.ReviewerID = gitlab.ReviewerID(id)
182 reviewerMrs, err := listMRsBase(client, projectID, opts)
183 if err != nil {
184 return nil, err
185 }
186 for _, mr := range reviewerMrs {
187 mrMap[mr.ID] = mr
188 }
189 }
191 mrs := make([]*gitlab.BasicMergeRequest, 0, len(mrMap))
192 for _, mr := range mrMap {
193 mrs = append(mrs, mr)
194 }
196 // Sort by CreatedAt if no custom sort is specified, otherwise let API sorting take precedence
197 if opts.OrderBy == nil {
198 sort.Slice(mrs, func(i, j int) bool {
199 return mrs[i].CreatedAt.After(*mrs[j].CreatedAt)
200 })
201 }
203 return mrs, nil
206// UpdateMR updates an MR
207// Attention: this is a global variable and may be overridden in tests.
208var UpdateMR = func(client *gitlab.Client, projectID any, mrID int64, opts *gitlab.UpdateMergeRequestOptions) (*gitlab.MergeRequest, error) {
209 mr, _, err := client.MergeRequests.UpdateMergeRequest(projectID, mrID, opts)
210 if err != nil {
211 return nil, err
212 }
214 return mr, nil
217// UpdateMR updates an MR
218// Attention: this is a global variable and may be overridden in tests.
219var DeleteMR = func(client *gitlab.Client, projectID any, mrID int64) error {
220 _, err := client.MergeRequests.DeleteMergeRequest(projectID, mrID)
221 if err != nil {
222 return err
223 }
225 return nil
228type cliListMROptions struct {
229 assigneeIds []int
230 reviewerIds []int
233type CliListMROption func(*cliListMROptions)
235func WithMRAssignees(assigneeIds []int) CliListMROption {
236 return func(c *cliListMROptions) {
237 c.assigneeIds = assigneeIds
238 }
241func WithMRReviewers(reviewerIds []int) CliListMROption {
242 return func(c *cliListMROptions) {
243 c.reviewerIds = reviewerIds
244 }
247func composeCliListMROptions(optionSetters ...CliListMROption) *cliListMROptions {
248 opts := &cliListMROptions{}
249 for _, setter := range optionSetters {
250 setter(opts)
251 }
252 return opts
Diagnostics6 git calls · 29 ms
CallStatusSizeTime029 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=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
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 · hit5 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=5
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
fetchtree:02001.1 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=21
git.response.bytes=1165
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb
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=1165
git.pkt.payload.max=1138
git.sideband.band1.bytes=1138
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1138
git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141c
Open pack map
fetch2001.5 KiB · hit21 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1549
git.response.cache=HIT
git.response.duration_ms=21
git.fetch.wants=5532c3555cf3fb3b1c4e2f8bd261524c45fd9052
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=1549
git.pkt.payload.max=1522
git.sideband.band1.bytes=1522
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
a5e1c5640d78227c
Request
42a8f3da…
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