github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/comment/comment.go (about)

     1  package comment
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/MakeNowJust/heredoc"
     7  	"github.com/cli/cli/internal/ghrepo"
     8  	"github.com/cli/cli/pkg/cmd/pr/shared"
     9  	"github.com/cli/cli/pkg/cmdutil"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) error) *cobra.Command {
    14  	opts := &shared.CommentableOptions{
    15  		IO:                    f.IOStreams,
    16  		HttpClient:            f.HttpClient,
    17  		EditSurvey:            shared.CommentableEditSurvey(f.Config, f.IOStreams),
    18  		InteractiveEditSurvey: shared.CommentableInteractiveEditSurvey(f.Config, f.IOStreams),
    19  		ConfirmSubmitSurvey:   shared.CommentableConfirmSubmitSurvey,
    20  		OpenInBrowser:         f.Browser.Browse,
    21  	}
    22  
    23  	var bodyFile string
    24  
    25  	cmd := &cobra.Command{
    26  		Use:   "comment [<number> | <url> | <branch>]",
    27  		Short: "Create a new pr comment",
    28  		Long: heredoc.Doc(`
    29  			Create a new pr comment.
    30  
    31  			Without an argument, the pull request that belongs to the current branch
    32  			is selected.			
    33  
    34  			With '--web', comment on the pull request in a web browser instead.
    35  		`),
    36  		Example: heredoc.Doc(`
    37  			$ gh pr comment 22 --body "This looks great, lets get it deployed."
    38  		`),
    39  		Args: cobra.MaximumNArgs(1),
    40  		PreRunE: func(cmd *cobra.Command, args []string) error {
    41  			if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
    42  				return &cmdutil.FlagError{Err: errors.New("argument required when using the --repo flag")}
    43  			}
    44  			var selector string
    45  			if len(args) > 0 {
    46  				selector = args[0]
    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:   []string{"id", "url"},
    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", "", "Supply a body. Will prompt for one otherwise.")
    74  	cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file`")
    75  	cmd.Flags().BoolP("editor", "e", false, "Add body using editor")
    76  	cmd.Flags().BoolP("web", "w", false, "Add body in browser")
    77  
    78  	return cmd
    79  }