GitLabgitlab-org

cli

main
ply8 git calls473 ms99 KiB6 cachedprotocol v2
219 lines6.5 KiBRaw
1package edit
3import (
4 "errors"
5 "testing"
6 "time"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "go.uber.org/mock/gomock"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
15 "gitlab.com/gitlab-org/cli/internal/api"
16 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
19func Test_EditProjectMilestone(t *testing.T) {
20 type testCase struct {
21 Name string
22 ExpectedMsg []string
23 wantErr bool
24 cli string
25 wantStderr string
26 setupMock func(tc *gitlabtesting.TestClient)
27 }
29 testMilestone := &gitlab.Milestone{
30 ID: 123,
31 ProjectID: 456,
32 Title: "Example",
33 Description: "Updated description",
34 State: "active",
35 DueDate: new(gitlab.ISOTime(time.Date(2025, 12, 16, 0, 0, 0, 0, time.UTC))),
36 }
38 testCases := []testCase{
39 {
40 Name: "Edit project milestone",
41 ExpectedMsg: []string{"Updated project milestone Example (ID: 123)"},
42 cli: "123 --title='Example' --project=456",
43 setupMock: func(tc *gitlabtesting.TestClient) {
44 tc.MockMilestones.EXPECT().UpdateMilestone("456", int64(123), &gitlab.UpdateMilestoneOptions{
45 Title: new("Example"),
46 }).Return(testMilestone, nil, nil)
47 },
48 },
49 {
50 Name: "Edit project milestone with description",
51 ExpectedMsg: []string{"Updated project milestone Example (ID: 123)"},
52 cli: "123 --description='Updated description' --project 456",
53 setupMock: func(tc *gitlabtesting.TestClient) {
54 tc.MockMilestones.EXPECT().UpdateMilestone("456", int64(123), &gitlab.UpdateMilestoneOptions{
55 Description: new("Updated description"),
56 }).Return(testMilestone, nil, nil)
57 },
58 },
59 {
60 Name: "When milestone is not found returns an error",
61 wantErr: true,
62 wantStderr: "404 Not found",
63 cli: "111 --project 456",
64 setupMock: func(tc *gitlabtesting.TestClient) {
65 tc.MockMilestones.EXPECT().UpdateMilestone("456", int64(111), gomock.Any()).Return(nil, nil, errors.New("404 Not found"))
66 },
67 },
68 {
69 Name: "When milestone is not set returns an error that it is required",
70 wantErr: true,
71 wantStderr: "accepts 1 arg(s), received 0",
72 cli: "--project 456 --title='example'",
73 setupMock: func(tc *gitlabtesting.TestClient) {},
74 },
75 {
76 Name: "When neither project nor group is set it updates the current project",
77 cli: "123 --title='Example'",
78 setupMock: func(tc *gitlabtesting.TestClient) {
79 tc.MockMilestones.EXPECT().UpdateMilestone("OWNER/REPO", int64(123), &gitlab.UpdateMilestoneOptions{
80 Title: new("Example"),
81 }).Return(testMilestone, nil, nil)
82 },
83 },
84 }
86 for _, tc := range testCases {
87 t.Run(tc.Name, func(t *testing.T) {
88 // GIVEN
89 testClient := gitlabtesting.NewTestClient(t)
90 tc.setupMock(testClient)
91 exec := cmdtest.SetupCmdForTest(
92 t,
93 NewCmdEdit,
94 false,
95 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
96 )
98 // WHEN
99 out, err := exec(tc.cli)
101 // THEN
102 if tc.wantErr {
103 require.Error(t, err)
104 assert.Contains(t, err.Error(), tc.wantStderr)
105 return
106 }
107 require.NoError(t, err)
108 for _, msg := range tc.ExpectedMsg {
109 assert.Equal(t, msg, out.OutBuf.String())
110 }
111 })
112 }
115func Test_EditGroupMilestone(t *testing.T) {
116 type testCase struct {
117 Name string
118 ExpectedMsg []string
119 wantErr bool
120 cli string
121 wantStderr string
122 setupMock func(tc *gitlabtesting.TestClient)
123 }
125 testMilestone := &gitlab.GroupMilestone{
126 ID: 123,
127 GroupID: 456,
128 Title: "Example",
129 Description: "Updated description",
130 State: "active",
131 DueDate: new(gitlab.ISOTime(time.Date(2025, 12, 16, 0, 0, 0, 0, time.UTC))),
132 }
134 testCases := []testCase{
135 {
136 Name: "Edit group milestone",
137 ExpectedMsg: []string{"Updated group milestone Example (ID: 123)"},
138 cli: "123 --title='Example' --group=456",
139 setupMock: func(tc *gitlabtesting.TestClient) {
140 tc.MockGroupMilestones.EXPECT().UpdateGroupMilestone("456", int64(123), &gitlab.UpdateGroupMilestoneOptions{
141 Title: new("Example"),
142 }).Return(testMilestone, nil, nil)
143 },
144 },
145 {
146 Name: "Edit group milestone with description",
147 ExpectedMsg: []string{"Updated group milestone Example (ID: 123)"},
148 cli: "123 --description='Updated description' --group 456",
149 setupMock: func(tc *gitlabtesting.TestClient) {
150 tc.MockGroupMilestones.EXPECT().UpdateGroupMilestone("456", int64(123), &gitlab.UpdateGroupMilestoneOptions{
151 Description: new("Updated description"),
152 }).Return(testMilestone, nil, nil)
153 },
154 },
155 {
156 Name: "When milestone is not found returns an error",
157 wantErr: true,
158 wantStderr: "404 Not found",
159 cli: "111 --group 456",
160 setupMock: func(tc *gitlabtesting.TestClient) {
161 tc.MockGroupMilestones.EXPECT().UpdateGroupMilestone("456", int64(111), gomock.Any()).Return(nil, nil, errors.New("404 Not found"))
162 },
163 },
164 {
165 Name: "When milestone is not set returns an error that it is required",
166 wantErr: true,
167 wantStderr: "accepts 1 arg(s), received 0",
168 cli: "--group 456 --title='example'",
169 setupMock: func(tc *gitlabtesting.TestClient) {},
170 },
171 }
173 for _, tc := range testCases {
174 t.Run(tc.Name, func(t *testing.T) {
175 // GIVEN
176 testClient := gitlabtesting.NewTestClient(t)
177 tc.setupMock(testClient)
178 exec := cmdtest.SetupCmdForTest(
179 t,
180 NewCmdEdit,
181 false,
182 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
183 )
185 // WHEN
186 out, err := exec(tc.cli)
188 // THEN
189 if tc.wantErr {
190 require.Error(t, err)
191 assert.Contains(t, err.Error(), tc.wantStderr)
192 return
193 }
194 require.NoError(t, err)
195 for _, msg := range tc.ExpectedMsg {
196 assert.Equal(t, msg, out.OutBuf.String())
197 }
198 })
199 }
202// Without an explicit --project or --group, these commands fall back to the
203// base repository. When that lookup fails the error has to surface instead of
204// being dropped, otherwise the nil repository is dereferenced and glab panics.
205func Test_EditProjectMilestone_BaseRepoError(t *testing.T) {
206 testClient := gitlabtesting.NewTestClient(t)
207 exec := cmdtest.SetupCmdForTest(
208 t,
209 NewCmdEdit,
210 false,
211 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
212 cmdtest.WithBaseRepoError(errors.New("no base repository")),
213 )
215 _, err := exec("123 --title Example")
217 require.Error(t, err)
218 assert.Contains(t, err.Error(), "no base repository")
Diagnostics8 git calls · 473 ms
CallStatusSizeTime0473 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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=2
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
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 · 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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:0200287 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=287
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=4b2a62b649e97f7e66e5dd178207a12ec137d596
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=287
git.pkt.payload.max=260
git.sideband.band1.bytes=260
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=260
git.pack.trailer=6593eafdcd01a12bbd3c8da0f75aaca8bc99568f
Open pack map
fetchtree:0200135 B · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=135
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=36a47b09bc13b1752f059e580024ac77b4d01f01
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=135
git.pkt.payload.max=108
git.sideband.band1.bytes=108
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=108
git.pack.trailer=177bd9243ff28f36f129a466d481406949338a74
Open pack map
fetch2001.4 KiB461 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=461
git.response.bytes=1450
git.response.cache=MISS
git.response.duration_ms=461
git.fetch.wants=2a40e81f90c985a8c87015814e557f6190c08380
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=1450
git.pkt.payload.max=1423
git.sideband.band1.bytes=1423
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
a5eafa255b66b1bb
Request
14961147…
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