GitLabgitlab-org
cli
main
1package update
3import (
4 "errors"
5 "fmt"
6 "strings"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 "gitlab.com/gitlab-org/cli/internal/api"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/commands/issue/issueutils"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/text"
18 "gitlab.com/gitlab-org/cli/internal/utils"
19)
21func NewCmdUpdate(f cmdutils.Factory) *cobra.Command {
22 var attach []string
24 issueUpdateCmd := &cobra.Command{
25 Use: "update <id>",
26 Short: `Update issue.`,
27 Long: heredoc.Docf(`
28 Change an issue's labels, assignees, milestone, title, or
29 description. Use %[1]s--label%[1]s and %[1]s--unlabel%[1]s to add or remove
30 labels.
32 Use %[1]s--estimate%[1]s and %[1]s--spend%[1]s to track time on the issue, the same as the %[1]s/estimate%[1]s and %[1]s/spend%[1]s quick actions. Durations use the GitLab time tracking format, for example %[1]s1mo 2w 3d 4h 5m%[1]s. %[1]s--spend%[1]s adds to the time already spent. %[1]s--remove-estimate%[1]s and %[1]s--remove-time-spent%[1]s reset the estimate and the time spent.
34 %[1]s--attach%[1]s uploads a file and references it at the end of the description. Repeat the flag for more than one file, or pass %[1]s-%[1]s to read the file from standard input. Without %[1]s--description%[1]s the references are added to the description the issue already has, instead of replacing it.
35 %[2]s`, "`", fmt.Sprintf(text.ExperimentalFlagString, "`--attach`")),
36 Example: heredoc.Doc(`
37 glab issue update 42 --label ui,ux
38 glab issue update 42 --unlabel working
40 # Read the description from a file
41 glab issue update 42 --description-file description.md
43 # Read the description from standard input
44 cat description.md | glab issue update 42 --description-file -
46 # Add a screenshot to the existing description
47 glab issue update 42 --attach ./screenshot.png
49 # Set a time estimate and log time spent
50 glab issue update 42 --estimate 2d --spend 1h30m
52 # Reset the time estimate and the time spent
53 glab issue update 42 --remove-estimate --remove-time-spent`),
54 Args: cobra.ExactArgs(1),
55 Annotations: map[string]string{
56 mcpannotations.Destructive: "true",
57 },
58 RunE: func(cmd *cobra.Command, args []string) error {
59 var err error
60 var actions []string
61 var ua *cmdutils.UserAssignments
62 c := f.IO().Color()
64 if cmd.Flags().Changed("unassign") && cmd.Flags().Changed("assignee") {
65 return &cmdutils.FlagError{Err: errors.New("--assignee and --unassign are mutually exclusive")}
66 }
68 // Parse assignees early so we can fail early in case of conflicts
69 if cmd.Flags().Changed("assignee") {
70 givenAssignees, err := cmd.Flags().GetStringSlice("assignee")
71 if err != nil {
72 return err
73 }
74 ua = cmdutils.ParseAssignees(givenAssignees)
76 err = ua.VerifyAssignees()
77 if err != nil {
78 return &cmdutils.FlagError{Err: fmt.Errorf("--assignee: %w", err)}
79 }
80 }
82 if cmd.Flags().Changed("lock-discussion") && cmd.Flags().Changed("unlock-discussion") {
83 return &cmdutils.FlagError{
84 Err: errors.New("--lock-discussion and --unlock-discussion can't be used together"),
85 }
86 }
87 if cmd.Flags().Changed("confidential") && cmd.Flags().Changed("public") {
88 return &cmdutils.FlagError{Err: errors.New("--public and --confidential can't be used together")}
89 }
90 for _, name := range []string{"estimate", "spend"} {
91 if !cmd.Flags().Changed(name) {
92 continue
93 }
94 if v, _ := cmd.Flags().GetString(name); strings.TrimSpace(v) == "" {
95 return &cmdutils.FlagError{Err: fmt.Errorf("--%s can't be empty", name)}
96 }
97 }
99 if err := cmdutils.ResolveDescriptionFile(f.IO(), cmd); err != nil {
100 return err
101 }
103 client, err := f.GitLabClient()
104 if err != nil {
105 return err
106 }
107 issue, repo, err := issueutils.IssueFromArg(f.ApiClient, client, f.BaseRepo, f.DefaultHostname(), f.Config(), args[0])
108 if err != nil {
109 return err
110 }
111 l := &gitlab.UpdateIssueOptions{}
113 if m, _ := cmd.Flags().GetString("title"); m != "" {
114 actions = append(actions, fmt.Sprintf("updated title to %q", m))
115 l.Title = new(m)
116 }
117 if m, _ := cmd.Flags().GetBool("lock-discussion"); m {
118 actions = append(actions, "locked discussion")
119 l.DiscussionLocked = new(m)
120 }
121 if m, _ := cmd.Flags().GetBool("unlock-discussion"); m {
122 actions = append(actions, "unlocked discussion")
123 l.DiscussionLocked = new(bool)
124 }
125 if cmd.Flags().Changed("weight") {
126 weight, _ := cmd.Flags().GetInt("weight")
127 if weight < 0 {
128 return &cmdutils.FlagError{Err: errors.New("weight must be a positive integer or zero")}
129 }
130 actions = append(actions, fmt.Sprintf("set weight to %d", weight))
131 l.Weight = new(int64(weight))
132 }
134 if m, _ := cmd.Flags().GetString("description"); m != "" {
135 actions = append(actions, "updated description")
137 // Edit the description via editor
138 if m == "-" {
140 // Fetch the current issue and description
141 gitlabClient, err := f.GitLabClient()
142 if err != nil {
143 return err
144 }
145 issue, _, err := issueutils.IssueFromArg(f.ApiClient, gitlabClient, f.BaseRepo, f.DefaultHostname(), f.Config(), args[0])
146 if err != nil {
147 return err
148 }
150 editor, err := cmdutils.GetEditor(f.Config)
151 if err != nil {
152 return err
153 }
155 l.Description = new("")
156 err = cmdutils.EditorPrompt(cmd.Context(), f.IO(), l.Description, "Description", issue.Description, editor)
157 if err != nil {
158 return err
159 }
160 } else {
161 l.Description = new(m)
162 }
163 }
165 if len(attach) > 0 {
166 body, err := cmdutils.AppendAttachmentsToUpdate(cmd.Context(), f.IO(), client, repo.FullName(), l.Description, func() (string, error) { return issue.Description, nil }, attach)
167 if err != nil {
168 return err
169 }
171 actions = append(actions, fmt.Sprintf("attached %s", utils.Pluralize(len(attach), "file")))
172 l.Description = &body
173 }
174 if m, _ := cmd.Flags().GetStringSlice("label"); len(m) != 0 {
175 actions = append(actions, fmt.Sprintf("added labels %s", strings.Join(m, " ")))
176 l.AddLabels = (*gitlab.LabelOptions)(&m)
177 }
178 if m, _ := cmd.Flags().GetStringSlice("unlabel"); len(m) != 0 {
179 actions = append(actions, fmt.Sprintf("removed labels %s", strings.Join(m, " ")))
180 l.RemoveLabels = (*gitlab.LabelOptions)(&m)
181 }
182 if m, _ := cmd.Flags().GetBool("public"); m {
183 actions = append(actions, "made public")
184 l.Confidential = new(false)
185 }
186 if m, _ := cmd.Flags().GetBool("confidential"); m {
187 actions = append(actions, "made confidential")
188 l.Confidential = new(true)
189 }
190 if ok := cmd.Flags().Changed("milestone"); ok {
191 if m, _ := cmd.Flags().GetString("milestone"); m != "" || m == "0" {
192 mID, err := cmdutils.ParseMilestone(client, repo, m)
193 if err != nil {
194 return err
195 }
196 actions = append(actions, fmt.Sprintf("added milestone %q", m))
197 l.MilestoneID = new(mID)
198 } else {
199 actions = append(actions, "unassigned milestone")
200 l.MilestoneID = new(int64(0))
201 }
202 }
203 if cmd.Flags().Changed("unassign") {
204 l.AssigneeIDs = &[]int64{0} // 0 or an empty int[] is the documented way to unassign
205 actions = append(actions, "unassigned all users")
206 }
207 if ua != nil {
208 if len(ua.ToReplace) != 0 {
209 l.AssigneeIDs, actions, err = ua.UsersFromReplaces(client, actions)
210 if err != nil {
211 return err
212 }
213 } else if len(ua.ToAdd) != 0 || len(ua.ToRemove) != 0 {
214 issue, err := api.GetIssue(client, repo.FullName(), issue.IID)
215 if err != nil {
216 return err
217 }
218 l.AssigneeIDs, actions, err = ua.UsersFromAddRemove(issue.Assignees, nil, client, actions)
219 if err != nil {
220 return err
221 }
222 }
223 }
224 if m, _ := cmd.Flags().GetString("due-date"); m != "" {
225 actions = append(actions, "added due date")
226 dueDate, err := gitlab.ParseISOTime(m)
227 if err != nil {
228 return err
229 }
230 l.DueDate = new(dueDate)
231 }
233 timeTrackingChanged := cmd.Flags().Changed("estimate") || cmd.Flags().Changed("remove-estimate") ||
234 cmd.Flags().Changed("spend") || cmd.Flags().Changed("remove-time-spent")
236 f.IO().LogInfof("- Updating issue #%d\n", issue.IID)
238 if *l != (gitlab.UpdateIssueOptions{}) || !timeTrackingChanged {
239 issue, err = api.UpdateIssue(client, repo.FullName(), issue.IID, l)
240 if err != nil {
241 return err
242 }
243 }
245 ctx := gitlab.WithContext(cmd.Context())
246 if cmd.Flags().Changed("estimate") {
247 estimate, _ := cmd.Flags().GetString("estimate")
248 _, _, err := client.Issues.SetTimeEstimate(repo.FullName(), issue.IID, &gitlab.SetTimeEstimateOptions{Duration: new(estimate)}, ctx)
249 if err != nil {
250 return err
251 }
252 actions = append(actions, fmt.Sprintf("set time estimate to %s", estimate))
253 }
254 if cmd.Flags().Changed("remove-estimate") {
255 _, _, err := client.Issues.ResetTimeEstimate(repo.FullName(), issue.IID, ctx)
256 if err != nil {
257 return err
258 }
259 actions = append(actions, "removed time estimate")
260 }
261 if cmd.Flags().Changed("spend") {
262 spend, _ := cmd.Flags().GetString("spend")
263 _, _, err := client.Issues.AddSpentTime(repo.FullName(), issue.IID, &gitlab.AddSpentTimeOptions{Duration: new(spend)}, ctx)
264 if err != nil {
265 return err
266 }
267 actions = append(actions, fmt.Sprintf("added %s of time spent", spend))
268 }
269 if cmd.Flags().Changed("remove-time-spent") {
270 _, _, err := client.Issues.ResetSpentTime(repo.FullName(), issue.IID, ctx)
271 if err != nil {
272 return err
273 }
274 actions = append(actions, "removed time spent")
275 }
277 for _, s := range actions {
278 f.IO().LogInfo(c.GreenCheck(), s)
279 }
281 f.IO().LogInfo(issueutils.DisplayIssue(c, issue, f.IO().IsaTTY))
282 return nil
283 },
284 }
286 issueUpdateCmd.Flags().StringP("title", "t", "", "Title of issue.")
287 issueUpdateCmd.Flags().BoolP("lock-discussion", "", false, "Lock discussion on issue.")
288 issueUpdateCmd.Flags().BoolP("unlock-discussion", "", false, "Unlock discussion on issue.")
289 issueUpdateCmd.Flags().StringP("description", "d", "", "Issue description. Set to \"-\" to open an editor.")
290 issueUpdateCmd.Flags().StringSliceP("label", "l", []string{}, "Add labels.")
291 issueUpdateCmd.Flags().StringSliceP("unlabel", "u", []string{}, "Remove labels.")
292 issueUpdateCmd.Flags().BoolP("public", "p", false, "Make issue public.")
293 issueUpdateCmd.Flags().BoolP("confidential", "c", false, "Make issue confidential.")
294 issueUpdateCmd.Flags().StringP("milestone", "m", "", "Title of the milestone to assign Set to \"\" or 0 to unassign.")
295 issueUpdateCmd.Flags().
296 StringSliceP("assignee", "a", []string{}, "Assign users by username. Prefix with '!' or '-' to remove from existing assignees, or '+' to add new. Otherwise, replace existing assignees with these users. Multiple usernames can be comma-separated or specified by repeating the flag.")
297 issueUpdateCmd.Flags().Bool("unassign", false, "Unassign all users.")
298 issueUpdateCmd.Flags().IntP("weight", "w", 0, "Set weight of the issue.")
299 issueUpdateCmd.Flags().StringP("due-date", "", "", "A date in 'YYYY-MM-DD' format.")
300 issueUpdateCmd.Flags().String("estimate", "", "Set the time estimate for the issue, for example '2d' or '3h30m'.")
301 issueUpdateCmd.Flags().Bool("remove-estimate", false, "Remove the time estimate from the issue.")
302 issueUpdateCmd.Flags().String("spend", "", "Add time spent on the issue, for example '1h30m'.")
303 issueUpdateCmd.Flags().Bool("remove-time-spent", false, "Remove all time spent from the issue.")
304 issueUpdateCmd.MarkFlagsMutuallyExclusive("estimate", "remove-estimate")
305 issueUpdateCmd.MarkFlagsMutuallyExclusive("spend", "remove-time-spent")
306 cmdutils.AddDescriptionFileFlag(issueUpdateCmd, "issue")
307 cmdutils.AddAttachFlag(issueUpdateCmd, &attach, "description")
309 return issueUpdateCmd
310}
Diagnostics8 git calls · 274 ms
CallStatusSizeTime0274 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 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
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=44Open pack map
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: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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200492 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=492 git.response.cache=HIT git.response.duration_ms=3 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=2 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
fetch2003.3 KiB261 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=261 git.response.bytes=3335 git.response.cache=MISS git.response.duration_ms=261 git.fetch.wants=53a36cc0818a5f756a1165cf90847c8cb9320f64 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=3335 git.pkt.payload.max=3308 git.sideband.band1.bytes=3308 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
- a5e2c56438254afc
- Request
- 799614b7…
- 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