|
| 1 | +on: |
| 2 | + workflow_run: |
| 3 | + workflows: ["CI"] |
| 4 | + types: |
| 5 | + - completed |
| 6 | + push: |
| 7 | + pull_request: |
| 8 | + types: [unlabeled, opened, synchronize, reopened] |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +name: Auto format |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + auto_format_commit: |
| 19 | + permissions: |
| 20 | + contents: write |
| 21 | + pull-requests: write |
| 22 | + name: Auto-format code |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: | |
| 25 | + ${{ |
| 26 | + github.event_name == 'workflow_dispatch' || github.event_name == 'pull_request' || |
| 27 | + (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' && !contains(github.event.workflow_run.head_commit.message, '[skip ci]')) || |
| 28 | + (github.event_name == 'push' && !contains(github.event.head_commit.message, '[skip ci]')) |
| 29 | + }} |
| 30 | + concurrency: |
| 31 | + group: fmt-${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_branch || github.ref }} |
| 32 | + cancel-in-progress: true |
| 33 | + |
| 34 | + steps: |
| 35 | + - name: Checkout code |
| 36 | + uses: actions/checkout@v5 |
| 37 | + with: |
| 38 | + fetch-depth: 0 |
| 39 | + ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_branch || github.head_ref || github.ref_name }} |
| 40 | + repository: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_repository.full_name || github.repository }} |
| 41 | + |
| 42 | + - name: Setup Rust |
| 43 | + uses: dtolnay/rust-toolchain@stable |
| 44 | + with: |
| 45 | + components: rustfmt |
| 46 | + |
| 47 | + - name: Run cargo fmt |
| 48 | + run: | |
| 49 | + echo "Running cargo fmt --all" |
| 50 | + cargo fmt --all |
| 51 | +
|
| 52 | + - name: Commit and push if changes |
| 53 | + id: commit |
| 54 | + run: | |
| 55 | + git config user.name "github-actions[bot]" |
| 56 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 57 | + if [ -n "$(git status --porcelain)" ]; then |
| 58 | + git add -u |
| 59 | + git commit -m "Auto-format code [skip ci]" |
| 60 | + git push |
| 61 | + echo "formatted=true" >> $GITHUB_OUTPUT |
| 62 | + else |
| 63 | + echo "formatted=false" >> $GITHUB_OUTPUT |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Comment on PR if formatting was applied |
| 67 | + if: steps.commit.outputs.formatted == 'true' && (github.event_name == 'pull_request' || github.event.workflow_run.event == 'pull_request') |
| 68 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 69 | + with: |
| 70 | + message: | |
| 71 | + Code has been automatically formatted. |
| 72 | + No action needed. |
| 73 | + the changes were committed with `[skip ci]`. |
0 commit comments