GitLabgitlab-org

cli

main
ply10 git calls206 ms100 KiB8 cachedprotocol v2
177 lines5.5 KiBRaw
1//go:build !integration
3package delete
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"
20func TestDraftDelete(t *testing.T) {
21 t.Parallel()
23 t.Run("--yes deletes the pending comment", func(t *testing.T) {
24 t.Parallel()
26 tc := setupMR(t)
27 mockGetDraft(tc)
28 tc.MockDraftNotes.EXPECT().
29 DeleteDraftNote("OWNER/REPO", int64(1), int64(501), gomock.Any()).
30 Return(nil, nil)
32 output, err := setupExec(t, tc, false)(`1 501 --yes`)
33 require.NoError(t, err)
34 assert.Equal(t, "✓ Deleted pending review comment 501 from !1\n", output.String())
35 })
37 t.Run("a lone ID is the pending comment on the current branch's merge request", func(t *testing.T) {
38 t.Parallel()
40 tc := setupMR(t)
41 mockBranchMR(t, tc)
42 mockGetDraft(tc)
43 tc.MockDraftNotes.EXPECT().
44 DeleteDraftNote("OWNER/REPO", int64(1), int64(501), gomock.Any()).
45 Return(nil, nil)
47 output, err := setupExec(t, tc, false, cmdtest.WithBranch("feature"))(`501 --yes`)
48 require.NoError(t, err)
49 assert.Equal(t, "✓ Deleted pending review comment 501 from !1\n", output.String())
50 })
52 t.Run("non-interactive without --yes fails before any API call", func(t *testing.T) {
53 t.Parallel()
55 tc := gitlabtesting.NewTestClient(t)
57 _, err := setupExec(t, tc, false)(`1 501`)
58 require.ErrorContains(t, err, "--yes required when not running interactively")
59 })
61 t.Run("unknown ID", func(t *testing.T) {
62 t.Parallel()
64 tc := setupMR(t)
65 tc.MockDraftNotes.EXPECT().
66 GetDraftNote("OWNER/REPO", int64(1), int64(999), gomock.Any()).
67 Return(nil, nil, gitlab.ErrNotFound)
69 _, err := setupExec(t, tc, false)(`1 999 --yes`)
70 require.EqualError(t, err, "pending review comment 999 not found in merge request !1")
71 })
73 t.Run("lookup error other than not found is wrapped", func(t *testing.T) {
74 t.Parallel()
76 tc := setupMR(t)
77 tc.MockDraftNotes.EXPECT().
78 GetDraftNote("OWNER/REPO", int64(1), int64(501), gomock.Any()).
79 Return(nil, nil, errors.New("500 Internal Server Error"))
81 _, err := setupExec(t, tc, false)(`1 501 --yes`)
82 require.EqualError(t, err, "failed to get pending review comment 501: 500 Internal Server Error")
83 })
85 t.Run("non-numeric ID is rejected before any API call", func(t *testing.T) {
86 t.Parallel()
88 tc := gitlabtesting.NewTestClient(t)
90 _, err := setupExec(t, tc, false)(`1 abc12345 --yes`)
91 require.EqualError(t, err, `invalid pending review comment ID "abc12345": must be a number`)
92 })
94 t.Run("delete error is wrapped", func(t *testing.T) {
95 t.Parallel()
97 tc := setupMR(t)
98 mockGetDraft(tc)
99 tc.MockDraftNotes.EXPECT().
100 DeleteDraftNote("OWNER/REPO", int64(1), int64(501), gomock.Any()).
101 Return(nil, errors.New("boom"))
103 _, err := setupExec(t, tc, false)(`1 501 --yes`)
104 require.ErrorContains(t, err, "failed to delete pending review comment: boom")
105 })
108func TestDraftDelete_Prompt(t *testing.T) {
109 // NOTE: This test cannot run in parallel because the huh form library
110 // uses global state (charmbracelet/bubbles runeutil sanitizer).
112 t.Run("confirming deletes", func(t *testing.T) {
113 tc := setupMR(t)
114 mockGetDraft(tc)
115 tc.MockDraftNotes.EXPECT().
116 DeleteDraftNote("OWNER/REPO", int64(1), int64(501), gomock.Any()).
117 Return(nil, nil)
119 c := ugh.New(t)
120 c.Expect(ugh.Confirm("Delete this pending review comment?")).Do(ugh.Affirm)
122 out, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))(`1 501`)
123 require.NoError(t, err)
124 assert.Contains(t, out.String(), "Pending review comment 501: Pending draft")
125 assert.Contains(t, out.String(), "✓ Deleted pending review comment 501 from !1")
126 })
128 t.Run("declining deletes nothing", func(t *testing.T) {
129 tc := setupMR(t)
130 mockGetDraft(tc)
131 tc.MockDraftNotes.EXPECT().
132 DeleteDraftNote(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
133 Times(0)
135 c := ugh.New(t)
136 c.Expect(ugh.Confirm("Delete this pending review comment?")).Do(ugh.Reject)
138 out, err := setupExec(t, tc, true, cmdtest.WithConsole(t, c))(`1 501`)
139 require.NoError(t, err)
140 assert.Contains(t, out.String(), "Aborted.")
141 })
144func mockGetDraft(tc *gitlabtesting.TestClient) {
145 tc.MockDraftNotes.EXPECT().
146 GetDraftNote("OWNER/REPO", int64(1), int64(501), gomock.Any()).
147 Return(&gitlab.DraftNote{ID: 501, Note: "Pending draft"}, nil, nil)
150func setupMR(t *testing.T) *gitlabtesting.TestClient {
151 t.Helper()
152 tc := gitlabtesting.NewTestClient(t)
153 tc.MockMergeRequests.EXPECT().
154 GetMergeRequest("OWNER/REPO", int64(1), gomock.Any()).
155 Return(&gitlab.MergeRequest{BasicMergeRequest: gitlab.BasicMergeRequest{ID: 1, IID: 1}}, nil, nil)
156 return tc
159// mockBranchMR resolves branch "feature" to merge request !1.
160func mockBranchMR(t *testing.T, tc *gitlabtesting.TestClient) {
161 t.Helper()
162 tc.MockMergeRequests.EXPECT().
163 ListProjectMergeRequests("OWNER/REPO", gomock.Any()).
164 DoAndReturn(func(_ any, opts *gitlab.ListProjectMergeRequestsOptions, _ ...gitlab.RequestOptionFunc) ([]*gitlab.BasicMergeRequest, *gitlab.Response, error) {
165 assert.Equal(t, "feature", *opts.SourceBranch)
166 return []*gitlab.BasicMergeRequest{{ID: 1, IID: 1}}, nil, nil
167 })
170func setupExec(t *testing.T, tc *gitlabtesting.TestClient, isTTY bool, opts ...cmdtest.FactoryOption) cmdtest.CmdExecFunc {
171 t.Helper()
172 opts = append([]cmdtest.FactoryOption{
173 cmdtest.WithGitLabClient(tc.Client),
174 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
175 }, opts...)
176 return cmdtest.SetupCmdForTest(t, NewCmd, isTTY, opts...)
Diagnostics10 git calls · 206 ms
CallStatusSizeTime0206 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
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=3
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 · 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: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 · 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
fetchtree:0200259 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=259
git.response.cache=HIT
git.response.duration_ms=3
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:0200139 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=139
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=ab90e01a9f4f08a6b0a9b69db49354f9dee75a09
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=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=ba2164c4d3f529421e2b90f2acfcb1ed0991729c
Open pack map
fetch2001.5 KiB190 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=190
git.response.bytes=1581
git.response.cache=MISS
git.response.duration_ms=190
git.fetch.wants=98b130feb7e705e6245ba87956f3bf6422ff93f7
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=1581
git.pkt.payload.max=1554
git.sideband.band1.bytes=1554
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
a5e2c440f917aedd
Request
2e4464b8…
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