$npx -y skills add NeoLabHQ/context-engineering-kit --skill attach-review-to-prAdd line-specific review comments to pull requests using GitHub CLI API
| 1 | # How to Attach Line-Specific Review Comments to Pull Requests |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## Preferred Approach: Using MCP GitHub Tools |
| 6 | |
| 7 | **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. |
| 8 | |
| 9 | **Fallback**: If the MCP tool is not available, use the GitHub CLI (`gh`) API methods described below: |
| 10 | |
| 11 | - For single comments: Use the `/comments` endpoint (see [Adding a Single Line-Specific Comment](#adding-a-single-line-specific-comment)) |
| 12 | - For multiple comments: Use the `/reviews` endpoint (see [Adding Multiple Line-Specific Comments Together](#adding-multiple-line-specific-comments-together)) |
| 13 | |
| 14 | ## Overview |
| 15 | |
| 16 | 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. |
| 17 | |
| 18 | ## Prerequisites |
| 19 | |
| 20 | 1. GitHub CLI installed and authenticated: |
| 21 | |
| 22 | ```bash |
| 23 | gh auth status |
| 24 | ``` |
| 25 | |
| 26 | 2. Access to the repository and pull request you want to review |
| 27 | |
| 28 | ## Understanding GitHub's Review Comment System |
| 29 | |
| 30 | GitHub has two types of PR comments: |
| 31 | |
| 32 | 1. **Issue Comments** - General comments on the PR conversation |
| 33 | 2. **Review Comments** - Line-specific comments on code changes |
| 34 | |
| 35 | Review comments can be added in two ways: |
| 36 | |
| 37 | - **Single comment** - Using the `/pulls/{pr}/comments` endpoint |
| 38 | - **Review with multiple comments** - Using the `/pulls/{pr}/reviews` endpoint |
| 39 | |
| 40 | ## Adding a Single Line-Specific Comment |
| 41 | |
| 42 | ### Basic Syntax |
| 43 | |
| 44 | ```bash |
| 45 | gh api repos/{owner}/{repo}/pulls/{pr_number}/comments \ |
| 46 | -f body='Your comment text here' \ |
| 47 | -f commit_id='<commit-sha>' \ |
| 48 | -f path='path/to/file.js' \ |
| 49 | -F line=42 \ |
| 50 | -f side='RIGHT' |
| 51 | ``` |
| 52 | |
| 53 | ### Parameters Explained |
| 54 | |
| 55 | | Parameter | Type | Required | Description | |
| 56 | |-----------|------|----------|-------------| |
| 57 | | `body` | string | Yes | The text of the review comment (supports Markdown) | |
| 58 | | `commit_id` | string | Yes | The SHA of the commit to comment on | |
| 59 | | `path` | string | Yes | Relative path to the file being commented on | |
| 60 | | `line` | integer | Yes | The line number in the diff (use `-F` for integers) | |
| 61 | | `side` | string | Yes | `RIGHT` for new/modified lines, `LEFT` for deleted lines | |
| 62 | | `start_line` | integer | No | For multi-line comments, the starting line | |
| 63 | | `start_side` | string | No | For multi-line comments, the starting side | |
| 64 | |
| 65 | ### Parameter Flags |
| 66 | |
| 67 | - `-f` (--field) - For string values |
| 68 | - `-F` (--field) - For integer values (note the capital F) |
| 69 | |
| 70 | ### Complete Example |
| 71 | |
| 72 | ```bash |
| 73 | # First, get the latest commit SHA for the PR |
| 74 | gh api repos/NeoLabHQ/learning-platform-app/pulls/4 --jq '.head.sha' |
| 75 | |
| 76 | # Then add your comment |
| 77 | gh api repos/NeoLabHQ/learning-platform-app/pulls/4/comments \ |
| 78 | -f body='Consider adding error handling here. Should we confirm the lesson was successfully marked as completed before navigating away?' \ |
| 79 | -f commit_id='e152d0dd6cf498467eadbeb638bf05abe11c64d4' \ |
| 80 | -f path='src/components/LessonNavigationButtons.tsx' \ |
| 81 | -F line=26 \ |
| 82 | -f side='RIGHT' |
| 83 | ``` |
| 84 | |
| 85 | ### Understanding Line Numbers |
| 86 | |
| 87 | The `line` parameter refers to the **position in the diff**, not the absolute line number in the file: |
| 88 | |
| 89 | - For **new files**: Line numbers match the file's line numbers |
| 90 | - For **modified files**: Use the line number as it appears in the "Files changed" tab |
| 91 | - For **multi-line comments**: Use `start_line` and `line` to specify the range |
| 92 | |
| 93 | ### Response |
| 94 | |
| 95 | On success, returns a JSON object with comment details: |
| 96 | |
| 97 | ```json |
| 98 | { |
| 99 | "id": 2532291222, |
| 100 | "pull_request_review_id": 3470545909, |
| 101 | "path": "src/components/LessonNavigationButtons.tsx", |
| 102 | "line": 26, |
| 103 | "body": "Consider adding error handling here...", |
| 104 | "html_url": "https://github.com/NeoLabHQ/learning-platform-app/pull/4#discussion_r2532291222", |
| 105 | "created_at": "2025-11-16T22:40:46Z" |
| 106 | } |
| 107 | ``` |
| 108 | |
| 109 | ## Adding Multiple Line-Specific Comments Together |
| 110 | |
| 111 | To add multiple comments across different files in a single review, use the `/reviews` endpoint with JSON input. |
| 112 | |
| 113 | ### Why Use Reviews for Multiple Comments? |
| 114 | |
| 115 | - **Atomic operation** - All comments are added together |
| 116 | - **Single notification** - Doesn't spam with multiple notifications |
| 117 | - **Better UX** - Appears as one cohesive review |
| 118 | - **Same mechanism as GitHub UI** - "Start a review" → "Finish review" |
| 119 | |
| 120 | ### Basic Synta |