GitLabgitlab-org

cli

main
ply10 git calls250 ms100 KiB7 cachedprotocol v2
173 lines5.2 KiBRaw
1//go:build !integration
3package list
5import (
6 "encoding/json"
7 "errors"
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 TestDraftList(t *testing.T) {
21 t.Parallel()
23 t.Run("prints pending comments", func(t *testing.T) {
24 t.Parallel()
26 tc := setupMR(t)
27 mockDrafts(tc,
28 &gitlab.DraftNote{ID: 501, Note: "General draft", Position: &gitlab.NotePosition{}},
29 &gitlab.DraftNote{ID: 502, Note: "Diff draft", Position: &gitlab.NotePosition{NewPath: "main.go", NewLine: 42}},
30 )
32 output, err := setupExec(t, tc)(`1`)
33 require.NoError(t, err)
34 assert.Equal(t, "[draft #501]\n General draft\n\n[draft #502] on main.go:42\n Diff draft\n\n", output.String())
35 })
37 t.Run("--file keeps only pending comments on that file", func(t *testing.T) {
38 t.Parallel()
40 tc := setupMR(t)
41 mockDrafts(tc,
42 &gitlab.DraftNote{ID: 501, Note: "General draft"},
43 &gitlab.DraftNote{ID: 502, Note: "On main", Position: &gitlab.NotePosition{NewPath: "main.go", NewLine: 42}},
44 &gitlab.DraftNote{ID: 503, Note: "On other", Position: &gitlab.NotePosition{NewPath: "other.go", NewLine: 7}},
45 &gitlab.DraftNote{ID: 504, Note: "On removed main", Position: &gitlab.NotePosition{OldPath: "main.go", OldLine: 3}},
46 )
48 output, err := setupExec(t, tc)(`1 --file main.go`)
49 require.NoError(t, err)
50 assert.Equal(t, "[draft #502] on main.go:42\n On main\n\n[draft #504] on main.go:3\n On removed main\n\n", output.String())
51 })
53 t.Run("JSON output returns the pending comment objects", func(t *testing.T) {
54 t.Parallel()
56 tc := setupMR(t)
57 mockDrafts(tc,
58 &gitlab.DraftNote{ID: 501, Note: "General draft"},
59 &gitlab.DraftNote{ID: 502, Note: "Reply", DiscussionID: "abc123"},
60 )
62 output, err := setupExec(t, tc)(`1 -F json`)
63 require.NoError(t, err)
65 var got []struct {
66 ID int64 `json:"id"`
67 Note string `json:"note"`
68 DiscussionID string `json:"discussion_id"`
69 }
70 require.NoError(t, json.Unmarshal([]byte(output.String()), &got))
71 require.Len(t, got, 2)
72 assert.Equal(t, int64(501), got[0].ID)
73 assert.Equal(t, "General draft", got[0].Note)
74 assert.Equal(t, int64(502), got[1].ID)
75 assert.Equal(t, "abc123", got[1].DiscussionID)
76 })
78 t.Run("JSON output honours --file", func(t *testing.T) {
79 t.Parallel()
81 tc := setupMR(t)
82 mockDrafts(tc,
83 &gitlab.DraftNote{ID: 501, Note: "General draft"},
84 &gitlab.DraftNote{ID: 502, Note: "On main", Position: &gitlab.NotePosition{NewPath: "main.go", NewLine: 42}},
85 &gitlab.DraftNote{ID: 503, Note: "On other", Position: &gitlab.NotePosition{NewPath: "other.go", NewLine: 7}},
86 )
88 output, err := setupExec(t, tc)(`1 --file main.go -F json`)
89 require.NoError(t, err)
91 var got []struct {
92 ID int64 `json:"id"`
93 }
94 require.NoError(t, json.Unmarshal([]byte(output.String()), &got))
95 require.Len(t, got, 1)
96 assert.Equal(t, int64(502), got[0].ID)
97 })
99 t.Run("JSON output with no pending comments is an empty array", func(t *testing.T) {
100 t.Parallel()
102 tc := setupMR(t)
103 mockDrafts(tc)
105 output, err := setupExec(t, tc)(`1 -F json`)
106 require.NoError(t, err)
107 assert.JSONEq(t, "[]", output.String())
108 })
110 t.Run("no pending comments", func(t *testing.T) {
111 t.Parallel()
113 tc := setupMR(t)
114 mockDrafts(tc)
116 output, err := setupExec(t, tc)(`1`)
117 require.NoError(t, err)
118 assert.Equal(t, "No pending review comments found.\n", output.String())
119 })
121 t.Run("collects every page", func(t *testing.T) {
122 t.Parallel()
124 tc := setupMR(t)
125 gomock.InOrder(
126 tc.MockDraftNotes.EXPECT().
127 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
128 Return([]*gitlab.DraftNote{{ID: 501, Note: "first"}}, &gitlab.Response{NextPage: 2}, nil),
129 tc.MockDraftNotes.EXPECT().
130 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
131 Return([]*gitlab.DraftNote{{ID: 502, Note: "second"}}, &gitlab.Response{}, nil),
132 )
134 output, err := setupExec(t, tc)(`1`)
135 require.NoError(t, err)
136 assert.Equal(t, "[draft #501]\n first\n\n[draft #502]\n second\n\n", output.String())
137 })
139 t.Run("API error is wrapped", func(t *testing.T) {
140 t.Parallel()
142 tc := setupMR(t)
143 tc.MockDraftNotes.EXPECT().
144 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
145 Return(nil, nil, errors.New("boom"))
147 _, err := setupExec(t, tc)(`1`)
148 require.ErrorContains(t, err, "failed to list pending review comments: boom")
149 })
152func mockDrafts(tc *gitlabtesting.TestClient, drafts ...*gitlab.DraftNote) {
153 tc.MockDraftNotes.EXPECT().
154 ListDraftNotes("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
155 Return(drafts, &gitlab.Response{}, nil)
158func setupMR(t *testing.T) *gitlabtesting.TestClient {
159 t.Helper()
160 tc := gitlabtesting.NewTestClient(t)
161 tc.MockMergeRequests.EXPECT().
162 GetMergeRequest("OWNER/REPO", int64(1), gomock.Any()).
163 Return(&gitlab.MergeRequest{BasicMergeRequest: gitlab.BasicMergeRequest{ID: 1, IID: 1}}, nil, nil)
164 return tc
167func setupExec(t *testing.T, tc *gitlabtesting.TestClient) cmdtest.CmdExecFunc {
168 t.Helper()
169 return cmdtest.SetupCmdForTest(t, NewCmd, false,
170 cmdtest.WithGitLabClient(tc.Client),
171 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
172 )
Diagnostics10 git calls · 250 ms
CallStatusSizeTime0250 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 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 · 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=3
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=2
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=3
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=2
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:0200134 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=233
git.response.bytes=134
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=844f3e4c5e4134cddc834103e7d104ed5eeb3cd0
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=134
git.pkt.payload.max=107
git.sideband.band1.bytes=107
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=107
git.pack.trailer=8e6ed7a1882f58b8548ab7f7f041c414bd2747e5
Open pack map
fetch2001.3 KiB233 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1376
git.response.cache=MISS
git.response.duration_ms=233
git.fetch.wants=ec56ce55126e680e4f04e62ac4111f7e1bb299d5
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=1376
git.pkt.payload.max=1349
git.sideband.band1.bytes=1349
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
a5e2d1b1bed3cf3a
Request
35aff18c…
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