GitLabgitlab-org

cli

main
ply8 git calls216 ms99 KiB5 cachedprotocol v2
192 lines6.4 KiBRaw
1//go:build !integration
3package create
5import (
6 "net/url"
7 "os"
8 "path/filepath"
9 "testing"
11 "github.com/MakeNowJust/heredoc/v2"
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/git"
20 "gitlab.com/gitlab-org/cli/internal/glinstance"
21 "gitlab.com/gitlab-org/cli/internal/glrepo"
22 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
23 "gitlab.com/gitlab-org/cli/test"
26// Differs from the repo the merge request is created against, so the tests can
27// tell which one the upload used.
28const attachTargetProject = "UPSTREAM/REPO"
30func writeAttachFixture(t *testing.T, name string) string {
31 t.Helper()
33 path := filepath.Join(t.TempDir(), name)
34 require.NoError(t, os.WriteFile(path, []byte("\x89PNG\r\n\x1a\nfake png body"), 0o600))
35 return path
38func attachFactoryOptions(t *testing.T, testClient *gitlabtesting.TestClient) []cmdtest.FactoryOption {
39 t.Helper()
41 cs, csTeardown := test.InitCmdStubber()
42 t.Cleanup(csTeardown)
43 cs.Stub("HEAD branch: master\n")
44 cs.Stub(heredoc.Doc(`
45 deadbeef HEAD
46 deadb00f refs/remotes/upstream/feat-new-mr
47 deadbeef refs/remotes/origin/feat-new-mr
48 `))
50 pu, _ := url.Parse("https://gitlab.com/OWNER/REPO.git")
52 return []cmdtest.FactoryOption{
53 cmdtest.WithGitLabClient(testClient.Client),
54 func(f *cmdtest.Factory) {
55 f.RemotesStub = func() (glrepo.Remotes, error) {
56 return glrepo.Remotes{
57 {
58 Remote: &git.Remote{Name: "upstream", Resolved: "head", PushURL: pu},
59 Repo: glrepo.New("OWNER", "REPO", glinstance.DefaultHostname),
60 },
61 {
62 Remote: &git.Remote{Name: "origin", Resolved: "base", PushURL: pu},
63 Repo: glrepo.New("monalisa", "REPO", glinstance.DefaultHostname),
64 },
65 }, nil
66 }
67 f.BranchStub = func() (string, error) { return "feat-new-mr", nil }
68 },
69 }
72func expectProjectForAttach(testClient *gitlabtesting.TestClient) {
73 testClient.MockProjects.EXPECT().
74 GetProject("OWNER/REPO", gomock.Any()).
75 Return(&gitlab.Project{
76 ID: 1,
77 DefaultBranch: "master",
78 WebURL: "http://gitlab.com/OWNER/REPO",
79 MergeRequestsEnabled: true,
80 PathWithNamespace: attachTargetProject,
81 }, nil, nil)
82 testClient.MockProjects.EXPECT().
83 ListProjectTargetBranchRules(attachTargetProject, gomock.Any()).
84 Return([]gitlab.TargetBranchRule{}, nil, nil)
87func TestMRCreate_AttachUploadsToTargetProjectNotHeadRepo(t *testing.T) {
88 t.Setenv("NO_COLOR", "true")
90 path := writeAttachFixture(t, "screenshot.png")
92 testClient := gitlabtesting.NewTestClient(t)
93 expectProjectForAttach(testClient)
95 testClient.MockProjectMarkdownUploads.EXPECT().
96 UploadProjectMarkdown(attachTargetProject, gomock.Any(), "screenshot.png", gomock.Any()).
97 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: "![screenshot](/uploads/abc/screenshot.png)"}, nil, nil)
99 var gotDescription *string
100 testClient.MockMergeRequests.EXPECT().
101 CreateMergeRequest("OWNER/REPO", gomock.Any()).
102 DoAndReturn(func(_ any, opts *gitlab.CreateMergeRequestOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
103 gotDescription = opts.Description
104 return &gitlab.MergeRequest{
105 BasicMergeRequest: gitlab.BasicMergeRequest{
106 IID: 12,
107 WebURL: "https://gitlab.com/OWNER/REPO/-/merge_requests/12",
108 },
109 }, nil, nil
110 })
112 exec := cmdtest.SetupCmdForTest(t, NewCmdCreate, false, attachFactoryOptions(t, testClient)...)
114 _, err := exec(`-t myMRtitle -d "Look at this." --attach ` + path + ` --yes`)
115 require.NoError(t, err)
117 require.NotNil(t, gotDescription)
118 assert.Equal(t, "Look at this.\n\n![screenshot](/uploads/abc/screenshot.png)", *gotDescription)
121func TestMRCreate_AttachWithoutDescription(t *testing.T) {
122 t.Setenv("NO_COLOR", "true")
124 path := writeAttachFixture(t, "screenshot.png")
126 testClient := gitlabtesting.NewTestClient(t)
127 expectProjectForAttach(testClient)
128 testClient.MockProjectMarkdownUploads.EXPECT().
129 UploadProjectMarkdown(attachTargetProject, gomock.Any(), "screenshot.png", gomock.Any()).
130 Return(&gitlab.ProjectMarkdownUploadedFile{Markdown: "![screenshot](/uploads/abc/screenshot.png)"}, nil, nil)
132 var gotDescription *string
133 testClient.MockMergeRequests.EXPECT().
134 CreateMergeRequest("OWNER/REPO", gomock.Any()).
135 DoAndReturn(func(_ any, opts *gitlab.CreateMergeRequestOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
136 gotDescription = opts.Description
137 return &gitlab.MergeRequest{
138 BasicMergeRequest: gitlab.BasicMergeRequest{IID: 12, WebURL: "https://gitlab.com/OWNER/REPO/-/merge_requests/12"},
139 }, nil, nil
140 })
142 exec := cmdtest.SetupCmdForTest(t, NewCmdCreate, false, attachFactoryOptions(t, testClient)...)
144 _, err := exec(`-t myMRtitle --attach ` + path + ` --yes`)
145 require.NoError(t, err)
147 require.NotNil(t, gotDescription)
148 assert.Equal(t, "![screenshot](/uploads/abc/screenshot.png)", *gotDescription)
151func TestMRCreate_NoAttachLeavesTheDescriptionAlone(t *testing.T) {
152 t.Setenv("NO_COLOR", "true")
154 testClient := gitlabtesting.NewTestClient(t)
155 expectProjectForAttach(testClient)
157 var gotDescription *string
158 testClient.MockMergeRequests.EXPECT().
159 CreateMergeRequest("OWNER/REPO", gomock.Any()).
160 DoAndReturn(func(_ any, opts *gitlab.CreateMergeRequestOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
161 gotDescription = opts.Description
162 return &gitlab.MergeRequest{
163 BasicMergeRequest: gitlab.BasicMergeRequest{IID: 12, WebURL: "https://gitlab.com/OWNER/REPO/-/merge_requests/12"},
164 }, nil, nil
165 })
167 exec := cmdtest.SetupCmdForTest(t, NewCmdCreate, false, attachFactoryOptions(t, testClient)...)
169 _, err := exec(`-t myMRtitle -d "Look at this." --yes`)
170 require.NoError(t, err)
172 require.NotNil(t, gotDescription)
173 assert.Equal(t, "Look at this.", *gotDescription)
176func TestGenerateMRCompareURL_IncludesAttachmentReferences(t *testing.T) {
177 opts := &options{
178 body: "Look at this.\n\n![screenshot](/uploads/abc/screenshot.png)",
179 SourceBranch: "feat-new-mr",
180 TargetBranch: "master",
181 Title: "myMRtitle",
182 TargetProject: &gitlab.Project{ID: 100},
183 SourceProject: &gitlab.Project{
184 ID: 101,
185 WebURL: "https://gitlab.com/OWNER/REPO",
186 },
187 }
189 u, err := generateMRCompareURL(opts)
190 require.NoError(t, err)
191 assert.Contains(t, u, url.QueryEscape("![screenshot](/uploads/abc/screenshot.png)"))
Diagnostics8 git calls · 216 ms
CallStatusSizeTime0216 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 KiB10 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=10
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 · hit5 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=5
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=44
Open 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=4
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=fb03183b7909876778f740d196d7516811003517
Open 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=330baf80c59a4208b55120dff7dd824b7952b790
Open pack map
fetchtree:0200202 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=202
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=02d69f71dcced584f60d05385e3a545c407a76f4
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=202
git.pkt.payload.max=175
git.sideband.band1.bytes=175
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=175
git.pack.trailer=55b73a97e35a386cfe7e8d263dc4f59f2b43b52a
Open pack map
fetch2001.7 KiB193 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=193
git.response.bytes=1778
git.response.cache=MISS
git.response.duration_ms=193
git.fetch.wants=ba02f528c7a925aa8e4a5737d17e41c6ea0b88df
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=1778
git.pkt.payload.max=1751
git.sideband.band1.bytes=1751
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
a5e297cbcd6d2407
Request
2fae1053…
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