GitLabgitlab-org

cli

main
ply8 git calls364 ms100 KiB5 cachedprotocol v2
421 lines12 KiBRaw
1//go:build !integration
3package update
5import (
6 "os"
7 "path/filepath"
8 "testing"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/glinstance"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22func TestWorkItemsUpdate(t *testing.T) {
23 t.Run("success cases", func(t *testing.T) {
24 tests := []struct {
25 name string
26 args string
27 workItem *gitlab.WorkItem
28 wantOutput string
29 }{
30 {
31 name: "updates work item title in project scope",
32 args: "1 --title \"Test Issue\"",
33 workItem: &gitlab.WorkItem{
34 IID: 1,
35 Title: "Test Issue",
36 WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1",
37 },
38 wantOutput: "https://gitlab.com/OWNER/REPO/-/work_items/1",
39 },
40 {
41 name: "updates work item title in group scope",
42 args: "2 --group my-group --title \"Test Epic\"",
43 workItem: &gitlab.WorkItem{
44 IID: 2,
45 Title: "Test Epic",
46 WebURL: "https://gitlab.com/groups/my-group/-/work_items/2",
47 },
48 wantOutput: "https://gitlab.com/groups/my-group/-/work_items/2",
49 },
50 }
52 for _, tt := range tests {
53 t.Run(tt.name, func(t *testing.T) {
54 tc := gitlabtesting.NewTestClient(t)
55 tc.MockWorkItems.EXPECT().
56 UpdateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
57 Return(tt.workItem, &gitlab.Response{}, nil)
59 exec := cmdtest.SetupCmdForTest(
60 t,
61 NewCmd,
62 false,
63 cmdtest.WithGitLabClient(tc.Client),
64 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
65 )
67 out, err := exec(tt.args)
68 require.NoError(t, err)
69 assert.Contains(t, out.OutBuf.String(), tt.wantOutput)
70 })
71 }
72 })
75func TestWorkItemsUpdate_DescriptionFile(t *testing.T) {
76 const body = "# Heading\n\nMulti-line body with \"quotes\", $VARS and `backticks`.\n"
78 tests := []struct {
79 name string
80 useStdin bool
81 }{
82 {name: "reads the description from a file"},
83 {name: "reads the description from standard input", useStdin: true},
84 }
86 for _, tt := range tests {
87 t.Run(tt.name, func(t *testing.T) {
88 tc := gitlabtesting.NewTestClient(t)
90 var gotDescription *string
91 tc.MockWorkItems.EXPECT().
92 UpdateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
93 DoAndReturn(func(_ string, _ int64, opts *gitlab.UpdateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
94 gotDescription = opts.Description
95 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
96 })
98 opts := []cmdtest.FactoryOption{
99 cmdtest.WithGitLabClient(tc.Client),
100 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
101 }
103 path := "-"
104 if tt.useStdin {
105 opts = append(opts, cmdtest.WithStdin(body))
106 } else {
107 path = filepath.Join(t.TempDir(), "description.md")
108 require.NoError(t, os.WriteFile(path, []byte(body), 0o600))
109 }
111 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, opts...)
113 _, err := exec("1 --description-file " + path)
114 require.NoError(t, err)
116 require.NotNil(t, gotDescription)
117 assert.Equal(t, body, *gotDescription)
118 })
119 }
122func TestWorkItemsUpdate_Iteration(t *testing.T) {
123 tests := []struct {
124 name string
125 args string
126 wantIteration *int64
127 }{
128 {
129 name: "sets the iteration",
130 args: "1 --iteration 123",
131 wantIteration: new(int64(123)),
132 },
133 {
134 name: "leaves the iteration unchanged without the flag",
135 args: "1 --title Test",
136 },
137 }
139 for _, tt := range tests {
140 t.Run(tt.name, func(t *testing.T) {
141 tc := gitlabtesting.NewTestClient(t)
143 var gotIteration *int64
144 tc.MockWorkItems.EXPECT().
145 UpdateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
146 DoAndReturn(func(_ string, _ int64, opts *gitlab.UpdateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
147 gotIteration = opts.IterationID
148 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
149 })
151 exec := cmdtest.SetupCmdForTest(
152 t,
153 NewCmd,
154 false,
155 cmdtest.WithGitLabClient(tc.Client),
156 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
157 )
159 _, err := exec(tt.args)
160 require.NoError(t, err)
161 assert.Equal(t, tt.wantIteration, gotIteration)
162 })
163 }
166func TestWorkItemsUpdate_DescriptionFileConflictsWithDescription(t *testing.T) {
167 exec := cmdtest.SetupCmdForTest(
168 t,
169 NewCmd,
170 false,
171 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
172 )
174 _, err := exec(`1 --description inline --description-file some.md`)
176 require.Error(t, err)
177 assert.Contains(t, err.Error(), "[description description-file]")
180func TestWorkItemsUpdate_FlagValidation(t *testing.T) {
181 tests := []struct {
182 name string
183 args string
184 wantErr string
185 }{
186 {
187 name: "invalid <iid> arg",
188 args: "abc",
189 wantErr: "invalid work item ID",
190 },
191 {
192 name: "non-numeric --iteration",
193 args: "1 --iteration abc",
194 wantErr: "invalid argument \"abc\" for \"--iteration\"",
195 },
196 }
198 for _, tt := range tests {
199 t.Run(tt.name, func(t *testing.T) {
200 exec := cmdtest.SetupCmdForTest(
201 t,
202 NewCmd,
203 false,
204 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
205 )
207 _, err := exec(tt.args)
208 require.Error(t, err)
209 assert.Contains(t, err.Error(), tt.wantErr)
210 })
211 }
214func TestWorkItemsUpdate_Parent(t *testing.T) {
215 tests := []struct {
216 name string
217 args string
218 wantParentPath string
219 wantParentIID int64
220 wantParentID *int64
221 }{
222 {
223 name: "sets the parent from an ID in the current project",
224 args: "1 --parent 42",
225 wantParentPath: "OWNER/REPO",
226 wantParentIID: 42,
227 wantParentID: new(int64(9001)),
228 },
229 {
230 name: "sets the parent from a URL",
231 args: "1 --parent https://gitlab.com/groups/my-group/-/work_items/7",
232 wantParentPath: "my-group",
233 wantParentIID: 7,
234 wantParentID: new(int64(9001)),
235 },
236 {
237 name: "leaves the parent unset without the flag",
238 args: "1 --title Test",
239 },
240 }
242 for _, tt := range tests {
243 t.Run(tt.name, func(t *testing.T) {
244 tc := gitlabtesting.NewTestClient(t, gitlab.WithBaseURL("https://gitlab.com/api/v4"))
246 if tt.wantParentID != nil {
247 tc.MockWorkItems.EXPECT().
248 GetWorkItem(tt.wantParentPath, tt.wantParentIID, gomock.Any()).
249 Return(&gitlab.WorkItem{ID: *tt.wantParentID, IID: tt.wantParentIID}, &gitlab.Response{}, nil)
250 }
252 var gotParentID *int64
253 tc.MockWorkItems.EXPECT().
254 UpdateWorkItem("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
255 DoAndReturn(func(_ string, _ int64, opts *gitlab.UpdateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
256 gotParentID = opts.ParentID
257 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
258 })
260 exec := cmdtest.SetupCmdForTest(
261 t,
262 NewCmd,
263 false,
264 cmdtest.WithGitLabClient(tc.Client),
265 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
266 )
268 _, err := exec(tt.args)
269 require.NoError(t, err)
270 assert.Equal(t, tt.wantParentID, gotParentID)
271 })
272 }
275func TestWorkItemsUpdate_ParentErrors(t *testing.T) {
276 t.Run("invalid parent", func(t *testing.T) {
277 tc := gitlabtesting.NewTestClient(t)
279 exec := cmdtest.SetupCmdForTest(
280 t,
281 NewCmd,
282 false,
283 cmdtest.WithGitLabClient(tc.Client),
284 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
285 )
287 _, err := exec("1 --parent abc")
288 var flagErr cmdutils.FlagError
289 require.ErrorAs(t, err, &flagErr)
290 assert.Contains(t, err.Error(), `invalid work item: "abc"`)
291 })
293 t.Run("parent URL on another host", func(t *testing.T) {
294 tc := gitlabtesting.NewTestClient(t, gitlab.WithBaseURL("https://gitlab.com/api/v4"))
296 exec := cmdtest.SetupCmdForTest(
297 t,
298 NewCmd,
299 false,
300 cmdtest.WithGitLabClient(tc.Client),
301 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
302 )
304 _, err := exec("1 --parent https://gitlab.example.com/group/project/-/work_items/42")
305 var flagErr cmdutils.FlagError
306 require.ErrorAs(t, err, &flagErr)
307 assert.Contains(t, err.Error(), "is not on gitlab.com")
308 })
310 t.Run("parent not found", func(t *testing.T) {
311 tc := gitlabtesting.NewTestClient(t)
312 tc.MockWorkItems.EXPECT().
313 GetWorkItem("OWNER/REPO", int64(42), gomock.Any()).
314 Return(nil, nil, gitlab.ErrNotFound)
316 exec := cmdtest.SetupCmdForTest(
317 t,
318 NewCmd,
319 false,
320 cmdtest.WithGitLabClient(tc.Client),
321 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
322 )
324 // No upload mock is set, so an upload before the parent lookup fails the test.
325 _, err := exec("1 --parent 42 --attach " + writeAttachFixture(t, "screenshot.png"))
326 require.ErrorIs(t, err, gitlab.ErrNotFound)
327 assert.Contains(t, err.Error(), "failed to find parent work item OWNER/REPO#42")
328 })
331func TestWorkItemsUpdate_Status(t *testing.T) {
332 tests := []struct {
333 name string
334 args string
335 want gitlab.WorkItemStatusID
336 }{
337 {name: "todo", args: `1 --status todo`, want: gitlab.WorkItemStatusToDo},
338 {name: "in-progress", args: `1 --status in-progress`, want: gitlab.WorkItemStatusInProgress},
339 {name: "done", args: `1 --status done`, want: gitlab.WorkItemStatusDone},
340 {name: "wont-do", args: `1 --status wont-do`, want: gitlab.WorkItemStatusWontDo},
341 {name: "duplicate", args: `1 --status duplicate`, want: gitlab.WorkItemStatusDuplicate},
342 {name: "in a group", args: `2 --group my-group --status done`, want: gitlab.WorkItemStatusDone},
343 }
345 for _, tt := range tests {
346 t.Run(tt.name, func(t *testing.T) {
347 tc := gitlabtesting.NewTestClient(t)
349 var got *gitlab.WorkItemStatusID
350 tc.MockWorkItems.EXPECT().
351 UpdateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
352 DoAndReturn(func(_ string, _ int64, opts *gitlab.UpdateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
353 got = opts.Status
354 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
355 })
357 exec := cmdtest.SetupCmdForTest(
358 t,
359 NewCmd,
360 false,
361 cmdtest.WithGitLabClient(tc.Client),
362 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
363 )
365 _, err := exec(tt.args)
366 require.NoError(t, err)
368 require.NotNil(t, got)
369 assert.Equal(t, tt.want, *got)
370 })
371 }
374func TestWorkItemsUpdate_StatusUnsetByDefault(t *testing.T) {
375 tc := gitlabtesting.NewTestClient(t)
377 var got *gitlab.UpdateWorkItemOptions
378 tc.MockWorkItems.EXPECT().
379 UpdateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
380 DoAndReturn(func(_ string, _ int64, opts *gitlab.UpdateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
381 got = opts
382 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
383 })
385 exec := cmdtest.SetupCmdForTest(
386 t,
387 NewCmd,
388 false,
389 cmdtest.WithGitLabClient(tc.Client),
390 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
391 )
393 _, err := exec(`1 --title "New title"`)
394 require.NoError(t, err)
396 require.NotNil(t, got)
397 assert.Nil(t, got.Status, "a title-only update must not touch the status")
400func TestWorkItemsUpdate_StatusRejectsUnknownName(t *testing.T) {
401 for _, arg := range []string{`"blocked"`, `""`, `"In progress"`, `Done`} {
402 t.Run(arg, func(t *testing.T) {
403 // No UpdateWorkItem call is expected: the flag is rejected before any request.
404 tc := gitlabtesting.NewTestClient(t)
406 exec := cmdtest.SetupCmdForTest(
407 t,
408 NewCmd,
409 false,
410 cmdtest.WithGitLabClient(tc.Client),
411 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
412 )
414 _, err := exec("1 --status " + arg)
415 require.Error(t, err)
416 for _, name := range []string{"todo", "in-progress", "done", "wont-do", "duplicate"} {
417 assert.Contains(t, err.Error(), name)
418 }
419 })
420 }
Diagnostics8 git calls · 364 ms
CallStatusSizeTime0364 ms
discover200185 B2 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
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: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=2
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
fetchtree:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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 · hit2 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=317
git.response.cache=HIT
git.response.duration_ms=2
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:0200170 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=351
git.response.bytes=170
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=21db039062ee6d041ad414dc4572fef7eccec043
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=170
git.pkt.payload.max=143
git.sideband.band1.bytes=143
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=143
git.pack.trailer=9a371b3c0ac0d73d3fe90ebc27ca40d9383b3a90
Open pack map
fetch2002.5 KiB351 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2593
git.response.cache=MISS
git.response.duration_ms=351
git.fetch.wants=2f12291b42e4fb94cd072039e1ccbfe2ca2c6574
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=2593
git.pkt.payload.max=2566
git.sideband.band1.bytes=2566
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
a5e29fecbc141293
Request
cbb5dae1…
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