Skip to content

Commit 2793361

Browse files
authored
Auto-fix file formatting in PRs (#84)
1 parent d5f7925 commit 2793361

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

.github/workflows/format.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Auto-fix file format
2+
3+
# Since pre-commit.ci cannot run local hooks, run them here manually to fixup formatting in pull requests
4+
5+
on: pull_request
6+
7+
env:
8+
# SSH Deploy Key with write access (needed to trigger push workflow runs in created pull request)
9+
SSH_KEY: ${{ secrets.SSH_KEY_GITHUB_ACTION }}
10+
11+
jobs:
12+
pre-commit:
13+
name: Format files with pre-commit
14+
runs-on: ubuntu-24.04
15+
steps:
16+
17+
- uses: actions/checkout@v4
18+
with:
19+
ssh-key: ${{ env.SSH_KEY }}
20+
fetch-depth: 0
21+
22+
- name: Install python
23+
uses: actions/setup-python@v3
24+
25+
- name: Fix formatting
26+
continue-on-error: true
27+
id: format
28+
run: |
29+
git checkout ${{github.event.pull_request.head.ref}}
30+
# Skip for commits created by bots (such as me)
31+
AUTHOR=$(git log -1 --pretty="%an %ae")
32+
if [[ $AUTHOR =~ [Bb]ot ]]; then
33+
echo "Commit made by $AUTHOR, skipping further steps in the pipeline."
34+
exit 0
35+
fi
36+
# Install pre-commit
37+
python3 -m pip install pre-commit
38+
pre-commit install
39+
# Format files
40+
FILES=$(git diff --name-only HEAD origin/$GITHUB_BASE_REF | tr '\n' ' ')
41+
echo $FILES
42+
pre-commit run --files $FILES # non-zero exit code if files were modified
43+
44+
- name: Push formatted fixes
45+
if: steps.format.outcome == 'failure'
46+
run: |
47+
git config user.name "Bot"
48+
git config user.email "<[email protected]>"
49+
git commit -am "Fix file format"
50+
git config push.autoSetupRemote true
51+
git push

0 commit comments

Comments
 (0)