Skip to content

Commit 3a7aa79

Browse files
fix: claude code review for forked branches (#2149)
Co-authored-by: Mark Sze <66362098+marklysze@users.noreply.github.com>
1 parent 10bc294 commit 3a7aa79

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

.github/workflows/claude-code-review.yml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
name: Claude Code Review
22

33
on:
4+
# For PRs from the same repository (fast path)
45
pull_request:
56
types: [opened, synchronize]
6-
# Optional: Only run on specific file changes
7-
# paths:
8-
# - "src/**/*.ts"
9-
# - "src/**/*.tsx"
10-
# - "src/**/*.js"
11-
# - "src/**/*.jsx"
7+
# For PRs from forked repositories (secure path with secrets)
8+
pull_request_target:
9+
types: [opened, synchronize]
1210

1311
jobs:
1412
claude-review:
15-
# Skip draft PRs
16-
if: github.event.pull_request.draft == false
17-
18-
# Optional: Filter by PR author
19-
# if: |
20-
# github.event.pull_request.draft == false &&
21-
# (github.event.pull_request.user.login == 'external-contributor' ||
22-
# github.event.pull_request.user.login == 'new-developer' ||
23-
# github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR')
13+
# Skip draft PRs and prevent duplicate runs
14+
if: |
15+
github.event.pull_request.draft == false &&
16+
(
17+
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository) ||
18+
(github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository)
19+
)
2420
2521
runs-on: ubuntu-latest
2622
permissions:
@@ -35,6 +31,20 @@ jobs:
3531
with:
3632
fetch-depth: 1
3733

34+
- name: Checkout PR branch
35+
env:
36+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
run: |
38+
if [ "${{ github.event_name }}" = "pull_request_target" ]; then
39+
echo "⚠️ Forked PR detected - running in secure mode"
40+
echo "PR from: ${{ github.event.pull_request.head.repo.full_name }}"
41+
echo "Base repo: ${{ github.repository }}"
42+
fi
43+
44+
echo "Checking out PR #${{ github.event.pull_request.number }}"
45+
gh pr checkout ${{ github.event.pull_request.number }}
46+
echo "✅ PR branch checked out successfully"
47+
3848
- name: Run Claude Code Review
3949
id: claude-review
4050
uses: anthropics/claude-code-action@v1

.github/workflows/claude.yml

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ jobs:
3030
with:
3131
fetch-depth: 1
3232

33+
- name: Checkout PR branch (if comment is on a PR)
34+
if: github.event.issue.pull_request || github.event.pull_request
35+
env:
36+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
run: |
38+
if [ -n "${{ github.event.issue.number }}" ]; then
39+
PR_NUMBER="${{ github.event.issue.number }}"
40+
elif [ -n "${{ github.event.pull_request.number }}" ]; then
41+
PR_NUMBER="${{ github.event.pull_request.number }}"
42+
fi
43+
44+
if [ -n "$PR_NUMBER" ]; then
45+
echo "Detected comment on PR #$PR_NUMBER"
46+
47+
# Check if it's a forked PR
48+
PR_INFO=$(gh pr view $PR_NUMBER --json isCrossRepository,headRepositoryOwner 2>/dev/null || echo '{}')
49+
IS_FORK=$(echo "$PR_INFO" | jq -r '.isCrossRepository // false')
50+
51+
if [ "$IS_FORK" = "true" ]; then
52+
echo "⚠️ Forked PR detected - running in secure mode"
53+
FORK_OWNER=$(echo "$PR_INFO" | jq -r '.headRepositoryOwner.login')
54+
echo "PR from: $FORK_OWNER"
55+
fi
56+
57+
echo "Checking out PR #$PR_NUMBER"
58+
gh pr checkout $PR_NUMBER
59+
echo "✅ PR branch checked out successfully"
60+
fi
61+
3362
- name: Run Claude Code
3463
id: claude
3564
uses: anthropics/claude-code-action@v1
@@ -39,11 +68,3 @@ jobs:
3968
# This is an optional setting that allows Claude to read CI results on PRs
4069
additional_permissions: |
4170
actions: read
42-
43-
# Optional: Give a custom prompt to Claude. If this is not specified, Claude will perform the instructions specified in the comment that tagged it.
44-
# prompt: 'Update the pull request description to include a summary of changes.'
45-
46-
# Optional: Add claude_args to customize behavior and configuration
47-
# See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
48-
# or https://docs.claude.com/en/docs/claude-code/cli-reference for available options
49-
# claude_args: '--allowed-tools Bash(gh pr:*)'

0 commit comments

Comments
 (0)