GitLabgitlab-org
cli
main
1package update
3import (
4 "context"
5 "fmt"
6 "maps"
7 "slices"
8 "strconv"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 a "gitlab.com/gitlab-org/cli/internal/api"
16 "gitlab.com/gitlab-org/cli/internal/cmdutils"
17 "gitlab.com/gitlab-org/cli/internal/commands/workitems/api"
18 "gitlab.com/gitlab-org/cli/internal/commands/workitems/utils"
19 "gitlab.com/gitlab-org/cli/internal/config"
20 "gitlab.com/gitlab-org/cli/internal/glrepo"
21 "gitlab.com/gitlab-org/cli/internal/iostreams"
22 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
23 "gitlab.com/gitlab-org/cli/internal/text"
24)
26type options struct {
27 // Dependencies
28 io *iostreams.IOStreams
29 baseRepo func() (glrepo.Interface, error)
30 gitlabClient func() (*gitlab.Client, error)
31 config func() config.Config
33 // Flags
34 group string
35 iid int64
36 title string
37 description string
38 attach []string
39 assignee []string
40 milestone string
41 startDate string
42 dueDate string
43 weight int64
44 iteration int64
45 parent string
46 status string
48 // internal state
49 scope *api.ScopeInfo
50 statusID *gitlab.WorkItemStatusID
52 // Output
53 outputFormat string
54}
56func NewCmd(f cmdutils.Factory) *cobra.Command {
57 opts := &options{
58 io: f.IO(),
59 baseRepo: f.BaseRepo,
60 gitlabClient: f.GitLabClient,
61 config: f.Config,
62 }
64 cmd := &cobra.Command{
65 Use: "update <iid> [flags]",
66 Short: "Update work items in a project or group. (EXPERIMENTAL)",
67 Long: heredoc.Docf(`
68 The command uses your repository context to detect scope automatically.
70 Use %[1]s--group%[1]s to target a group or subgroup. %[1]s--group%[1]s and %[1]s--repo%[1]s are mutually exclusive.
72 %[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 work item already has, instead of replacing it. Uploads are project-scoped, so %[1]s--attach%[1]s cannot be combined with %[1]s--group%[1]s.
74 %[1]s--parent%[1]s accepts a work item ID in the current scope, a reference such as %[1]sgroup/project#123%[1]s, or a URL.
75 `, "`") + text.ExperimentalString,
76 Example: heredoc.Doc(`
77 # Update a work item in current project
78 glab work-items update 42 --description "this issue tracks a new feature"
80 # Update a work item in a group
81 glab work-items update 40 --group MYGROUP --description "this epic tracks a new feature"
83 # Read the description from a file
84 glab work-items update 42 --description-file description.md
86 # Read the description from standard input
87 cat description.md | glab work-items update 42 --description-file -
89 # Add a screenshot to the existing description
90 glab work-items update 42 --attach ./screenshot.png
92 # Move a work item to another iteration
93 glab work-items update 42 --iteration 123
95 # Move a work item under work item 40
96 glab work-items update 42 --parent 40
98 # Set the status of a work item
99 glab work-items update 42 --status in-progress
101 # Move a work item under a work item in another project
102 glab work-items update 42 --parent https://gitlab.com/my-group/my-project/-/work_items/40
103 `),
104 Args: cobra.ExactArgs(1),
105 Annotations: map[string]string{
106 mcpannotations.Destructive: "true",
107 },
108 RunE: func(cmd *cobra.Command, args []string) error {
109 if err := opts.complete(cmd.Context(), cmd, args); err != nil {
110 return err
111 }
113 return opts.run(cmd)
114 },
115 }
117 // enable -R flag for repo override
118 cmdutils.EnableRepoOverride(cmd, f)
120 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
122 // Flags
123 fl := cmd.Flags()
124 fl.StringVarP(&opts.group, "group", "g", "", "Update work items for a group or subgroup.")
125 fl.StringVarP(&opts.title, "title", "t", "", "Update the title for the work item.")
126 fl.StringVarP(&opts.description, "description", "d", "", "Update the description for the work item.")
127 fl.Int64VarP(&opts.weight, "weight", "w", 0, "Update the weight value for the work item.")
128 fl.StringSliceVarP(&opts.assignee, "assignee", "a", []string{}, "Update the work item assignee with the supplied GitLab usernames.")
129 fl.StringVarP(&opts.milestone, "milestone", "m", "", "Update the work item milestone with the title or ID.")
130 fl.StringVar(&opts.startDate, "startdate", "", "Update the start date for the work item.")
131 fl.StringVar(&opts.dueDate, "duedate", "", "Update the due date for the work item.")
132 fl.Int64Var(&opts.iteration, "iteration", 0, "Move the work item to the iteration with this ID.")
133 fl.StringVar(&opts.parent, "parent", "", "Set the parent work item by <id>, reference, or URL.")
134 fl.Var(cmdutils.NewEnumValue(statusSlugs(), "", &opts.status), "status", "Set the work item status: todo, in-progress, done, wont-do, or duplicate. Needs the status widget, which is Premium and Ultimate only.")
135 cmdutils.AddDescriptionFileFlag(cmd, "work item")
136 cmdutils.AddAttachFlag(cmd, &opts.attach, "description")
138 cmd.MarkFlagsMutuallyExclusive("group", "repo")
139 // Uploads are project-scoped, and GitLab has no group-level equivalent.
140 cmd.MarkFlagsMutuallyExclusive("group", "attach")
142 return cmd
143}
145func (opts *options) complete(ctx context.Context, cmd *cobra.Command, args []string) error {
146 iid, err := strconv.ParseInt(args[0], 10, 64)
147 if err != nil {
148 return fmt.Errorf("invalid work item ID: %w", err)
149 }
150 opts.iid = iid
152 group, err := cmdutils.GroupOverride(cmd)
153 if err != nil {
154 return err
155 }
156 opts.group = group
158 if err := cmdutils.ResolveDescriptionFile(opts.io, cmd); err != nil {
159 return err
160 }
162 if err := cmdutils.HandleDescriptionEditor(ctx, &opts.description, opts.io, opts.config, nil); err != nil {
163 return err
164 }
166 if cmd.Flags().Changed("status") {
167 id := systemStatuses[opts.status]
168 opts.statusID = &id
169 }
171 scope, err := utils.DetectScope(opts.group, opts.baseRepo)
172 if err != nil {
173 return err
174 }
175 opts.scope = scope
177 return nil
178}
180// systemStatuses maps the --status values to GitLab's system-defined work
181// item statuses. Custom statuses are not supported.
182var systemStatuses = map[string]gitlab.WorkItemStatusID{
183 "todo": gitlab.WorkItemStatusToDo,
184 "in-progress": gitlab.WorkItemStatusInProgress,
185 "done": gitlab.WorkItemStatusDone,
186 "wont-do": gitlab.WorkItemStatusWontDo,
187 "duplicate": gitlab.WorkItemStatusDuplicate,
188}
190func statusSlugs() []string {
191 return slices.Collect(maps.Keys(systemStatuses))
192}
194func (opts *options) run(cmd *cobra.Command) error {
195 client, err := opts.gitlabClient()
196 if err != nil {
197 return fmt.Errorf("failed to get GitLab client: %w", err)
198 }
200 updateOpts := gitlab.UpdateWorkItemOptions{}
202 if opts.startDate != "" {
203 startDate, err := gitlab.ParseISOTime(opts.startDate)
204 if err != nil {
205 return cmdutils.FlagError{Err: fmt.Errorf("--startdate must be ISO 8601 (YYYY-MM-DD), got %q", opts.startDate)}
206 }
207 updateOpts.StartDate = new(startDate)
208 }
210 if opts.dueDate != "" {
211 dueDate, err := gitlab.ParseISOTime(opts.dueDate)
212 if err != nil {
213 return cmdutils.FlagError{Err: fmt.Errorf("--duedate must be ISO 8601 (YYYY-MM-DD), got %q", opts.dueDate)}
214 }
215 updateOpts.DueDate = new(dueDate)
216 }
218 if opts.title != "" {
219 updateOpts.Title = new(opts.title)
220 }
222 if cmd.Flags().Changed("description") {
223 updateOpts.Description = new(opts.description)
224 }
226 if opts.parent != "" {
227 parentID, err := utils.ResolveParentID(cmd.Context(), client, opts.config(), opts.parent, opts.scope.Path)
228 if err != nil {
229 return err
230 }
231 updateOpts.ParentID = new(parentID)
232 }
234 if len(opts.attach) > 0 {
235 current := func() (string, error) {
236 wi, _, err := client.WorkItems.GetWorkItem(opts.scope.Path, opts.iid, gitlab.WithContext(cmd.Context()), a.WithSafeRetries())
237 if err != nil {
238 return "", fmt.Errorf("failed to read the current description: %w", err)
239 }
240 return wi.Description, nil
241 }
243 body, err := cmdutils.AppendAttachmentsToUpdate(cmd.Context(), opts.io, client, opts.scope.Path, updateOpts.Description, current, opts.attach)
244 if err != nil {
245 return err
246 }
247 updateOpts.Description = &body
248 }
250 if len(opts.assignee) != 0 {
251 user, err := a.UsersByNames(client, opts.assignee)
252 if err != nil {
253 return cmdutils.FlagError{Err: fmt.Errorf("failed to find assignee: %w", err)}
254 }
256 assignees := make([]int64, 0)
258 for _, i := range user {
259 assignees = append(assignees, i.ID)
260 }
261 updateOpts.AssigneeIDs = assignees
262 }
264 if opts.milestone != "" {
265 if milestoneID, err := strconv.ParseInt(opts.milestone, 10, 64); err == nil {
266 updateOpts.MilestoneID = new(milestoneID)
267 } else {
268 if opts.scope.Type == "project" {
270 l := &a.ListMilestonesOptions{
271 Title: new(opts.milestone),
272 }
274 m, err := a.ListAllMilestones(client, opts.scope.Path, l)
275 if err != nil || len(m) == 0 {
276 return cmdutils.FlagError{Err: fmt.Errorf("failed to find project milestone by title")}
277 }
279 updateOpts.MilestoneID = new(m[0].ID)
280 } else if opts.scope.Type == "group" {
281 m, _, err := client.GroupMilestones.ListGroupMilestones(opts.scope.Path, &gitlab.ListGroupMilestonesOptions{Title: new(opts.milestone)})
282 if err != nil || len(m) == 0 {
283 return cmdutils.FlagError{Err: fmt.Errorf("failed to find group milestone by title")}
284 }
286 updateOpts.MilestoneID = new(m[0].ID)
287 }
288 }
289 }
291 if cmd.Flags().Changed("weight") {
292 updateOpts.Weight = new(opts.weight)
293 }
295 if cmd.Flags().Changed("iteration") {
296 updateOpts.IterationID = new(opts.iteration)
297 }
299 updateOpts.Status = opts.statusID
301 wi, _, err := client.WorkItems.UpdateWorkItem(opts.scope.Path, opts.iid, &updateOpts, gitlab.WithContext(cmd.Context()))
302 if err != nil {
303 return err
304 }
306 switch opts.outputFormat {
307 case "json":
308 return opts.io.PrintJSON(wi)
309 default:
310 opts.io.LogInfo("- Updating work item in", opts.scope.Path)
312 if opts.io.IsaTTY {
313 opts.io.LogInfof("#%d %s\n%s\n", wi.IID, wi.Title, wi.WebURL)
314 } else {
315 opts.io.LogInfo(wi.WebURL)
316 }
317 }
319 return nil
320}
Diagnostics8 git calls · 195 ms
CallStatusSizeTime0195 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 · 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 · 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 · 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=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:0200317 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=317 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=33cd62a3f1bc55e6bd56a9b96f181ff12c30955a 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=317 git.pkt.payload.max=290 git.sideband.band1.bytes=290 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=290 git.pack.trailer=5711d3cc007ef3bd902f87854e977c46d0913d04Open pack map
fetchtree:0200170 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=170 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=21db039062ee6d041ad414dc4572fef7eccec043 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=170 git.pkt.payload.max=143 git.sideband.band1.bytes=143 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=143 git.pack.trailer=9a371b3c0ac0d73d3fe90ebc27ca40d9383b3a90Open pack map
fetch2003.2 KiB180 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=180 git.response.bytes=3313 git.response.cache=MISS git.response.duration_ms=180 git.fetch.wants=ac2c815c99016d3fef4bc73702250cfacb095b07 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=3313 git.pkt.payload.max=3286 git.sideband.band1.bytes=3286 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
- a5e2786c3cb1d806
- Request
- 0bd46b82…
- 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