|
| 1 | +name: Format on demand |
| 2 | + |
| 3 | +# Triggered by commenting "/format" on a pull request. |
| 4 | +# Works for fork PRs when the contributor leaves "Allow edits from maintainers" |
| 5 | +# enabled (default). Gated to repo collaborators to prevent drive-by abuse. |
| 6 | + |
| 7 | +on: |
| 8 | + issue_comment: |
| 9 | + types: [created] |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: format-${{ github.event.issue.number }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + fmt: |
| 17 | + if: >- |
| 18 | + github.event.issue.pull_request != null |
| 19 | + && startsWith(github.event.comment.body, '/format') |
| 20 | + && (github.event.comment.author_association == 'OWNER' |
| 21 | + || github.event.comment.author_association == 'MEMBER' |
| 22 | + || github.event.comment.author_association == 'COLLABORATOR') |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: write |
| 26 | + pull-requests: write |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: React to triggering comment |
| 30 | + uses: actions/github-script@v7 |
| 31 | + with: |
| 32 | + script: | |
| 33 | + await github.rest.reactions.createForIssueComment({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + comment_id: context.payload.comment.id, |
| 37 | + content: 'eyes', |
| 38 | + }); |
| 39 | +
|
| 40 | + - name: Check out PR branch (handles fork PRs) |
| 41 | + env: |
| 42 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + run: | |
| 44 | + # `gh pr checkout` fetches the fork's branch and sets up a remote |
| 45 | + # that we can push back to via the token. |
| 46 | + gh pr checkout ${{ github.event.issue.number }} --repo "$GITHUB_REPOSITORY" |
| 47 | +
|
| 48 | + - name: Install Rust (rustfmt) |
| 49 | + uses: dtolnay/rust-toolchain@stable |
| 50 | + with: |
| 51 | + components: rustfmt |
| 52 | + |
| 53 | + - name: Run cargo fmt |
| 54 | + run: cargo fmt --all |
| 55 | + |
| 56 | + - name: Commit and push if changed |
| 57 | + id: push |
| 58 | + run: | |
| 59 | + if git diff --quiet; then |
| 60 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 61 | + echo "No formatting changes needed." |
| 62 | + exit 0 |
| 63 | + fi |
| 64 | + git config user.name 'github-actions[bot]' |
| 65 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 66 | + git add -A |
| 67 | + git commit -m "style: cargo fmt" |
| 68 | + git push |
| 69 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 70 | +
|
| 71 | + - name: Comment result on PR |
| 72 | + if: always() |
| 73 | + uses: actions/github-script@v7 |
| 74 | + with: |
| 75 | + script: | |
| 76 | + const status = '${{ job.status }}'; |
| 77 | + const changed = '${{ steps.push.outputs.changed }}' === 'true'; |
| 78 | + let body; |
| 79 | + if (status !== 'success') { |
| 80 | + body = ':x: `/format` failed. If this is a fork PR, make sure "Allow edits from maintainers" is enabled.'; |
| 81 | + } else if (changed) { |
| 82 | + body = ':sparkles: Formatted and pushed.'; |
| 83 | + } else { |
| 84 | + body = ':white_check_mark: Already formatted — nothing to do.'; |
| 85 | + } |
| 86 | + await github.rest.issues.createComment({ |
| 87 | + owner: context.repo.owner, |
| 88 | + repo: context.repo.repo, |
| 89 | + issue_number: context.issue.number, |
| 90 | + body, |
| 91 | + }); |
0 commit comments