GitLabgitlab-org

cli

main
ply8 git calls232 ms98 KiB6 cachedprotocol v2
184 lines5.3 KiBRaw
1package create
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_CreateProjectMilestone(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 title",
33 Description: "Example 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: "Create project milestone",
41 ExpectedMsg: []string{"Created project milestone Example title (ID: 123)"},
42 cli: "--title='Example title' --project=456",
43 setupMock: func(tc *gitlabtesting.TestClient) {
44 tc.MockMilestones.EXPECT().CreateMilestone("456", gomock.Any()).Return(testMilestone, nil, nil)
45 },
46 },
47 {
48 Name: "Create project milestone with specific due date",
49 ExpectedMsg: []string{"Created project milestone Example title (ID: 123)"},
50 cli: "--title='Example title' --due-date='2025-12-16' --project 456",
51 setupMock: func(tc *gitlabtesting.TestClient) {
52 tc.MockMilestones.EXPECT().CreateMilestone("456", gomock.Any()).Return(testMilestone, nil, nil)
53 },
54 },
55 {
56 Name: "Should return an error if title is not supplied",
57 wantErr: true,
58 wantStderr: "required flag(s) \"title\" not set",
59 cli: "--due-date='2025-12-16' --project 456",
60 setupMock: func(tc *gitlabtesting.TestClient) {},
61 },
62 }
64 for _, tc := range testCases {
65 t.Run(tc.Name, func(t *testing.T) {
66 // GIVEN
67 testClient := gitlabtesting.NewTestClient(t)
68 tc.setupMock(testClient)
69 exec := cmdtest.SetupCmdForTest(
70 t,
71 NewCmdCreate,
72 false,
73 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
74 )
76 // WHEN
77 out, err := exec(tc.cli)
79 // THEN
80 if tc.wantErr {
81 require.Error(t, err)
82 assert.Contains(t, err.Error(), tc.wantStderr)
83 return
84 }
85 require.NoError(t, err)
86 for _, msg := range tc.ExpectedMsg {
87 assert.Equal(t, msg, out.OutBuf.String())
88 }
89 })
90 }
93func Test_CreateGroupMilestone(t *testing.T) {
94 type testCase struct {
95 Name string
96 ExpectedMsg []string
97 wantErr bool
98 cli string
99 wantStderr string
100 setupMock func(tc *gitlabtesting.TestClient)
101 }
103 testMilestone := &gitlab.GroupMilestone{
104 ID: 123,
105 GroupID: 456,
106 Title: "Example title",
107 Description: "Example description",
108 State: "active",
109 DueDate: new(gitlab.ISOTime(time.Date(2025, 12, 16, 0, 0, 0, 0, time.UTC))),
110 }
112 testCases := []testCase{
113 {
114 Name: "Create group milestone",
115 ExpectedMsg: []string{"Created group milestone Example title (ID: 123)"},
116 cli: "--title='Example title' --group=456",
117 setupMock: func(tc *gitlabtesting.TestClient) {
118 tc.MockGroupMilestones.EXPECT().CreateGroupMilestone("456", gomock.Any()).Return(testMilestone, nil, nil)
119 },
120 },
121 {
122 Name: "Create group milestone with specific due date",
123 ExpectedMsg: []string{"Created group milestone Example title (ID: 123)"},
124 cli: "--title='Example title' --due-date='2025-12-16' --group 456",
125 setupMock: func(tc *gitlabtesting.TestClient) {
126 tc.MockGroupMilestones.EXPECT().CreateGroupMilestone("456", gomock.Any()).Return(testMilestone, nil, nil)
127 },
128 },
129 {
130 Name: "Should return an error if title is not supplied",
131 wantErr: true,
132 wantStderr: "required flag(s) \"title\" not set",
133 cli: "--due-date='2025-12-16' --group 456",
134 setupMock: func(tc *gitlabtesting.TestClient) {},
135 },
136 }
138 for _, tc := range testCases {
139 t.Run(tc.Name, func(t *testing.T) {
140 // GIVEN
141 testClient := gitlabtesting.NewTestClient(t)
142 tc.setupMock(testClient)
143 exec := cmdtest.SetupCmdForTest(
144 t,
145 NewCmdCreate,
146 false,
147 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
148 )
150 // WHEN
151 out, err := exec(tc.cli)
153 // THEN
154 if tc.wantErr {
155 require.Error(t, err)
156 assert.Contains(t, err.Error(), tc.wantStderr)
157 return
158 }
159 require.NoError(t, err)
160 for _, msg := range tc.ExpectedMsg {
161 assert.Equal(t, msg, out.OutBuf.String())
162 }
163 })
164 }
167// Without an explicit --project or --group, these commands fall back to the
168// base repository. When that lookup fails the error has to surface instead of
169// being dropped, otherwise the nil repository is dereferenced and glab panics.
170func Test_CreateProjectMilestone_BaseRepoError(t *testing.T) {
171 testClient := gitlabtesting.NewTestClient(t)
172 exec := cmdtest.SetupCmdForTest(
173 t,
174 NewCmdCreate,
175 false,
176 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
177 cmdtest.WithBaseRepoError(errors.New("no base repository")),
178 )
180 _, err := exec("--title Example")
182 require.Error(t, err)
183 assert.Contains(t, err.Error(), "no base repository")
Diagnostics8 git calls · 232 ms
CallStatusSizeTime0232 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 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=4273
git.response.cache=HIT
git.response.duration_ms=3
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 · 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=44
Open pack map
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:0200139 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=139
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0ce745a78aa912da4dde1375fc7435eca1c70ebf
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=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=1d57eb13f88bf11e31241cc65128337b1c5cf5f0
Open pack map
fetch2001.3 KiB220 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=220
git.response.bytes=1304
git.response.cache=MISS
git.response.duration_ms=220
git.fetch.wants=d7659484a0f8ecfb9693b7f3ca4ead9767cb8cbf
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=1304
git.pkt.payload.max=1277
git.sideband.band1.bytes=1277
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
a5eafa2a1f45b1bb
Request
00313693…
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