GitLabgitlab-org
cli
main
1//go:build !integration
3package note
5import (
6 "os"
7 "path/filepath"
8 "testing"
10 "github.com/acarl005/stripansi"
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)
25var pngBytes = []byte("\x89PNG\r\n\x1a\nfake png body")
27func writeAttachment(t *testing.T, name string) string {
28 t.Helper()
30 path := filepath.Join(t.TempDir(), name)
31 require.NoError(t, os.WriteFile(path, pngBytes, 0o600))
32 return path
33}
35func expectIssue(testClient *gitlabtesting.TestClient) {
36 testClient.MockIssues.EXPECT().
37 GetIssue("OWNER/REPO", int64(1), gomock.Any()).
38 Return(&gitlab.Issue{
39 ID: 1,
40 IID: 1,
41 IssueType: new("issue"),
42 WebURL: "https://gitlab.com/OWNER/REPO/issues/1",
43 }, nil, nil)
44}
46func setupNoteCmd(t *testing.T, testClient *gitlabtesting.TestClient) cmdtest.CmdExecFunc {
47 t.Helper()
49 return cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
50 return NewCmdNote(f, issuable.TypeIssue)
51 }, true,
52 cmdtest.WithGitLabClient(testClient.Client),
53 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
54 cmdtest.WithConfig(config.NewFromString("editor: vi")),
55 )
56}
58func TestNoteCreate_AttachAppendsReferenceToMessage(t *testing.T) {
59 t.Parallel()
61 path := writeAttachment(t, "screenshot.png")
63 testClient := gitlabtesting.NewTestClient(t)
64 expectIssue(testClient)
65 testClient.MockProjectMarkdownUploads.EXPECT().
66 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "screenshot.png", gomock.Any()).
67 Return(&gitlab.ProjectMarkdownUploadedFile{
68 Markdown: "",
69 }, nil, nil)
70 testClient.MockNotes.EXPECT().
71 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
72 DoAndReturn(func(pid any, iid int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
73 assert.Equal(t, "Here is the repro.\n\n", *opts.Body)
74 return &gitlab.Note{ID: 301}, nil, nil
75 })
77 exec := setupNoteCmd(t, testClient)
79 output, err := exec(`1 --message "Here is the repro." --attach ` + path)
80 require.NoError(t, err)
81 assert.Equal(t, "https://gitlab.com/OWNER/REPO/issues/1#note_301\n", output.String())
82 assert.Contains(t, stripansi.Strip(output.Stderr()), "Uploading screenshot.png")
83}
85func TestNoteCreate_AttachOnlyPostsWithoutAMessage(t *testing.T) {
86 t.Parallel()
88 path := writeAttachment(t, "screenshot.png")
90 testClient := gitlabtesting.NewTestClient(t)
91 expectIssue(testClient)
92 testClient.MockProjectMarkdownUploads.EXPECT().
93 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "screenshot.png", gomock.Any()).
94 Return(&gitlab.ProjectMarkdownUploadedFile{
95 Markdown: "",
96 }, nil, nil)
97 testClient.MockNotes.EXPECT().
98 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
99 DoAndReturn(func(pid any, iid int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
100 assert.Equal(t, "", *opts.Body)
101 return &gitlab.Note{ID: 302}, nil, nil
102 })
104 exec := setupNoteCmd(t, testClient)
106 output, err := exec(`1 --attach ` + path)
107 require.NoError(t, err)
108 assert.Equal(t, "https://gitlab.com/OWNER/REPO/issues/1#note_302\n", output.String())
109}
111func setupPipedNoteCmd(t *testing.T, testClient *gitlabtesting.TestClient, opts ...cmdtest.FactoryOption) cmdtest.CmdExecFunc {
112 t.Helper()
114 opts = append([]cmdtest.FactoryOption{
115 cmdtest.WithGitLabClient(testClient.Client),
116 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
117 cmdtest.WithConfig(config.NewFromString("editor: vi")),
118 }, opts...)
119 return cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
120 return NewCmdNote(f, issuable.TypeIssue)
121 }, false, opts...)
122}
124func TestNoteCreate_AttachKeepsABodyPipedOnStdin(t *testing.T) {
125 t.Parallel()
127 path := writeAttachment(t, "screenshot.png")
129 testClient := gitlabtesting.NewTestClient(t)
130 expectIssue(testClient)
131 testClient.MockProjectMarkdownUploads.EXPECT().
132 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "screenshot.png", gomock.Any()).
133 Return(&gitlab.ProjectMarkdownUploadedFile{
134 Markdown: "",
135 }, nil, nil)
136 testClient.MockNotes.EXPECT().
137 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
138 DoAndReturn(func(pid any, iid int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
139 assert.Equal(t, "Piped body.\n\n", *opts.Body)
140 return &gitlab.Note{ID: 305}, nil, nil
141 })
143 exec := setupPipedNoteCmd(t, testClient, cmdtest.WithStdin("Piped body.\n"))
145 _, err := exec(`1 --attach ` + path)
146 require.NoError(t, err)
147}
149func TestNoteCreate_AttachWithEmptyPipedStdinPostsOnlyTheReference(t *testing.T) {
150 t.Parallel()
152 path := writeAttachment(t, "screenshot.png")
154 testClient := gitlabtesting.NewTestClient(t)
155 expectIssue(testClient)
156 testClient.MockProjectMarkdownUploads.EXPECT().
157 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "screenshot.png", gomock.Any()).
158 Return(&gitlab.ProjectMarkdownUploadedFile{
159 Markdown: "",
160 }, nil, nil)
161 testClient.MockNotes.EXPECT().
162 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
163 DoAndReturn(func(pid any, iid int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
164 assert.Equal(t, "", *opts.Body)
165 return &gitlab.Note{ID: 306}, nil, nil
166 })
168 exec := setupPipedNoteCmd(t, testClient)
170 _, err := exec(`1 --attach ` + path)
171 require.NoError(t, err)
172}
174func TestNoteCreate_MultipleAttachmentsAppendInOrder(t *testing.T) {
175 t.Parallel()
177 first := writeAttachment(t, "first.png")
178 second := writeAttachment(t, "second.png")
180 testClient := gitlabtesting.NewTestClient(t)
181 expectIssue(testClient)
182 gomock.InOrder(
183 testClient.MockProjectMarkdownUploads.EXPECT().
184 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "first.png", gomock.Any()).
185 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: ""}, nil, nil),
186 testClient.MockProjectMarkdownUploads.EXPECT().
187 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "second.png", gomock.Any()).
188 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: ""}, nil, nil),
189 )
190 testClient.MockNotes.EXPECT().
191 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
192 DoAndReturn(func(pid any, iid int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
193 assert.Equal(t, "Two files.\n\n\n", *opts.Body)
194 return &gitlab.Note{ID: 303}, nil, nil
195 })
197 exec := setupNoteCmd(t, testClient)
199 _, err := exec(`1 --message "Two files." --attach ` + first + ` --attach ` + second)
200 require.NoError(t, err)
201}
203func TestNoteCreate_AttachFromStdinSniffsAName(t *testing.T) {
204 t.Parallel()
206 testClient := gitlabtesting.NewTestClient(t)
207 expectIssue(testClient)
208 testClient.MockProjectMarkdownUploads.EXPECT().
209 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "upload.png", gomock.Any()).
210 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: ""}, nil, nil)
211 testClient.MockNotes.EXPECT().
212 CreateIssueNote("OWNER/REPO", int64(1), gomock.Any()).
213 DoAndReturn(func(pid any, iid int64, opts *gitlab.CreateIssueNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
214 assert.Equal(t, "", *opts.Body)
215 return &gitlab.Note{ID: 304}, nil, nil
216 })
218 exec := cmdtest.SetupCmdForTest(t, func(f cmdutils.Factory) *cobra.Command {
219 return NewCmdNote(f, issuable.TypeIssue)
220 }, true,
221 cmdtest.WithGitLabClient(testClient.Client),
222 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
223 cmdtest.WithConfig(config.NewFromString("editor: vi")),
224 cmdtest.WithStdin(string(pngBytes)),
225 )
227 _, err := exec(`1 --attach -`)
228 require.NoError(t, err)
229}
231func TestNoteCreate_AttachMissingFileUploadsNothing(t *testing.T) {
232 t.Parallel()
234 testClient := gitlabtesting.NewTestClient(t)
235 expectIssue(testClient)
237 exec := setupNoteCmd(t, testClient)
239 _, err := exec(`1 --message "hi" --attach ` + filepath.Join(t.TempDir(), "absent.png"))
240 require.Error(t, err)
241 assert.Contains(t, err.Error(), "failed to read attachment")
242}
Diagnostics8 git calls · 441 ms
CallStatusSizeTime0441 ms
discover200185 B5 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=5 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 KiB178 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=153 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=178 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 · hit17 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=4273 git.response.cache=HIT git.response.duration_ms=17 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 · hit45 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=32926 git.response.cache=HIT git.response.duration_ms=45 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 · hit4 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=4 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 · hit38 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=7 git.response.bytes=347 git.response.cache=HIT git.response.duration_ms=38 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 · 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=184 git.response.cache=HIT git.response.duration_ms=4 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
fetch2001.6 KiB200 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=200 git.response.bytes=1656 git.response.cache=MISS git.response.duration_ms=200 git.fetch.wants=278bb138c0e754cb4b026662457e14afc9a0e3f8 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=1656 git.pkt.payload.max=1629 git.sideband.band1.bytes=1629 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
- a5eaffedfa59d287
- Request
- c33fbc02…
- 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