GitLabgitlab-org
cli
main
1//go:build !integration
3package subscribe
5import (
6 "fmt"
7 "net/http"
8 "testing"
9 "time"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/spf13/cobra"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "go.uber.org/mock/gomock"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
20 "gitlab.com/gitlab-org/cli/internal/cmdutils"
21 "gitlab.com/gitlab-org/cli/internal/commands/issuable"
22 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
23)
25func Test_IssuableSubscribe(t *testing.T) {
26 // NOTE: we need to force disable colors, otherwise we'd need ANSI sequences in our test output assertions.
27 t.Setenv("NO_COLOR", "true")
29 type testCase struct {
30 name string
31 iid int
32 issueType issuable.IssueType
33 wantOutput string
34 wantErr bool
35 setupMock func(tc *gitlabtesting.TestClient)
36 }
38 createdAt, _ := time.Parse(time.RFC3339, "2023-05-02T10:51:26.371Z")
40 testCases := []testCase{
41 {
42 name: "issue_subscribe",
43 iid: 1,
44 issueType: issuable.TypeIssue,
45 wantOutput: heredoc.Doc(`
46 - Subscribing to issue #1 in OWNER/REPO
47 ✓ Subscribed
48 `),
49 setupMock: func(tc *gitlabtesting.TestClient) {
50 tc.MockIssues.EXPECT().
51 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
52 Return(&gitlab.Issue{
53 ID: 1,
54 IID: 1,
55 Title: "test issue",
56 Subscribed: false,
57 IssueType: new("issue"),
58 CreatedAt: &createdAt,
59 }, nil, nil)
60 tc.MockIssues.EXPECT().
61 SubscribeToIssue("OWNER/REPO", int64(1), gomock.Any()).
62 Return(&gitlab.Issue{
63 ID: 1,
64 IID: 1,
65 Subscribed: true,
66 IssueType: new("issue"),
67 CreatedAt: &createdAt,
68 }, nil, nil)
69 },
70 },
71 {
72 name: "incident_subscribe",
73 iid: 2,
74 issueType: issuable.TypeIncident,
75 wantOutput: heredoc.Doc(`
76 - Subscribing to incident #2 in OWNER/REPO
77 ✓ Subscribed
78 `),
79 setupMock: func(tc *gitlabtesting.TestClient) {
80 tc.MockIssues.EXPECT().
81 GetIssue("OWNER/REPO", int64(2), gomock.Any()).
82 Return(&gitlab.Issue{
83 ID: 2,
84 IID: 2,
85 Title: "test incident",
86 Subscribed: false,
87 IssueType: new("incident"),
88 CreatedAt: &createdAt,
89 }, nil, nil)
90 tc.MockIssues.EXPECT().
91 SubscribeToIssue("OWNER/REPO", int64(2), gomock.Any()).
92 Return(&gitlab.Issue{
93 ID: 2,
94 IID: 2,
95 Subscribed: true,
96 IssueType: new("incident"),
97 CreatedAt: &createdAt,
98 }, nil, nil)
99 },
100 },
101 {
102 name: "incident_subscribe_using_issue_command",
103 iid: 2,
104 issueType: issuable.TypeIssue,
105 wantOutput: heredoc.Doc(`
106 - Subscribing to issue #2 in OWNER/REPO
107 ✓ Subscribed
108 `),
109 setupMock: func(tc *gitlabtesting.TestClient) {
110 tc.MockIssues.EXPECT().
111 GetIssue("OWNER/REPO", int64(2), gomock.Any()).
112 Return(&gitlab.Issue{
113 ID: 2,
114 IID: 2,
115 Title: "test incident",
116 Subscribed: false,
117 IssueType: new("incident"),
118 CreatedAt: &createdAt,
119 }, nil, nil)
120 tc.MockIssues.EXPECT().
121 SubscribeToIssue("OWNER/REPO", int64(2), gomock.Any()).
122 Return(&gitlab.Issue{
123 ID: 2,
124 IID: 2,
125 Subscribed: true,
126 IssueType: new("incident"),
127 CreatedAt: &createdAt,
128 }, nil, nil)
129 },
130 },
131 {
132 name: "issue_subscribe_using_incident_command",
133 iid: 1,
134 issueType: issuable.TypeIncident,
135 wantOutput: "Incident not found, but an issue with the provided ID exists. Run `glab issue subscribe <id>` to subscribe.\n",
136 setupMock: func(tc *gitlabtesting.TestClient) {
137 tc.MockIssues.EXPECT().
138 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
139 Return(&gitlab.Issue{
140 ID: 1,
141 IID: 1,
142 Title: "test issue",
143 Subscribed: false,
144 IssueType: new("issue"),
145 CreatedAt: &createdAt,
146 }, nil, nil)
147 },
148 },
149 {
150 name: "issue_subscribe_to_already_subscribed_issue",
151 iid: 3,
152 issueType: issuable.TypeIssue,
153 wantOutput: heredoc.Doc(`
154 - Subscribing to issue #3 in OWNER/REPO
155 x You are already subscribed to this issue.
156 `),
157 setupMock: func(tc *gitlabtesting.TestClient) {
158 tc.MockIssues.EXPECT().
159 GetIssue("OWNER/REPO", int64(3), gomock.Any()).
160 Return(&gitlab.Issue{
161 ID: 3,
162 IID: 3,
163 Title: "test issue",
164 Subscribed: true,
165 IssueType: new("issue"),
166 CreatedAt: &createdAt,
167 }, nil, nil)
168 notModifiedResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusNotModified}}
169 tc.MockIssues.EXPECT().
170 SubscribeToIssue("OWNER/REPO", int64(3), gomock.Any()).
171 Return(nil, notModifiedResponse, fmt.Errorf("304 Not Modified"))
172 },
173 },
174 {
175 name: "incident_subscribe_to_already_subscribed_incident",
176 iid: 3,
177 issueType: issuable.TypeIncident,
178 wantOutput: heredoc.Doc(`
179 - Subscribing to incident #3 in OWNER/REPO
180 x You are already subscribed to this incident.
181 `),
182 setupMock: func(tc *gitlabtesting.TestClient) {
183 tc.MockIssues.EXPECT().
184 GetIssue("OWNER/REPO", int64(3), gomock.Any()).
185 Return(&gitlab.Issue{
186 ID: 3,
187 IID: 3,
188 Title: "test incident",
189 Subscribed: true,
190 IssueType: new("incident"),
191 CreatedAt: &createdAt,
192 }, nil, nil)
193 notModifiedResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusNotModified}}
194 tc.MockIssues.EXPECT().
195 SubscribeToIssue("OWNER/REPO", int64(3), gomock.Any()).
196 Return(nil, notModifiedResponse, fmt.Errorf("304 Not Modified"))
197 },
198 },
199 {
200 name: "issue_not_found",
201 iid: 404,
202 issueType: issuable.TypeIssue,
203 wantOutput: "404 Not Found",
204 wantErr: true,
205 setupMock: func(tc *gitlabtesting.TestClient) {
206 notFoundResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}
207 tc.MockIssues.EXPECT().
208 GetIssue("OWNER/REPO", int64(404), gomock.Any()).
209 Return(nil, notFoundResponse, fmt.Errorf("404 Not Found"))
210 },
211 },
212 }
214 for _, tc := range testCases {
215 t.Run(tc.name, func(t *testing.T) {
216 // GIVEN
217 testClient := gitlabtesting.NewTestClient(t)
218 tc.setupMock(testClient)
220 cmdFunc := func(f cmdutils.Factory) *cobra.Command {
221 return NewCmdSubscribe(f, tc.issueType)
222 }
224 exec := cmdtest.SetupCmdForTest(
225 t,
226 cmdFunc,
227 true, // TTY mode for subscribe output
228 cmdtest.WithGitLabClient(testClient.Client),
229 )
231 // WHEN
232 out, err := exec(fmt.Sprint(tc.iid))
234 // THEN
235 if tc.wantErr {
236 require.Error(t, err)
237 assert.Contains(t, err.Error(), tc.wantOutput)
238 return
239 }
240 require.NoError(t, err)
241 assert.Contains(t, out.String(), tc.wantOutput)
242 assert.Empty(t, out.Stderr())
243 })
244 }
245}
Diagnostics8 git calls · 235 ms
CallStatusSizeTime0235 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
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=3 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 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:02001.8 KiB · 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=1857 git.response.cache=HIT git.response.duration_ms=3 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:0200347 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=347 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=73f6a798386c4a4df64da0b5349881ded38885ca 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=347 git.pkt.payload.max=320 git.sideband.band1.bytes=320 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=320 git.pack.trailer=93ed36ed1884d41429801c50b3bb5d32ef1307a5Open pack map
fetchtree:0200150 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=150 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0d8a4853f153ab6bf1270fa17e8387fd27d9b064 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=150 git.pkt.payload.max=123 git.sideband.band1.bytes=123 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=123 git.pack.trailer=7b43b83d975888b44b41535afcdc630b1fa37956Open pack map
fetch2001.5 KiB221 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=221 git.response.bytes=1553 git.response.cache=MISS git.response.duration_ms=221 git.fetch.wants=e6a91b626048895a72049cc4502611ca78a863cc 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=1553 git.pkt.payload.max=1526 git.sideband.band1.bytes=1526 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
- a5e2f5d0d83b3a24
- Request
- d90c019c…
- 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