GitLabgitlab-org
cli
main
1//go:build !integration
3package update
5import (
6 "errors"
7 "fmt"
8 "os"
9 "path/filepath"
10 "testing"
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/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/glinstance"
21 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22)
24func TestIssueUpdate_DescriptionFile(t *testing.T) {
25 const body = "# Heading\n\nMulti-line body with \"quotes\", $VARS and `backticks`.\n"
27 tests := []struct {
28 name string
29 useStdin bool
30 }{
31 {name: "reads the description from a file"},
32 {name: "reads the description from standard input", useStdin: true},
33 }
35 for _, tt := range tests {
36 t.Run(tt.name, func(t *testing.T) {
37 tc := gitlabtesting.NewTestClient(t)
38 tc.MockIssues.EXPECT().
39 GetIssue("OWNER/REPO", int64(42)).
40 Return(&gitlab.Issue{IID: 42, Title: "issueTitle"}, nil, nil)
42 var gotDescription *string
43 tc.MockIssues.EXPECT().
44 UpdateIssue("OWNER/REPO", int64(42), gomock.Any()).
45 DoAndReturn(func(_ any, _ int64, opts *gitlab.UpdateIssueOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.Issue, *gitlab.Response, error) {
46 gotDescription = opts.Description
47 return &gitlab.Issue{IID: 42, Title: "issueTitle", WebURL: "https://gitlab.com/OWNER/REPO/-/issues/42"}, nil, nil
48 })
50 factoryOpts := []cmdtest.FactoryOption{
51 cmdtest.WithGitLabClient(tc.Client),
52 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
53 }
55 path := "-"
56 if tt.useStdin {
57 factoryOpts = append(factoryOpts, cmdtest.WithStdin(body))
58 } else {
59 path = filepath.Join(t.TempDir(), "description.md")
60 require.NoError(t, os.WriteFile(path, []byte(body), 0o600))
61 }
63 exec := cmdtest.SetupCmdForTest(t, NewCmdUpdate, false, factoryOpts...)
65 _, err := exec(fmt.Sprintf("42 --description-file %q", path))
66 require.NoError(t, err)
68 require.NotNil(t, gotDescription)
69 assert.Equal(t, body, *gotDescription)
70 })
71 }
72}
74func TestIssueUpdate_DescriptionFileConflictsWithDescription(t *testing.T) {
75 exec := cmdtest.SetupCmdForTest(
76 t,
77 NewCmdUpdate,
78 false,
79 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
80 )
82 _, err := exec(`42 --description inline --description-file some.md`)
84 require.Error(t, err)
85 assert.Contains(t, err.Error(), "[description description-file]")
86}
88func TestIssueUpdate_TimeTracking(t *testing.T) {
89 updatedIssue := &gitlab.Issue{IID: 42, Title: "issueTitle", WebURL: "https://gitlab.com/OWNER/REPO/-/issues/42"}
91 tests := []struct {
92 name string
93 cli string
94 expectMocks func(m *gitlabtesting.MockIssuesServiceInterface)
95 wantOutput []string
96 }{
97 {
98 name: "sets the time estimate",
99 cli: "42 --estimate 2d",
100 expectMocks: func(m *gitlabtesting.MockIssuesServiceInterface) {
101 m.EXPECT().
102 SetTimeEstimate("OWNER/REPO", int64(42), &gitlab.SetTimeEstimateOptions{Duration: new("2d")}, gomock.Any()).
103 Return(&gitlab.TimeStats{}, nil, nil)
104 },
105 wantOutput: []string{"set time estimate to 2d"},
106 },
107 {
108 name: "removes the time estimate",
109 cli: "42 --remove-estimate",
110 expectMocks: func(m *gitlabtesting.MockIssuesServiceInterface) {
111 m.EXPECT().
112 ResetTimeEstimate("OWNER/REPO", int64(42), gomock.Any()).
113 Return(&gitlab.TimeStats{}, nil, nil)
114 },
115 wantOutput: []string{"removed time estimate"},
116 },
117 {
118 name: "adds time spent",
119 cli: "42 --spend 1h30m",
120 expectMocks: func(m *gitlabtesting.MockIssuesServiceInterface) {
121 m.EXPECT().
122 AddSpentTime("OWNER/REPO", int64(42), &gitlab.AddSpentTimeOptions{Duration: new("1h30m")}, gomock.Any()).
123 Return(&gitlab.TimeStats{}, nil, nil)
124 },
125 wantOutput: []string{"added 1h30m of time spent"},
126 },
127 {
128 name: "removes time spent",
129 cli: "42 --remove-time-spent",
130 expectMocks: func(m *gitlabtesting.MockIssuesServiceInterface) {
131 m.EXPECT().
132 ResetSpentTime("OWNER/REPO", int64(42), gomock.Any()).
133 Return(&gitlab.TimeStats{}, nil, nil)
134 },
135 wantOutput: []string{"removed time spent"},
136 },
137 {
138 name: "updates attributes and time tracking together",
139 cli: `42 --title "new title" --estimate 2d --spend 1h`,
140 expectMocks: func(m *gitlabtesting.MockIssuesServiceInterface) {
141 gomock.InOrder(
142 m.EXPECT().
143 UpdateIssue("OWNER/REPO", int64(42), &gitlab.UpdateIssueOptions{Title: new("new title")}).
144 Return(updatedIssue, nil, nil),
145 m.EXPECT().
146 SetTimeEstimate("OWNER/REPO", int64(42), &gitlab.SetTimeEstimateOptions{Duration: new("2d")}, gomock.Any()).
147 Return(&gitlab.TimeStats{}, nil, nil),
148 m.EXPECT().
149 AddSpentTime("OWNER/REPO", int64(42), &gitlab.AddSpentTimeOptions{Duration: new("1h")}, gomock.Any()).
150 Return(&gitlab.TimeStats{}, nil, nil),
151 )
152 },
153 wantOutput: []string{`updated title to "new title"`, "set time estimate to 2d", "added 1h of time spent"},
154 },
155 }
157 for _, tt := range tests {
158 t.Run(tt.name, func(t *testing.T) {
159 tc := gitlabtesting.NewTestClient(t)
160 tc.MockIssues.EXPECT().
161 GetIssue("OWNER/REPO", int64(42)).
162 Return(updatedIssue, nil, nil)
163 tt.expectMocks(tc.MockIssues)
165 exec := cmdtest.SetupCmdForTest(
166 t,
167 NewCmdUpdate,
168 false,
169 cmdtest.WithGitLabClient(tc.Client),
170 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
171 )
173 out, err := exec(tt.cli)
174 require.NoError(t, err)
176 for _, want := range tt.wantOutput {
177 assert.Contains(t, out.String(), want)
178 }
179 })
180 }
181}
183func TestIssueUpdate_TimeTrackingAPIError(t *testing.T) {
184 tc := gitlabtesting.NewTestClient(t)
185 tc.MockIssues.EXPECT().
186 GetIssue("OWNER/REPO", int64(42)).
187 Return(&gitlab.Issue{IID: 42}, nil, nil)
188 tc.MockIssues.EXPECT().
189 AddSpentTime("OWNER/REPO", int64(42), gomock.Any(), gomock.Any()).
190 Return(nil, nil, errors.New("400 Bad Request"))
192 exec := cmdtest.SetupCmdForTest(
193 t,
194 NewCmdUpdate,
195 false,
196 cmdtest.WithGitLabClient(tc.Client),
197 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
198 )
200 _, err := exec("42 --spend nonsense")
202 require.Error(t, err)
203 assert.Contains(t, err.Error(), "400 Bad Request")
204}
206func TestIssueUpdate_TimeTrackingConflicts(t *testing.T) {
207 tests := []struct {
208 name string
209 cli string
210 wantErr string
211 }{
212 {
213 name: "estimate with remove-estimate",
214 cli: "42 --estimate 2d --remove-estimate",
215 wantErr: "[estimate remove-estimate]",
216 },
217 {
218 name: "spend with remove-time-spent",
219 cli: "42 --spend 1h --remove-time-spent",
220 wantErr: "[spend remove-time-spent]",
221 },
222 }
224 for _, tt := range tests {
225 t.Run(tt.name, func(t *testing.T) {
226 exec := cmdtest.SetupCmdForTest(
227 t,
228 NewCmdUpdate,
229 false,
230 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
231 )
233 _, err := exec(tt.cli)
235 require.Error(t, err)
236 assert.Contains(t, err.Error(), tt.wantErr)
237 })
238 }
239}
241func TestIssueUpdate_TimeTrackingEmptyDuration(t *testing.T) {
242 tests := []struct {
243 name string
244 cli string
245 wantErr string
246 }{
247 {
248 name: "empty estimate",
249 cli: `42 --estimate ""`,
250 wantErr: "--estimate can't be empty",
251 },
252 {
253 name: "empty spend",
254 cli: `42 --spend ""`,
255 wantErr: "--spend can't be empty",
256 },
257 {
258 name: "whitespace-only spend",
259 cli: `42 --spend " "`,
260 wantErr: "--spend can't be empty",
261 },
262 {
263 name: "empty spend with an attribute update",
264 cli: `42 --title "new title" --spend ""`,
265 wantErr: "--spend can't be empty",
266 },
267 }
269 for _, tt := range tests {
270 t.Run(tt.name, func(t *testing.T) {
271 // No mock expectations: any API call, including UpdateIssue, fails the test.
272 tc := gitlabtesting.NewTestClient(t)
274 exec := cmdtest.SetupCmdForTest(
275 t,
276 NewCmdUpdate,
277 false,
278 cmdtest.WithGitLabClient(tc.Client),
279 cmdtest.WithBaseRepo("OWNER", "REPO", glinstance.DefaultHostname),
280 )
282 _, err := exec(tt.cli)
284 var flagErr *cmdutils.FlagError
285 require.ErrorAs(t, err, &flagErr)
286 assert.Contains(t, err.Error(), tt.wantErr)
287 })
288 }
289}
Diagnostics8 git calls · 239 ms
CallStatusSizeTime0239 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=1 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 · hit2 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=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=44Open pack map
fetchtree:02001.8 KiB · hit1 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=1 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:0200492 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=492 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=409e7038e253494f94691a22a250deee77ba03bf 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=492 git.pkt.payload.max=465 git.sideband.band1.bytes=465 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=465 git.pack.trailer=c8a67aa26d573734cf0c8f302f8c1dd08e47d76aOpen pack map
fetchtree:0200211 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=231 git.response.bytes=211 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=223946acadcafdcc7d72618ed1be1c04a332ea22 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=211 git.pkt.payload.max=184 git.sideband.band1.bytes=184 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=184 git.pack.trailer=4892bf72a4513d7bc02bb1fb3a31f2c4f0883c30Open pack map
fetch2001.9 KiB231 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1992 git.response.cache=MISS git.response.duration_ms=231 git.fetch.wants=5595cfbf53fe58695f733f287d07d072a2a3720f 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=1992 git.pkt.payload.max=1965 git.sideband.band1.bytes=1965 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
- a5e2c640ac314afc
- Request
- 881c350c…
- 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