Skip to content

Commit 2d46172

Browse files
Update CI auto-formate (RustPython#6237)
--------- Signed-off-by: Yash Suthar <yashsuthar983@gmail.com> Co-authored-by: fanninpm <luxverdans@sbcglobal.net>
1 parent c9ba956 commit 2d46172

File tree

2 files changed

+153
-4
lines changed

2 files changed

+153
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,13 @@ jobs:
307307
run: python -I whats_left.py
308308

309309
lint:
310-
name: Check Rust code with rustfmt and clippy
310+
name: Check Rust code with clippy
311311
runs-on: ubuntu-latest
312312
steps:
313313
- uses: actions/checkout@v5
314314
- uses: dtolnay/rust-toolchain@stable
315315
with:
316-
components: rustfmt, clippy
317-
- name: run rustfmt
318-
run: cargo fmt --check
316+
components: clippy
319317
- name: run clippy on wasm
320318
run: cargo clippy --manifest-path=wasm/lib/Cargo.toml -- -Dwarnings
321319
- uses: actions/setup-python@v6
@@ -450,3 +448,55 @@ jobs:
450448
run: wasmer run --dir `pwd` target/wasm32-wasip1/release/rustpython.wasm -- `pwd`/extra_tests/snippets/stdlib_random.py
451449
- name: run cpython unittest
452450
run: wasmer run --dir `pwd` target/wasm32-wasip1/release/rustpython.wasm -- `pwd`/Lib/test/test_int.py
451+
452+
auto_format_commit:
453+
needs: [rust_tests, exotic_targets, snippets_cpython, lint, miri, wasm, wasm-wasi]
454+
permissions:
455+
contents: write
456+
pull-requests: write
457+
name: Auto-format code
458+
runs-on: ubuntu-latest
459+
if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
460+
concurrency:
461+
group: fmt-${{ github.ref }}
462+
cancel-in-progress: true
463+
464+
steps:
465+
- name: Checkout code
466+
uses: actions/checkout@v5
467+
with:
468+
fetch-depth: 0
469+
ref: ${{ github.head_ref || github.ref_name }}
470+
471+
- name: Setup Rust
472+
uses: dtolnay/rust-toolchain@stable
473+
with:
474+
components: rustfmt
475+
476+
- name: Run cargo fmt
477+
run: |
478+
echo "Running cargo fmt --all"
479+
cargo fmt --all
480+
481+
- name: Commit and push if changes
482+
id: commit
483+
run: |
484+
git config user.name "github-actions[bot]"
485+
git config user.email "github-actions[bot]@users.noreply.github.com"
486+
if [ -n "$(git status --porcelain)" ]; then
487+
git add -u
488+
git commit -m "Auto-format code [skip ci]"
489+
git push
490+
echo "formatted=true" >> $GITHUB_OUTPUT
491+
else
492+
echo "formatted=false" >> $GITHUB_OUTPUT
493+
fi
494+
495+
- name: Comment on PR if formatting was applied
496+
if: steps.commit.outputs.formatted == 'true' && github.event_name == 'pull_request'
497+
uses: marocchino/sticky-pull-request-comment@v2
498+
with:
499+
message: |
500+
Code has been automatically formatted.
501+
No action needed.
502+
the changes were committed with `[skip ci]`.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
**Triggered by commit:** `${{ github.event.pull_request.head.sha }}`
90+
**Last formatted:** ${{ github.event.pull_request.updated_at }}
91+
92+
You may need to pull the latest changes before pushing again:
93+
```bash
94+
git pull origin ${{ github.event.pull_request.head.ref }}
95+
```
96+
97+
- name: No formatting needed
98+
if: steps.check_changes.outputs.has_changes == 'false'
99+
run: echo "Code is already properly formatted"

0 commit comments

Comments
 (0)