github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/comment/comment.go (about)

     1  package comment
     2  
     3  import (
     4  	"github.com/MakeNowJust/heredoc"
     5  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
     6  	"github.com/ungtb10d/cli/v2/pkg/cmd/pr/shared"
     7  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) error) *cobra.Command {
    12  	opts := &shared.CommentableOptions{
    13  		IO:                    f.IOStreams,
    14  		HttpClient:            f.HttpClient,
    15  		EditSurvey:            shared.CommentableEditSurvey(f.Config, f.IOStreams),
    16  		InteractiveEditSurvey: shared.CommentableInteractiveEditSurvey(f.Config, f.IOStreams),
    17  		ConfirmSubmitSurvey:   shared.CommentableConfirmSubmitSurvey,
    18  		OpenInBrowser:         f.Browser.Browse,
    19  	}
    20  
    21  	var bodyFile string
    22  
    23  	cmd := &cobra.Command{
    24  		Use:   "comment [<number> | <url> | <branch>]",
    25  		Short: "Add a comment to a pull request",
    26  		Long: heredoc.Doc(`
    27  			Add a comment to a GitHub pull request.
    28  
    29  			Without the body text supplied through flags, the command will interactively
    30  			prompt for the comment text.
    31  		`),
    32  		Example: heredoc.Doc(`
    33  			$ gh pr comment 13 --body "Hi from GitHub CLI"
    34  		`),
    35  		Args: cobra.MaximumNArgs(1),
    36  		PreRunE: func(cmd *cobra.Command, args []string) error {
    37  			if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
    38  				return cmdutil.FlagErrorf("argument required when using the --repo flag")
    39  			}
    40  			var selector string
    41  			if len(args) > 0 {
    42  				selector = args[0]
    43  			}
    44  			fields := []string{"id", "url"}
    45  			if opts.EditLast {
    46  				fields = append(fields, "comments")
    47  			}
    48  			finder := shared.NewFinder(f)
    49  			opts.RetrieveCommentable = func() (shared.Commentable, ghrepo.Interface, error) {
    50  				return finder.Find(shared.FindOptions{
    51  					Selector: selector,
    52  					Fields:   fields,
    53  				})
    54  			}
    55  			return shared.CommentablePreRun(cmd, opts)
    56  		},
    57  		RunE: func(cmd *cobra.Command, args []string) error {
    58  			if bodyFile != "" {
    59  				b, err := cmdutil.ReadFile(bodyFile, opts.IO.In)
    60  				if err != nil {
    61  					return err
    62  				}
    63  				opts.Body = string(b)
    64  			}
    65  
    66  			if runF != nil {
    67  				return runF(opts)
    68  			}
    69  			return shared.CommentableRun(opts)
    70  		},
    71  	}
    72  
    73  	cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "The comment body `text`")
    74  	cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)")
    75  	cmd.Flags().BoolP("editor", "e", false, "Skip prompts and open the text editor to write the body in")
    76  	cmd.Flags().BoolP("web", "w", false, "Open the web browser to write the comment")
    77  	cmd.Flags().BoolVar(&opts.EditLast, "edit-last", false, "Edit the last comment of the same author")
    78  
    79  	return cmd
    80  }