|
| 1 | +# SPDX-FileCopyrightText: 2025 DB Systel GmbH |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +--- |
| 5 | +name: Auto-Format with Stylelint and Prettier on PR for "self-healing" PRs |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_call: |
| 9 | + |
| 10 | +jobs: |
| 11 | + format: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + ref: ${{github.event.pull_request.head.ref}} |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Check if Stylelint or Prettier update PR |
| 21 | + id: check_pr |
| 22 | + run: | |
| 23 | + echo "PR title: ${{ github.event.pull_request.title }}" |
| 24 | + if [[ "${{ github.event.pull_request.title }}" =~ "bump stylelint from" ]]; then |
| 25 | + echo "Stylelint update detected." |
| 26 | + echo "stylelint_update=true" >> $GITHUB_ENV |
| 27 | + elif [[ "${{ github.event.pull_request.title }}" =~ "bump prettier from" ]]; then |
| 28 | + echo "Prettier update detected." |
| 29 | + echo "prettier_update=true" >> $GITHUB_ENV |
| 30 | + else |
| 31 | + echo "No Stylelint or prettier updates detected." |
| 32 | + fi |
| 33 | +
|
| 34 | + - name: Set up Node.js |
| 35 | + if: env.stylelint_update == 'true' || env.prettier_update == 'true' |
| 36 | + uses: actions/setup-node@v4 |
| 37 | + with: |
| 38 | + node-version-file: ".nvmrc" |
| 39 | + |
| 40 | + - name: Install dependencies |
| 41 | + if: env.stylelint_update == 'true' || env.prettier_update == 'true' |
| 42 | + run: | |
| 43 | + npm ci |
| 44 | +
|
| 45 | + - name: Run Stylelint to format the code |
| 46 | + if: env.stylelint_update == 'true' |
| 47 | + run: | |
| 48 | + npx --no stylelint "**/*.*css" --fix |
| 49 | +
|
| 50 | + - name: Run Prettier to format the code |
| 51 | + if: env.prettier_update == 'true' |
| 52 | + run: | |
| 53 | + npx --no prettier . --write |
| 54 | +
|
| 55 | + - name: Commit changes if formatting is done |
| 56 | + if: env.stylelint_update == 'true' || env.prettier_update == 'true' |
| 57 | + run: | |
| 58 | + git config --global user.name 'github-actions[bot]' |
| 59 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 60 | + echo 'COMMIT_MAIL=github-actions[bot]@users.noreply.github.com' >> .env |
| 61 | +
|
| 62 | + git add . |
| 63 | + git commit --all -m "refactor(test): auto-format codebase" || echo "No changes to commit" |
| 64 | + git push origin HEAD:${{ github.head_ref }} # Push back to the same PR branch |
0 commit comments