GitLabgitlab-org
cli
main
1//go:build !integration
3package view
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "go.uber.org/mock/gomock"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
15 "gitlab.com/gitlab-org/cli/internal/glinstance"
16 "gitlab.com/gitlab-org/cli/internal/glrepo"
17)
19func TestGetProjectBoardIssuesPaginates(t *testing.T) {
20 t.Parallel()
22 testClient := gitlabtesting.NewTestClient(t)
23 call := 0
24 testClient.MockIssues.EXPECT().
25 ListProjectIssues("OWNER/REPO", gomock.Any(), gomock.Any()).
26 Times(2).
27 DoAndReturn(func(_ any, _ *gitlab.ListProjectIssuesOptions, _ ...gitlab.RequestOptionFunc) ([]*gitlab.Issue, *gitlab.Response, error) {
28 call++
29 if call == 1 {
30 return []*gitlab.Issue{{IID: 1}}, &gitlab.Response{NextPage: 2}, nil
31 }
32 return []*gitlab.Issue{{IID: 2}}, &gitlab.Response{}, nil
33 })
35 issues, err := getProjectBoardIssues(
36 testClient.Client,
37 glrepo.New("OWNER", "REPO", glinstance.DefaultHostname),
38 &issueBoardViewOptions{paginate: true},
39 )
41 require.NoError(t, err)
42 require.Len(t, issues, 2)
43 assert.Equal(t, int64(1), issues[0].IID)
44 assert.Equal(t, int64(2), issues[1].IID)
45}
47func TestGetGroupBoardIssuesPaginates(t *testing.T) {
48 t.Parallel()
50 testClient := gitlabtesting.NewTestClient(t)
51 call := 0
52 testClient.MockIssues.EXPECT().
53 ListGroupIssues(int64(42), gomock.Any(), gomock.Any()).
54 Times(2).
55 DoAndReturn(func(_ any, _ *gitlab.ListGroupIssuesOptions, _ ...gitlab.RequestOptionFunc) ([]*gitlab.Issue, *gitlab.Response, error) {
56 call++
57 if call == 1 {
58 return []*gitlab.Issue{{IID: 1}}, &gitlab.Response{NextPage: 2}, nil
59 }
60 return []*gitlab.Issue{{IID: 2}}, &gitlab.Response{}, nil
61 })
63 issues, err := getGroupBoardIssues(testClient.Client, 42, &issueBoardViewOptions{paginate: true})
65 require.NoError(t, err)
66 require.Len(t, issues, 2)
67 assert.Equal(t, int64(1), issues[0].IID)
68 assert.Equal(t, int64(2), issues[1].IID)
69}
71func TestGetProjectBoardIssuesUsesSinglePageByDefault(t *testing.T) {
72 t.Parallel()
74 testClient := gitlabtesting.NewTestClient(t)
75 testClient.MockIssues.EXPECT().
76 ListProjectIssues("OWNER/REPO", gomock.Any()).
77 Return([]*gitlab.Issue{{IID: 1}}, &gitlab.Response{NextPage: 2}, nil)
79 issues, err := getProjectBoardIssues(
80 testClient.Client,
81 glrepo.New("OWNER", "REPO", glinstance.DefaultHostname),
82 &issueBoardViewOptions{},
83 )
85 require.NoError(t, err)
86 require.Len(t, issues, 1)
87 assert.Equal(t, int64(1), issues[0].IID)
88}
90func Test_issueBoardViewOptions_getListProjectIssueOptions(t *testing.T) {
91 withLabelDetails := true
92 labels := []string{"a", "b", "c"}
93 milestone := "milestone"
94 user := "user"
95 state := "open"
96 type fields struct {
97 assignee string
98 labels []string
99 milestone string
100 state string
101 }
102 tests := []struct {
103 name string
104 fields fields
105 want *gitlab.ListProjectIssuesOptions
106 }{
107 {
108 name: "return default values when passed empty options",
109 fields: fields{},
110 want: &gitlab.ListProjectIssuesOptions{
111 WithLabelDetails: &withLabelDetails,
112 },
113 },
114 {
115 name: "return corresponding values when passing options",
116 fields: fields{
117 assignee: user,
118 labels: labels,
119 milestone: milestone,
120 state: state,
121 },
122 want: &gitlab.ListProjectIssuesOptions{
123 WithLabelDetails: &withLabelDetails,
124 AssigneeUsername: &user,
125 Milestone: &milestone,
126 Labels: &gitlab.LabelOptions{"a", "b", "c"},
127 State: &state,
128 },
129 },
130 }
131 for _, tt := range tests {
132 t.Run(tt.name, func(t *testing.T) {
133 opts := &issueBoardViewOptions{
134 assignee: tt.fields.assignee,
135 labels: tt.fields.labels,
136 milestone: tt.fields.milestone,
137 state: tt.fields.state,
138 }
139 got := opts.getListProjectIssueOptions()
140 assert.Equal(t, tt.want, got)
141 })
142 }
143}
145func Test_issueBoardViewOptions_getListGroupIssueOptions(t *testing.T) {
146 withLabelDetails := true
147 labels := []string{"a", "b", "c"}
148 milestone := "milestone"
149 user := "user"
150 state := "open"
151 type fields struct {
152 assignee string
153 labels []string
154 milestone string
155 state string
156 }
157 tests := []struct {
158 name string
159 fields fields
160 want *gitlab.ListGroupIssuesOptions
161 }{
162 {
163 name: "return default values when passed empty options",
164 fields: fields{},
165 want: &gitlab.ListGroupIssuesOptions{
166 WithLabelDetails: &withLabelDetails,
167 },
168 },
169 {
170 name: "return corresponding values when passing options",
171 fields: fields{
172 assignee: user,
173 labels: labels,
174 milestone: milestone,
175 state: state,
176 },
177 want: &gitlab.ListGroupIssuesOptions{
178 WithLabelDetails: &withLabelDetails,
179 AssigneeUsername: &user,
180 Milestone: &milestone,
181 Labels: &gitlab.LabelOptions{"a", "b", "c"},
182 State: &state,
183 },
184 },
185 }
186 for _, tt := range tests {
187 t.Run(tt.name, func(t *testing.T) {
188 opts := &issueBoardViewOptions{
189 assignee: tt.fields.assignee,
190 labels: tt.fields.labels,
191 milestone: tt.fields.milestone,
192 state: tt.fields.state,
193 }
194 got := opts.getListGroupIssueOptions()
195 assert.Equal(t, tt.want, got)
196 })
197 }
198}
200func Test_buildLabelString(t *testing.T) {
201 type args struct {
202 labelDetails []*gitlab.LabelDetails
203 }
204 tests := []struct {
205 name string
206 args args
207 want string
208 }{
209 {
210 name: "return empty string if no labeldetails are passed",
211 args: args{[]*gitlab.LabelDetails{}},
212 want: "",
213 },
214 {
215 name: "return formatted string when labeldetails are passed",
216 args: args{[]*gitlab.LabelDetails{
217 {
218 Color: "blue",
219 Name: "cold",
220 },
221 {
222 Color: "red",
223 Name: "hot",
224 },
225 }},
226 want: "[white:blue:-]cold[white:-:-] [white:red:-]hot[white:-:-]\n",
227 },
228 }
229 for _, tt := range tests {
230 t.Run(tt.name, func(t *testing.T) {
231 got := buildLabelString(tt.args.labelDetails)
232 assert.Equal(t, tt.want, got)
233 })
234 }
235}
237func Test_mapBoardData(t *testing.T) {
238 type args struct {
239 projectIssueBoards []*gitlab.IssueBoard
240 projectGroupIssueBoards []*gitlab.GroupIssueBoard
241 }
242 type result struct {
243 menuOptions []string
244 boardMetaMap map[string]boardMeta
245 }
246 tests := []struct {
247 name string
248 args args
249 want result
250 }{
251 {
252 name: "return empty map on empty inputs",
253 args: args{
254 projectIssueBoards: []*gitlab.IssueBoard{},
255 projectGroupIssueBoards: []*gitlab.GroupIssueBoard{},
256 },
257 want: result{
258 menuOptions: []string{},
259 boardMetaMap: map[string]boardMeta{},
260 },
261 },
262 {
263 name: "return metadata map with input values",
264 args: args{
265 projectIssueBoards: []*gitlab.IssueBoard{
266 {
267 Name: "projectBoard",
268 Project: &gitlab.Project{Name: "project"},
269 ID: 1,
270 },
271 },
272 projectGroupIssueBoards: []*gitlab.GroupIssueBoard{
273 {
274 Name: "groupBoard",
275 ID: 2,
276 Group: &gitlab.Group{Name: "group"},
277 },
278 },
279 },
280 want: result{
281 menuOptions: []string{
282 "groupBoard (GROUP: group)",
283 "projectBoard (PROJECT: project)",
284 },
285 boardMetaMap: map[string]boardMeta{
286 "projectBoard (PROJECT: project)": {
287 id: 1,
288 name: "projectBoard",
289 project: &gitlab.Project{Name: "project"},
290 },
291 "groupBoard (GROUP: group)": {
292 id: 2,
293 name: "groupBoard",
294 group: &gitlab.Group{Name: "group"},
295 },
296 },
297 },
298 },
299 }
300 for _, tt := range tests {
301 t.Run(tt.name, func(t *testing.T) {
302 menuOptions, boardMetaMap := mapBoardData(
303 tt.args.projectIssueBoards,
304 tt.args.projectGroupIssueBoards,
305 )
306 assert.Equal(t, tt.want.menuOptions, menuOptions)
307 assert.Equal(t, tt.want.boardMetaMap, boardMetaMap)
308 })
309 }
310}
312func Test_filterIssues(t *testing.T) {
313 type args struct {
314 boardLists []*gitlab.BoardList
315 issues []*gitlab.Issue
316 targetList *gitlab.BoardList
317 state string
318 }
319 tests := []struct {
320 name string
321 args args
322 want string
323 }{
324 {
325 name: "return empty string on no matches",
326 want: "",
327 },
328 {
329 name: "filter out closed issues when targetList is not the 'closed' list",
330 args: args{
331 boardLists: []*gitlab.BoardList{{Label: &gitlab.Label{Name: "A"}}},
332 issues: []*gitlab.Issue{{Labels: []string{"A"}, State: "closed"}},
333 targetList: &gitlab.BoardList{Label: &gitlab.Label{Name: "A"}},
334 },
335 want: "",
336 },
337 {
338 name: "filter out issues not in the 'closed' state when populating the 'closed' list",
339 args: args{
340 boardLists: []*gitlab.BoardList{
341 {Label: &gitlab.Label{Name: "Closed"}},
342 {Label: &gitlab.Label{Name: "A"}},
343 },
344 issues: []*gitlab.Issue{{Labels: []string{"A"}, State: "opened"}},
345 targetList: &gitlab.BoardList{Label: &gitlab.Label{Name: "Closed"}},
346 state: closed,
347 },
348 want: "",
349 },
350 {
351 name: "filter out issues labeled for other board lists when iterating over the 'open' list",
352 args: args{
353 boardLists: []*gitlab.BoardList{
354 {Label: &gitlab.Label{Name: "Open"}},
355 {Label: &gitlab.Label{Name: "A"}},
356 },
357 issues: []*gitlab.Issue{{Labels: []string{"A"}, State: "opened"}},
358 targetList: &gitlab.BoardList{Label: &gitlab.Label{Name: "Open"}},
359 state: opened,
360 },
361 want: "",
362 },
363 {
364 name: "return formatted string on successful filter and match",
365 args: args{
366 boardLists: []*gitlab.BoardList{
367 {Label: &gitlab.Label{Name: "A"}},
368 {Label: &gitlab.Label{Name: "B"}},
369 {Label: &gitlab.Label{Name: "C"}},
370 },
371 issues: []*gitlab.Issue{
372 {
373 Assignees: []*gitlab.IssueAssignee{{Username: "user"}},
374 Labels: []string{"A"},
375 LabelDetails: []*gitlab.LabelDetails{{Name: "A", Color: "green"}},
376 Title: "Issue",
377 IID: 1,
378 },
379 },
380 targetList: &gitlab.BoardList{Label: &gitlab.Label{Name: "A"}},
381 },
382 want: "[white::b]Issue\n[white:green:-]A[white:-:-]\n[green:-:-]#1[darkgray] - user\n\n",
383 },
384 }
385 for _, tt := range tests {
386 t.Run(tt.name, func(t *testing.T) {
387 got := filterIssues(
388 tt.args.boardLists,
389 tt.args.issues,
390 tt.args.targetList,
391 tt.args.state,
392 )
393 assert.Equal(t, tt.want, got)
394 })
395 }
396}
Diagnostics9 git calls · 210 ms
CallStatusSizeTime0210 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 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=44Open pack map
fetchtree:02001.8 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=1 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200492 B · hit5 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=492 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=409e7038e253494f94691a22a250deee77ba03bf 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=492 git.pkt.payload.max=465 git.sideband.band1.bytes=465 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=465 git.pack.trailer=c8a67aa26d573734cf0c8f302f8c1dd08e47d76aOpen pack map
fetchtree:0200199 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=199 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=a1f66b0e40117fa2bfdc8be6d6391be5a122ec04 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=199 git.pkt.payload.max=172 git.sideband.band1.bytes=172 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=172 git.pack.trailer=58a90817ea69bb3446d8835c6f1c2a46a892fda4Open pack map
fetchtree:0200148 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=192 git.response.bytes=148 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=24f9ee986f260d4571e81224e5c1f58f30237166 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=148 git.pkt.payload.max=121 git.sideband.band1.bytes=121 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=121 git.pack.trailer=65a7e0f1eb69e59888c2fc382c23e9573dda1da4Open pack map
fetch2002.1 KiB192 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2121 git.response.cache=MISS git.response.duration_ms=192 git.fetch.wants=16a945c041ccdd918a79d1f84ad2bc7636545c45 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=2121 git.pkt.payload.max=2094 git.sideband.band1.bytes=2094 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
- a5e2ed4c4d5977be
- Request
- 149004b1…
- 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