diff --git a/.github/workflows/phpcs-autofix.yml b/.github/workflows/phpcs-autofix.yml new file mode 100644 index 0000000..f510efc --- /dev/null +++ b/.github/workflows/phpcs-autofix.yml @@ -0,0 +1,79 @@ +name: PHP CodeSniffer Autofix + +permissions: + contents: write + pull-requests: write + +on: + pull_request: + branches: + - master + workflow_dispatch: + +jobs: + phpcs_autofix: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.1" + extensions: mbstring, xml, ctype, iconv, json + tools: composer + + - name: Allow Composer plugins + run: | + composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true + composer config --no-plugins allow-plugins.phpcompatibility/phpcs-compatinfo true + # Add any other plugins if they cause issues + + - name: Install PHP_CodeSniffer and WordPress Coding Standards + run: | + composer require --dev squizlabs/php_codesniffer "*" + composer require --dev wp-coding-standards/wpcs "*" + composer require --dev phpcompatibility/phpcompatibility-wp "*" + + - name: Run PHPCBF to fix coding standards + run: vendor/bin/phpcbf --standard=phpcs.xml --extensions=php . || true + + - name: Commit and Push PHPCBF fixes + run: | + git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + + echo "Current git status:" + git status + + # Add all changed files. phpcbf modifies files in place. + # We expect only .php files to be changed by phpcbf. + git add . + + # Check again if staging made any difference to status, then commit + echo "Git status after add:" + git status + + if git diff --staged --quiet; then + echo "No changes staged, nothing to commit." + else + echo "Staged changes found, attempting commit..." + git commit -m "Apply PHPCBF fixes [skip ci]" -m "Automated PHPCBF fixes." --no-verify + + # Determine the branch to push to + # For PRs, github.head_ref is the source branch name. + # For workflow_dispatch, github.ref_name is the branch the workflow was run on. + BRANCH_NAME="" + if [[ -n "${{ github.head_ref }}" ]]; then + BRANCH_NAME="${{ github.head_ref }}" + else + BRANCH_NAME="${{ github.ref_name }}" + fi + + echo "Pushing changes to branch: $BRANCH_NAME" + git push origin "$BRANCH_NAME" + echo "Changes pushed successfully." + fi