|
| 1 | +# Code Review Skill |
| 2 | + |
| 3 | +PR code review mechanics: create reviews via GitHub API, post inline comments, respond to mentions. |
| 4 | + |
| 5 | +## Input Context |
| 6 | + |
| 7 | +The workflow provides: |
| 8 | +- `EVENT`: GitHub event type (`pull_request`, `issue_comment`, `pull_request_review_comment`) |
| 9 | +- `REPO`: Repository in `owner/repo` format |
| 10 | +- `PR_NUMBER`: Pull request number |
| 11 | +- `COMMENT`: Comment body (empty for `pull_request` events) |
| 12 | + |
| 13 | +## Execution Context Detection |
| 14 | + |
| 15 | +```bash |
| 16 | +echo "GITHUB_ACTIONS=${GITHUB_ACTIONS:-not_set} CI=${CI:-not_set}" |
| 17 | +``` |
| 18 | + |
| 19 | +**Execution modes:** |
| 20 | +- **CI mode**: `GITHUB_ACTIONS=true` or `CI=true` — Posts reviews to GitHub |
| 21 | +- **Local mode**: Neither set — Auto-enables dry-run, outputs review to console |
| 22 | + |
| 23 | +When running locally, inform the user: |
| 24 | +> "Running in local mode — dry-run enabled automatically. Review output will be displayed but not posted to GitHub." |
| 25 | +
|
| 26 | +--- |
| 27 | + |
| 28 | +## Intent Detection |
| 29 | + |
| 30 | +``` |
| 31 | +If EVENT == "pull_request": |
| 32 | + → INTENT = "review" |
| 33 | +
|
| 34 | +If EVENT == "issue_comment" or "pull_request_review_comment": |
| 35 | + If COMMENT contains "@claude review" or "@claude re-review": |
| 36 | + → INTENT = "review" |
| 37 | + Else: |
| 38 | + → INTENT = "respond" |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Loading Guidelines |
| 44 | + |
| 45 | +Before reviewing or responding, check for project-specific guidelines: |
| 46 | + |
| 47 | +1. **Read CLAUDE.md** (if exists) - may contain review instructions |
| 48 | +2. **Follow references** - if CLAUDE.md mentions other files (e.g., "see `REVIEW_GUIDELINES.md`"), read them |
| 49 | +3. **Check common locations**: `REVIEW_GUIDELINES.md`, `.github/CONTRIBUTING.md`, `docs/code-standards.md` |
| 50 | + |
| 51 | +**Use project guidelines for:** |
| 52 | +- What to focus on in reviews |
| 53 | +- Review summary format |
| 54 | +- Inline comment format |
| 55 | +- Response format/tone |
| 56 | + |
| 57 | +**If no guidelines found, use defaults:** |
| 58 | + |
| 59 | +1. **Test Coverage** - Sufficient tests covering success, error, and edge cases |
| 60 | +2. **Guidelines & Architecture** - Follows coding guidelines and project patterns |
| 61 | +3. **Code Quality** - No ignored errors, unnecessary complexity, or dead code |
| 62 | +4. **Security** - No injection vulnerabilities, path traversal, or exposed secrets |
| 63 | + |
| 64 | +Use a concise and direct tone. Put detailed feedback in inline comments with fix prompts. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Action: Review |
| 69 | + |
| 70 | +### Pre-Review Check |
| 71 | + |
| 72 | +Determine what needs reviewing: |
| 73 | + |
| 74 | +```bash |
| 75 | +# Get Claude's previous reviews |
| 76 | +LAST_REVIEW=$(gh api repos/$REPO/pulls/$PR_NUMBER/reviews \ |
| 77 | + --jq '[.[] | select(.user.login == "github-actions[bot]" or .user.login == "claude[bot]")] | sort_by(.submitted_at) | last') |
| 78 | + |
| 79 | +# Get current HEAD commit |
| 80 | +CURRENT_HEAD=$(gh pr view $PR_NUMBER --json headRefOid --jq '.headRefOid') |
| 81 | + |
| 82 | +# Get commit SHA from last review |
| 83 | +LAST_REVIEW_COMMIT=$(echo "$LAST_REVIEW" | jq -r '.commit_id // empty') |
| 84 | +``` |
| 85 | + |
| 86 | +**Decision:** |
| 87 | +- No previous review → Full review (`gh pr diff $PR_NUMBER`) |
| 88 | +- Same commit → Post "No new changes to review" |
| 89 | +- New commits in history → Incremental review |
| 90 | +- Last commit not in history → Force-push handling |
| 91 | + |
| 92 | +### Incremental Review |
| 93 | + |
| 94 | +```bash |
| 95 | +git diff $LAST_REVIEW_COMMIT..$CURRENT_HEAD |
| 96 | +``` |
| 97 | + |
| 98 | +Only comment on lines changed in new commits. |
| 99 | + |
| 100 | +### Force-Push Handling |
| 101 | + |
| 102 | +```bash |
| 103 | +git merge-base --is-ancestor $LAST_REVIEW_COMMIT $CURRENT_HEAD |
| 104 | +# Exit 0 = normal push, non-zero = force-push |
| 105 | +``` |
| 106 | + |
| 107 | +When force-push detected: |
| 108 | +1. Full review against base branch |
| 109 | +2. Fetch previous comments: `gh api repos/$REPO/pulls/$PR_NUMBER/comments` |
| 110 | +3. Skip re-commenting on same file+line+issue |
| 111 | + |
| 112 | +### Submitting the Review |
| 113 | + |
| 114 | +Use GitHub's Reviews API to submit review with inline comments: |
| 115 | + |
| 116 | +```bash |
| 117 | +COMMIT_SHA=$(gh pr view $PR_NUMBER --json headRefOid --jq '.headRefOid') |
| 118 | + |
| 119 | +cat > /tmp/review.json << 'EOF' |
| 120 | +{ |
| 121 | + "body": "Review summary here (general remarks, not file-specific)", |
| 122 | + "event": "COMMENT", |
| 123 | + "commit_id": "SHA_PLACEHOLDER", |
| 124 | + "comments": [ |
| 125 | + {"path": "src/file.js", "line": 42, "body": "Inline feedback here"} |
| 126 | + ] |
| 127 | +} |
| 128 | +EOF |
| 129 | + |
| 130 | +sed -i.bak "s/SHA_PLACEHOLDER/$COMMIT_SHA/" /tmp/review.json && rm /tmp/review.json.bak |
| 131 | +gh api repos/$REPO/pulls/$PR_NUMBER/reviews --input /tmp/review.json |
| 132 | +``` |
| 133 | + |
| 134 | +**Review structure:** |
| 135 | +- `body`: General remarks and proposals not tied to specific files |
| 136 | +- `comments`: Array of inline comments on specific lines |
| 137 | +- `event`: "COMMENT", "APPROVE", or "REQUEST_CHANGES" |
| 138 | + |
| 139 | +**Line numbers:** |
| 140 | +- `line` = line number in new file version (right side of diff) |
| 141 | +- For deleted lines, add `"side": "LEFT"` to comment object |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## Action: Respond |
| 146 | + |
| 147 | +Handle @claude mentions that aren't review requests. |
| 148 | + |
| 149 | +### Fetch Context |
| 150 | + |
| 151 | +```bash |
| 152 | +# Get review comments (inline on code) |
| 153 | +gh api repos/$REPO/pulls/$PR_NUMBER/comments --paginate |
| 154 | + |
| 155 | +# Get issue comments (general PR discussion) |
| 156 | +gh api repos/$REPO/issues/$PR_NUMBER/comments --paginate |
| 157 | +``` |
| 158 | + |
| 159 | +### Responding |
| 160 | + |
| 161 | +```bash |
| 162 | +# Reply to inline review comment |
| 163 | +gh api repos/$REPO/pulls/$PR_NUMBER/comments/$COMMENT_ID/replies \ |
| 164 | + -f body="Response here" |
| 165 | + |
| 166 | +# Reply to general PR comment |
| 167 | +gh pr comment $PR_NUMBER --body "Response here" |
| 168 | +``` |
| 169 | + |
| 170 | +Respond based on the question/request. If project guidelines specify a response format, use it. |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## Dry Run Mode |
| 175 | + |
| 176 | +When `--dry-run` is passed OR running in local mode (no CI environment detected): |
| 177 | +- Analyze normally but DO NOT post to GitHub |
| 178 | +- Output what would be posted |
| 179 | + |
| 180 | +``` |
| 181 | +DRY RUN - No changes will be made |
| 182 | +
|
| 183 | +Would submit review to PR #123: |
| 184 | + Event: COMMENT |
| 185 | + Body: "..." |
| 186 | +
|
| 187 | +Inline comments (3): |
| 188 | + src/file.js:10 - "..." |
| 189 | + src/file.js:25 - "..." |
| 190 | + src/api.js:100 - "..." |
| 191 | +``` |
0 commit comments