github-cli

Use CLI to write a comment pinned to a file and line number on a github pull request


I would like to comment on a pull request from a script using gh github cli. When using the web interface one could click on a change, like in: How can I manually add suggestions in code reviews on GitHub? and add a comment but I can't find any such options for the cli tool.

Neither the linked manual or gh --help not offer any further insights. Is there a magic markup format to be used in the body or is this not supported from cli?

$ gh pr comment --help
Add a comment to a GitHub pull request.

Without the body text supplied through flags, the command will interactively
prompt for the comment text.


USAGE
  gh pr comment [<number> | <url> | <branch>] [flags]

FLAGS
  -b, --body text        The comment body text
  -F, --body-file file   Read body text from file (use "-" to read from standard input)
      --edit-last        Edit the last comment of the same author
  -e, --editor           Skip prompts and open the text editor to write the body in
  -w, --web              Open the web browser to write the comment

INHERITED FLAGS
      --help                     Show help for command
  -R, --repo [HOST/]OWNER/REPO   Select another repository using the [HOST/]OWNER/REPO format

EXAMPLES
  $ gh pr comment 13 --body "Hi from GitHub CLI"

LEARN MORE
  Use `gh <command> <subcommand> --help` for more information about a command.
  Read the manual at https://cli.github.com/manual

Solution

  • The features are not available in CLI with the gh pr comment command but they are available in the REST API which can be used by the gh cli with a command like this:

    # GitHub CLI api
    # https://cli.github.com/manual/gh_api
    
    gh api \
      --method POST \
      -H "Accept: application/vnd.github+json" \
      -H "X-GitHub-Api-Version: 2022-11-28" \
      /repos/OWNER/REPO/pulls/PULL_NUMBER/comments \
      -f body='Great stuff!' \
     -f commit_id='6dcb09b5b57875f334f61aebed695e2e4193db5e' \
     -f path='file1.txt' \
     -F start_line=1 \
     -f start_side='RIGHT' \
     -F line=2 \
     -f side='RIGHT'