fix(workflow): use prefixed temporary branch name for fork PRs to prevent overwriting real branches

This commit is contained in:
Gergo Magyar 2026-03-18 07:40:53 +00:00
parent b48cfe9894
commit 1326490a5b
2 changed files with 9 additions and 4 deletions

View file

@ -85,14 +85,17 @@ jobs:
# for fork PRs. Create a temporary branch ref via the API so the action
# can find it. Using the API (not git push) avoids the GITHUB_TOKEN
# restriction that blocks pushing commits containing workflow file changes.
# Use a prefixed temporary branch name to avoid overwriting real branches
# (e.g. a fork branch named "main" would overwrite origin/main).
- name: Create fork branch ref on origin
id: push-fork
if: steps.pr.outputs.is_fork == 'true'
env:
FORK_BRANCH: ${{ steps.pr.outputs.branch }}
FORK_BRANCH: claude-tmp/fork-pr-${{ steps.pr.outputs.number }}
FORK_SHA: ${{ steps.pr.outputs.sha }}
GH_TOKEN: ${{ github.token }}
run: |
echo "FORK_BRANCH=$FORK_BRANCH" >> "$GITHUB_ENV"
gh api "repos/${{ github.repository }}/git/refs" \
--method POST \
-f ref="refs/heads/$FORK_BRANCH" \
@ -116,6 +119,5 @@ jobs:
- name: Delete fork branch ref from origin
if: always() && steps.push-fork.outcome == 'success'
env:
FORK_BRANCH: ${{ steps.pr.outputs.branch }}
GH_TOKEN: ${{ github.token }}
run: gh api "repos/${{ github.repository }}/git/refs/heads/$FORK_BRANCH" --method DELETE || true

View file

@ -64,6 +64,7 @@ jobs:
const isFork = pr.head.repo.full_name !== pr.base.repo.full_name;
core.setOutput('is_pr', 'true');
core.setOutput('number', String(prNumber));
core.setOutput('is_fork', String(isFork));
core.setOutput('branch', pr.head.ref);
core.setOutput('sha', pr.head.sha);
@ -78,14 +79,17 @@ jobs:
# for fork PRs. Create a temporary branch ref via the API so the action
# can find it. Using the API (not git push) avoids the GITHUB_TOKEN
# restriction that blocks pushing commits containing workflow file changes.
# Use a prefixed temporary branch name to avoid overwriting real branches
# (e.g. a fork branch named "main" would overwrite origin/main).
- name: Create fork branch ref on origin
id: push-fork
if: steps.pr.outputs.is_fork == 'true'
env:
FORK_BRANCH: ${{ steps.pr.outputs.branch }}
FORK_BRANCH: claude-tmp/fork-pr-${{ steps.pr.outputs.number }}
FORK_SHA: ${{ steps.pr.outputs.sha }}
GH_TOKEN: ${{ github.token }}
run: |
echo "FORK_BRANCH=$FORK_BRANCH" >> "$GITHUB_ENV"
gh api "repos/${{ github.repository }}/git/refs" \
--method POST \
-f ref="refs/heads/$FORK_BRANCH" \
@ -110,6 +114,5 @@ jobs:
- name: Delete fork branch ref from origin
if: always() && steps.push-fork.outcome == 'success'
env:
FORK_BRANCH: ${{ steps.pr.outputs.branch }}
GH_TOKEN: ${{ github.token }}
run: gh api "repos/${{ github.repository }}/git/refs/heads/$FORK_BRANCH" --method DELETE || true