GitLabgitlab-org
cli
main
1package note
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 updateOptions 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 mrArgs []string
34 client *gitlab.Client
35 mr *gitlab.MergeRequest
36 repo glrepo.Interface
37 discussionID string
38 noteID int64
39 body string
40}
42func NewCmdUpdate(f cmdutils.Factory) *cobra.Command {
43 opts := &updateOptions{
44 io: f.IO(),
45 factory: f,
46 gitlabClient: f.GitLabClient,
47 }
49 cmd := &cobra.Command{
50 Use: "update [<id> | <branch>] <note-id>",
51 Short: "Update the body of a note on a merge request. (EXPERIMENTAL)",
52 Long: heredoc.Docf(`
53 Replace the body of an existing note on a merge request.
55 %[1]s<note-id>%[1]s is a numeric note ID, not a hex discussion ID.
56 You can find note IDs with:
58 - %[1]sglab mr note list -F json%[1]s (the %[1]s.id%[1]s field)
59 - Note URLs: %[1]s.../merge_requests/1#note_12345%[1]s
61 You can change only the note body. You cannot move the position of diff notes.
63 %[1]s--attach%[1]s uploads a file and references it at the end of the note. 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 note 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.
64 `, "`") + text.ExperimentalString,
65 Example: heredoc.Doc(`
66 # Update note 12345 on merge request 1 with a new message
67 glab mr note update 1 12345 -m "Updated comment"
69 # Update a note on the current branch's merge request, composing in an editor
70 glab mr note update 12345
72 # Pipe the new body from stdin
73 echo "new body" | glab mr note update 1 12345
75 # Add a screenshot to the existing note body
76 glab mr note update 1 12345 --attach ./screenshot.png
77 `),
78 Args: cobra.RangeArgs(1, 2),
79 Annotations: map[string]string{
80 mcpannotations.Destructive: "true",
81 },
82 RunE: func(cmd *cobra.Command, args []string) error {
83 if err := opts.complete(cmd, args); err != nil {
84 return err
85 }
86 if err := opts.validate(); err != nil {
87 return err
88 }
89 return opts.run(cmd.Context())
90 },
91 }
93 cmd.Flags().StringVarP(&opts.message, "message", "m", "", "New note body. If omitted, opens an editor or reads from stdin.")
94 cmdutils.AddAttachFlag(cmd, &opts.attach, "note")
96 return cmd
97}
99func (o *updateOptions) complete(cmd *cobra.Command, args []string) error {
100 // Last arg is always the note ID.
101 noteIDStr := args[len(args)-1]
102 noteID, err := strconv.ParseInt(noteIDStr, 10, 64)
103 if err != nil {
104 return fmt.Errorf("invalid note ID %q: must be a numeric note ID, not a discussion ID prefix", noteIDStr)
105 }
106 o.noteID = noteID
108 if len(args) == 2 {
109 o.mrArgs = args[:1]
110 }
112 client, err := o.gitlabClient()
113 if err != nil {
114 return err
115 }
116 o.client = client
118 mr, repo, err := mrutils.MRFromArgs(cmd.Context(), o.factory, o.mrArgs, "any")
119 if err != nil {
120 return err
121 }
122 o.mr = mr
123 o.repo = repo
125 discussionID, note, err := mrutils.FindNoteInDiscussionPaginated(cmd.Context(), client, repo.FullName(), mr.IID, noteID)
126 if err != nil {
127 if errors.Is(err, mrutils.ErrNoteNotFound) {
128 return fmt.Errorf("note %d not found in merge request !%d", noteID, mr.IID)
129 }
130 return fmt.Errorf("failed to look up note %d in merge request !%d: %w", noteID, mr.IID, err)
131 }
132 o.discussionID = discussionID
134 body := o.message
135 if strings.TrimSpace(body) == "" && !cmdutils.SkipNoteBody(o.io, o.attach) {
136 body, err = mrutils.NoteBodyFromStdinOrEditor(cmd.Context(), o.io, o.factory.Config)
137 if err != nil {
138 return err
139 }
140 }
141 // With attachments an empty body means "keep the existing note text".
142 if strings.TrimSpace(body) == "" && len(o.attach) > 0 {
143 body = note.Body
144 }
145 o.body = body
147 return nil
148}
150func (o *updateOptions) validate() error {
151 if strings.TrimSpace(o.body) == "" && len(o.attach) == 0 {
152 return fmt.Errorf("aborted: note has an empty message")
153 }
154 return nil
155}
157func (o *updateOptions) run(ctx context.Context) error {
158 body, err := cmdutils.AppendAttachments(ctx, o.io, o.client, o.repo.FullName(), o.body, o.attach)
159 if err != nil {
160 return err
161 }
162 o.body = body
164 _, _, err = o.client.Discussions.UpdateMergeRequestDiscussionNote(
165 o.repo.FullName(),
166 o.mr.IID,
167 o.discussionID,
168 o.noteID,
169 &gitlab.UpdateMergeRequestDiscussionNoteOptions{
170 Body: &o.body,
171 },
172 gitlab.WithContext(ctx),
173 )
174 if err != nil {
175 return fmt.Errorf("failed to update note: %w", err)
176 }
178 o.io.LogInfof("%s#note_%d\n", o.mr.WebURL, o.noteID)
179 return nil
180}
Diagnostics8 git calls · 235 ms
CallStatusSizeTime0235 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 · 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
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 · 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=1857 git.response.cache=HIT git.response.duration_ms=4 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=2 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
fetch2002.1 KiB221 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=221 git.response.bytes=2164 git.response.cache=MISS git.response.duration_ms=221 git.fetch.wants=8f64744031b90f700eec028d0ec05cf54c62a7e7 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=2164 git.pkt.payload.max=2137 git.sideband.band1.bytes=2137 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
- a5e2beaffe8689da
- Request
- e1d7b20c…
- 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