GitLabgitlab-org
cli
main
1//go:build !integration
3package note
5import (
6 "os"
7 "path/filepath"
8 "testing"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/config"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
19)
21func writeAttachFixture(t *testing.T) string {
22 t.Helper()
24 path := filepath.Join(t.TempDir(), "screenshot.png")
25 require.NoError(t, os.WriteFile(path, []byte("\x89PNG\r\n\x1a\nfake png body"), 0o600))
26 return path
27}
29func expectUpload(tc *gitlabtesting.TestClient) {
30 tc.MockProjectMarkdownUploads.EXPECT().
31 UploadProjectMarkdown("OWNER/REPO", gomock.Any(), "screenshot.png", gomock.Any()).
32 Return(&gitlab.ProjectMarkdownUploadedFile{
33 Markdown: "",
34 }, nil, nil)
35}
37func TestNoteCreate_AttachAppendsReferenceToTheDiscussionBody(t *testing.T) {
38 t.Parallel()
40 path := writeAttachFixture(t)
42 tc := gitlabtesting.NewTestClient(t)
43 mockMR1(t, tc)
44 expectUpload(tc)
45 tc.MockDiscussions.EXPECT().
46 CreateMergeRequestDiscussion("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
47 DoAndReturn(func(_ any, _ int64, opts *gitlab.CreateMergeRequestDiscussionOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
48 assert.Equal(t, "Renders wrong here.\n\n", *opts.Body)
49 return &gitlab.Discussion{Notes: []*gitlab.Note{{ID: 301}}}, nil, nil
50 })
52 exec := setupCreateExec(t, tc)
54 _, err := exec(`1 --message "Renders wrong here." --attach ` + path)
55 require.NoError(t, err)
56}
58func TestNoteCreate_AttachOnlyPostsWithoutReadingABody(t *testing.T) {
59 t.Parallel()
61 path := writeAttachFixture(t)
63 tc := gitlabtesting.NewTestClient(t)
64 mockMR1(t, tc)
65 expectUpload(tc)
66 tc.MockDiscussions.EXPECT().
67 CreateMergeRequestDiscussion("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
68 DoAndReturn(func(_ any, _ int64, opts *gitlab.CreateMergeRequestDiscussionOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
69 assert.Equal(t, "", *opts.Body)
70 return &gitlab.Discussion{Notes: []*gitlab.Note{{ID: 302}}}, nil, nil
71 })
73 exec := setupCreateExec(t, tc)
75 _, err := exec(`1 --attach ` + path)
76 require.NoError(t, err)
77}
79func TestNoteCreate_AttachKeepsABodyPipedOnStdin(t *testing.T) {
80 t.Parallel()
82 path := writeAttachFixture(t)
84 tc := gitlabtesting.NewTestClient(t)
85 mockMR1(t, tc)
86 expectUpload(tc)
87 tc.MockDiscussions.EXPECT().
88 CreateMergeRequestDiscussion("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
89 DoAndReturn(func(_ any, _ int64, opts *gitlab.CreateMergeRequestDiscussionOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
90 assert.Equal(t, "Piped body.\n\n", *opts.Body)
91 return &gitlab.Discussion{Notes: []*gitlab.Note{{ID: 303}}}, nil, nil
92 })
94 exec := cmdtest.SetupCmdForTest(t, NewCmdCreate, false,
95 cmdtest.WithGitLabClient(tc.Client),
96 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
97 cmdtest.WithConfig(config.NewFromString("editor: vi")),
98 cmdtest.WithStdin("Piped body.\n"),
99 )
101 _, err := exec(`1 --attach ` + path)
102 require.NoError(t, err)
103}
105func TestNoteCreate_AttachWithEmptyPipedStdinPostsOnlyTheReference(t *testing.T) {
106 t.Parallel()
108 path := writeAttachFixture(t)
110 tc := gitlabtesting.NewTestClient(t)
111 mockMR1(t, tc)
112 expectUpload(tc)
113 tc.MockDiscussions.EXPECT().
114 CreateMergeRequestDiscussion("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
115 DoAndReturn(func(_ any, _ int64, opts *gitlab.CreateMergeRequestDiscussionOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
116 assert.Equal(t, "", *opts.Body)
117 return &gitlab.Discussion{Notes: []*gitlab.Note{{ID: 304}}}, nil, nil
118 })
120 exec := cmdtest.SetupCmdForTest(t, NewCmdCreate, false,
121 cmdtest.WithGitLabClient(tc.Client),
122 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
123 cmdtest.WithConfig(config.NewFromString("editor: vi")),
124 )
126 _, err := exec(`1 --attach ` + path)
127 require.NoError(t, err)
128}
130func TestNoteCreate_AttachIsRejectedWithUnique(t *testing.T) {
131 t.Parallel()
133 tc := gitlabtesting.NewTestClient(t)
134 exec := setupCreateExec(t, tc)
136 _, err := exec(`1 --message "LGTM" --unique --attach ./screenshot.png`)
137 require.Error(t, err)
138 assert.Contains(t, err.Error(), "unique")
139 assert.Contains(t, err.Error(), "attach")
140}
142func TestNoteUpdate_AttachAppendsToTheExistingNoteBody(t *testing.T) {
143 t.Parallel()
145 path := writeAttachFixture(t)
147 tc := gitlabtesting.NewTestClient(t)
148 mockMR1(t, tc)
149 tc.MockDiscussions.EXPECT().
150 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
151 Return([]*gitlab.Discussion{{
152 ID: "abc123",
153 Notes: []*gitlab.Note{{ID: 12345, Body: "Original note body."}},
154 }}, &gitlab.Response{}, nil)
155 expectUpload(tc)
156 tc.MockDiscussions.EXPECT().
157 UpdateMergeRequestDiscussionNote("OWNER/REPO", int64(1), "abc123", int64(12345), gomock.Any(), gomock.Any()).
158 DoAndReturn(func(_ any, _ int64, _ string, _ int64, opts *gitlab.UpdateMergeRequestDiscussionNoteOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
159 assert.Equal(t, "Original note body.\n\n", *opts.Body)
160 return &gitlab.Note{ID: 12345}, nil, nil
161 })
163 exec := cmdtest.SetupCmdForTest(t, NewCmdUpdate, true,
164 cmdtest.WithGitLabClient(tc.Client),
165 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
166 cmdtest.WithConfig(config.NewFromString("editor: vi")),
167 )
169 _, err := exec(`1 12345 --attach ` + path)
170 require.NoError(t, err)
171}
173func TestNoteUpdate_AttachWithMessageReplacesTheNoteBody(t *testing.T) {
174 t.Parallel()
176 path := writeAttachFixture(t)
178 tc := gitlabtesting.NewTestClient(t)
179 mockMR1(t, tc)
180 tc.MockDiscussions.EXPECT().
181 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
182 Return([]*gitlab.Discussion{{
183 ID: "abc123",
184 Notes: []*gitlab.Note{{ID: 12345, Body: "Original note body."}},
185 }}, &gitlab.Response{}, nil)
186 expectUpload(tc)
187 tc.MockDiscussions.EXPECT().
188 UpdateMergeRequestDiscussionNote("OWNER/REPO", int64(1), "abc123", int64(12345), gomock.Any(), gomock.Any()).
189 DoAndReturn(func(_ any, _ int64, _ string, _ int64, opts *gitlab.UpdateMergeRequestDiscussionNoteOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
190 assert.Equal(t, "Replaced body.\n\n", *opts.Body)
191 return &gitlab.Note{ID: 12345}, nil, nil
192 })
194 exec := cmdtest.SetupCmdForTest(t, NewCmdUpdate, true,
195 cmdtest.WithGitLabClient(tc.Client),
196 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
197 cmdtest.WithConfig(config.NewFromString("editor: vi")),
198 )
200 _, err := exec(`1 12345 --message "Replaced body." --attach ` + path)
201 require.NoError(t, err)
202}
204func TestNoteUpdate_AttachWithPipedStdinReplacesTheNoteBody(t *testing.T) {
205 t.Parallel()
207 path := writeAttachFixture(t)
209 tc := gitlabtesting.NewTestClient(t)
210 mockMR1(t, tc)
211 tc.MockDiscussions.EXPECT().
212 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
213 Return([]*gitlab.Discussion{{
214 ID: "abc123",
215 Notes: []*gitlab.Note{{ID: 12345, Body: "Original note body."}},
216 }}, &gitlab.Response{}, nil)
217 expectUpload(tc)
218 tc.MockDiscussions.EXPECT().
219 UpdateMergeRequestDiscussionNote("OWNER/REPO", int64(1), "abc123", int64(12345), gomock.Any(), gomock.Any()).
220 DoAndReturn(func(_ any, _ int64, _ string, _ int64, opts *gitlab.UpdateMergeRequestDiscussionNoteOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
221 assert.Equal(t, "Replacement body.\n\n", *opts.Body)
222 return &gitlab.Note{ID: 12345}, nil, nil
223 })
225 exec := cmdtest.SetupCmdForTest(t, NewCmdUpdate, false,
226 cmdtest.WithGitLabClient(tc.Client),
227 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
228 cmdtest.WithConfig(config.NewFromString("editor: vi")),
229 cmdtest.WithStdin("Replacement body.\n"),
230 )
232 _, err := exec(`1 12345 --attach ` + path)
233 require.NoError(t, err)
234}
236func TestNoteUpdate_AttachWithEmptyPipedStdinAppendsToTheExistingNoteBody(t *testing.T) {
237 t.Parallel()
239 path := writeAttachFixture(t)
241 tc := gitlabtesting.NewTestClient(t)
242 mockMR1(t, tc)
243 tc.MockDiscussions.EXPECT().
244 ListMergeRequestDiscussions("OWNER/REPO", int64(1), gomock.Any(), gomock.Any()).
245 Return([]*gitlab.Discussion{{
246 ID: "abc123",
247 Notes: []*gitlab.Note{{ID: 12345, Body: "Original note body."}},
248 }}, &gitlab.Response{}, nil)
249 expectUpload(tc)
250 tc.MockDiscussions.EXPECT().
251 UpdateMergeRequestDiscussionNote("OWNER/REPO", int64(1), "abc123", int64(12345), gomock.Any(), gomock.Any()).
252 DoAndReturn(func(_ any, _ int64, _ string, _ int64, opts *gitlab.UpdateMergeRequestDiscussionNoteOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
253 assert.Equal(t, "Original note body.\n\n", *opts.Body)
254 return &gitlab.Note{ID: 12345}, nil, nil
255 })
257 exec := cmdtest.SetupCmdForTest(t, NewCmdUpdate, false,
258 cmdtest.WithGitLabClient(tc.Client),
259 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
260 cmdtest.WithConfig(config.NewFromString("editor: vi")),
261 )
263 _, err := exec(`1 12345 --attach ` + path)
264 require.NoError(t, err)
265}
Diagnostics8 git calls · 204 ms
CallStatusSizeTime0204 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:0200746 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=746 git.response.cache=HIT git.response.duration_ms=2 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:0200540 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=540 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=d1be7f980a856805fa31e30e01fff4db335cbe1b 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=540 git.pkt.payload.max=513 git.sideband.band1.bytes=513 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=513 git.pack.trailer=6c82398efd91c9e023d583b948fde889bd4991b2Open pack map
fetch2001.4 KiB191 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=191 git.response.bytes=1385 git.response.cache=MISS git.response.duration_ms=191 git.fetch.wants=dbc1e4494341baa9bc6573d6fe285149a07e1980 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=1385 git.pkt.payload.max=1358 git.sideband.band1.bytes=1358 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
- a5e2c673083a4afc
- Request
- 8fbe1b57…
- 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