Просмотр исходного кода

Second attempt to fix @claude for PRs from forks (#21633)

Jeremy Stretch 1 день назад
Родитель
Сommit
f938309ed9
1 измененных файлов с 25 добавлено и 7 удалено
  1. 25 7
      .github/workflows/claude.yml

+ 25 - 7
.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 <branch>), 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