Skip to content

Commit 0b6e11a

Browse files
committed
👷 Add pre-commit workflow
1 parent f5fe306 commit 0b6e11a

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

.github/workflows/pre-commit.yml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
9+
env:
10+
IS_FORK: ${{ github.event.pull_request.head.repo.full_name != github.repository }}
11+
12+
jobs:
13+
pre-commit:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Dump GitHub context
17+
env:
18+
GITHUB_CONTEXT: ${{ toJson(github) }}
19+
run: echo "$GITHUB_CONTEXT"
20+
- uses: actions/checkout@v5
21+
name: Checkout PR for own repo
22+
if: env.IS_FORK == 'false'
23+
with:
24+
# To be able to commit it needs to fetch the head of the branch, not the
25+
# merge commit
26+
ref: ${{ github.head_ref }}
27+
# And it needs the full history to be able to compute diffs
28+
fetch-depth: 0
29+
# A token other than the default GITHUB_TOKEN is needed to be able to trigger CI
30+
token: ${{ secrets.PRE_COMMIT }}
31+
# pre-commit lite ci needs the default checkout configs to work
32+
- uses: actions/checkout@v5
33+
name: Checkout PR for fork
34+
if: env.IS_FORK == 'true'
35+
with:
36+
# To be able to commit it needs the head branch of the PR, the remote one
37+
ref: ${{ github.event.pull_request.head.sha }}
38+
fetch-depth: 0
39+
- name: Set up Python
40+
uses: actions/setup-python@v6
41+
with:
42+
python-version: "3.14"
43+
- name: Setup uv
44+
uses: astral-sh/setup-uv@v7
45+
with:
46+
cache-dependency-glob: |
47+
requirements**.txt
48+
pyproject.toml
49+
uv.lock
50+
- name: Install Dependencies
51+
run: |
52+
uv venv
53+
uv pip install -r requirements.txt
54+
- name: Run prek - pre-commit
55+
id: precommit
56+
run: uvx prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure
57+
continue-on-error: true
58+
- name: Commit and push changes
59+
if: env.IS_FORK == 'false'
60+
run: |
61+
git config user.name "github-actions[bot]"
62+
git config user.email "github-actions[bot]@users.noreply.github.com"
63+
git add -A
64+
if git diff --staged --quiet; then
65+
echo "No changes to commit"
66+
else
67+
git commit -m "🎨 Auto format"
68+
git push
69+
fi
70+
- uses: pre-commit-ci/[email protected]
71+
if: env.IS_FORK == 'true'
72+
with:
73+
msg: 🎨 Auto format
74+
- name: Error out on pre-commit errors
75+
if: steps.precommit.outcome == 'failure'
76+
run: exit 1
77+
78+
# https://github.com/marketplace/actions/alls-green#why
79+
pre-commit-alls-green: # This job does nothing and is only used for the branch protection
80+
if: always()
81+
needs:
82+
- pre-commit
83+
runs-on: ubuntu-latest
84+
steps:
85+
- name: Dump GitHub context
86+
env:
87+
GITHUB_CONTEXT: ${{ toJson(github) }}
88+
run: echo "$GITHUB_CONTEXT"
89+
- name: Decide whether the needed jobs succeeded or failed
90+
uses: re-actors/alls-green@release/v1
91+
with:
92+
jobs: ${{ toJSON(needs) }}

0 commit comments

Comments
 (0)