-
Notifications
You must be signed in to change notification settings - Fork 8
80 lines (70 loc) · 3.07 KB
/
Copy pathautoformat.yml
File metadata and controls
80 lines (70 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: Auto-format & Auto-fix
# On every push (master AND PR branches), run `cargo fmt` and
# `cargo clippy --fix`, then commit any changes back to the same branch.
# Skips itself on bot-authored commits to avoid infinite loops, and on
# commits that are already CI-clean. Forked-repo PRs aren't covered — the
# default token can't push to forks.
on:
push:
branches: ['**']
permissions:
contents: write
# One run per branch at a time; a fix-commit superseding the triggering push
# cancels the now-stale run instead of racing it.
concurrency:
group: autofix-${{ github.ref }}
cancel-in-progress: true
jobs:
autofix:
# Skip when the previous commit was the bot itself (prevents loops) and
# when the commit message contains [skip autofmt].
if: |
github.event.head_commit.author.email != 'noreply@github.com' &&
!contains(github.event.head_commit.message, '[skip autofmt]') &&
!contains(github.event.head_commit.message, 'chore(autofmt)')
runs-on: blacksmith-4vcpu-ubuntu-2404
steps:
- uses: actions/checkout@v4
with:
# Need write access via the default token; fetch full history so we
# can push the resulting commit cleanly.
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Install nightly + stable toolchains
run: |
rustup toolchain install nightly --component rustfmt
rustup toolchain install stable --component clippy
- name: Install protoc
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
- uses: Swatinem/rust-cache@v2
# rustfmt.toml uses nightly-only options — must invoke +nightly explicitly
# so it matches the CI fmt-check job.
- name: Run cargo fmt
run: cargo +nightly fmt --all
- name: Run cargo clippy --fix
# --allow-dirty because fmt may have already produced uncommitted changes.
# --allow-staged for the same reason in case any pre-stage runs.
# Intentionally non-fatal: clippy autofixes are best-effort. If a fix
# introduces a compile error, we revert and only push the fmt changes.
run: |
set +e
cargo clippy --fix --all-targets --all-features --allow-dirty --allow-staged -- -D warnings
rc=$?
if [ $rc -ne 0 ]; then
echo "clippy --fix failed (likely couldn't auto-fix every warning); reverting clippy hunks only"
# Re-apply fmt result on top of HEAD without clippy's partial changes.
git checkout -- .
cargo +nightly fmt --all
fi
exit 0
- name: Commit & push if changed
run: |
if [ -z "$(git status --porcelain)" ]; then
echo "No formatting / clippy changes — nothing to push."
exit 0
fi
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git add -A
git commit -m "chore(autofmt): apply cargo fmt + clippy --fix [skip autofmt]"
git push origin "HEAD:${{ github.ref_name }}"