GitLabgitlab-org

cli

main
ply10 git calls224 ms101 KiB8 cachedprotocol v2
360 lines11 KiBRaw
1//go:build !integration
3package publish
5import (
6 "errors"
7 "testing"
9 "git.sr.ht/~timofurrer/ugh"
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/testing/cmdtest"
20const mr1AuthorID int64 = 42
22func TestDraftPublish(t *testing.T) {
23 t.Parallel()
25 t.Run("publishes with summary note, internal, and reviewer state", func(t *testing.T) {
26 t.Parallel()
28 testClient := setupMR(t)
29 mockDraftList(t, testClient, 2)
31 testClient.MockDraftNotes.EXPECT().
32 PublishAllDraftNotesWithOptions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
33 DoAndReturn(func(pid any, mrIID int64, opts *gitlab.PublishAllDraftNotesOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
34 require.NotNil(t, opts.Note)
35 assert.Equal(t, "Overall LGTM", *opts.Note)
36 require.NotNil(t, opts.Internal)
37 assert.True(t, *opts.Internal)
38 require.NotNil(t, opts.ReviewerState)
39 assert.Equal(t, "requested_changes", *opts.ReviewerState)
40 return nil, nil
41 })
43 exec := setupPublishExec(t, testClient)
45 output, err := exec(`1 -y -m "Overall LGTM" --internal --reviewer-state requested_changes`)
46 require.NoError(t, err)
47 assert.Contains(t, output.String(), "✓ Published 2 pending review comments.")
48 assert.Contains(t, output.String(), "https://gitlab.com/OWNER/REPO/merge_requests/1")
49 })
51 t.Run("bare publish sends no body fields", func(t *testing.T) {
52 t.Parallel()
54 testClient := setupMR(t)
55 mockDraftList(t, testClient, 1)
57 testClient.MockDraftNotes.EXPECT().
58 PublishAllDraftNotesWithOptions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
59 DoAndReturn(func(pid any, mrIID int64, opts *gitlab.PublishAllDraftNotesOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
60 assert.Nil(t, opts.Note)
61 assert.Nil(t, opts.Internal)
62 assert.Nil(t, opts.ReviewerState)
63 return nil, nil
64 })
66 exec := setupPublishExec(t, testClient)
68 output, err := exec(`1 -y`)
69 require.NoError(t, err)
70 assert.Contains(t, output.String(), "✓ Published 1 pending review comment.")
71 })
73 t.Run("warns when author who is not a reviewer sets --reviewer-state", func(t *testing.T) {
74 t.Parallel()
76 testClient := gitlabtesting.NewTestClient(t)
77 mockMR1(t, testClient, &gitlab.BasicUser{ID: mr1AuthorID}, 7)
78 mockDraftList(t, testClient, 1)
79 expectPublishReviewed(t, testClient)
80 testClient.MockUsers.EXPECT().
81 CurrentUser(gomock.Any()).
82 Return(&gitlab.User{ID: mr1AuthorID}, nil, nil)
84 exec := setupPublishExec(t, testClient)
86 output, err := exec(`1 -y --reviewer-state reviewed`)
87 require.NoError(t, err)
88 assert.Contains(t, output.Stderr(), "GitLab did not set your review state: you authored this merge request and are not one of its reviewers.")
89 assert.Contains(t, output.String(), "✓ Published 1 pending review comment.")
90 })
92 t.Run("no warning when author is also a reviewer", func(t *testing.T) {
93 t.Parallel()
95 testClient := gitlabtesting.NewTestClient(t)
96 mockMR1(t, testClient, &gitlab.BasicUser{ID: mr1AuthorID}, 7, mr1AuthorID)
97 mockDraftList(t, testClient, 1)
98 expectPublishReviewed(t, testClient)
99 testClient.MockUsers.EXPECT().
100 CurrentUser(gomock.Any()).
101 Return(&gitlab.User{ID: mr1AuthorID}, nil, nil)
103 exec := setupPublishExec(t, testClient)
105 output, err := exec(`1 -y --reviewer-state reviewed`)
106 require.NoError(t, err)
107 assert.NotContains(t, output.Stderr(), "GitLab did not set your review state")
108 assert.Contains(t, output.String(), "✓ Published 1 pending review comment.")
109 })
111 t.Run("no warning when a different user sets --reviewer-state", func(t *testing.T) {
112 t.Parallel()
114 testClient := gitlabtesting.NewTestClient(t)
115 mockMR1(t, testClient, &gitlab.BasicUser{ID: mr1AuthorID})
116 mockDraftList(t, testClient, 1)
117 expectPublishReviewed(t, testClient)
118 testClient.MockUsers.EXPECT().
119 CurrentUser(gomock.Any()).
120 Return(&gitlab.User{ID: 7}, nil, nil)
122 exec := setupPublishExec(t, testClient)
124 output, err := exec(`1 -y --reviewer-state reviewed`)
125 require.NoError(t, err)
126 assert.NotContains(t, output.Stderr(), "GitLab did not set your review state")
127 assert.Contains(t, output.String(), "✓ Published 1 pending review comment.")
128 })
130 t.Run("publishes without warning when the current user lookup fails", func(t *testing.T) {
131 t.Parallel()
133 testClient := gitlabtesting.NewTestClient(t)
134 mockMR1(t, testClient, &gitlab.BasicUser{ID: mr1AuthorID})
135 mockDraftList(t, testClient, 1)
136 expectPublishReviewed(t, testClient)
137 testClient.MockUsers.EXPECT().
138 CurrentUser(gomock.Any()).
139 Return(nil, nil, errors.New("boom"))
141 exec := setupPublishExec(t, testClient)
143 output, err := exec(`1 -y --reviewer-state reviewed`)
144 require.NoError(t, err)
145 assert.NotContains(t, output.Stderr(), "GitLab did not set your review state")
146 assert.Contains(t, output.String(), "✓ Published 1 pending review comment.")
147 })
149 t.Run("no author check when publish fails", func(t *testing.T) {
150 t.Parallel()
152 testClient := gitlabtesting.NewTestClient(t)
153 mockMR1(t, testClient, &gitlab.BasicUser{ID: mr1AuthorID})
154 mockDraftList(t, testClient, 1)
155 testClient.MockDraftNotes.EXPECT().
156 PublishAllDraftNotesWithOptions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
157 Return(nil, errors.New("boom"))
159 exec := setupPublishExec(t, testClient)
161 output, err := exec(`1 -y --reviewer-state reviewed`)
162 require.ErrorContains(t, err, "failed to publish pending review comments")
163 assert.NotContains(t, output.Stderr(), "GitLab did not set your review state")
164 })
166 t.Run("errors when there are no pending comments", func(t *testing.T) {
167 t.Parallel()
169 testClient := setupMR(t)
170 mockDraftList(t, testClient, 0)
172 exec := setupPublishExec(t, testClient)
174 _, err := exec(`1 -y`)
175 require.ErrorContains(t, err, "no pending review comments on !1")
176 })
178 t.Run("--internal requires --message", func(t *testing.T) {
179 t.Parallel()
181 exec := setupPublishExec(t, gitlabtesting.NewTestClient(t))
183 _, err := exec(`1 -y --internal`)
184 require.ErrorContains(t, err, "--internal requires --message")
185 })
187 t.Run("rejects whitespace-only --message", func(t *testing.T) {
188 t.Parallel()
190 exec := setupPublishExec(t, gitlabtesting.NewTestClient(t))
192 _, err := exec(`1 -y -m " "`)
193 require.ErrorContains(t, err, "--message cannot be empty")
194 })
196 t.Run("rejects unknown reviewer state", func(t *testing.T) {
197 t.Parallel()
199 exec := setupPublishExec(t, gitlabtesting.NewTestClient(t))
201 _, err := exec(`1 -y --reviewer-state bogus`)
202 require.ErrorContains(t, err, "must be one of")
203 })
205 t.Run("--yes required non-interactively, before any API call", func(t *testing.T) {
206 t.Parallel()
208 testClient := gitlabtesting.NewTestClient(t)
210 exec := cmdtest.SetupCmdForTest(t, NewCmd, false,
211 cmdtest.WithGitLabClient(testClient.Client),
212 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
213 )
215 _, err := exec(`1`)
216 require.ErrorContains(t, err, "--yes required when not running interactively")
217 })
219 t.Run("ListDraftNotes error wrapped", func(t *testing.T) {
220 t.Parallel()
222 testClient := setupMR(t)
224 testClient.MockDraftNotes.EXPECT().
225 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
226 Return(nil, nil, errors.New("boom"))
228 exec := setupPublishExec(t, testClient)
230 _, err := exec(`1 -y`)
231 require.ErrorContains(t, err, "failed to list pending review comments: boom")
232 })
234 t.Run("paginates draft list", func(t *testing.T) {
235 t.Parallel()
237 testClient := setupMR(t)
239 page1 := make([]*gitlab.DraftNote, 100)
240 for i := range page1 {
241 page1[i] = &gitlab.DraftNote{ID: int64(701 + i)}
242 }
243 page2 := []*gitlab.DraftNote{{ID: 801}, {ID: 802}, {ID: 803}}
245 gomock.InOrder(
246 testClient.MockDraftNotes.EXPECT().
247 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
248 Return(page1, &gitlab.Response{NextPage: 2}, nil),
249 testClient.MockDraftNotes.EXPECT().
250 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
251 Return(page2, &gitlab.Response{NextPage: 0}, nil),
252 )
254 testClient.MockDraftNotes.EXPECT().
255 PublishAllDraftNotesWithOptions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
256 Return(nil, nil)
258 exec := setupPublishExec(t, testClient)
260 output, err := exec(`1 -y`)
261 require.NoError(t, err)
262 assert.Contains(t, output.String(), "✓ Published 103 pending review comments.")
263 })
265 t.Run("PublishAllDraftNotes error wrapped", func(t *testing.T) {
266 t.Parallel()
268 testClient := setupMR(t)
269 mockDraftList(t, testClient, 1)
271 testClient.MockDraftNotes.EXPECT().
272 PublishAllDraftNotesWithOptions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
273 Return(nil, errors.New("boom"))
275 exec := setupPublishExec(t, testClient)
277 _, err := exec(`1 -y`)
278 require.ErrorContains(t, err, "failed to publish pending review comments: boom")
279 })
282func TestDraftPublish_DeclinePrompt(t *testing.T) {
283 // NOTE: This test cannot run in parallel because the huh form library
284 // uses global state (charmbracelet/bubbles runeutil sanitizer).
285 testClient := setupMR(t)
286 mockDraftList(t, testClient, 2)
287 testClient.MockDraftNotes.EXPECT().
288 PublishAllDraftNotesWithOptions(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
289 Times(0)
291 c := ugh.New(t)
292 c.Expect(ugh.Confirm("Publish 2 pending review comments?")).
293 Do(ugh.Reject)
295 exec := cmdtest.SetupCmdForTest(t, NewCmd, true,
296 cmdtest.WithGitLabClient(testClient.Client),
297 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
298 cmdtest.WithConsole(t, c),
299 )
301 out, err := exec(`1`)
302 require.NoError(t, err)
303 assert.Contains(t, out.String(), "Aborted.")
306func setupMR(t *testing.T) *gitlabtesting.TestClient {
307 t.Helper()
308 testClient := gitlabtesting.NewTestClient(t)
309 mockMR1(t, testClient, nil)
310 return testClient
313func mockMR1(t *testing.T, tc *gitlabtesting.TestClient, author *gitlab.BasicUser, reviewerIDs ...int64) {
314 t.Helper()
315 reviewers := make([]*gitlab.BasicUser, 0, len(reviewerIDs))
316 for _, id := range reviewerIDs {
317 reviewers = append(reviewers, &gitlab.BasicUser{ID: id})
318 }
319 tc.MockMergeRequests.EXPECT().
320 GetMergeRequest("OWNER/REPO", int64(1), gomock.Any()).
321 Return(&gitlab.MergeRequest{
322 BasicMergeRequest: gitlab.BasicMergeRequest{
323 ID: 1,
324 IID: 1,
325 WebURL: "https://gitlab.com/OWNER/REPO/merge_requests/1",
326 Author: author,
327 Reviewers: reviewers,
328 },
329 }, nil, nil)
332func mockDraftList(t *testing.T, testClient *gitlabtesting.TestClient, count int) {
333 t.Helper()
334 drafts := make([]*gitlab.DraftNote, count)
335 for i := range drafts {
336 drafts[i] = &gitlab.DraftNote{ID: int64(701 + i)}
337 }
338 testClient.MockDraftNotes.EXPECT().
339 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
340 Return(drafts, &gitlab.Response{NextPage: 0}, nil)
343func expectPublishReviewed(t *testing.T, testClient *gitlabtesting.TestClient) {
344 t.Helper()
345 testClient.MockDraftNotes.EXPECT().
346 PublishAllDraftNotesWithOptions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
347 DoAndReturn(func(pid any, mrIID int64, opts *gitlab.PublishAllDraftNotesOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
348 require.NotNil(t, opts.ReviewerState)
349 assert.Equal(t, "reviewed", *opts.ReviewerState)
350 return nil, nil
351 })
354func setupPublishExec(t *testing.T, testClient *gitlabtesting.TestClient) cmdtest.CmdExecFunc {
355 t.Helper()
356 return cmdtest.SetupCmdForTest(t, NewCmd, true,
357 cmdtest.WithGitLabClient(testClient.Client),
358 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
359 )
Diagnostics10 git calls · 224 ms
CallStatusSizeTime0224 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 KiB · hit2 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=HIT
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 · 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:0200746 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=746
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f
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=746
git.pkt.payload.max=719
git.sideband.band1.bytes=719
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=719
git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790
Open pack map
fetchtree:0200540 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=540
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=d1be7f980a856805fa31e30e01fff4db335cbe1b
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=540
git.pkt.payload.max=513
git.sideband.band1.bytes=513
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=513
git.pack.trailer=6c82398efd91c9e023d583b948fde889bd4991b2
Open pack map
fetchtree:0200259 B · hit8 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=8
git.response.bytes=259
git.response.cache=HIT
git.response.duration_ms=8
git.fetch.filter=tree:0
git.fetch.wants=17c60670b892d93213e07c56d27bf8c78b2f6eee
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=259
git.pkt.payload.max=232
git.sideband.band1.bytes=232
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=232
git.pack.trailer=a86d2eccaf7aae3b0fec29dbfe655131faf5a4ad
Open pack map
fetchtree:0200176 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=176
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=f4803b475319c7928519f6199427aeb534eec830
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=176
git.pkt.payload.max=149
git.sideband.band1.bytes=149
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=149
git.pack.trailer=9a9a2a79859324a1daf1ee0f70d8289fbb901820
Open pack map
fetch2002.1 KiB199 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=199
git.response.bytes=2194
git.response.cache=MISS
git.response.duration_ms=199
git.fetch.wants=74f49cfd751d6503a4d69a2b359e8e1acdd662e2
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=2194
git.pkt.payload.max=2167
git.sideband.band1.bytes=2167
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
a5e2f7e6ffaf3a24
Request
3c7f3e18…
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