GitLabgitlab-org

cli

main
ply8 git calls339 ms99 KiB5 cachedprotocol v2
341 lines9.5 KiBRaw
1//go:build !integration
3package create
5import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.uber.org/mock/gomock"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
18 "gitlab.com/gitlab-org/cli/internal/cmdutils"
19 "gitlab.com/gitlab-org/cli/internal/glinstance"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
23func TestWorkItemsCreate(t *testing.T) {
24 t.Run("success cases", func(t *testing.T) {
25 tests := []struct {
26 name string
27 args string
28 workItem *gitlab.WorkItem
29 wantOutput string
30 }{
31 {
32 name: "creates work item in project scope",
33 args: "--type issue --title \"Test Issue\"",
34 workItem: &gitlab.WorkItem{
35 IID: 1,
36 Title: "Test Issue",
37 WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1",
38 },
39 wantOutput: "https://gitlab.com/OWNER/REPO/-/work_items/1",
40 },
41 {
42 name: "creates work item in group scope",
43 args: "--group my-group --type epic --title \"Test Epic\"",
44 workItem: &gitlab.WorkItem{
45 IID: 2,
46 Title: "Test Epic",
47 WebURL: "https://gitlab.com/groups/my-group/-/work_items/2",
48 },
49 wantOutput: "https://gitlab.com/groups/my-group/-/work_items/2",
50 },
51 }
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 tc := gitlabtesting.NewTestClient(t)
56 tc.MockWorkItems.EXPECT().
57 CreateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
58 Return(tt.workItem, &gitlab.Response{}, nil)
60 exec := cmdtest.SetupCmdForTest(
61 t,
62 NewCmd,
63 false,
64 cmdtest.WithGitLabClient(tc.Client),
65 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
66 )
68 out, err := exec(tt.args)
69 require.NoError(t, err)
70 assert.Contains(t, out.OutBuf.String(), tt.wantOutput)
71 })
72 }
73 })
75 t.Run("API error", func(t *testing.T) {
76 tc := gitlabtesting.NewTestClient(t)
77 tc.MockWorkItems.EXPECT().
78 CreateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
79 Return(nil, nil, assert.AnError)
81 exec := cmdtest.SetupCmdForTest(
82 t,
83 NewCmd,
84 false,
85 cmdtest.WithGitLabClient(tc.Client),
86 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
87 )
89 _, err := exec("--type issue --title \"Test Issue\"")
90 require.Error(t, err)
91 })
94func TestWorkItemsCreate_DescriptionFile(t *testing.T) {
95 const body = "# Heading\n\nMulti-line body with \"quotes\", $VARS and `backticks`.\n"
97 tests := []struct {
98 name string
99 useStdin bool
100 }{
101 {name: "reads the description from a file"},
102 {name: "reads the description from standard input", useStdin: true},
103 }
105 for _, tt := range tests {
106 t.Run(tt.name, func(t *testing.T) {
107 tc := gitlabtesting.NewTestClient(t)
109 var gotDescription *string
110 tc.MockWorkItems.EXPECT().
111 CreateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
112 DoAndReturn(func(_ string, _ gitlab.WorkItemTypeID, opts *gitlab.CreateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
113 gotDescription = opts.Description
114 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
115 })
117 factoryOpts := []cmdtest.FactoryOption{
118 cmdtest.WithGitLabClient(tc.Client),
119 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
120 }
122 path := "-"
123 if tt.useStdin {
124 factoryOpts = append(factoryOpts, cmdtest.WithStdin(body))
125 } else {
126 path = filepath.Join(t.TempDir(), "description.md")
127 require.NoError(t, os.WriteFile(path, []byte(body), 0o600))
128 }
130 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, factoryOpts...)
132 _, err := exec(fmt.Sprintf("--type issue --title Test --description-file %q", path))
133 require.NoError(t, err)
135 require.NotNil(t, gotDescription)
136 assert.Equal(t, body, *gotDescription)
137 })
138 }
141func TestWorkItemsCreate_Iteration(t *testing.T) {
142 tests := []struct {
143 name string
144 args string
145 wantIteration *int64
146 }{
147 {
148 name: "sets the iteration",
149 args: "--type issue --title Test --iteration 123",
150 wantIteration: new(int64(123)),
151 },
152 {
153 name: "leaves the iteration unset without the flag",
154 args: "--type issue --title Test",
155 },
156 }
158 for _, tt := range tests {
159 t.Run(tt.name, func(t *testing.T) {
160 tc := gitlabtesting.NewTestClient(t)
162 var gotIteration *int64
163 tc.MockWorkItems.EXPECT().
164 CreateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
165 DoAndReturn(func(_ string, _ gitlab.WorkItemTypeID, opts *gitlab.CreateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
166 gotIteration = opts.IterationID
167 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
168 })
170 exec := cmdtest.SetupCmdForTest(
171 t,
172 NewCmd,
173 false,
174 cmdtest.WithGitLabClient(tc.Client),
175 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
176 )
178 _, err := exec(tt.args)
179 require.NoError(t, err)
180 assert.Equal(t, tt.wantIteration, gotIteration)
181 })
182 }
185func TestWorkItemsCreate_DescriptionFileConflictsWithDescription(t *testing.T) {
186 exec := cmdtest.SetupCmdForTest(
187 t,
188 NewCmd,
189 false,
190 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
191 )
193 _, err := exec(`--type issue --title Test --description inline --description-file some.md`)
195 require.Error(t, err)
196 assert.Contains(t, err.Error(), "[description description-file]")
199func TestWorkItemsCreate_FlagValidation(t *testing.T) {
200 tests := []struct {
201 name string
202 args string
203 wantErr string
204 }{
205 {
206 name: "missing work item type",
207 args: "",
208 wantErr: "required flag(s) \"type\" not set",
209 },
210 {
211 name: "unknown type",
212 args: "--type bogus",
213 wantErr: "must be one of",
214 },
215 {
216 name: "missing --title",
217 args: "--type issue",
218 wantErr: "--title required",
219 },
220 {
221 name: "non-numeric --iteration",
222 args: "--type issue --title Test --iteration abc",
223 wantErr: "invalid argument \"abc\" for \"--iteration\"",
224 },
225 }
227 for _, tt := range tests {
228 t.Run(tt.name, func(t *testing.T) {
229 exec := cmdtest.SetupCmdForTest(
230 t,
231 NewCmd,
232 false,
233 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
234 )
236 _, err := exec(tt.args)
237 require.Error(t, err)
238 assert.Contains(t, err.Error(), tt.wantErr)
239 })
240 }
243func TestWorkItemsCreate_Parent(t *testing.T) {
244 tests := []struct {
245 name string
246 args string
247 wantParentPath string
248 wantParentIID int64
249 wantParentID *int64
250 }{
251 {
252 name: "sets the parent from an ID in the current project",
253 args: "--type task --title Test --parent 42",
254 wantParentPath: "OWNER/REPO",
255 wantParentIID: 42,
256 wantParentID: new(int64(9001)),
257 },
258 {
259 name: "sets the parent from a reference",
260 args: "--type task --title Test --parent my-group/my-project#42",
261 wantParentPath: "my-group/my-project",
262 wantParentIID: 42,
263 wantParentID: new(int64(9001)),
264 },
265 {
266 name: "leaves the parent unset without the flag",
267 args: "--type task --title Test",
268 },
269 }
271 for _, tt := range tests {
272 t.Run(tt.name, func(t *testing.T) {
273 tc := gitlabtesting.NewTestClient(t)
275 if tt.wantParentID != nil {
276 tc.MockWorkItems.EXPECT().
277 GetWorkItem(tt.wantParentPath, tt.wantParentIID, gomock.Any()).
278 Return(&gitlab.WorkItem{ID: *tt.wantParentID, IID: tt.wantParentIID}, &gitlab.Response{}, nil)
279 }
281 var gotParentID *int64
282 tc.MockWorkItems.EXPECT().
283 CreateWorkItem(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
284 DoAndReturn(func(_ string, _ gitlab.WorkItemTypeID, opts *gitlab.CreateWorkItemOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.WorkItem, *gitlab.Response, error) {
285 gotParentID = opts.ParentID
286 return &gitlab.WorkItem{IID: 1, WebURL: "https://gitlab.com/OWNER/REPO/-/work_items/1"}, &gitlab.Response{}, nil
287 })
289 exec := cmdtest.SetupCmdForTest(
290 t,
291 NewCmd,
292 false,
293 cmdtest.WithGitLabClient(tc.Client),
294 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
295 )
297 _, err := exec(tt.args)
298 require.NoError(t, err)
299 assert.Equal(t, tt.wantParentID, gotParentID)
300 })
301 }
304func TestWorkItemsCreate_ParentErrors(t *testing.T) {
305 t.Run("invalid parent", func(t *testing.T) {
306 tc := gitlabtesting.NewTestClient(t)
308 exec := cmdtest.SetupCmdForTest(
309 t,
310 NewCmd,
311 false,
312 cmdtest.WithGitLabClient(tc.Client),
313 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
314 )
316 _, err := exec("--type task --title Test --parent abc")
317 var flagErr cmdutils.FlagError
318 require.ErrorAs(t, err, &flagErr)
319 assert.Contains(t, err.Error(), `invalid work item: "abc"`)
320 })
322 t.Run("parent not found", func(t *testing.T) {
323 tc := gitlabtesting.NewTestClient(t)
324 tc.MockWorkItems.EXPECT().
325 GetWorkItem("OWNER/REPO", int64(42), gomock.Any()).
326 Return(nil, nil, gitlab.ErrNotFound)
328 exec := cmdtest.SetupCmdForTest(
329 t,
330 NewCmd,
331 false,
332 cmdtest.WithGitLabClient(tc.Client),
333 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
334 )
336 // No upload mock is set, so an upload before the parent lookup fails the test.
337 _, err := exec("--type task --title Test --parent 42 --attach " + writeAttachFixture(t, "screenshot.png"))
338 require.ErrorIs(t, err, gitlab.ErrNotFound)
339 assert.Contains(t, err.Error(), "failed to find parent work item OWNER/REPO#42")
340 })
Diagnostics8 git calls · 339 ms
CallStatusSizeTime0339 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 KiB3 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=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
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=3
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=4
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:0200317 B · hit11 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=11
git.response.bytes=317
git.response.cache=HIT
git.response.duration_ms=11
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:0200169 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=169
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=e2a55b61933487e8e6f2e62d12464503856ecc34
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=169
git.pkt.payload.max=142
git.sideband.band1.bytes=142
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=142
git.pack.trailer=009c97babab804d87b16c1c6fb6145fa33f218b8
Open pack map
fetch2002.2 KiB315 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=315
git.response.bytes=2254
git.response.cache=MISS
git.response.duration_ms=315
git.fetch.wants=4bae0c1c296ee27364662317a444d702036683a7
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=2254
git.pkt.payload.max=2227
git.sideband.band1.bytes=2227
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
a5e1ecde2b91452a
Request
44d59f66…
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