githubpull-requestreviewgithub-cli

How can I add in-code annotations in PR reviews usign Github's `gh` tool?


On the Github web UI, I can click on a line and say something like:

Good architecture, but please pass the std::vector<std::uint8_t> hugedata as const &, to avoid a copy.

and bundle such comments as one review with a final verdict.

So far, I've only found gh pr review, which only allows me to generally approve/comment/reject a PR that I'm reviewing.


Solution

  • gh doesn't seem to have built in support for this, but you can still use gh api to call the API:

    1. Note the repository owner, repository name, and pull request ID
    2. Get a diff of the pull request so you can get the right files and positions
    gh api -H "Accept: application/vnd.github.v3.diff" /repos/OWNER/REPO/pulls/ID
    
    1. Note any files you want to comment on after +++
    2. Note any positions you want to comment on after @@ (by number of lines after that line)
    3. Create a pull request review with your comments (using the file as path, the line offset from the start of the hunk as position, and your commend as body)
    echo '{ "comments": [{"path": ...,"position": ...,"body": ...}, ...] }' |
      gh api -X POST /repos/OWNER/REPO/pulls/ID/reviews
    
    1. Submit the pull request review on GitHub (alternatively if you want to automate this, add the body and event properties to your review's body)