Skip to content

Commit 3520ac8

Browse files
authored
Update claude action (#1805)
Signed-off-by: Quanyi Ma <eli@patch.sh>
1 parent 33b05c4 commit 3520ac8

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

.github/workflows/claude-review.yml

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
name: Claude Code Review with Progress Tracking
22

3+
# Trigger Claude review on PR lifecycle events and explicit mentions
4+
35
on:
6+
# Trigger when a new issue comment is created (for @claude mentions)
47
issue_comment:
58
types: [created]
9+
# Trigger when a PR review comment is created/edited/deleted (for @claude mentions)
610
pull_request_review_comment:
711
types: [created, edited, deleted]
12+
# Trigger on new or assigned issues (for future extension or automation)
813
issues:
914
types: [opened, assigned]
15+
# Trigger when a PR review is submitted (for @claude in the review body)
1016
pull_request_review:
1117
types: [submitted]
18+
# Main trigger for PR events, using pull_request_target for elevated permissions
1219
pull_request_target:
1320
types: [opened, synchronize, reopened]
1421

1522
permissions:
23+
# Read repository contents needed for code review
1624
contents: read
25+
# Allow Claude to post review comments on pull requests
1726
pull-requests: write
27+
# Allow Claude to interact with issues if needed
1828
issues: write
29+
# Allow this workflow to manage its own actions if required
1930
actions: write
2031

2132
jobs:
2233
claude-review-with-tracking:
2334
runs-on: ubuntu-latest
2435

36+
# Only run for trusted authors or when explicitly mentioned by them
2537
if: |
2638
(
2739
github.event_name == 'pull_request_target' &&
@@ -51,43 +63,79 @@ jobs:
5163
)
5264
5365
steps:
66+
# Checkout the repository at the appropriate commit for review
5467
- name: Checkout repository
5568
uses: actions/checkout@v4
5669
with:
70+
# Use PR head SHA for pull_request_target, fallback to current SHA otherwise
5771
fetch-depth: 0
5872
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
5973

74+
# Handle fork branches for pull_request_target events
75+
- name: Setup Fork Remote (for pull_request_target)
76+
if: ${{ github.event_name == 'pull_request_target' }}
77+
env:
78+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
run: |
80+
PR_NUMBER=${{ github.event.pull_request.number }}
81+
HEAD_REF="${{ github.event.pull_request.head.ref }}"
82+
HEAD_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
83+
HEAD_REPO="${{ github.event.pull_request.head.repo.name }}"
84+
CURRENT_OWNER="${{ github.repository_owner }}"
85+
86+
# For forked PRs, temporarily change origin URL to fork repository
87+
# This allows claude-code-action to fetch the PR branch correctly
88+
if [ "$HEAD_OWNER" != "$CURRENT_OWNER" ]; then
89+
echo "PR is from fork: $HEAD_OWNER/$HEAD_REPO"
90+
FORK_URL="https://github.com/$HEAD_OWNER/$HEAD_REPO.git"
91+
echo "Temporarily changing origin URL to fork: $FORK_URL"
92+
git remote set-url origin "$FORK_URL"
93+
git fetch origin "$HEAD_REF"
94+
git branch "$HEAD_REF" "origin/$HEAD_REF" 2>/dev/null || git branch -f "$HEAD_REF" "origin/$HEAD_REF"
95+
fi
96+
97+
# For comment-driven triggers, ensure we have the correct PR branch checked out
6098
- name: Checkout PR Branch (for comments)
6199
if: ${{ github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment' }}
62100
env:
63101
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64102
run: |
65-
# Get PR info first
66103
PR_NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}
67-
BASE_BRANCH=$(gh pr view $PR_NUMBER --json baseRefName -q '.baseRefName')
68-
69-
# Fetch the base branch if it exists
70-
if git ls-remote --exit-code origin "refs/heads/$BASE_BRANCH" > /dev/null 2>&1; then
71-
if ! git fetch origin "$BASE_BRANCH"; then
72-
echo "Warning: Failed to fetch base branch '$BASE_BRANCH'"
73-
fi
74-
else
75-
echo "Warning: Base branch '$BASE_BRANCH' not found, PR may need to be retargeted"
104+
105+
# Fetch PR metadata: head branch name and source repository
106+
PR_DATA=$(gh pr view $PR_NUMBER --json headRefName,headRepositoryOwner,headRepository,baseRefName)
107+
HEAD_REF=$(echo "$PR_DATA" | jq -r '.headRefName')
108+
HEAD_OWNER=$(echo "$PR_DATA" | jq -r '.headRepositoryOwner.login')
109+
HEAD_REPO=$(echo "$PR_DATA" | jq -r '.headRepository.name')
110+
BASE_BRANCH=$(echo "$PR_DATA" | jq -r '.baseRefName')
111+
CURRENT_OWNER="${{ github.repository_owner }}"
112+
113+
# For forked PRs, temporarily change origin URL to fork repository
114+
# This allows claude-code-action to fetch the PR branch correctly
115+
if [ "$HEAD_OWNER" != "$CURRENT_OWNER" ]; then
116+
echo "PR is from fork: $HEAD_OWNER/$HEAD_REPO"
117+
FORK_URL="https://github.com/$HEAD_OWNER/$HEAD_REPO.git"
118+
echo "Temporarily changing origin URL to fork: $FORK_URL"
119+
git remote set-url origin "$FORK_URL"
76120
fi
77-
78-
gh pr checkout $PR_NUMBER
79121
122+
# Fetch and checkout the PR branch
123+
git fetch origin "$HEAD_REF"
124+
git branch "$HEAD_REF" "origin/$HEAD_REF" 2>/dev/null || git branch -f "$HEAD_REF" "origin/$HEAD_REF"
125+
git checkout "$HEAD_REF"
126+
127+
# Invoke Claude to perform an automated PR review with progress tracking
80128
- name: PR Review with Progress Tracking
81129
uses: anthropics/claude-code-action@v1
82130
with:
83131
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
84132
github_token: ${{ secrets.GITHUB_TOKEN }}
85133

86-
# Enable progress tracking
134+
# Enable progress tracking and show full Claude output in logs
87135
track_progress: true
88136
show_full_output: true
89137

90-
# Your custom review instructions
138+
# Custom review instructions passed to Claude
91139
prompt: |
92140
REPO: ${{ github.repository }}
93141
@@ -121,6 +169,6 @@ jobs:
121169
Provide detailed feedback using inline comments for specific issues.
122170
Use top-level comments for general observations or praise.
123171
124-
# Tools for comprehensive PR review
172+
# Restrict tools that Claude can use during the review
125173
claude_args: |
126174
--allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)"

0 commit comments

Comments
 (0)