GitLabgitlab-org

cli

main
ply8 git calls254 ms99 KiB6 cachedprotocol v2
318 lines8.9 KiBRaw
1//go:build !integration
3package note
5import (
6 "fmt"
7 "net/http"
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/testing/cmdtest"
20func makeDiscussionsWithAuthor() []*gitlab.Discussion {
21 return []*gitlab.Discussion{
22 {
23 ID: "abc12345deadbeef1234567890abcdef12345678",
24 Notes: []*gitlab.Note{
25 {
26 ID: 100,
27 Body: "First discussion note",
28 Author: gitlab.NoteAuthor{
29 Username: "testuser",
30 },
31 },
32 },
33 },
34 {
35 ID: "def67890cafebabe1234567890abcdef12345678",
36 Notes: []*gitlab.Note{
37 {
38 ID: 200,
39 Body: "Second discussion note",
40 Author: gitlab.NoteAuthor{
41 Username: "otheruser",
42 },
43 },
44 },
45 },
46 }
49func Test_delete_subcommand(t *testing.T) {
50 t.Parallel()
52 t.Run("delete with --yes flag", func(t *testing.T) {
53 t.Parallel()
55 testClient := gitlabtesting.NewTestClient(t)
56 mockMR1(t, testClient)
58 testClient.MockDiscussions.EXPECT().
59 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
60 Return(makeDiscussionsWithAuthor(), nil, nil)
62 testClient.MockDiscussions.EXPECT().
63 DeleteMergeRequestDiscussionNote(
64 "OWNER/REPO",
65 int64(1),
66 "abc12345deadbeef1234567890abcdef12345678",
67 int64(100),
68 gomock.Any(),
69 ).
70 Return(nil, nil)
72 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
73 cmdtest.WithGitLabClient(testClient.Client),
74 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
75 )
77 output, err := exec(`delete 1 100 --yes`)
78 require.NoError(t, err)
79 assert.Contains(t, output.String(), "✓ Deleted note 100 from !1")
80 })
82 t.Run("delete note found on second page", func(t *testing.T) {
83 t.Parallel()
85 testClient := gitlabtesting.NewTestClient(t)
86 mockMR1(t, testClient)
88 nextPageResponse := &gitlab.Response{
89 Response: &http.Response{StatusCode: http.StatusOK},
90 NextPage: 2,
91 }
92 lastPageResponse := &gitlab.Response{
93 Response: &http.Response{StatusCode: http.StatusOK},
94 }
96 // First page: no match.
97 testClient.MockDiscussions.EXPECT().
98 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
99 Return([]*gitlab.Discussion{
100 {
101 ID: "abc12345deadbeef1234567890abcdef12345678",
102 Notes: []*gitlab.Note{
103 {ID: 100, Body: "First discussion note", Author: gitlab.NoteAuthor{Username: "testuser"}},
104 },
105 },
106 }, nextPageResponse, nil)
108 // Second page: match. No further pages should be requested.
109 testClient.MockDiscussions.EXPECT().
110 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
111 Return([]*gitlab.Discussion{
112 {
113 ID: "def67890cafebabe1234567890abcdef12345678",
114 Notes: []*gitlab.Note{
115 {ID: 200, Body: "Second discussion note", Author: gitlab.NoteAuthor{Username: "otheruser"}},
116 },
117 },
118 }, lastPageResponse, nil)
120 testClient.MockDiscussions.EXPECT().
121 DeleteMergeRequestDiscussionNote(
122 "OWNER/REPO",
123 int64(1),
124 "def67890cafebabe1234567890abcdef12345678",
125 int64(200),
126 gomock.Any(),
127 ).
128 Return(nil, nil)
130 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
131 cmdtest.WithGitLabClient(testClient.Client),
132 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
133 )
135 output, err := exec(`delete 1 200 --yes`)
136 require.NoError(t, err)
137 assert.Contains(t, output.String(), "✓ Deleted note 200 from !1")
138 })
140 t.Run("delete non-TTY skips confirmation", func(t *testing.T) {
141 t.Parallel()
143 testClient := gitlabtesting.NewTestClient(t)
144 mockMR1(t, testClient)
146 testClient.MockDiscussions.EXPECT().
147 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
148 Return(makeDiscussionsWithAuthor(), nil, nil)
150 testClient.MockDiscussions.EXPECT().
151 DeleteMergeRequestDiscussionNote(
152 "OWNER/REPO",
153 int64(1),
154 "def67890cafebabe1234567890abcdef12345678",
155 int64(200),
156 gomock.Any(),
157 ).
158 Return(nil, nil)
160 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, false,
161 cmdtest.WithGitLabClient(testClient.Client),
162 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
163 )
165 output, err := exec(`delete 1 200 --yes`)
166 require.NoError(t, err)
167 assert.Contains(t, output.String(), "✓ Deleted note 200 from !1")
168 })
170 t.Run("non-TTY without --yes requires flag", func(t *testing.T) {
171 t.Parallel()
173 testClient := gitlabtesting.NewTestClient(t)
174 mockMR1(t, testClient)
176 testClient.MockDiscussions.EXPECT().
177 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
178 Return(makeDiscussionsWithAuthor(), nil, nil)
180 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, false,
181 cmdtest.WithGitLabClient(testClient.Client),
182 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
183 )
185 _, err := exec(`delete 1 100`)
186 require.Error(t, err)
187 assert.Contains(t, err.Error(), "--yes required when not running interactively")
188 })
190 t.Run("non-integer identifier", func(t *testing.T) {
191 t.Parallel()
193 testClient := gitlabtesting.NewTestClient(t)
195 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
196 cmdtest.WithGitLabClient(testClient.Client),
197 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
198 )
200 _, err := exec(`delete 1 abc12345`)
201 require.Error(t, err)
202 assert.Contains(t, err.Error(), "invalid note ID")
203 assert.Contains(t, err.Error(), "must be a numeric note ID")
204 })
206 t.Run("note ID not found", func(t *testing.T) {
207 t.Parallel()
209 testClient := gitlabtesting.NewTestClient(t)
210 mockMR1(t, testClient)
212 testClient.MockDiscussions.EXPECT().
213 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
214 Return(makeDiscussionsWithAuthor(), &gitlab.Response{Response: &http.Response{StatusCode: http.StatusOK}}, nil)
216 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
217 cmdtest.WithGitLabClient(testClient.Client),
218 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
219 )
221 _, err := exec(`delete 1 999999`)
222 require.Error(t, err)
223 assert.Contains(t, err.Error(), "note 999999 not found in merge request !1")
224 })
226 t.Run("note ID not found across multiple pages", func(t *testing.T) {
227 t.Parallel()
229 testClient := gitlabtesting.NewTestClient(t)
230 mockMR1(t, testClient)
232 nextPageResponse := &gitlab.Response{
233 Response: &http.Response{StatusCode: http.StatusOK},
234 NextPage: 2,
235 }
236 lastPageResponse := &gitlab.Response{
237 Response: &http.Response{StatusCode: http.StatusOK},
238 }
240 testClient.MockDiscussions.EXPECT().
241 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
242 Return(makeDiscussionsWithAuthor(), nextPageResponse, nil)
244 testClient.MockDiscussions.EXPECT().
245 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
246 Return([]*gitlab.Discussion{
247 {
248 ID: "aaaa1111bbbb2222cccc3333dddd4444eeee5555",
249 Notes: []*gitlab.Note{
250 {ID: 300, Body: "Third discussion note", Author: gitlab.NoteAuthor{Username: "thirduser"}},
251 },
252 },
253 }, lastPageResponse, nil)
255 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
256 cmdtest.WithGitLabClient(testClient.Client),
257 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
258 )
260 _, err := exec(`delete 1 999999`)
261 require.Error(t, err)
262 assert.Contains(t, err.Error(), "note 999999 not found in merge request !1")
263 })
265 t.Run("underlying API error during lookup is not masked as not-found", func(t *testing.T) {
266 t.Parallel()
268 testClient := gitlabtesting.NewTestClient(t)
269 mockMR1(t, testClient)
271 forbiddenResponse := &gitlab.Response{
272 Response: &http.Response{StatusCode: http.StatusForbidden},
273 }
274 testClient.MockDiscussions.EXPECT().
275 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
276 Return(nil, forbiddenResponse, fmt.Errorf("403 Forbidden"))
278 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
279 cmdtest.WithGitLabClient(testClient.Client),
280 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
281 )
283 _, err := exec(`delete 1 100 --yes`)
284 require.Error(t, err)
285 assert.Contains(t, err.Error(), "403 Forbidden")
286 assert.NotContains(t, err.Error(), "not found", "a real API failure must not be reported as note-not-found")
287 })
289 t.Run("API error", func(t *testing.T) {
290 t.Parallel()
292 testClient := gitlabtesting.NewTestClient(t)
293 mockMR1(t, testClient)
295 testClient.MockDiscussions.EXPECT().
296 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
297 Return(makeDiscussionsWithAuthor(), nil, nil)
299 testClient.MockDiscussions.EXPECT().
300 DeleteMergeRequestDiscussionNote(
301 "OWNER/REPO",
302 int64(1),
303 "abc12345deadbeef1234567890abcdef12345678",
304 int64(100),
305 gomock.Any(),
306 ).
307 Return(nil, fmt.Errorf("403 Forbidden"))
309 exec := cmdtest.SetupCmdForTest(t, NewCmdNote, true,
310 cmdtest.WithGitLabClient(testClient.Client),
311 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
312 )
314 _, err := exec(`delete 1 100 --yes`)
315 require.Error(t, err)
316 assert.Contains(t, err.Error(), "failed to delete note")
317 })
Diagnostics8 git calls · 254 ms
CallStatusSizeTime0254 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 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
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
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 · 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
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:0200746 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=746
git.response.cache=HIT
git.response.duration_ms=3
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 · 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=540
git.response.cache=HIT
git.response.duration_ms=2
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
fetch2001.4 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=1470
git.response.cache=MISS
git.response.duration_ms=242
git.fetch.wants=6bff4cb8b5fdaf59040971ccf6b733b98075da5b
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=1470
git.pkt.payload.max=1443
git.sideband.band1.bytes=1443
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
a5e2ba6b8c76cf46
Request
6c293fac…
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