Skip to content

Commit be80a14

Browse files
ci: auto format code when formatting step fails and update Cargo.lock if changed (#2567)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 053c79d commit be80a14

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

.github/workflows/CI-pr.yml

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,43 @@ jobs:
4141
name: Check formatting
4242
runs-on: ubuntu-latest
4343
steps:
44-
- name: Checkout repository
44+
- name: Checkout repository with token
45+
if: ${{ github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name }}
4546
uses: actions/checkout@v3
46-
47+
with:
48+
fetch-depth: 0
49+
ref: ${{ github.event.pull_request.head.ref }}
50+
token: ${{ secrets.AUTO_FILE_UPDATE_PAT }}
51+
52+
- name: Checkout repository for fork
53+
if: ${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name }}
54+
uses: actions/checkout@v3
55+
4756
- name: Install Rust
4857
uses: dtolnay/rust-toolchain@master
4958
with:
5059
toolchain: nightly
5160
components: rustfmt
5261

53-
- name: Check formatting
62+
- name: Check formatting for forked pull requests
63+
if: ${{ github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name }}
5464
shell: bash
5565
run: cargo +nightly fmt --all --check
5666

67+
- name: Run formatter
68+
if: ${{ github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name }}
69+
shell: bash
70+
run: |
71+
cargo +nightly fmt --all
72+
if ! git diff --exit-code --quiet -- crates; then
73+
echo "::notice::Formatting check failed"
74+
git config --local user.name 'github-actions[bot]'
75+
git config --local user.email '41898282+github-actions[bot]@users.noreply.github.com'
76+
git add crates
77+
git commit --message 'chore: run formatter'
78+
git push
79+
fi
80+
5781
check-msrv:
5882
name: Check compilation on MSRV toolchain
5983
runs-on: ${{ matrix.os }}
@@ -275,8 +299,17 @@ jobs:
275299
# - windows-latest
276300

277301
steps:
278-
- name: Checkout repository
302+
- name: Checkout repository for fork
303+
if: ${{ (github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) }}
304+
uses: actions/checkout@v3
305+
306+
- name: Checkout repository with token
307+
if: ${{ (github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name) }}
279308
uses: actions/checkout@v3
309+
with:
310+
fetch-depth: 0
311+
ref: ${{ github.event.pull_request.head.ref }}
312+
token: ${{ secrets.AUTO_FILE_UPDATE_PAT }}
280313

281314
- name: "Fetch base branch"
282315
shell: bash
@@ -322,6 +355,18 @@ jobs:
322355
shell: bash
323356
run: cargo clippy --all-features --all-targets
324357

358+
- name: Check Cargo.lock changed
359+
if: ${{ (github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name) }}
360+
shell: bash
361+
run: |
362+
if ! git diff --quiet --exit-code -- Cargo.lock ; then
363+
git config --local user.name 'github-actions[bot]'
364+
git config --local user.email '41898282+github-actions[bot]@users.noreply.github.com'
365+
git add Cargo.lock
366+
git commit --message 'chore: update Cargo.lock'
367+
git push
368+
fi
369+
325370
- name: Check files changed
326371
shell: bash
327372
run: |

0 commit comments

Comments
 (0)