package publish import ( "context" "errors" "fmt" "slices" "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/dbg" "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" "gitlab.com/gitlab-org/cli/internal/utils" ) type options struct { io *iostreams.IOStreams factory cmdutils.Factory gitlabClient func() (*gitlab.Client, error) // Flags. message string internal bool reviewerState string yes bool // Populated in complete. client *gitlab.Client mr *gitlab.MergeRequest repo glrepo.Interface } func NewCmd(f cmdutils.Factory) *cobra.Command { opts := &options{ io: f.IO(), factory: f, gitlabClient: f.GitLabClient, } cmd := &cobra.Command{ Use: "publish [ | ]", Short: "Publish all your pending review comments on a merge request. (EXPERIMENTAL)", Long: heredoc.Docf(` Publish every pending review comment you created on a merge request, as when you submit a review from the merge request page. Only your own pending comments are published; other reviewers' pending comments are unaffected. Check what is pending with %[1]sglab mr note draft list%[1]s first. Use %[1]s--message%[1]s to add a summary note to the merge request when publishing, and %[1]s--internal%[1]s to restrict that summary to project members with at least the Reporter role. Use %[1]s--reviewer-state%[1]s to set your review state on the merge request. Neither state records a formal approval; use %[1]sglab mr approve%[1]s to approve. If you are the merge request author, GitLab ignores this flag unless you are also listed as one of the reviewers. Unless you pass %[1]s--yes%[1]s, the command shows the number of pending comments and prompts you to confirm. When not running interactively, %[1]s--yes%[1]s is required. If there are no pending comments, the command exits with an error. `, "`") + text.ExperimentalString, Example: heredoc.Doc(` # Publish your pending review comments on merge request 123 glab mr note draft publish 123 # Publish the current branch's pending review comments glab mr note draft publish # Publish with a summary note and request changes glab mr note draft publish 123 -m "A few blockers, see the comments." --reviewer-state requested_changes # Publish without confirmation glab mr note draft publish 123 --yes `), Args: cobra.MaximumNArgs(1), Annotations: map[string]string{ mcpannotations.Destructive: "true", }, RunE: func(cmd *cobra.Command, args []string) error { if err := opts.validate(); err != nil { return err } if err := opts.complete(cmd, args); err != nil { return err } return opts.run(cmd.Context()) }, } fl := cmd.Flags() fl.StringVarP(&opts.message, "message", "m", "", "Summary note to add to the merge request when publishing.") fl.BoolVar(&opts.internal, "internal", false, "Mark the summary note as internal. Requires --message.") fl.Var( cmdutils.NewEnumValue([]string{"requested_changes", "reviewed"}, "", &opts.reviewerState), "reviewer-state", "Set the review state after publishing: requested_changes, reviewed. Does not record a formal approval.", ) fl.BoolVarP(&opts.yes, "yes", "y", false, "Skip confirmation prompt.") return cmd } // validate checks only flag values, so it runs before complete to reject bad // input without any API calls. func (o *options) validate() error { if o.message != "" && strings.TrimSpace(o.message) == "" { return errors.New("--message cannot be empty") } if o.internal && strings.TrimSpace(o.message) == "" { return errors.New("--internal requires --message") } if !o.yes && !o.io.PromptEnabled() { return cmdutils.FlagError{Err: errors.New("--yes required when not running interactively")} } return nil } func (o *options) 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 return nil } func (o *options) run(ctx context.Context) error { drafts, err := mrutils.ListAllDraftNotes(ctx, o.client, o.repo.FullName(), o.mr.IID) if err != nil { return err } if len(drafts) == 0 { return fmt.Errorf("no pending review comments on !%d", o.mr.IID) } if !o.yes { var confirmed bool if err := o.io.Confirm(ctx, &confirmed, fmt.Sprintf("Publish %s?", utils.Pluralize(len(drafts), "pending review comment"))); err != nil { return err } if !confirmed { o.io.LogInfo("Aborted.") return nil } } publishOpts := &gitlab.PublishAllDraftNotesOptions{} if message := strings.TrimSpace(o.message); message != "" { publishOpts.Note = new(message) } if o.internal { publishOpts.Internal = new(true) } if o.reviewerState != "" { publishOpts.ReviewerState = new(o.reviewerState) } //nolint:staticcheck // PublishAllDraftNotesWithOptions is the only way to pass note/internal/reviewer_state in SDK v3; options merge into PublishAllDraftNotes in 4.0. _, err = o.client.DraftNotes.PublishAllDraftNotesWithOptions( o.repo.FullName(), o.mr.IID, publishOpts, gitlab.WithContext(ctx), ) if err != nil { return fmt.Errorf("failed to publish pending review comments: %w", err) } o.io.LogInfof("✓ Published %s. %s\n", utils.Pluralize(len(drafts), "pending review comment"), o.mr.WebURL) // bulk_publish reports success but drops reviewer_state when the caller // authored the MR and is not one of its reviewers. if o.reviewerState != "" && o.mr.Author != nil { user, _, err := o.client.Users.CurrentUser(gitlab.WithContext(ctx)) if err != nil { dbg.Debugf("skipping --reviewer-state author check, current user lookup failed: %v", err) } else if user.ID == o.mr.Author.ID && !slices.ContainsFunc(o.mr.Reviewers, func(r *gitlab.BasicUser) bool { return r.ID == user.ID }) { o.io.LogErrorf("%s GitLab did not set your review state: you authored this merge request and are not one of its reviewers.\n", o.io.Color().WarnIcon()) } } return nil }