GitLabgitlab-org
cli
main
1package update
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strconv"
8 "strings"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/text"
21)
23type options struct {
24 io *iostreams.IOStreams
25 factory cmdutils.Factory
26 gitlabClient func() (*gitlab.Client, error)
28 // Flags.
29 message string
30 attach []string
32 // Populated in complete.
33 client *gitlab.Client
34 mr *gitlab.MergeRequest
35 repo glrepo.Interface
36 draftNoteID int64
37 position *gitlab.NotePosition
38 body string
39}
41func NewCmd(f cmdutils.Factory) *cobra.Command {
42 opts := &options{
43 io: f.IO(),
44 factory: f,
45 gitlabClient: f.GitLabClient,
46 }
48 cmd := &cobra.Command{
49 Use: "update [<id> | <branch>] <draft-note-id>",
50 Short: "Update the body of one of your pending review comments. (EXPERIMENTAL)",
51 Long: heredoc.Docf(`
52 Replace the body of a pending review comment before you publish the review. %[1]s<draft-note-id>%[1]s is the ID printed by %[1]sglab mr note draft create%[1]s or listed by %[1]sglab mr note draft list%[1]s.
54 You can change only the body. A pending diff comment stays on the same file and lines. Pending comments on images cannot be updated from the command line, because their position cannot be preserved.
56 %[1]s--attach%[1]s uploads a file and references it at the end of the comment. Repeat the flag for more than one file, or pass %[1]s-%[1]s to read the file from standard input. Without %[1]s--message%[1]s, standard input that is not a terminal is read as the new body. It replaces the existing body, unless %[1]s--attach -%[1]s reads the file from standard input. With neither %[1]s--message%[1]s nor piped input, the references are added to the body the comment already has, instead of replacing it. The command waits until standard input closes, so to keep the existing body from a script, redirect standard input from %[1]s/dev/null%[1]s.
57 `, "`") + text.ExperimentalString,
58 Example: heredoc.Doc(`
59 # Update pending review comment 456 on merge request 123
60 glab mr note draft update 123 456 -m "Revised comment"
62 # Update a pending review comment on the current branch's merge request, composing in an editor
63 glab mr note draft update 456
65 # Pipe the new body from stdin
66 echo "new body" | glab mr note draft update 123 456
68 # Add a screenshot to the existing comment body
69 glab mr note draft update 123 456 --attach ./screenshot.png
70 `),
71 Args: cobra.RangeArgs(1, 2),
72 Annotations: map[string]string{
73 mcpannotations.Destructive: "true",
74 },
75 RunE: func(cmd *cobra.Command, args []string) error {
76 if err := opts.complete(cmd, args); err != nil {
77 return err
78 }
79 if err := opts.validate(); err != nil {
80 return err
81 }
82 return opts.run(cmd.Context())
83 },
84 }
86 cmd.Flags().StringVarP(&opts.message, "message", "m", "", "New comment body. If omitted, opens an editor or reads from stdin.")
87 cmdutils.AddAttachFlag(cmd, &opts.attach, "comment")
89 return cmd
90}
92func (o *options) complete(cmd *cobra.Command, args []string) error {
93 idArg := args[len(args)-1]
94 draftNoteID, err := strconv.ParseInt(idArg, 10, 64)
95 if err != nil {
96 return fmt.Errorf("invalid pending review comment ID %q: must be a number", idArg)
97 }
98 o.draftNoteID = draftNoteID
100 client, err := o.gitlabClient()
101 if err != nil {
102 return err
103 }
104 o.client = client
106 mr, repo, err := mrutils.MRFromArgs(cmd.Context(), o.factory, args[:len(args)-1], "any")
107 if err != nil {
108 return err
109 }
110 o.mr = mr
111 o.repo = repo
113 draft, err := mrutils.GetDraftNote(cmd.Context(), client, repo.FullName(), mr.IID, draftNoteID)
114 if err != nil {
115 return err
116 }
117 if mrutils.HasFilePosition(draft.Position) {
118 // The client does not decode image coordinates, so re-sending the
119 // position would drop them. Fail before prompting for a body.
120 if draft.Position.PositionType == "image" {
121 return fmt.Errorf("pending review comment %d is on an image and cannot be updated without losing its position", draftNoteID)
122 }
123 o.position = draft.Position
124 }
126 o.body = o.message
127 if strings.TrimSpace(o.body) == "" && !cmdutils.SkipNoteBody(o.io, o.attach) {
128 o.body, err = mrutils.NoteBodyFromStdinOrEditor(cmd.Context(), o.io, o.factory.Config)
129 if err != nil {
130 return err
131 }
132 }
133 // With attachments an empty body means "keep the existing comment text".
134 if strings.TrimSpace(o.body) == "" && len(o.attach) > 0 {
135 o.body = draft.Note
136 }
138 return nil
139}
141func (o *options) validate() error {
142 if strings.TrimSpace(o.body) == "" && len(o.attach) == 0 {
143 return errors.New("aborted: comment has an empty message")
144 }
145 return nil
146}
148func (o *options) run(ctx context.Context) error {
149 body, err := cmdutils.AppendAttachments(ctx, o.io, o.client, o.repo.FullName(), o.body, o.attach)
150 if err != nil {
151 return err
152 }
154 updateOpts := &gitlab.UpdateDraftNoteOptions{Note: &body}
155 // GitLab before 19.5 clears the stored position when an update omits it,
156 // which turns a diff comment into a general one, so send it back.
157 if o.position != nil {
158 updateOpts.Position = positionOptions(o.position)
159 }
161 if _, _, err := o.client.DraftNotes.UpdateDraftNote(o.repo.FullName(), o.mr.IID, o.draftNoteID, updateOpts, gitlab.WithContext(ctx)); err != nil {
162 return fmt.Errorf("failed to update pending review comment: %w", err)
163 }
165 o.io.LogInfof("%d\n", o.draftNoteID)
166 return nil
167}
169func positionOptions(pos *gitlab.NotePosition) *gitlab.PositionOptions {
170 opts := &gitlab.PositionOptions{
171 BaseSHA: nonZero(pos.BaseSHA),
172 StartSHA: nonZero(pos.StartSHA),
173 HeadSHA: nonZero(pos.HeadSHA),
174 PositionType: nonZero(pos.PositionType),
175 NewPath: nonZero(pos.NewPath),
176 OldPath: nonZero(pos.OldPath),
177 NewLine: nonZero(pos.NewLine),
178 OldLine: nonZero(pos.OldLine),
179 }
180 if lr := pos.LineRange; lr != nil && lr.StartRange != nil && lr.EndRange != nil {
181 opts.LineRange = &gitlab.LineRangeOptions{
182 Start: linePositionOptions(lr.StartRange),
183 End: linePositionOptions(lr.EndRange),
184 }
185 }
186 return opts
187}
189func linePositionOptions(lp *gitlab.LinePosition) *gitlab.LinePositionOptions {
190 return &gitlab.LinePositionOptions{
191 LineCode: nonZero(lp.LineCode),
192 Type: nonZero(lp.Type),
193 OldLine: nonZero(lp.OldLine),
194 NewLine: nonZero(lp.NewLine),
195 }
196}
198func nonZero[T comparable](v T) *T {
199 var zero T
200 if v == zero {
201 return nil
202 }
203 return &v
204}
Diagnostics10 git calls · 370 ms
CallStatusSizeTime0370 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=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 KiB2 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=UPDATING 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
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=3 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 · 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:0200746 B · hit2 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=746 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f 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=746 git.pkt.payload.max=719 git.sideband.band1.bytes=719 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=719 git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200540 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=540 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=d1be7f980a856805fa31e30e01fff4db335cbe1b 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=540 git.pkt.payload.max=513 git.sideband.band1.bytes=513 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=513 git.pack.trailer=6c82398efd91c9e023d583b948fde889bd4991b2Open pack map
fetchtree:0200259 B · hit3 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=259 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=17c60670b892d93213e07c56d27bf8c78b2f6eee 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=259 git.pkt.payload.max=232 git.sideband.band1.bytes=232 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=232 git.pack.trailer=a86d2eccaf7aae3b0fec29dbfe655131faf5a4adOpen pack map
fetchtree:0200139 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=349 git.response.bytes=139 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=015a61e58c945c0dc30a4b51232d30449852384b 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=df946d39b75737b8081fdceb7535c2acf63ff133Open pack map
fetch2002.5 KiB349 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2540 git.response.cache=MISS git.response.duration_ms=349 git.fetch.wants=d054eccc06b6cfb399ecb08ea04d40a489c8a098 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=2540 git.pkt.payload.max=2513 git.sideband.band1.bytes=2513 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
- a5e2bf1bfdc989da
- Request
- 7a619401…
- 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