npx skills add ...
npx skills add neolabhq/context-engineering-kit --skill attach-review-to-pr
Add line-specific review comments to pull requests using GitHub CLI API
npx skills add neolabhq/context-engineering-kit --skill attach-review-to-pr
This guide explains how to add line-specific review comments to pull requests using the GitHub CLI (gh) API or mcp__github_inline_comment__create_inline_comment if it not available, similar to how the GitHub UI allows commenting on specific lines of code.
If available, use the mcp__github_inline_comment__create_inline_comment MCP tool for posting line-specific inline comments on pull requests. This approach provides better integration with GitHub's UI and is the recommended method.
Fallback: If the MCP tool is not available, use the GitHub CLI (gh) API methods described below:
/comments endpoint (see Adding a Single Line-Specific Comment)/reviews endpoint (see Adding Multiple Line-Specific Comments Together)While gh pr review provides basic review functionality (approve, request changes, general comments), it does not support line-specific comments directly. To add comments on specific lines of code, you must use the lower-level gh api command to call GitHub's REST API directly.
GitHub CLI installed and authenticated:
Access to the repository and pull request you want to review
GitHub has two types of PR comments:
Review comments can be added in two ways:
/pulls/{pr}/comments endpoint/pulls/{pr}/reviews endpoint| Parameter | Type | Required | Description |
|---|---|---|---|
body | string | Yes | The text of the review comment (supports Markdown) |
commit_id | string | Yes | The SHA of the commit to comment on |
path | string | Yes | Relative path to the file being commented on |
line | integer | Yes | The line number in the diff (use -F for integers) |
side | string | Yes | RIGHT for new/modified lines, LEFT for deleted lines |
start_line | integer | No | For multi-line comments, the starting line |
start_side | string | No | For multi-line comments, the starting side |
-f (--field) - For string values-F (--field) - For integer values (note the capital F)The line parameter refers to the position in the diff, not the absolute line number in the file:
start_line and line to specify the rangeOn success, returns a JSON object with comment details:
To add multiple comments across different files in a single review, use the /reviews endpoint with JSON input.
| Event | Description | When to Use |
|---|---|---|
COMMENT | General review comment | Just leaving feedback without approval |
APPROVE | Approve the PR | Changes look good, ready to merge |
REQUEST_CHANGES | Request changes | Issues that must be fixed before merge |
Error Message:
Cause: GitHub only allows one pending (unsubmitted) review per user per PR. If you previously started a review through the UI or API and didn't submit it, it blocks new review creation.
Solution 1: Submit the pending review
Solution 2: Use the single comment endpoint instead
Failed Attempt:
Error:
Solution: Use JSON input via heredoc:
Error Message:
Cause: The line number doesn't exist in the diff for this file.
Solutions:
commit_id (the latest commit in the PR)Error Message:
Solution: Get the latest commit SHA:
Before adding comments, gather necessary information:
Use the review endpoint to group related comments:
Creates a review comment on a specific line.
Endpoint: https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/comments
Parameters:
body (string, required): Comment textcommit_id (string, required): SHA of commitpath (string, required): Relative file pathline (integer, required): Line number in diffside (string, required): "LEFT" or "RIGHT"start_line (integer, optional): Start line for multi-linestart_side (string, optional): Start side for multi-lineCreates a review with optional line-specific comments.
Endpoint: https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews
Parameters:
event (string, required): "APPROVE", "REQUEST_CHANGES", or "COMMENT"body (string, optional): Overall review commentcomments (array, optional): Array of comment objectscommit_id (string, optional): SHA of commit to reviewComment Object:
path (string, required): Relative file pathbody (string, required): Comment textline (integer, required): Line number in diffside (string, required): "LEFT" or "RIGHT"start_line (integer, optional): Start line for multi-linestart_side (string, optional): Start side for multi-line