|
| 1 | +name: Auto-format PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - release |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: auto-format-${{ github.event.pull_request.number }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +jobs: |
| 15 | + auto_format: |
| 16 | + if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }} |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + pull-requests: write |
| 21 | + steps: |
| 22 | + - name: Checkout PR branch |
| 23 | + uses: actions/checkout@v5 |
| 24 | + with: |
| 25 | + ref: ${{ github.event.pull_request.head.sha }} |
| 26 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 27 | + token: ${{ secrets.AUTO_FORMAT_PAT }} |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Setup Rust |
| 31 | + uses: dtolnay/rust-toolchain@stable |
| 32 | + with: |
| 33 | + components: rustfmt |
| 34 | + |
| 35 | + - name: Run cargo fmt |
| 36 | + run: cargo fmt --all |
| 37 | + |
| 38 | + - name: Configure git |
| 39 | + run: | |
| 40 | + git config user.name "github-actions[bot]" |
| 41 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 42 | +
|
| 43 | + - name: Check for changes |
| 44 | + id: check-changes |
| 45 | + run: | |
| 46 | + if [ -n "$(git status --porcelain)" ]; then |
| 47 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 48 | + else |
| 49 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Commit and push |
| 53 | + if: steps.check-changes.outputs.has_changes == 'true' |
| 54 | + run: | |
| 55 | + git add -u |
| 56 | + git commit -m "Auto-format: cargo fmt --all" |
| 57 | + git push origin HEAD:${{ github.event.pull_request.head.ref }} |
| 58 | +
|
| 59 | + - name: Comment on PR |
| 60 | + if: steps.check-changes.outputs.has_changes == 'true' |
| 61 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 62 | + with: |
| 63 | + number: ${{ github.event.pull_request.number }} |
| 64 | + message: | |
| 65 | + **Code has been automatically formatted** |
| 66 | +
|
| 67 | + The code in this PR has been formatted using `cargo fmt --all`. |
| 68 | + Please pull the latest changes before pushing again: |
| 69 | + ```bash |
| 70 | + git pull origin ${{ github.event.pull_request.head.ref }} |
| 71 | + ``` |
0 commit comments