package note import ( "context" "fmt" "strings" "github.com/MakeNowJust/heredoc/v2" "github.com/spf13/cobra" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "gitlab.com/gitlab-org/cli/internal/cmdutils" "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils" "gitlab.com/gitlab-org/cli/internal/glrepo" "gitlab.com/gitlab-org/cli/internal/iostreams" "gitlab.com/gitlab-org/cli/internal/mcpannotations" "gitlab.com/gitlab-org/cli/internal/text" ) type createOptions struct { io *iostreams.IOStreams factory cmdutils.Factory gitlabClient func() (*gitlab.Client, error) // Flags. message string attach []string unique bool reply string filePath string line mrutils.LineRange oldLine int resolvable bool internal bool // Populated in complete. client *gitlab.Client mr *gitlab.MergeRequest repo glrepo.Interface body string position *gitlab.PositionOptions } func NewCmdCreate(f cmdutils.Factory) *cobra.Command { opts := &createOptions{ io: f.IO(), factory: f, gitlabClient: f.GitLabClient, } cmd := &cobra.Command{ Use: "create [ | ]", Short: "Create a comment or discussion on a merge request. (EXPERIMENTAL)", Long: heredoc.Docf(` Add a comment to a merge request. By default, the command creates the comment as a new discussion thread. Use %[1]s--resolvable=false%[1]s to create a non-resolvable note instead. Non-resolvable notes do not block merging when the project requires **All threads must be resolved**. Use this option for automation or status updates that do not need a human to resolve them. Use %[1]s--internal%[1]s to create an internal note, which only project members can view. A reply to an internal thread is itself internal. When combined with %[1]s--reply%[1]s, %[1]s--internal%[1]s checks the target thread's visibility and fails if the thread is public. Replying to an internal thread without %[1]s--internal%[1]s still produces an internal reply. Use %[1]s--reply%[1]s to add a note to an existing discussion thread instead of starting a new one. 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. Human-readable output uses eight characters before the ellipsis, for example %[1]s[discussion: abc12345…]%[1]s; pass only those characters, for example %[1]s--reply abc12345%[1]s. To get a full ID, use the %[1]sid%[1]s field of each discussion object: %[1]sglab mr note list -F json | jq -r '.[].id'%[1]s. Use %[1]s--file%[1]s to place a diff 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. To add the comment to a pending review instead of publishing it immediately, use %[1]sglab mr note draft create%[1]s. The flag rules are: - %[1]s--line%[1]s and %[1]s--old-line%[1]s require %[1]s--file%[1]s, and cannot be used together. - %[1]s--file%[1]s, %[1]s--reply%[1]s, and %[1]s--unique%[1]s are mutually exclusive. - %[1]s--resolvable=false%[1]s cannot be combined with %[1]s--reply%[1]s or %[1]s--file%[1]s (and by extension %[1]s--line%[1]s or %[1]s--old-line%[1]s). - %[1]s--internal%[1]s cannot be combined with %[1]s--file%[1]s (and by extension %[1]s--line%[1]s or %[1]s--old-line%[1]s), nor with %[1]s--resolvable=true%[1]s unless %[1]s--reply%[1]s is also given. - %[1]s--attach%[1]s and %[1]s--unique%[1]s are mutually exclusive, because every upload gets a fresh URL and so an attached comment can never match an existing one. %[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. 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. `, "`") + text.ExperimentalString, Example: heredoc.Doc(` # Add a comment to merge request 123 glab mr note create 123 -m "Looks good to me!" # Add a comment to the current branch's merge request glab mr note create -m "LGTM" # Open editor to compose the message glab mr note create 123 # Pipe from stdin echo "LGTM" | glab mr note create 123 # Read the body from a file glab mr note create 123 < plan.md # Skip if already posted glab mr note create 123 -m "LGTM" --unique # Create a non-resolvable note, for example for bot or CI status updates glab mr note create 123 -m "Build status: green" --resolvable=false # Create an internal note, visible only to project members glab mr note create 123 -m "Rotating the leaked token now." --internal # Reply to an internal thread, refusing to post if that thread is public glab mr note create 123 --reply abc12345 --internal -m "Patch is ready." # Reply to an existing discussion thread glab mr note create 123 --reply abc12345 -m "I agree!" # Add a diff comment on line 42 of main.go glab mr note create 123 --file main.go --line 42 -m "Needs refactoring" # Add a diff comment on lines 10-15 (multiline range) glab mr note create 123 --file main.go --line 10:15 -m "Extract this block" # Add a diff comment on a removed line (old side) glab mr note create 123 --file main.go --old-line 7 -m "Why was this removed?" # Add a file-level diff comment (no line specified) glab mr note create 123 --file main.go -m "General comment on this file" # Attach a screenshot alongside the message glab mr note create 123 -m "Renders wrong here." --attach ./screenshot.png # Attach an image piped from the clipboard pngpaste - | glab mr note create 123 --attach - `), Args: cobra.MaximumNArgs(1), Annotations: map[string]string{ mcpannotations.Destructive: "true", }, RunE: func(cmd *cobra.Command, args []string) error { if err := opts.validateFlags(cmd); err != nil { return err } if err := opts.complete(cmd, args); err != nil { return err } if err := opts.validate(); err != nil { return err } return opts.run(cmd.Context()) }, } fl := cmd.Flags() fl.StringVarP(&opts.message, "message", "m", "", "Comment or note message.") fl.BoolVar(&opts.unique, "unique", false, "Don't create a note if a note with the same body already exists. Reads all merge request comments first.") fl.StringVar(&opts.reply, "reply", "", "Reply to an existing discussion. Accepts a full discussion ID or a unique prefix of at least 8 characters.") fl.StringVar(&opts.filePath, "file", "", "File path for a diff comment, like . Targets the latest merge request diff version.") fl.Var(&opts.line, "line", "Line in the new version. A single line number, like 42, or a range, like 10:15.") fl.IntVar(&opts.oldLine, "old-line", 0, "Line in the old version, for commenting on a removed line.") fl.BoolVar(&opts.resolvable, "resolvable", true, "Create the note as a resolvable discussion thread. Set to false to create a non-resolvable note.") fl.BoolVar(&opts.internal, "internal", false, "Create the note as an internal note, visible only to project members.") cmdutils.AddAttachFlag(cmd, &opts.attach, "comment") // Each upload gets a fresh URL, so an attached note never matches an // existing one and --unique could not skip anything. cmd.MarkFlagsMutuallyExclusive("unique", "attach") cmd.MarkFlagsMutuallyExclusive("reply", "unique") cmd.MarkFlagsMutuallyExclusive("reply", "file") cmd.MarkFlagsMutuallyExclusive("unique", "file") cmd.MarkFlagsMutuallyExclusive("line", "old-line") cmd.MarkFlagsMutuallyExclusive("internal", "file") return cmd } func (o *createOptions) complete(cmd *cobra.Command, args []string) error { client, err := o.gitlabClient() if err != nil { return err } o.client = client mr, repo, err := mrutils.MRFromArgs(cmd.Context(), o.factory, args, "any") if err != nil { return err } o.mr = mr o.repo = repo body := o.message if strings.TrimSpace(body) == "" && !cmdutils.SkipNoteBody(o.io, o.attach) { body, err = mrutils.NoteBodyFromStdinOrEditor(cmd.Context(), o.io, o.factory.Config) if err != nil { return err } } o.body = body if o.filePath != "" { o.position, err = mrutils.DiffPosition(cmd.Context(), o.client, o.repo.FullName(), o.mr.IID, o.filePath, o.line, o.oldLine) if err != nil { return err } } return nil } func (o *createOptions) validateFlags(cmd *cobra.Command) error { if o.internal { if o.reply == "" { if cmd.Flags().Changed("resolvable") && o.resolvable { return fmt.Errorf("--internal cannot be used with --resolvable=true") } o.resolvable = false } } if !o.resolvable { if o.reply != "" { return fmt.Errorf("--resolvable=false cannot be used with --reply") } if o.filePath != "" { return fmt.Errorf("--resolvable=false cannot be used with --file, --line, or --old-line") } } return nil } func (o *createOptions) validate() error { if strings.TrimSpace(o.body) == "" && len(o.attach) == 0 { return fmt.Errorf("aborted: note has an empty message") } if o.reply != "" && len(o.reply) < 8 { return fmt.Errorf("discussion ID prefix must be at least 8 characters, got %d", len(o.reply)) } if (o.line.Start != 0 || o.oldLine != 0) && o.filePath == "" { return fmt.Errorf("--line and --old-line require --file") } return nil } func (o *createOptions) run(ctx context.Context) error { body, err := cmdutils.AppendAttachments(ctx, o.io, o.client, o.repo.FullName(), o.body, o.attach) if err != nil { return err } o.body = body switch { case o.reply != "": return o.runReply(ctx) case o.unique: found, err := deduplicateNote(o.io, o.client, o.repo.FullName(), o.mr.IID, o.body, o.mr.WebURL) if err != nil { return err } if found { return nil } } if !o.resolvable { return o.runCreateNote(ctx) } return o.runCreate(ctx) } func (o *createOptions) runCreate(ctx context.Context) error { createOpts := &gitlab.CreateMergeRequestDiscussionOptions{Body: &o.body} if o.position != nil { createOpts.Position = o.position } disc, _, err := o.client.Discussions.CreateMergeRequestDiscussion( o.repo.FullName(), o.mr.IID, createOpts, gitlab.WithContext(ctx), ) if err != nil { return fmt.Errorf("failed to create discussion: %w", err) } if len(disc.Notes) == 0 { return fmt.Errorf("discussion created but returned no notes") } o.io.LogInfof("%s#note_%d\n", o.mr.WebURL, disc.Notes[0].ID) return nil } func (o *createOptions) runCreateNote(ctx context.Context) error { createOpts := &gitlab.CreateMergeRequestNoteOptions{Body: &o.body} if o.internal { createOpts.Internal = &o.internal } note, _, err := o.client.Notes.CreateMergeRequestNote( o.repo.FullName(), o.mr.IID, createOpts, gitlab.WithContext(ctx), ) if err != nil { return fmt.Errorf("failed to create note: %w", err) } o.io.LogInfof("%s#note_%d\n", o.mr.WebURL, note.ID) return nil } func (o *createOptions) runReply(ctx context.Context) error { discussion, err := mrutils.ResolveDiscussion(ctx, o.client, o.repo.FullName(), o.mr.IID, o.reply) if err != nil { return err } discussionID := discussion.ID if o.internal && !mrutils.IsInternalDiscussion(discussion) { return fmt.Errorf( "--internal was requested but discussion %s is not internal, so the reply would be publicly visible", mrutils.TruncateDiscussionID(discussionID), ) } note, _, err := o.client.Discussions.AddMergeRequestDiscussionNote( o.repo.FullName(), o.mr.IID, discussionID, &gitlab.AddMergeRequestDiscussionNoteOptions{Body: &o.body}, gitlab.WithContext(ctx), ) if err != nil { return fmt.Errorf("failed to add reply: %w", err) } o.io.LogInfof("%s#note_%d\n", o.mr.WebURL, note.ID) return nil }