Skip to content

Commit 40a778d

Browse files
committed
Added a seprate auto format workflow
Signed-off-by: Yash Suthar <yashsuthar983@gmail.com>
1 parent 3ed9216 commit 40a778d

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: PR Auto-format
2+
3+
# This workflow triggers when a PR is opened/updated
4+
on:
5+
pull_request_target:
6+
types: [opened, synchronize, reopened]
7+
branches:
8+
- main
9+
- release
10+
11+
concurrency:
12+
group: pr-fmt-${{ github.event.pull_request.number }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
auto_format:
17+
if: |
18+
!contains(github.event.pull_request.labels.*.name, 'skip:ci') &&
19+
!contains(github.event.pull_request.head.sha, '[skip ci]')
20+
permissions:
21+
contents: write
22+
pull-requests: write
23+
checks: read
24+
runs-on: ubuntu-latest
25+
timeout-minutes: 60
26+
27+
steps:
28+
- name: Checkout PR branch
29+
uses: actions/checkout@v5
30+
with:
31+
ref: ${{ github.event.pull_request.head.ref }}
32+
repository: ${{ github.event.pull_request.head.repo.full_name }}
33+
token: ${{ secrets.GITHUB_TOKEN }}
34+
fetch-depth: 0
35+
36+
# Wait for all PR check runs to complete
37+
- name: Wait for all checks to complete
38+
uses: poseidon/wait-for-status-checks@v0.6.0
39+
with:
40+
token: ${{ secrets.GITHUB_TOKEN }}
41+
delay: 60
42+
interval: 30
43+
timeout: 7200
44+
45+
- name: CI completed successfully
46+
run: echo "CI workflow completed successfully - proceeding with auto-format"
47+
48+
- name: Setup Rust
49+
uses: dtolnay/rust-toolchain@stable
50+
with:
51+
components: rustfmt
52+
53+
- name: Run cargo fmt
54+
run: |
55+
echo "Running cargo fmt --all on PR #${{ github.event.pull_request.number }}"
56+
cargo fmt --all
57+
58+
- name: Check for formatting changes
59+
id: check_changes
60+
run: |
61+
if [ -n "$(git status --porcelain)" ]; then
62+
echo "has_changes=true" >> $GITHUB_OUTPUT
63+
else
64+
echo "has_changes=false" >> $GITHUB_OUTPUT
65+
fi
66+
67+
- name: Commit and push formatting changes
68+
if: steps.check_changes.outputs.has_changes == 'true'
69+
run: |
70+
git config user.name "github-actions[bot]"
71+
git config user.email "github-actions[bot]@users.noreply.github.com"
72+
73+
git add -u
74+
git commit -m "Auto-format code [skip ci]"
75+
76+
git push origin HEAD:${{ github.event.pull_request.head.ref }}
77+
78+
- name: Comment on PR
79+
if: steps.check_changes.outputs.has_changes == 'true'
80+
uses: marocchino/sticky-pull-request-comment@v2
81+
with:
82+
number: ${{ github.event.pull_request.number }}
83+
message: |
84+
**Code has been automatically formatted**
85+
86+
The code in this PR has been formatted using `cargo fmt`.
87+
The changes have been committed with `[skip ci]` to avoid triggering another CI run.
88+
89+
You may need to pull the latest changes before pushing again:
90+
```bash
91+
git pull origin ${{ github.event.pull_request.head.ref }}
92+
```
93+
94+
- name: No formatting needed
95+
if: steps.check_changes.outputs.has_changes == 'false'
96+
run: echo "Code is already properly formatted"

0 commit comments

Comments
 (0)