GitLabgitlab-org
cli
main
1package get
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"
17)
19func Test_GetProjectMilestone(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: "Milestone title",
33 Description: "Example description",
34 State: "closed",
35 DueDate: new(gitlab.ISOTime(time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC))),
36 }
38 testCases := []testCase{
39 {
40 Name: "Get project milestone",
41 ExpectedMsg: []string{"Title: Milestone title\nDescription: Example description\nState: closed\nDue Date: 2025-01-15\n\n"},
42 cli: "123 --project 456",
43 setupMock: func(tc *gitlabtesting.TestClient) {
44 tc.MockMilestones.EXPECT().GetMilestone("456", int64(123)).Return(testMilestone, nil, nil)
45 },
46 },
47 {
48 Name: "When milestone is not found returns an error",
49 wantErr: true,
50 cli: "111 --project 456",
51 setupMock: func(tc *gitlabtesting.TestClient) {
52 tc.MockMilestones.EXPECT().GetMilestone("456", int64(111)).Return(nil, nil, errors.New("404 Not found"))
53 },
54 },
55 }
57 for _, tc := range testCases {
58 t.Run(tc.Name, func(t *testing.T) {
59 // GIVEN
60 testClient := gitlabtesting.NewTestClient(t)
61 tc.setupMock(testClient)
62 exec := cmdtest.SetupCmdForTest(
63 t,
64 NewCmdGet,
65 false,
66 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
67 )
69 // WHEN
70 out, err := exec(tc.cli)
72 // THEN
73 if tc.wantErr {
74 require.Error(t, err)
75 assert.Contains(t, err.Error(), tc.wantStderr)
76 return
77 }
78 require.NoError(t, err)
79 for _, msg := range tc.ExpectedMsg {
80 assert.Equal(t, msg, out.OutBuf.String())
81 }
82 })
83 }
84}
86func Test_GetGroupMilestone(t *testing.T) {
87 type testCase struct {
88 Name string
89 ExpectedMsg []string
90 wantErr bool
91 cli string
92 wantStderr string
93 setupMock func(tc *gitlabtesting.TestClient)
94 }
96 testMilestone := &gitlab.GroupMilestone{
97 ID: 123,
98 GroupID: 456,
99 Title: "Milestone title",
100 Description: "Example description",
101 State: "closed",
102 DueDate: new(gitlab.ISOTime(time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC))),
103 }
105 testCases := []testCase{
106 {
107 Name: "Get group milestone",
108 ExpectedMsg: []string{"Title: Milestone title\nDescription: Example description\nState: closed\nDue Date: 2025-01-15\n\n"},
109 cli: "123 --group 456",
110 setupMock: func(tc *gitlabtesting.TestClient) {
111 tc.MockGroupMilestones.EXPECT().GetGroupMilestone(gomock.Any(), int64(123)).Return(testMilestone, nil, nil)
112 },
113 },
114 {
115 Name: "When milestone is not found returns an error",
116 wantErr: true,
117 cli: "111 --group 456",
118 setupMock: func(tc *gitlabtesting.TestClient) {
119 tc.MockGroupMilestones.EXPECT().GetGroupMilestone(gomock.Any(), int64(111)).Return(nil, nil, errors.New("404 Not found"))
120 },
121 },
122 }
124 for _, tc := range testCases {
125 t.Run(tc.Name, func(t *testing.T) {
126 // GIVEN
127 testClient := gitlabtesting.NewTestClient(t)
128 tc.setupMock(testClient)
129 exec := cmdtest.SetupCmdForTest(
130 t,
131 NewCmdGet,
132 false,
133 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
134 )
136 // WHEN
137 out, err := exec(tc.cli)
139 // THEN
140 if tc.wantErr {
141 require.Error(t, err)
142 assert.Contains(t, err.Error(), tc.wantStderr)
143 return
144 }
145 require.NoError(t, err)
146 for _, msg := range tc.ExpectedMsg {
147 assert.Equal(t, msg, out.OutBuf.String())
148 }
149 })
150 }
151}
153func TestMilestoneGet_JSON(t *testing.T) {
154 t.Parallel()
156 testMilestone := &gitlab.Milestone{
157 ID: 123,
158 ProjectID: 456,
159 Title: "Milestone title",
160 Description: "Example description",
161 State: "closed",
162 DueDate: new(gitlab.ISOTime(time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC))),
163 }
165 testClient := gitlabtesting.NewTestClient(t)
166 testClient.MockMilestones.EXPECT().
167 GetMilestone("456", int64(123)).
168 Return(testMilestone, nil, nil)
170 exec := cmdtest.SetupCmdForTest(
171 t,
172 NewCmdGet,
173 false,
174 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
175 )
177 out, err := exec("123 --project 456 --output json")
178 require.NoError(t, err)
180 assert.Contains(t, out.String(), `"id":123`)
181 assert.Contains(t, out.String(), `"title":"Milestone title"`)
182 assert.Contains(t, out.String(), `"state":"closed"`)
183 assert.Empty(t, out.Stderr())
184}
186// Without an explicit --project or --group, these commands fall back to the
187// base repository. When that lookup fails the error has to surface instead of
188// being dropped, otherwise the nil repository is dereferenced and glab panics.
189func Test_GetProjectMilestone_BaseRepoError(t *testing.T) {
190 testClient := gitlabtesting.NewTestClient(t)
191 exec := cmdtest.SetupCmdForTest(
192 t,
193 NewCmdGet,
194 false,
195 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(testClient.Client))),
196 cmdtest.WithBaseRepoError(errors.New("no base repository")),
197 )
199 _, err := exec("123")
201 require.Error(t, err)
202 assert.Contains(t, err.Error(), "no base repository")
203}
Diagnostics8 git calls · 266 ms
CallStatusSizeTime0266 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 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 · hit3 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=HIT git.response.duration_ms=3 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=3 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 · 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=32926 git.response.cache=HIT git.response.duration_ms=4 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 · 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=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:0200287 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=287 git.response.cache=HIT git.response.duration_ms=2 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=6593eafdcd01a12bbd3c8da0f75aaca8bc99568fOpen pack map
fetchtree:0200134 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=253 git.response.bytes=134 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=813925209c8da4145349bfe60854299328adf75e 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=ac007dac9fc89ef596839603bc2ad75bd8573671Open pack map
fetch2001.3 KiB253 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1363 git.response.cache=MISS git.response.duration_ms=253 git.fetch.wants=91f8f15217d20fdafe37908dee41af4cc8cbb369 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=1363 git.pkt.payload.max=1336 git.sideband.band1.bytes=1336 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
- a5e2d037d8edefad
- Request
- 15fe473c…
- 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