1package comment 2 3import ( 4 "github.com/MakeNowJust/heredoc" 5 "github.com/cli/cli/v2/internal/ghrepo" 6 "github.com/cli/cli/v2/pkg/cmd/pr/shared" 7 "github.com/cli/cli/v2/pkg/cmdutil" 8 "github.com/spf13/cobra" 9) 10 11func 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: "Create a new pr comment", 26 Long: heredoc.Doc(` 27 Create a new pr comment. 28 29 Without an argument, the pull request that belongs to the current branch 30 is selected. 31 32 With '--web', comment on the pull request in a web browser instead. 33 `), 34 Example: heredoc.Doc(` 35 $ gh pr comment 22 --body "This looks great, lets get it deployed." 36 `), 37 Args: cobra.MaximumNArgs(1), 38 PreRunE: func(cmd *cobra.Command, args []string) error { 39 if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 { 40 return cmdutil.FlagErrorf("argument required when using the --repo flag") 41 } 42 var selector string 43 if len(args) > 0 { 44 selector = args[0] 45 } 46 finder := shared.NewFinder(f) 47 opts.RetrieveCommentable = func() (shared.Commentable, ghrepo.Interface, error) { 48 return finder.Find(shared.FindOptions{ 49 Selector: selector, 50 Fields: []string{"id", "url"}, 51 }) 52 } 53 return shared.CommentablePreRun(cmd, opts) 54 }, 55 RunE: func(cmd *cobra.Command, args []string) error { 56 if bodyFile != "" { 57 b, err := cmdutil.ReadFile(bodyFile, opts.IO.In) 58 if err != nil { 59 return err 60 } 61 opts.Body = string(b) 62 } 63 64 if runF != nil { 65 return runF(opts) 66 } 67 return shared.CommentableRun(opts) 68 }, 69 } 70 71 cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Supply a body. Will prompt for one otherwise.") 72 cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)") 73 cmd.Flags().BoolP("editor", "e", false, "Add body using editor") 74 cmd.Flags().BoolP("web", "w", false, "Add body in browser") 75 76 return cmd 77} 78