|
| 1 | +name: Auto-Format Code (Fork Only) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '**' # Run on all branches |
| 7 | + paths-ignore: |
| 8 | + - '**.md' |
| 9 | + - 'docs/**' |
| 10 | + |
| 11 | +jobs: |
| 12 | + auto-format: |
| 13 | + # Only run on forks, never on the main repository |
| 14 | + if: github.repository != github.event.repository.full_name || github.repository_owner != 'MAIN_REPO_OWNER' |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Check if auto-formatting is enabled |
| 19 | + id: check_enabled |
| 20 | + env: |
| 21 | + ENABLE_AUTO_FORMAT: ${{ vars.ENABLE_AUTO_FORMAT }} |
| 22 | + run: | |
| 23 | + # Check for opt-in via repository variable |
| 24 | + if [ "$ENABLE_AUTO_FORMAT" = "true" ]; then |
| 25 | + echo "enabled=true" >> $GITHUB_OUTPUT |
| 26 | + echo "✅ Auto-formatting is enabled for this fork" |
| 27 | + else |
| 28 | + echo "enabled=false" >> $GITHUB_OUTPUT |
| 29 | + echo "⏭️ Auto-formatting is disabled." |
| 30 | + echo "To enable: Go to Settings > Secrets and variables > Actions > Variables" |
| 31 | + echo "Create a variable named ENABLE_AUTO_FORMAT with value: true" |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +
|
| 35 | + - name: Checkout code |
| 36 | + if: steps.check_enabled.outputs.enabled == 'true' |
| 37 | + uses: actions/checkout@v5 |
| 38 | + with: |
| 39 | + fetch-depth: 0 |
| 40 | + |
| 41 | + - name: Install PHP dependencies |
| 42 | + uses: ./.github/actions/composer-install |
| 43 | + with: |
| 44 | + cache-version: ${{ secrets.CACHE_VERSION }} |
| 45 | + |
| 46 | + |
| 47 | + - name: Run code formatter |
| 48 | + if: steps.check_enabled.outputs.enabled == 'true' |
| 49 | + run: | |
| 50 | + ./vendor/bin/pint --parallel --diff=${{ github.base_ref || github.ref_name }} |
| 51 | +
|
| 52 | + - name: Check for changes |
| 53 | + if: steps.check_enabled.outputs.enabled == 'true' |
| 54 | + id: check_changes |
| 55 | + run: | |
| 56 | + if [[ -n $(git status -s) ]]; then |
| 57 | + echo "changes=true" >> $GITHUB_OUTPUT |
| 58 | + echo "Code formatting changes detected" |
| 59 | + else |
| 60 | + echo "changes=false" >> $GITHUB_OUTPUT |
| 61 | + echo "No formatting changes needed" |
| 62 | + fi |
| 63 | +
|
| 64 | + - name: Commit and push changes |
| 65 | + if: steps.check_enabled.outputs.enabled == 'true' && steps.check_changes.outputs.changes == 'true' |
| 66 | + run: | |
| 67 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 68 | + git config --local user.name "github-actions[bot]" |
| 69 | + git add -A |
| 70 | + git commit -m "style: auto-format code [skip ci]" |
| 71 | + git push |
0 commit comments