GitLabgitlab-org

cli

main
ply8 git calls291 ms100 KiB6 cachedprotocol v2
308 lines8.3 KiBRaw
1//go:build !integration
3package mrutils
5import (
6 "net/http"
7 "testing"
8 "time"
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"
18func newTimer() *time.Time {
19 t, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
20 return &t
23func Test_FilterDiscussions_ByState(t *testing.T) {
24 t.Parallel()
25 timer := newTimer()
27 discussions := []*gitlab.Discussion{
28 {
29 ID: "disc1",
30 Notes: []*gitlab.Note{
31 {ID: 1, Resolvable: true, Resolved: true, CreatedAt: timer},
32 {ID: 2, Resolvable: true, Resolved: true, CreatedAt: timer},
33 },
34 },
35 {
36 ID: "disc2",
37 Notes: []*gitlab.Note{
38 {ID: 3, Resolvable: true, Resolved: false, CreatedAt: timer},
39 },
40 },
41 {
42 ID: "disc3",
43 Notes: []*gitlab.Note{
44 {ID: 4, Resolvable: true, Resolved: true, CreatedAt: timer},
45 {ID: 5, Resolvable: true, Resolved: false, CreatedAt: timer},
46 },
47 },
48 {
49 ID: "disc4",
50 Notes: []*gitlab.Note{
51 {ID: 6, Resolvable: false, CreatedAt: timer},
52 },
53 },
54 }
56 tests := []struct {
57 name string
58 state string
59 wantIDs []string
60 }{
61 {"resolved only", "resolved", []string{"disc1"}},
62 {"unresolved only", "unresolved", []string{"disc2", "disc3"}},
63 {"resolvable (all with resolvable notes)", "resolvable", []string{"disc1", "disc2", "disc3"}},
64 {"no filter", "", []string{"disc1", "disc2", "disc3", "disc4"}},
65 }
67 for _, tt := range tests {
68 t.Run(tt.name, func(t *testing.T) {
69 t.Parallel()
70 got := FilterDiscussions(discussions, FilterOpts{State: tt.state})
71 gotIDs := make([]string, len(got))
72 for i, d := range got {
73 gotIDs[i] = d.ID
74 }
75 assert.Equal(t, tt.wantIDs, gotIDs)
76 })
77 }
80func Test_FilterDiscussions_ByType(t *testing.T) {
81 t.Parallel()
82 timer := newTimer()
84 discussions := []*gitlab.Discussion{
85 {
86 ID: "general",
87 Notes: []*gitlab.Note{
88 {ID: 1, System: false, Position: nil, CreatedAt: timer},
89 },
90 },
91 {
92 ID: "diff",
93 Notes: []*gitlab.Note{
94 {ID: 2, System: false, Position: &gitlab.NotePosition{NewPath: "file.go", NewLine: 10}, CreatedAt: timer},
95 },
96 },
97 {
98 ID: "system",
99 Notes: []*gitlab.Note{
100 {ID: 3, System: true, CreatedAt: timer},
101 },
102 },
103 }
105 tests := []struct {
106 name string
107 typ string
108 wantIDs []string
109 }{
110 {"general", "general", []string{"general"}},
111 {"diff", "diff", []string{"diff"}},
112 {"system", "system", []string{"system"}},
113 {"all", "", []string{"general", "diff", "system"}},
114 }
116 for _, tt := range tests {
117 t.Run(tt.name, func(t *testing.T) {
118 t.Parallel()
119 got := FilterDiscussions(discussions, FilterOpts{Type: tt.typ})
120 gotIDs := make([]string, len(got))
121 for i, d := range got {
122 gotIDs[i] = d.ID
123 }
124 assert.Equal(t, tt.wantIDs, gotIDs)
125 })
126 }
129func Test_FilterDiscussions_ByFilePath(t *testing.T) {
130 t.Parallel()
131 timer := newTimer()
133 discussions := []*gitlab.Discussion{
134 {
135 ID: "on-file-a",
136 Notes: []*gitlab.Note{
137 {ID: 1, Position: &gitlab.NotePosition{NewPath: "a.go", NewLine: 10}, CreatedAt: timer},
138 },
139 },
140 {
141 ID: "on-file-b",
142 Notes: []*gitlab.Note{
143 {ID: 2, Position: &gitlab.NotePosition{NewPath: "b.go", NewLine: 5}, CreatedAt: timer},
144 },
145 },
146 {
147 ID: "general",
148 Notes: []*gitlab.Note{
149 {ID: 3, Position: nil, CreatedAt: timer},
150 },
151 },
152 {
153 ID: "old-path-match",
154 Notes: []*gitlab.Note{
155 {ID: 4, Position: &gitlab.NotePosition{OldPath: "a.go", OldLine: 3}, CreatedAt: timer},
156 },
157 },
158 }
160 got := FilterDiscussions(discussions, FilterOpts{FilePath: "a.go"})
161 require.Len(t, got, 2)
162 assert.Equal(t, "on-file-a", got[0].ID)
163 assert.Equal(t, "old-path-match", got[1].ID)
166func Test_FilterDiscussions_Combined(t *testing.T) {
167 t.Parallel()
168 timer := newTimer()
170 discussions := []*gitlab.Discussion{
171 {
172 ID: "resolved-diff-a",
173 Notes: []*gitlab.Note{
174 {ID: 1, Resolvable: true, Resolved: true, Position: &gitlab.NotePosition{NewPath: "a.go", NewLine: 10}, CreatedAt: timer},
175 },
176 },
177 {
178 ID: "unresolved-diff-a",
179 Notes: []*gitlab.Note{
180 {ID: 2, Resolvable: true, Resolved: false, Position: &gitlab.NotePosition{NewPath: "a.go", NewLine: 20}, CreatedAt: timer},
181 },
182 },
183 {
184 ID: "unresolved-diff-b",
185 Notes: []*gitlab.Note{
186 {ID: 3, Resolvable: true, Resolved: false, Position: &gitlab.NotePosition{NewPath: "b.go", NewLine: 5}, CreatedAt: timer},
187 },
188 },
189 }
191 got := FilterDiscussions(discussions, FilterOpts{
192 State: "unresolved",
193 Type: "diff",
194 FilePath: "a.go",
195 })
196 require.Len(t, got, 1)
197 assert.Equal(t, "unresolved-diff-a", got[0].ID)
200func Test_FilterDiscussions_EmptyNotes(t *testing.T) {
201 t.Parallel()
202 discussions := []*gitlab.Discussion{
203 {ID: "empty", Notes: []*gitlab.Note{}},
204 }
206 got := FilterDiscussions(discussions, FilterOpts{State: "resolved"})
207 assert.Empty(t, got)
210func Test_FindNoteInDiscussionPaginated(t *testing.T) {
211 t.Parallel()
213 lastPageResponse := &gitlab.Response{
214 Response: &http.Response{StatusCode: http.StatusOK},
215 }
216 nextPageResponse := &gitlab.Response{
217 Response: &http.Response{StatusCode: http.StatusOK},
218 NextPage: 2,
219 }
221 t.Run("match on first page stops pagination", func(t *testing.T) {
222 t.Parallel()
224 testClient := gitlabtesting.NewTestClient(t)
226 // Only one call expected: no second page should ever be requested.
227 testClient.MockDiscussions.EXPECT().
228 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
229 Return([]*gitlab.Discussion{
230 {
231 ID: "disc1",
232 Notes: []*gitlab.Note{{ID: 100}},
233 },
234 }, nextPageResponse, nil)
236 discussionID, note, err := FindNoteInDiscussionPaginated(t.Context(), testClient.Client, "OWNER/REPO", int64(1), 100)
237 require.NoError(t, err)
238 assert.Equal(t, "disc1", discussionID)
239 require.NotNil(t, note)
240 assert.Equal(t, int64(100), note.ID)
241 })
243 t.Run("match on a later page", func(t *testing.T) {
244 t.Parallel()
246 testClient := gitlabtesting.NewTestClient(t)
248 // First page: no match, has a next page.
249 testClient.MockDiscussions.EXPECT().
250 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
251 Return([]*gitlab.Discussion{
252 {ID: "disc1", Notes: []*gitlab.Note{{ID: 100}}},
253 }, nextPageResponse, nil)
255 // Second page: match, last page.
256 testClient.MockDiscussions.EXPECT().
257 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
258 Return([]*gitlab.Discussion{
259 {ID: "disc2", Notes: []*gitlab.Note{{ID: 200}}},
260 }, lastPageResponse, nil)
262 discussionID, note, err := FindNoteInDiscussionPaginated(t.Context(), testClient.Client, "OWNER/REPO", int64(1), 200)
263 require.NoError(t, err)
264 assert.Equal(t, "disc2", discussionID)
265 require.NotNil(t, note)
266 assert.Equal(t, int64(200), note.ID)
267 })
269 t.Run("no match across multiple pages walks all pages and returns error", func(t *testing.T) {
270 t.Parallel()
272 testClient := gitlabtesting.NewTestClient(t)
274 testClient.MockDiscussions.EXPECT().
275 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
276 Return([]*gitlab.Discussion{
277 {ID: "disc1", Notes: []*gitlab.Note{{ID: 100}}},
278 }, nextPageResponse, nil)
280 testClient.MockDiscussions.EXPECT().
281 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
282 Return([]*gitlab.Discussion{
283 {ID: "disc2", Notes: []*gitlab.Note{{ID: 200}}},
284 }, lastPageResponse, nil)
286 discussionID, note, err := FindNoteInDiscussionPaginated(t.Context(), testClient.Client, "OWNER/REPO", int64(1), 999)
287 require.ErrorIs(t, err, ErrNoteNotFound)
288 assert.Contains(t, err.Error(), "999")
289 assert.Empty(t, discussionID)
290 assert.Nil(t, note)
291 })
293 t.Run("API error on a page is propagated and is not ErrNoteNotFound", func(t *testing.T) {
294 t.Parallel()
296 testClient := gitlabtesting.NewTestClient(t)
298 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
299 testClient.MockDiscussions.EXPECT().
300 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
301 Return(nil, forbiddenResponse, assert.AnError)
303 _, _, err := FindNoteInDiscussionPaginated(t.Context(), testClient.Client, "OWNER/REPO", int64(1), 100)
304 require.Error(t, err)
305 assert.Contains(t, err.Error(), "fetch discussions")
306 assert.NotErrorIs(t, err, ErrNoteNotFound, "a real API failure must not be reported as ErrNoteNotFound")
307 })
Diagnostics8 git calls · 291 ms
CallStatusSizeTime0291 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=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 · 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:0200469 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=469
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=6020159a68b7314d330ee4ac0f7529b651d7accc
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=469
git.pkt.payload.max=442
git.sideband.band1.bytes=442
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=442
git.pack.trailer=97b1fd715810f2e169ed93a75830c9ee2eff1fc0
Open pack map
fetch2001.7 KiB278 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=278
git.response.bytes=1786
git.response.cache=MISS
git.response.duration_ms=278
git.fetch.wants=2213b6c0f9c6c727568102c6f234c0814f4d4989
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=1786
git.pkt.payload.max=1759
git.sideband.band1.bytes=1759
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
a5e2d2662f53cf3a
Request
d10c3434…
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