GitLabgitlab-org
cli
main
1package create
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strings"
9 "github.com/MakeNowJust/heredoc/v2"
10 "github.com/spf13/cobra"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 "gitlab.com/gitlab-org/cli/internal/cmdutils"
15 "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils"
16 "gitlab.com/gitlab-org/cli/internal/glrepo"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19 "gitlab.com/gitlab-org/cli/internal/text"
20)
22type options struct {
23 io *iostreams.IOStreams
24 factory cmdutils.Factory
25 gitlabClient func() (*gitlab.Client, error)
27 // Flags.
28 message string
29 attach []string
30 reply string
31 filePath string
32 line mrutils.LineRange
33 oldLine int
35 // Populated in complete.
36 client *gitlab.Client
37 mr *gitlab.MergeRequest
38 repo glrepo.Interface
39 body string
40}
42func NewCmd(f cmdutils.Factory) *cobra.Command {
43 opts := &options{
44 io: f.IO(),
45 factory: f,
46 gitlabClient: f.GitLabClient,
47 }
49 cmd := &cobra.Command{
50 Use: "create [<id> | <branch>]",
51 Short: "Add a pending review comment to a merge request. (EXPERIMENTAL)",
52 Long: heredoc.Docf(`
53 Add a comment to your pending review. The comment stays visible only to you until you publish the review with %[1]sglab mr note draft publish%[1]s or submit it from the merge request page.
55 On success, the command prints the ID of the pending comment. Pass that ID to %[1]sglab mr note draft update%[1]s or %[1]sglab mr note draft delete%[1]s.
57 Use %[1]s--file%[1]s to place the comment on a specific file in the latest merge request diff version. Combine with %[1]s--line%[1]s (new side) or %[1]s--old-line%[1]s (old/removed side) to target a specific line. Omit both flags for a file-level comment.
59 Use %[1]s--reply%[1]s to reply to an existing discussion thread. The value can be a full discussion ID or a unique prefix of at least 8 characters. Find discussion IDs with %[1]sglab mr note list%[1]s.
61 The flag rules are:
63 - %[1]s--line%[1]s and %[1]s--old-line%[1]s require %[1]s--file%[1]s, and cannot be used together.
64 - %[1]s--file%[1]s and %[1]s--reply%[1]s are mutually exclusive.
66 %[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. The upload happens immediately, even though the comment stays pending. An attachment is content on its own, so a comment with only %[1]s--attach%[1]s does not prompt for a body. When standard input is not a terminal, it is read as the comment body, unless %[1]s--attach -%[1]s reads the file from standard input. The command waits until standard input closes, so to post only the attachment from a script, redirect standard input from %[1]s/dev/null%[1]s.
67 `, "`") + text.ExperimentalString,
68 Example: heredoc.Doc(`
69 # Add a pending comment to merge request 123
70 glab mr note draft create 123 -m "Consider renaming this."
72 # Add a pending comment to the current branch's merge request
73 glab mr note draft create -m "Looks good overall."
75 # Pipe the body from stdin
76 echo "Needs a test." | glab mr note draft create 123
78 # Add a pending diff comment on line 42 of main.go
79 glab mr note draft create 123 --file main.go --line 42 -m "Off-by-one?"
81 # Add a pending diff comment on lines 10-15
82 glab mr note draft create 123 --file main.go --line 10:15 -m "Extract this block."
84 # Add a pending diff comment on a removed line
85 glab mr note draft create 123 --file main.go --old-line 7 -m "Why was this removed?"
87 # Add a pending reply to an existing discussion thread
88 glab mr note draft create 123 --reply abc12345 -m "Agreed."
90 # Keep the ID of the pending comment for a later update
91 id=$(glab mr note draft create 123 -m "First pass")
92 `),
93 Args: cobra.MaximumNArgs(1),
94 Annotations: map[string]string{
95 mcpannotations.Destructive: "true",
96 },
97 RunE: func(cmd *cobra.Command, args []string) error {
98 if err := opts.validate(); err != nil {
99 return err
100 }
101 if err := opts.complete(cmd, args); err != nil {
102 return err
103 }
104 return opts.run(cmd.Context())
105 },
106 }
108 fl := cmd.Flags()
109 fl.StringVarP(&opts.message, "message", "m", "", "Comment message. If omitted, opens an editor or reads from stdin.")
110 fl.StringVar(&opts.reply, "reply", "", "Reply to an existing discussion. Accepts a full discussion ID or a unique prefix of at least 8 characters.")
111 fl.StringVar(&opts.filePath, "file", "", "File path for a diff comment, like <path/to/file>. Targets the latest merge request diff version.")
112 fl.Var(&opts.line, "line", "Line in the new version. A single line number, like 42, or a range, like 10:15.")
113 fl.IntVar(&opts.oldLine, "old-line", 0, "Line in the old version, for commenting on a removed line.")
114 cmdutils.AddAttachFlag(cmd, &opts.attach, "comment")
116 cmd.MarkFlagsMutuallyExclusive("reply", "file")
117 cmd.MarkFlagsMutuallyExclusive("line", "old-line")
119 return cmd
120}
122func (o *options) complete(cmd *cobra.Command, args []string) error {
123 client, err := o.gitlabClient()
124 if err != nil {
125 return err
126 }
127 o.client = client
129 mr, repo, err := mrutils.MRFromArgs(cmd.Context(), o.factory, args, "any")
130 if err != nil {
131 return err
132 }
133 o.mr = mr
134 o.repo = repo
136 o.body = o.message
137 if strings.TrimSpace(o.body) == "" && !cmdutils.SkipNoteBody(o.io, o.attach) {
138 o.body, err = mrutils.NoteBodyFromStdinOrEditor(cmd.Context(), o.io, o.factory.Config)
139 if err != nil {
140 return err
141 }
142 }
143 if strings.TrimSpace(o.body) == "" && len(o.attach) == 0 {
144 return errors.New("aborted: comment has an empty message")
145 }
147 return nil
148}
150// validate checks only flag values, so it runs before complete to reject bad
151// input before any API call or editor prompt.
152func (o *options) validate() error {
153 if o.reply != "" && len(o.reply) < 8 {
154 return fmt.Errorf("discussion ID prefix must be at least 8 characters, got %d", len(o.reply))
155 }
156 if (o.line.Start != 0 || o.oldLine != 0) && o.filePath == "" {
157 return errors.New("--line and --old-line require --file")
158 }
159 return nil
160}
162func (o *options) run(ctx context.Context) error {
163 createOpts := &gitlab.CreateDraftNoteOptions{}
165 switch {
166 case o.filePath != "":
167 position, err := mrutils.DiffPosition(ctx, o.client, o.repo.FullName(), o.mr.IID, o.filePath, o.line, o.oldLine)
168 if err != nil {
169 return err
170 }
171 createOpts.Position = position
172 case o.reply != "":
173 discussionID, err := mrutils.ResolveDiscussionID(ctx, o.client, o.repo.FullName(), o.mr.IID, o.reply)
174 if err != nil {
175 return err
176 }
177 createOpts.InReplyToDiscussionID = &discussionID
178 }
180 // Uploads cannot be undone, so they run only after the position and reply
181 // target have resolved.
182 body, err := cmdutils.AppendAttachments(ctx, o.io, o.client, o.repo.FullName(), o.body, o.attach)
183 if err != nil {
184 return err
185 }
186 createOpts.Note = &body
188 draft, _, err := o.client.DraftNotes.CreateDraftNote(o.repo.FullName(), o.mr.IID, createOpts, gitlab.WithContext(ctx))
189 if err != nil {
190 return fmt.Errorf("failed to create pending review comment: %w", err)
191 }
193 o.io.LogInfof("%d\n", draft.ID)
194 return nil
195}
Diagnostics10 git calls · 326 ms
CallStatusSizeTime0326 ms
discover200185 B5 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 KiB4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=4 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=2 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=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
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=3 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:0200746 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=746 git.response.cache=HIT git.response.duration_ms=3 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=540 git.response.cache=HIT git.response.duration_ms=4 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 · hit5 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=5 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:0200138 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=301 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=53bbd39c78f961e0a0fe37ba4fcbf1fe21e9024c 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=138 git.pkt.payload.max=111 git.sideband.band1.bytes=111 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=111 git.pack.trailer=0704bbe2e9084500bbea5f0ef50ecd394148a127Open pack map
fetch2002.7 KiB301 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2732 git.response.cache=MISS git.response.duration_ms=301 git.fetch.wants=e7ca0215a661ab798b73595b68e92e433a8d7793 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=2732 git.pkt.payload.max=2705 git.sideband.band1.bytes=2705 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
- a5e29579d8b22407
- Request
- 454db5a4…
- 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