|
| 1 | +name: Auto Format |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + pull_request: |
| 7 | + branches: [main, develop] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +jobs: |
| 15 | + format: |
| 16 | + name: 🔧 Auto Format |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + steps: |
| 22 | + - name: 📥 Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + ref: ${{ github.head_ref || github.ref_name }} |
| 27 | + |
| 28 | + - name: Setup Node.js |
| 29 | + uses: actions/setup-node@v4 |
| 30 | + with: |
| 31 | + node-version: 24 |
| 32 | + cache: 'npm' |
| 33 | + |
| 34 | + - name: Cache node_modules |
| 35 | + uses: actions/cache@v4 |
| 36 | + with: |
| 37 | + path: node_modules |
| 38 | + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} |
| 39 | + restore-keys: | |
| 40 | + ${{ runner.os }}-node- |
| 41 | +
|
| 42 | + - name: Install dependencies |
| 43 | + run: npm ci |
| 44 | + |
| 45 | + - name: Check current formatting |
| 46 | + id: format-check |
| 47 | + run: | |
| 48 | + if npm run format:check; then |
| 49 | + echo "format_needed=false" >> $GITHUB_OUTPUT |
| 50 | + echo "✅ Code is already properly formatted" |
| 51 | + else |
| 52 | + echo "format_needed=true" >> $GITHUB_OUTPUT |
| 53 | + echo "⚠️ Code formatting issues found" |
| 54 | + fi |
| 55 | +
|
| 56 | + - name: Format code |
| 57 | + if: steps.format-check.outputs.format_needed == 'true' |
| 58 | + run: npm run format |
| 59 | + |
| 60 | + - name: Check for changes |
| 61 | + id: changes |
| 62 | + if: steps.format-check.outputs.format_needed == 'true' |
| 63 | + run: | |
| 64 | + if git diff --quiet; then |
| 65 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 66 | + echo "ℹ️ No formatting changes to commit" |
| 67 | + else |
| 68 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 69 | + echo "📝 Formatting changes detected" |
| 70 | + fi |
| 71 | +
|
| 72 | + # CI cannot commit to workflow files, so we need to check for non-workflow changes |
| 73 | + - name: Check for non-workflow changes |
| 74 | + id: non-workflow-changes |
| 75 | + if: steps.changes.outputs.has_changes == 'true' |
| 76 | + run: | |
| 77 | + git add -A |
| 78 | + git reset .github/workflows/ |
| 79 | + if git diff --cached --quiet; then |
| 80 | + echo "has_non_workflow_changes=false" >> $GITHUB_OUTPUT |
| 81 | + echo "ℹ️ Only workflow files have formatting changes - skipping commit" |
| 82 | + else |
| 83 | + echo "has_non_workflow_changes=true" >> $GITHUB_OUTPUT |
| 84 | + echo "📝 Non-workflow formatting changes detected" |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Configure Git |
| 88 | + if: |
| 89 | + steps.non-workflow-changes.outputs.has_non_workflow_changes == 'true' |
| 90 | + run: | |
| 91 | + git config --local user.email "[email protected]" |
| 92 | + git config --local user.name "GitHub Action" |
| 93 | +
|
| 94 | + - name: Commit and push changes |
| 95 | + if: |
| 96 | + steps.non-workflow-changes.outputs.has_non_workflow_changes == 'true' |
| 97 | + env: |
| 98 | + TARGET_REF: ${{ github.head_ref || github.ref_name }} |
| 99 | + run: | |
| 100 | + git commit -m "chore: 🔧 Auto-format code with Prettier [skip ci] |
| 101 | +
|
| 102 | + This commit was automatically generated by the auto-format workflow. |
| 103 | + Changes include: |
| 104 | + - Code formatting fixes |
| 105 | + - Consistent indentation |
| 106 | + - Proper line endings" |
| 107 | + git push origin HEAD:"$TARGET_REF" |
| 108 | +
|
| 109 | + - name: Comment on PR |
| 110 | + if: |
| 111 | + steps.non-workflow-changes.outputs.has_non_workflow_changes == 'true' |
| 112 | + && github.event_name == 'pull_request' |
| 113 | + uses: actions/github-script@v7 |
| 114 | + with: |
| 115 | + script: | |
| 116 | + github.rest.issues.createComment({ |
| 117 | + issue_number: context.issue.number, |
| 118 | + owner: context.repo.owner, |
| 119 | + repo: context.repo.repo, |
| 120 | + body: '🔧 **Auto-formatting applied!**\n\nI\'ve automatically formatted the code using Prettier and pushed the changes. The formatting is now consistent with the project standards.' |
| 121 | + }) |
| 122 | +
|
| 123 | + - name: Success message |
| 124 | + if: |
| 125 | + steps.changes.outputs.has_changes == 'false' || |
| 126 | + steps.format-check.outputs.format_needed == 'false' || |
| 127 | + steps.non-workflow-changes.outputs.has_non_workflow_changes == 'false' |
| 128 | + run: | |
| 129 | + if [ "${{ steps.changes.outputs.has_changes }}" == "true" ] && [ "${{ steps.non-workflow-changes.outputs.has_non_workflow_changes }}" == "false" ]; then |
| 130 | + echo "✅ Only workflow files had formatting changes - no commit needed!" |
| 131 | + else |
| 132 | + echo "✅ No formatting changes needed - code is already properly formatted!" |
| 133 | + fi |
0 commit comments