@@ -63,74 +63,44 @@ jobs:
6363 )
6464
6565 steps :
66+ # Checkout PR Branch (for comments)
67+ - name : Resolve PR Ref
68+ id : resolve-pr-ref
69+ if : github.event_name == 'issue_comment' && github.event.issue.pull_request
70+ env :
71+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
72+ run : |
73+ PR_NUMBER=${{ github.event.issue.number }}
74+ echo "Resolving HEAD SHA for PR #$PR_NUMBER..."
75+ PR_SHA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefOid -q .headRefOid)
76+ echo "Resolved SHA: $PR_SHA"
77+ echo "pr_sha=$PR_SHA" >> $GITHUB_OUTPUT
78+
6679 # Checkout the repository at the appropriate commit for review
6780 - name : Checkout repository
68- uses : actions/checkout@v4
81+ uses : actions/checkout@v6
6982 with :
70- # Use PR head SHA for pull_request_target, fallback to current SHA otherwise
71- fetch-depth : 0
72- ref : ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
73-
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- PR_NUMBER : ${{ github.event.pull_request.number }}
80- PR_HEAD_REF : ${{ github.event.pull_request.head.ref }}
81- PR_HEAD_OWNER : ${{ github.event.pull_request.head.repo.owner.login }}
82- PR_HEAD_REPO : ${{ github.event.pull_request.head.repo.name }}
83- REPO_OWNER : ${{ github.repository_owner }}
84- run : |
85- PR_NUMBER="$PR_NUMBER"
86- HEAD_REF="$PR_HEAD_REF"
87- HEAD_OWNER="$PR_HEAD_OWNER"
88- HEAD_REPO="$PR_HEAD_REPO"
89- CURRENT_OWNER="$REPO_OWNER"
90-
91- # For forked PRs, temporarily change origin URL to fork repository
92- # This allows claude-code-action to fetch the PR branch correctly
93- if [ "$HEAD_OWNER" != "$CURRENT_OWNER" ]; then
94- echo "PR is from fork: $HEAD_OWNER/$HEAD_REPO"
95- FORK_URL="https://github.com/$HEAD_OWNER/$HEAD_REPO.git"
96- echo "Temporarily changing origin URL to fork: $FORK_URL"
97- git remote set-url origin "$FORK_URL"
98- git fetch origin "$HEAD_REF"
99- git branch "$HEAD_REF" "origin/$HEAD_REF" 2>/dev/null || git branch -f "$HEAD_REF" "origin/$HEAD_REF"
100- fi
101-
102- # For comment-driven triggers, ensure we have the correct PR branch checked out
103- - name : Checkout PR Branch (for comments)
104- if : ${{ github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment' }}
105- env :
106- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
83+ # Use PR head SHA for pull_request_target to review the actual PR code
84+ # For comment events, this will default to the base branch (PR context is inferred by Claude action)
85+ ref : ${{ steps.resolve-pr-ref.outputs.pr_sha || github.event.pull_request.head.sha || github.sha }}
86+ fetch-depth : 20
87+
88+ # Fix git ref access for claude-code-action PR branch checkout
89+ # The action internally runs: git fetch origin pull/N/head:pr-N
90+ # This can fail for two reasons:
91+ # 1. Under git protocol v2, refs/pull/* may not be discoverable if not in
92+ # the configured fetch refspec (causes "couldn't find remote ref")
93+ # 2. If a local branch pr-N already exists and is checked out, git refuses
94+ # to fetch into it (causes "refusing to fetch into branch")
95+ # Adding refs/pull/*/head to the fetch refspec solves issue 1, and we avoid
96+ # creating local branches to prevent issue 2.
97+ - name : Configure git for PR ref access
10798 run : |
108- PR_NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}
109-
110- # Fetch PR metadata: head branch name and source repository
111- PR_DATA=$(gh pr view $PR_NUMBER --json headRefName,headRepositoryOwner,headRepository,baseRefName)
112- HEAD_REF=$(echo "$PR_DATA" | jq -r '.headRefName')
113- HEAD_OWNER=$(echo "$PR_DATA" | jq -r '.headRepositoryOwner.login')
114- HEAD_REPO=$(echo "$PR_DATA" | jq -r '.headRepository.name')
115- BASE_BRANCH=$(echo "$PR_DATA" | jq -r '.baseRefName')
116- CURRENT_OWNER="${{ github.repository_owner }}"
117-
118- # For forked PRs, temporarily change origin URL to fork repository
119- # This allows claude-code-action to fetch the PR branch correctly
120- if [ "$HEAD_OWNER" != "$CURRENT_OWNER" ]; then
121- echo "PR is from fork: $HEAD_OWNER/$HEAD_REPO"
122- FORK_URL="https://github.com/$HEAD_OWNER/$HEAD_REPO.git"
123- echo "Temporarily changing origin URL to fork: $FORK_URL"
124- git remote set-url origin "$FORK_URL"
125- fi
126-
127- # Fetch and checkout the PR branch
128- git fetch origin "$HEAD_REF"
129- git branch "$HEAD_REF" "origin/$HEAD_REF" 2>/dev/null || git branch -f "$HEAD_REF" "origin/$HEAD_REF"
130- git checkout "$HEAD_REF"
99+ git config --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pull/*/head'
131100
132101 # Invoke Claude to perform an automated PR review with progress tracking
133- - name : PR Review with Progress Tracking
102+ - name : Run Claude Code Review
103+ id : claude-review
134104 uses : anthropics/claude-code-action@v1
135105 with :
136106 anthropic_api_key : ${{ secrets.ANTHROPIC_API_KEY }}
@@ -176,4 +146,3 @@ jobs:
176146 # Restrict tools that Claude can use during the review
177147 claude_args : |
178148 --allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)"
179-
0 commit comments