From 2c7f8faab4ccfe3a0d3536e7dd32665e48a18086 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 10 Mar 2026 12:59:58 -0400 Subject: [PATCH] Second attempt to fix @claude for PRs from forks --- .github/workflows/claude.yml | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/.github/workflows/claude.yml b/.github/workflows/claude.yml index fd1b528f3..235de3d36 100644 --- a/.github/workflows/claude.yml +++ b/.github/workflows/claude.yml @@ -30,16 +30,34 @@ jobs: with: fetch-depth: 1 - # Workaround for claude-code-action bug with fork PRs: The action tries to fetch by branch name, which doesn't - # exist on origin for forks. Pre-fetch the PR ref so it's available as a local ref. - - name: Fetch fork PR ref (if applicable) - if: github.event.issue.pull_request != '' && github.event.issue.pull_request != null + # Workaround for claude-code-action bug with fork PRs: The action fetches by branch name + # (git fetch origin --depth=N ), but fork PR branches don't exist on origin. + # Fix: redirect origin to the fork's URL so the action can fetch the branch directly. + - name: Configure git remote for fork PRs env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - PR_NUMBER=$(gh pr view ${{ github.event.issue.number }} --json number -q .number 2>/dev/null || echo "") - if [ -n "$PR_NUMBER" ]; then - git fetch origin refs/pull/${PR_NUMBER}/head:refs/remotes/pull/${PR_NUMBER}/head || true + # Determine PR number based on event type + if [ "${{ github.event_name }}" = "issue_comment" ]; then + PR_NUMBER="${{ github.event.issue.number }}" + elif [ "${{ github.event_name }}" = "pull_request_review_comment" ] || [ "${{ github.event_name }}" = "pull_request_review" ]; then + PR_NUMBER="${{ github.event.pull_request.number }}" + else + exit 0 # issues event — no PR branch to worry about + fi + + # Fetch fork info in one API call; silently skip if this is not a PR + PR_INFO=$(gh pr view "${PR_NUMBER}" --json isCrossRepository,headRepositoryOwner,headRepository 2>/dev/null || echo "") + if [ -z "$PR_INFO" ]; then + exit 0 + fi + + IS_FORK=$(echo "$PR_INFO" | jq -r '.isCrossRepository') + if [ "$IS_FORK" = "true" ]; then + FORK_OWNER=$(echo "$PR_INFO" | jq -r '.headRepositoryOwner.login') + FORK_REPO=$(echo "$PR_INFO" | jq -r '.headRepository.name') + echo "Fork PR detected from ${FORK_OWNER}/${FORK_REPO}: updating origin to fork URL" + git remote set-url origin "https://github.com/${FORK_OWNER}/${FORK_REPO}.git" fi - name: Run Claude Code