GitLabgitlab-org
cli
main
1//go:build !integration
3package note
5import (
6 "errors"
7 "net/http"
8 "testing"
10 "git.sr.ht/~timofurrer/ugh"
11 "github.com/spf13/cobra"
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/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/commands/issuable"
21 "gitlab.com/gitlab-org/cli/internal/config"
22 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
23)
25func Test_NewCmdNote(t *testing.T) {
26 t.Parallel()
28 commands := []struct {
29 name string
30 issueType issuable.IssueType
31 }{
32 {"issue", issuable.TypeIssue},
33 {"incident", issuable.TypeIncident},
34 }
36 for _, cc := range commands {
37 t.Run(cc.name+"_message_flag_specified", func(t *testing.T) {
38 t.Parallel()
40 testClient := gitlabtesting.NewTestClient(t)
42 // Mock GetIssue
43 testClient.MockIssues.EXPECT().
44 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
45 Return(&gitlab.Issue{
46 ID: 1,
47 IID: 1,
48 IssueType: new(string(cc.issueType)),
49 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
50 }, nil, nil)
52 // Mock CreateIssueNote
53 testClient.MockNotes.EXPECT().
54 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
55 DoAndReturn(func(pid any, issueIID int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
56 assert.Equal(t, "Here is my note", *opts.Body)
57 assert.Nil(t, opts.Internal)
58 return &gitlab.Note{
59 ID: 301,
60 NoteableID: 1,
61 NoteableType: "Issue",
62 NoteableIID: 1,
63 }, nil, nil
64 })
66 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
67 return NewCmdNote(f, cc.issueType)
68 }, true,
69 cmdtest.WithGitLabClient(testClient.Client),
70 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
71 cmdtest.WithConfig(config.NewFromString("editor: vi")),
72 )
74 output, err := exec(`1 --message "Here is my note"`)
75 require.NoError(t, err)
76 assert.Empty(t, output.Stderr())
77 assert.Equal(t, "https://gitlab.com/OWNER/REPO/issues/1#note_301\n", output.String())
78 })
80 t.Run(cc.name+"_internal_flag_specified", func(t *testing.T) {
81 t.Parallel()
83 testClient := gitlabtesting.NewTestClient(t)
85 testClient.MockIssues.EXPECT().
86 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
87 Return(&gitlab.Issue{
88 ID: 1,
89 IID: 1,
90 IssueType: new(string(cc.issueType)),
91 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
92 }, nil, nil)
94 testClient.MockNotes.EXPECT().
95 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
96 DoAndReturn(func(pid any, issueIID int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
97 assert.Equal(t, "Secret note", *opts.Body)
98 require.NotNil(t, opts.Internal)
99 assert.True(t, *opts.Internal)
100 return &gitlab.Note{ID: 302, NoteableID: 1, NoteableType: "Issue", NoteableIID: 1, Internal: true}, nil, nil
101 })
103 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
104 return NewCmdNote(f, cc.issueType)
105 }, true,
106 cmdtest.WithGitLabClient(testClient.Client),
107 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
108 cmdtest.WithConfig(config.NewFromString("editor: vi")),
109 )
111 output, err := exec(`1 --message "Secret note" --internal`)
112 require.NoError(t, err)
113 assert.Empty(t, output.Stderr())
114 assert.Equal(t, "https://gitlab.com/OWNER/REPO/issues/1#note_302\n", output.String())
115 })
117 t.Run(cc.name+"_issue_not_found", func(t *testing.T) {
118 t.Parallel()
120 testClient := gitlabtesting.NewTestClient(t)
122 // Mock GetIssue - returns 404
123 notFoundResp := &gitlab.Response{
124 Response: &http.Response{StatusCode: http.StatusNotFound},
125 }
126 testClient.MockIssues.EXPECT().
127 GetIssue("OWNER/REPO", int64(122), gomock.Any()).
128 Return(nil, notFoundResp, gitlab.ErrNotFound)
130 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
131 return NewCmdNote(f, cc.issueType)
132 }, true,
133 cmdtest.WithGitLabClient(testClient.Client),
134 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
135 cmdtest.WithConfig(config.NewFromString("editor: vi")),
136 )
138 _, err := exec(`122`)
139 require.Error(t, err)
140 assert.Contains(t, err.Error(), "Not Found")
141 })
142 }
143}
145func Test_NewCmdNote_error(t *testing.T) {
146 t.Parallel()
148 commands := []struct {
149 name string
150 issueType issuable.IssueType
151 }{
152 {"issue", issuable.TypeIssue},
153 {"incident", issuable.TypeIncident},
154 }
156 for _, cc := range commands {
157 t.Run(cc.name+"_note_could_not_be_created", func(t *testing.T) {
158 t.Parallel()
160 testClient := gitlabtesting.NewTestClient(t)
162 // Mock GetIssue
163 testClient.MockIssues.EXPECT().
164 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
165 Return(&gitlab.Issue{
166 ID: 1,
167 IID: 1,
168 IssueType: new(string(cc.issueType)),
169 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
170 }, nil, nil)
172 // Mock CreateIssueNote - returns 401
173 unauthorizedResp := &gitlab.Response{
174 Response: &http.Response{StatusCode: http.StatusUnauthorized},
175 }
176 testClient.MockNotes.EXPECT().
177 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
178 Return(nil, unauthorizedResp, errors.New("401 Unauthorized"))
180 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
181 return NewCmdNote(f, cc.issueType)
182 }, true,
183 cmdtest.WithGitLabClient(testClient.Client),
184 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
185 cmdtest.WithConfig(config.NewFromString("editor: vi")),
186 )
188 _, err := exec(`1 -m "Some message"`)
189 require.Error(t, err)
190 })
191 }
193 t.Run("using incident note command with issue ID", func(t *testing.T) {
194 t.Parallel()
196 testClient := gitlabtesting.NewTestClient(t)
198 // Mock GetIssue - returns an issue (not incident)
199 testClient.MockIssues.EXPECT().
200 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
201 Return(&gitlab.Issue{
202 ID: 1,
203 IID: 1,
204 IssueType: new("issue"), // Not an incident
205 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
206 }, nil, nil)
208 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
209 return NewCmdNote(f, issuable.TypeIncident)
210 }, true,
211 cmdtest.WithGitLabClient(testClient.Client),
212 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
213 cmdtest.WithConfig(config.NewFromString("editor: vi")),
214 )
216 output, err := exec(`1 -m "Some message"`)
217 require.NoError(t, err)
218 assert.Equal(t, "Incident not found, but an issue with the provided ID exists. Run `glab issue comment <id>` to comment.\n", output.String())
219 })
220}
222func Test_IssuableNoteCreate_prompt(t *testing.T) {
223 // NOTE: This test cannot run in parallel because the huh form library
224 // uses global state (charmbracelet/bubbles runeutil sanitizer).
225 commands := []struct {
226 name string
227 issueType issuable.IssueType
228 }{
229 {"issue", issuable.TypeIssue},
230 {"incident", issuable.TypeIncident},
231 }
233 for _, cc := range commands {
234 t.Run(cc.name+"_message_provided", func(t *testing.T) {
235 testClient := gitlabtesting.NewTestClient(t)
237 // Mock GetIssue
238 testClient.MockIssues.EXPECT().
239 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
240 Return(&gitlab.Issue{
241 ID: 1,
242 IID: 1,
243 IssueType: new(string(cc.issueType)),
244 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
245 }, nil, nil)
247 // Mock CreateIssueNote
248 testClient.MockNotes.EXPECT().
249 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
250 DoAndReturn(func(pid any, issueIID int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
251 // The prompt adds a trailing newline
252 assert.Contains(t, *opts.Body, "some note message")
253 return &gitlab.Note{
254 ID: 301,
255 NoteableID: 1,
256 NoteableType: "Issue",
257 NoteableIID: 1,
258 }, nil, nil
259 })
261 c := ugh.New(t)
262 c.Expect(ugh.Input("Message:")).
263 Do(ugh.Type("some note message"))
265 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
266 return NewCmdNote(f, cc.issueType)
267 }, true,
268 cmdtest.WithGitLabClient(testClient.Client),
269 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
270 cmdtest.WithConfig(config.NewFromString("editor: vi")),
271 cmdtest.WithConsole(t, c),
272 )
274 output, err := exec(`1`)
275 require.NoError(t, err)
276 assert.Empty(t, output.Stderr())
277 assert.Contains(t, output.String(), "https://gitlab.com/OWNER/REPO/issues/1#note_301")
278 })
280 tests := []struct {
281 name string
282 message string
283 }{
284 {"message is empty", ""},
285 {"message contains only spaces", " "},
286 {"message contains only line breaks", "\n\n"},
287 }
289 for _, tt := range tests {
290 t.Run(cc.name+"_"+tt.name, func(t *testing.T) {
291 testClient := gitlabtesting.NewTestClient(t)
293 // Mock GetIssue
294 testClient.MockIssues.EXPECT().
295 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
296 Return(&gitlab.Issue{
297 ID: 1,
298 IID: 1,
299 IssueType: new(string(cc.issueType)),
300 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
301 }, nil, nil)
303 c := ugh.New(t)
304 c.Expect(ugh.Input("Message:")).
305 Do(ugh.Type(tt.message))
307 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
308 return NewCmdNote(f, cc.issueType)
309 }, true,
310 cmdtest.WithGitLabClient(testClient.Client),
311 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
312 cmdtest.WithConfig(config.NewFromString("editor: vi")),
313 cmdtest.WithConsole(t, c),
314 )
316 _, err := exec(`1`)
317 require.Error(t, err)
318 assert.Equal(t, "aborted: note is empty", err.Error())
319 })
320 }
321 }
322}
324func Test_IssuableNoteCreate_stdin(t *testing.T) {
325 t.Parallel()
327 commands := []struct {
328 name string
329 issueType issuable.IssueType
330 }{
331 {"issue", issuable.TypeIssue},
332 {"incident", issuable.TypeIncident},
333 }
335 tests := []struct {
336 name string
337 args string
338 stdin string
339 wantBody string
340 wantErr string
341 }{
342 {name: "reads_body_from_stdin_when_not_a_tty", args: `1`, stdin: "Message from stdin\n", wantBody: "Message from stdin"},
343 {
344 name: "aborts_when_stdin_is_empty", args: `1`, stdin: "", wantBody: "",
345 wantErr: "aborted: note is empty",
346 },
347 {name: "message_flag_wins_over_stdin", args: `1 --message "inline message"`, stdin: "Message from stdin\n", wantBody: "inline message"},
348 }
349 for _, test := range tests {
350 for _, cc := range commands {
351 t.Run(cc.name+"_"+test.name, func(t *testing.T) {
352 t.Parallel()
354 testClient := gitlabtesting.NewTestClient(t)
356 testClient.MockIssues.EXPECT().
357 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
358 Return(&gitlab.Issue{
359 ID: 1,
360 IID: 1,
361 IssueType: new(string(cc.issueType)),
362 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
363 }, nil, nil)
365 if test.wantErr == "" {
366 testClient.MockNotes.EXPECT().
367 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
368 DoAndReturn(func(pid any, issueIID int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
369 assert.Equal(t, test.wantBody, *opts.Body)
370 return &gitlab.Note{
371 ID: 301,
372 NoteableID: 1,
373 NoteableType: "Issue",
374 NoteableIID: 1,
375 }, nil, nil
376 })
377 }
379 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
380 return NewCmdNote(f, cc.issueType)
381 }, false,
382 cmdtest.WithGitLabClient(testClient.Client),
383 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
384 cmdtest.WithConfig(config.NewFromString("editor: vi")),
385 cmdtest.WithStdin(test.stdin),
386 )
388 output, err := exec(test.args)
389 if test.wantErr != "" {
390 require.Error(t, err)
391 assert.Equal(t, test.wantErr, err.Error())
392 return
393 }
394 require.NoError(t, err)
395 assert.Empty(t, output.Stderr())
396 assert.Equal(t, "https://gitlab.com/OWNER/REPO/issues/1#note_301\n", output.String())
397 })
398 }
399 }
400}
Diagnostics8 git calls · 441 ms
CallStatusSizeTime0441 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 · hit114 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=91 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=114 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 · 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:0 · deepen 1020032 KiB · hit55 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=55 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 · hit14 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=10 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=14 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 · hit19 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=347 git.response.cache=HIT git.response.duration_ms=19 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:0200184 B · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=18 git.response.bytes=184 git.response.cache=HIT git.response.duration_ms=21 git.fetch.filter=tree:0 git.fetch.wants=4ba4650ce53b940b2b4e0b7e7adbfd7c000097f8 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=184 git.pkt.payload.max=157 git.sideband.band1.bytes=157 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=157 git.pack.trailer=2f69124ae2ab54b2a01d88119a7a3f8627f17fb9Open pack map
fetch2002.2 KiB269 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=269 git.response.bytes=2220 git.response.cache=MISS git.response.duration_ms=269 git.fetch.wants=69e5d0e32382eea0f6cfbd2ec1f856f4bb40ae3e 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=2220 git.pkt.payload.max=2193 git.sideband.band1.bytes=2193 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
- a5eaffedfa5bd287
- Request
- a4ed5ec4…
- 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