| 10 | ) |
| 11 | |
| 12 | func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) error) *cobra.Command { |
| 13 | opts := &shared.CommentableOptions{ |
| 14 | IO: f.IOStreams, |
| 15 | HttpClient: f.HttpClient, |
| 16 | Config: f.Config, |
| 17 | EditSurvey: shared.CommentableEditSurvey(f.Config, f.IOStreams), |
| 18 | InteractiveEditSurvey: shared.CommentableInteractiveEditSurvey(f.Config, f.IOStreams), |
| 19 | ConfirmSubmitSurvey: shared.CommentableConfirmSubmitSurvey(f.Prompter), |
| 20 | ConfirmCreateIfNoneSurvey: shared.CommentableInteractiveCreateIfNoneSurvey(f.Prompter), |
| 21 | ConfirmDeleteLastComment: shared.CommentableConfirmDeleteLastComment(f.Prompter), |
| 22 | OpenInBrowser: f.Browser.Browse, |
| 23 | } |
| 24 | |
| 25 | var bodyFile string |
| 26 | |
| 27 | cmd := &cobra.Command{ |
| 28 | Use: "comment [<number> | <url> | <branch>]", |
| 29 | Short: "Add a comment to a pull request", |
| 30 | Long: heredoc.Docf(` |
| 31 | Add a comment to a GitHub pull request. |
| 32 | |
| 33 | Without body text or attachments supplied through flags, the command will |
| 34 | interactively prompt for the comment text. |
| 35 | |
| 36 | Use %[1]s--attach%[1]s to upload an image or video. If the body already references an |
| 37 | attached file, such as %[1]s%[1]s, that reference is rewritten to point |
| 38 | at the uploaded asset. Any attached file the body does not reference is appended |
| 39 | to the end of the comment. |
| 40 | You can attach up to 50 files per command. |
| 41 | |
| 42 | Alt text for an image follows the path after %[1]s#%[1]s, as in |
| 43 | %[1]s--attach './login.png#The login error state'%[1]s. Without it the filename is used. |
| 44 | A reference already in the body keeps the alt text written there. Video renders |
| 45 | as a player and has no alt text, so it cannot be given any. |
| 46 | `, "`"), |
| 47 | Example: heredoc.Doc(` |
| 48 | # Add a comment to a pull request |
| 49 | $ gh pr comment 13 --body "Hi from GitHub CLI" |
| 50 | |
| 51 | # Attach a screenshot, with alt text after "#" |
| 52 | $ gh pr comment 13 --attach './login.png#The login error state' |
| 53 | |
| 54 | # Attach multiple files by repeating the flag |
| 55 | $ gh pr comment 13 --attach ./before.png --attach ./after.png |
| 56 | `), |
| 57 | Args: cobra.MaximumNArgs(1), |
| 58 | PreRunE: func(cmd *cobra.Command, args []string) error { |
| 59 | if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 { |
| 60 | return cmdutil.FlagErrorf("argument required when using the --repo flag") |
| 61 | } |
| 62 | var selector string |
| 63 | if len(args) > 0 { |
| 64 | selector = args[0] |
| 65 | } |
| 66 | fields := []string{"id", "url"} |
| 67 | if opts.EditLast || opts.DeleteLast { |
| 68 | fields = append(fields, "comments") |
| 69 | } |