-
Notifications
You must be signed in to change notification settings - Fork 15
add /format action #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+91
−0
Merged
add /format action #353
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| name: Format on demand | ||
|
|
||
| # Triggered by commenting "/format" on a pull request. | ||
| # Works for fork PRs when the contributor leaves "Allow edits from maintainers" | ||
| # enabled (default). Gated to repo collaborators to prevent drive-by abuse. | ||
|
|
||
| on: | ||
| issue_comment: | ||
| types: [created] | ||
|
|
||
| concurrency: | ||
| group: format-${{ github.event.issue.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| fmt: | ||
| if: >- | ||
| github.event.issue.pull_request != null | ||
| && startsWith(github.event.comment.body, '/format') | ||
| && (github.event.comment.author_association == 'OWNER' | ||
| || github.event.comment.author_association == 'MEMBER' | ||
| || github.event.comment.author_association == 'COLLABORATOR') | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| steps: | ||
| - name: React to triggering comment | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| await github.rest.reactions.createForIssueComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: context.payload.comment.id, | ||
| content: 'eyes', | ||
| }); | ||
|
|
||
| - name: Check out PR branch (handles fork PRs) | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| # `gh pr checkout` fetches the fork's branch and sets up a remote | ||
| # that we can push back to via the token. | ||
| gh pr checkout ${{ github.event.issue.number }} --repo "$GITHUB_REPOSITORY" | ||
|
|
||
| - name: Install Rust (rustfmt) | ||
| uses: dtolnay/rust-toolchain@stable | ||
| with: | ||
| components: rustfmt | ||
|
|
||
| - name: Run cargo fmt | ||
| run: cargo fmt --all | ||
|
|
||
| - name: Commit and push if changed | ||
| id: push | ||
| run: | | ||
| if git diff --quiet; then | ||
| echo "changed=false" >> "$GITHUB_OUTPUT" | ||
| echo "No formatting changes needed." | ||
| exit 0 | ||
| fi | ||
| git config user.name 'github-actions[bot]' | ||
| git config user.email '41898282+github-actions[bot]@users.noreply.github.com' | ||
| git add -A | ||
| git commit -m "style: cargo fmt" | ||
| git push | ||
| echo "changed=true" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Comment result on PR | ||
| if: always() | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const status = '${{ job.status }}'; | ||
| const changed = '${{ steps.push.outputs.changed }}' === 'true'; | ||
| let body; | ||
| if (status !== 'success') { | ||
| body = ':x: `/format` failed. If this is a fork PR, make sure "Allow edits from maintainers" is enabled.'; | ||
| } else if (changed) { | ||
| body = ':sparkles: Formatted and pushed.'; | ||
| } else { | ||
| body = ':white_check_mark: Already formatted — nothing to do.'; | ||
| } | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body, | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.