From 72c40a7a5115ef62fffdc06f8db3abc56768099a Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Thu, 17 Sep 2026 20:26:12 +0200 Subject: [PATCH] Fix: CS-Fixer workflow can't auto-commit on pull_request events actions/checkout leaves a detached HEAD for pull_request-triggered runs (not a branch named e.g. "feature/x"), so stefanzweifel/git-auto-commit-action failed with "fatal: invalid reference" trying to push to that name. Worse, for PRs from forks the workflow token has no write access to the fork's branch at all, so even a fixed ref would still fail on permission. Split into two jobs: - pull_request (any branch, incl. forks): dry-run check only, fails the job on style violations without attempting to push anything. Contributors fix locally via `composer cs-fix`. - push to main/master (always same-repo): keeps the previous auto-fix-and-commit behavior, now pinned to the real branch name via an explicit ref/branch instead of relying on default detection. Also bumps actions/checkout v3 -> v4 and git-auto-commit-action v4 -> v7 (current Node-runtime versions, addressing the Node 20 deprecation warning surfaced in the failing run). Co-Authored-By: Claude Sonnet 5 --- .github/workflows/PHP-CS-Fixer.yml | 41 ++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/.github/workflows/PHP-CS-Fixer.yml b/.github/workflows/PHP-CS-Fixer.yml index 0451a59..3900a62 100644 --- a/.github/workflows/PHP-CS-Fixer.yml +++ b/.github/workflows/PHP-CS-Fixer.yml @@ -10,15 +10,45 @@ permissions: contents: read jobs: - code-style: - if: github.event.pull_request.draft == false + # Pull requests (including from forks) only ever get checked, never + # auto-committed to: the workflow token has no push access to a fork's + # branch, and even for same-repo PR branches actions/checkout leaves a + # detached HEAD, not the real branch ref, which broke the previous + # auto-commit step. Contributors run `composer cs-fix` locally instead. + check-style: + if: github.event_name == 'pull_request' && github.event.pull_request.draft == false + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + extensions: gd, intl, pdo_mysql + coverage: none # disable xdebug, pcov + + - name: Install Composer Dependencies + run: composer install --prefer-dist --no-progress + + - name: Check code style + run: PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix . --dry-run --diff --verbose + + # Direct pushes to main/master are same-repo by definition, so the + # workflow token can push a fix-up commit straight back to that branch. + fix-style: + if: github.event_name == 'push' runs-on: ubuntu-latest permissions: - contents: write # for Git to git apply + contents: write # for Git to commit and push fixes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 + with: + ref: ${{ github.ref_name }} - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -34,6 +64,7 @@ jobs: run: PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix . --diff --verbose - name: Commit and push fixed files - uses: stefanzweifel/git-auto-commit-action@v4 + uses: stefanzweifel/git-auto-commit-action@v7 with: commit_message: Apply php-cs-fixer changes + branch: ${{ github.ref_name }}