GitLabgitlab-org
cli
main
1//go:build !integration
3package issues
5import (
6 "net/http"
7 "regexp"
8 "strings"
9 "testing"
10 "time"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "go.uber.org/mock/gomock"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20)
22func TestMergeRequestClosesIssues_byID(t *testing.T) {
23 // NOTE: we need to force disable colors, otherwise we'd need ANSI sequences in our test output assertions.
24 t.Setenv("NO_COLOR", "true")
26 createdAt, _ := time.Parse(time.RFC3339, "2020-09-05T01:17:17.270Z")
28 // GIVEN
29 testClient := gitlabtesting.NewTestClient(t)
31 testClient.MockMergeRequests.EXPECT().
32 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
33 Return(&gitlab.MergeRequest{
34 BasicMergeRequest: gitlab.BasicMergeRequest{
35 ID: 123,
36 IID: 123,
37 WebURL: "https://gitlab.com/OWNER/REPO/merge_requests/123",
38 },
39 }, nil, nil)
41 testClient.MockMergeRequests.EXPECT().
42 GetIssuesClosedOnMerge("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
43 Return([]*gitlab.Issue{
44 {
45 ID: 123,
46 IID: 11,
47 ProjectID: 1,
48 Title: "new issue",
49 State: "opened",
50 CreatedAt: &createdAt,
51 Labels: gitlab.Labels{},
52 },
53 {
54 ID: 123,
55 IID: 15,
56 ProjectID: 1,
57 Title: "this is another new issue",
58 State: "opened",
59 CreatedAt: &createdAt,
60 Labels: gitlab.Labels{},
61 },
62 }, &gitlab.Response{Response: &http.Response{StatusCode: http.StatusOK}}, nil)
64 exec := cmdtest.SetupCmdForTest(
65 t,
66 NewCmdIssues,
67 true,
68 cmdtest.WithGitLabClient(testClient.Client),
69 )
71 // WHEN
72 output, err := exec("123")
74 // THEN
75 require.NoError(t, err)
77 out := output.String()
78 timeRE := regexp.MustCompile(`\d+ years`)
79 out = timeRE.ReplaceAllString(out, "X years")
81 assert.Contains(t, out, "Showing 2 issues in OWNER/REPO that match your search.")
82 assert.Contains(t, out, "#11\tnew issue")
83 assert.Contains(t, out, "#15\tthis is another new issue")
84 assert.Contains(t, out, "about X years ago")
85 assert.Empty(t, output.Stderr())
86}
88func TestMergeRequestClosesIssues_currentBranch(t *testing.T) {
89 // NOTE: we need to force disable colors, otherwise we'd need ANSI sequences in our test output assertions.
90 t.Setenv("NO_COLOR", "true")
92 createdAt, _ := time.Parse(time.RFC3339, "2020-09-05T01:17:17.270Z")
94 // GIVEN
95 testClient := gitlabtesting.NewTestClient(t)
97 testClient.MockMergeRequests.EXPECT().
98 ListProjectMergeRequests("OWNER/REPO", gomock.Any(), gomock.Any()).
99 Return([]*gitlab.BasicMergeRequest{
100 {
101 ID: 123,
102 IID: 123,
103 ProjectID: 1,
104 WebURL: "https://gitlab.com/OWNER/REPO/merge_requests/123",
105 },
106 }, &gitlab.Response{Response: &http.Response{StatusCode: http.StatusOK}}, nil)
108 testClient.MockMergeRequests.EXPECT().
109 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
110 Return(&gitlab.MergeRequest{
111 BasicMergeRequest: gitlab.BasicMergeRequest{
112 ID: 123,
113 IID: 123,
114 WebURL: "https://gitlab.com/OWNER/REPO/merge_requests/123",
115 },
116 }, nil, nil)
118 testClient.MockMergeRequests.EXPECT().
119 GetIssuesClosedOnMerge("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
120 Return([]*gitlab.Issue{
121 {
122 ID: 123,
123 IID: 11,
124 ProjectID: 1,
125 Title: "new issue",
126 State: "opened",
127 CreatedAt: &createdAt,
128 Labels: gitlab.Labels{},
129 },
130 {
131 ID: 123,
132 IID: 15,
133 ProjectID: 1,
134 Title: "this is another new issue",
135 State: "opened",
136 CreatedAt: &createdAt,
137 Labels: gitlab.Labels{},
138 },
139 }, &gitlab.Response{Response: &http.Response{StatusCode: http.StatusOK}}, nil)
141 exec := cmdtest.SetupCmdForTest(
142 t,
143 NewCmdIssues,
144 true,
145 cmdtest.WithGitLabClient(testClient.Client),
146 cmdtest.WithBranch("current_branch"),
147 )
149 // WHEN
150 output, err := exec("")
152 // THEN
153 require.NoError(t, err)
155 out := output.String()
156 timeRE := regexp.MustCompile(`\d+ years`)
157 out = timeRE.ReplaceAllString(out, "X years")
159 assert.Contains(t, out, "Showing 2 issues in OWNER/REPO that match your search.")
160 assert.Contains(t, out, "#11\tnew issue")
161 assert.Contains(t, out, "#15\tthis is another new issue")
162 assert.Contains(t, out, "about X years ago")
163 assert.Empty(t, output.Stderr())
164}
166func TestMergeRequestClosesIssues_HeaderTotal(t *testing.T) {
167 t.Parallel()
169 createdAt, _ := time.Parse(time.RFC3339, "2020-09-05T01:17:17.270Z")
170 issues := []*gitlab.Issue{
171 {ID: 123, IID: 11, ProjectID: 1, Title: "new issue", State: "opened", CreatedAt: &createdAt, Labels: gitlab.Labels{}},
172 }
174 tests := []struct {
175 name string
176 resp *gitlab.Response
177 wantHeader string
178 }{
179 {
180 name: "shows the total from the API",
181 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2, TotalPages: 2, TotalItems: 25},
182 wantHeader: "Showing 1 of 25 issues in OWNER/REPO that match your search. ",
183 },
184 {
185 name: "without a total from the API",
186 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2},
187 wantHeader: "Showing 1 issue in OWNER/REPO that match your search. ",
188 },
189 }
191 for _, tt := range tests {
192 t.Run(tt.name, func(t *testing.T) {
193 t.Parallel()
195 testClient := gitlabtesting.NewTestClient(t)
196 testClient.MockMergeRequests.EXPECT().
197 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
198 Return(&gitlab.MergeRequest{BasicMergeRequest: gitlab.BasicMergeRequest{ID: 123, IID: 123}}, nil, nil)
199 testClient.MockMergeRequests.EXPECT().
200 GetIssuesClosedOnMerge("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
201 Return(issues, tt.resp, nil)
203 exec := cmdtest.SetupCmdForTest(t, NewCmdIssues, true, cmdtest.WithGitLabClient(testClient.Client))
205 output, err := exec("123")
206 require.NoError(t, err)
208 header, _, _ := strings.Cut(output.String(), "\n")
209 assert.Equal(t, tt.wantHeader, header)
210 assert.Empty(t, output.Stderr())
211 })
212 }
213}
Diagnostics8 git calls · 209 ms
CallStatusSizeTime0209 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=44Open 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=fb03183b7909876778f740d196d7516811003517Open 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=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200175 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=175 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=2dde6d16f8785f5ed1a30a9b20319c61f42b989f 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=175 git.pkt.payload.max=148 git.sideband.band1.bytes=148 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=148 git.pack.trailer=926d0fd6787ff387794c60151e8082537db3a2eaOpen pack map
fetch2001.4 KiB198 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=198 git.response.bytes=1472 git.response.cache=MISS git.response.duration_ms=198 git.fetch.wants=a5ee4bcf39b30d0d958fa1cfa90aa9f63314c83d 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=1472 git.pkt.payload.max=1445 git.sideband.band1.bytes=1445 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
- a5eafd0bdd8c14c2
- Request
- 5d385af0…
- 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