Skip to content

Instantly share code, notes, and snippets.

@laughingman7743
Last active July 17, 2026 12:06
Show Gist options
  • Select an option

  • Save laughingman7743/629a568b96089de9a09271eb72dcc9f6 to your computer and use it in GitHub Desktop.

Select an option

Save laughingman7743/629a568b96089de9a09271eb72dcc9f6 to your computer and use it in GitHub Desktop.
Claude Code skill: github-pr-review — review PRs via gh CLI (inline comments, diff with line numbers, submit reviews, etc.)
#!/usr/bin/env bash
# Print a PR diff annotated with LEFT (base) and RIGHT (head) line numbers.
#
# Usage:
# diff-with-line-numbers.sh PR_NUMBER REPO [-- PATH...]
#
# Examples:
# diff-with-line-numbers.sh 123 octocat/Hello-World
# diff-with-line-numbers.sh 123 octocat/Hello-World -- path/to/file.py
#
# Uses POSIX-compatible awk (works with BSD awk on macOS, no gawk required).
set -euo pipefail
if [ "$#" -lt 2 ]; then
echo "Usage: $0 PR_NUMBER REPO [-- PATH...]" >&2
exit 1
fi
PR_NUMBER="$1"
REPO="$2"
shift 2
gh pr diff "$PR_NUMBER" --repo "$REPO" "$@" | awk '
/^diff --git/ { print "\n=== " $0 " ==="; next }
/^@@/ {
# Parse "@@ -OLD_START,OLD_COUNT +NEW_START,NEW_COUNT @@"
old_str = $2; sub(/^-/, "", old_str); sub(/,.*/, "", old_str)
new_str = $3; sub(/^\+/, "", new_str); sub(/,.*/, "", new_str)
old_line = old_str + 0
new_line = new_str + 0
print $0
next
}
/^-/ { printf "L%-4d | %s\n", old_line++, $0; next }
/^\+/ { printf " R%-4d| %s\n", new_line++, $0; next }
/^ / { printf "L%-4d R%-4d| %s\n", old_line++, new_line++, $0; next }
{ print }
'
name github-pr-review
description GitHub Pull Request review operations skill. Retrieve PR info, view diffs, get/post comments, add inline comments, submit reviews (approve/request-changes/comment), check CI status, and reply to comments using gh CLI. Use for PR review, code review, and PR operations.

GitHub PR Review Operations

Source: Based on Claude CodeでPRレビュー操作を行うスキルを作った by shibayu36

This skill provides comprehensive PR review capabilities using GitHub CLI (gh).

Operations

1. Get PR Information

Retrieve PR metadata including title, description, author, and branch info:

gh pr view NUMBER --repo OWNER/REPO --json title,body,author,state,baseRefName,headRefName,url,files

2. Check CI Status

Check the status of CI/CD checks on a PR before reviewing:

gh pr checks NUMBER --repo OWNER/REPO          # status of all checks
gh pr status                                   # PRs related to current user

3. View Diff with Line Numbers

Get diff with both LEFT (base) and RIGHT (head) line numbers for precise inline commenting.

Preferred: use the bundled helper script (POSIX awk, works on macOS BSD awk):

bash $CLAUDE_PLUGIN_ROOT/scripts/diff-with-line-numbers.sh PR_NUMBER REPO

If $CLAUDE_PLUGIN_ROOT is not set, use the absolute path: ~/.claude/skills/github-pr-review/scripts/diff-with-line-numbers.sh

Filter to specific file(s):

# List changed files only
gh pr diff NUMBER --repo OWNER/REPO --name-only

# Diff for a single file
bash .../diff-with-line-numbers.sh NUMBER REPO -- path/to/file.py

Inline (one-shot) equivalent, in case the helper isn't available:

gh pr diff NUMBER --repo OWNER/REPO | awk '
/^diff --git/ { print "\n=== " $0 " ==="; next }
/^@@/ {
  # Parse "@@ -OLD_START,OLD_COUNT +NEW_START,NEW_COUNT @@"
  # POSIX awk — no gawk match() capture groups, works on macOS BSD awk.
  old_str = $2; sub(/^-/, "", old_str); sub(/,.*/, "", old_str)
  new_str = $3; sub(/^\+/, "", new_str); sub(/,.*/, "", new_str)
  old_line = old_str + 0
  new_line = new_str + 0
  print $0
  next
}
/^-/ { printf "L%-4d     | %s\n", old_line++, $0; next }
/^\+/ { printf "     R%-4d| %s\n", new_line++, $0; next }
/^ / { printf "L%-4d R%-4d| %s\n", old_line++, new_line++, $0; next }
{ print }
'

Output format:

  • L[num]: Line number on base branch (LEFT side) — for deleted/unchanged lines
  • R[num]: Line number on head branch (RIGHT side) — for added/unchanged lines

4. Retrieve Comments

Important: GitHub API paginates at 30 items by default. Use --paginate to fetch all comments on PRs with many reviews — otherwise you'll silently miss content.

Get issue-level comments (general PR comments):

gh api --paginate repos/OWNER/REPO/issues/NUMBER/comments \
  --jq '.[] | {id, user: .user.login, created_at, body}'

Get review comments (inline code comments):

gh api --paginate repos/OWNER/REPO/pulls/NUMBER/comments \
  --jq '.[] | {id, user: .user.login, path, line, created_at, body, in_reply_to_id}'

5. Post PR Comment

Add a general comment to the PR:

gh pr comment NUMBER --repo OWNER/REPO --body "Your comment here"

6. Add Inline Comment

First, get the head commit SHA:

gh api repos/OWNER/REPO/pulls/NUMBER --jq '.head.sha'

Single-line inline comment:

gh api repos/OWNER/REPO/pulls/NUMBER/comments \
  --method POST \
  -f body="Your comment here" \
  -f commit_id="COMMIT_SHA" \
  -f path="path/to/file.py" \
  -F line=15 \
  -f side=RIGHT

Multi-line inline comment (e.g., lines 10-15):

gh api repos/OWNER/REPO/pulls/NUMBER/comments \
  --method POST \
  -f body="Your comment here" \
  -f commit_id="COMMIT_SHA" \
  -f path="path/to/file.py" \
  -F line=15 \
  -f side=RIGHT \
  -F start_line=10 \
  -f start_side=RIGHT

File-level comment (whole file, no specific line):

Use subject_type=file and omit line/side. Useful for comments like "delete this file" or "split this module":

gh api repos/OWNER/REPO/pulls/NUMBER/comments \
  --method POST \
  -f body="Consider splitting this file into smaller modules." \
  -f commit_id="COMMIT_SHA" \
  -f path="path/to/file.py" \
  -f subject_type=file

7. Reply to Comment

Reply to a specific existing comment:

gh api repos/OWNER/REPO/pulls/NUMBER/comments/COMMENT_ID/replies \
  --method POST \
  -f body="Your reply here"

8. Submit a Review (Approve / Request Changes / Comment)

After leaving inline comments, submit a PR-level review verdict:

# Approve
gh pr review NUMBER --repo OWNER/REPO --approve --body "LGTM"

# Request changes (use sparingly — prefer inline comments + comment review)
gh pr review NUMBER --repo OWNER/REPO --request-changes --body "See inline comments"

# Comment review (neutral — no approval, no block)
gh pr review NUMBER --repo OWNER/REPO --comment --body "A few questions inline"

Confirm the review state:

gh pr view NUMBER --repo OWNER/REPO --json reviewDecision,latestReviews

9. React to a Comment

Add an emoji reaction (👍, 🚀, 👀, etc.) instead of a text reply:

# React to an issue-level comment
gh api repos/OWNER/REPO/issues/comments/COMMENT_ID/reactions \
  --method POST \
  -f content=+1   # +1, -1, laugh, confused, heart, hooray, rocket, eyes

# React to an inline review comment
gh api repos/OWNER/REPO/pulls/comments/COMMENT_ID/reactions \
  --method POST \
  -f content=rocket

Important Notes

Inline comments must land on diff lines

The GitHub API rejects POST .../pulls/NUMBER/comments with 422 line could not be resolved when the target line is not present in the PR diff — i.e., it's neither a changed line nor a context line shown inside an @@ hunk. If you hit this:

  • Verify the line number using the helper script in §3 — only L[num] (deleted/context) and R[num] (added/context) rows are commentable.
  • Pick the closest related changed/context line and reference the actual location in the body, or
  • Use a file-level comment (subject_type=file, see §6) when no nearby diff line exists, or
  • Fall back to a general PR comment (§5).

Flag Usage

  • -F (uppercase): Use for numeric/boolean values (line, start_line).
  • -f (lowercase): Use for string values (body, commit_id, path, side, subject_type, content).

Using -f for line numbers will cause errors because it treats values as strings.

Side Parameter

  • side=LEFT: Comment on base branch (deleted or unchanged lines, use L[num]).
  • side=RIGHT: Comment on head branch (added or unchanged lines, use R[num]).

Repo auto-detection

Inside a checked-out repo, --repo OWNER/REPO is optional — gh reads the remote. Specify it explicitly when working across multiple repos or when running from outside a checkout.

Variables Reference

Variable Description
OWNER Repository owner (e.g., octocat)
REPO Repository name (e.g., Hello-World)
NUMBER PR number
COMMIT_SHA Head commit SHA (get via API)
COMMENT_ID ID of comment to reply to or react to

Example Workflow

  1. gh pr view NUMBER --json title,body,url,files — read PR overview.
  2. gh pr checks NUMBER — confirm CI is green before deep review.
  3. bash scripts/diff-with-line-numbers.sh NUMBER REPO — read diff with line numbers.
  4. gh api --paginate .../pulls/NUMBER/comments — read existing review comments.
  5. Post inline comments on diff lines (§6) or file-level comments where appropriate.
  6. gh pr review NUMBER --approve|--request-changes|--comment — submit the review verdict.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment