Skip to content

Commit 927762a

Browse files
👷 Add pre-commit workflow (#266)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b47d301 commit 927762a

File tree

4 files changed

+108
-14
lines changed

4 files changed

+108
-14
lines changed

.github/workflows/pre-commit.yml

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

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ jobs:
7171
limit-access-to-actor: true
7272
- name: Install Dependencies
7373
run: uv pip install -r requirements-tests.txt
74-
- name: Lint
75-
run: bash scripts/lint.sh
7674
- run: mkdir coverage
7775
- name: Test
7876
run: bash scripts/test.sh

.pre-commit-config.yaml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# See https://pre-commit.com for more information
22
# See https://pre-commit.com/hooks.html for more hooks
3-
default_language_version:
4-
python: python3.10
53
repos:
64
- repo: https://github.com/pre-commit/pre-commit-hooks
75
rev: v6.0.0
@@ -13,13 +11,18 @@ repos:
1311
- --unsafe
1412
- id: end-of-file-fixer
1513
- id: trailing-whitespace
16-
- repo: https://github.com/astral-sh/ruff-pre-commit
17-
rev: v0.14.6
14+
- repo: local
1815
hooks:
19-
- id: ruff
20-
args:
21-
- --fix
22-
- id: ruff-format
23-
ci:
24-
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
25-
autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate
16+
- id: local-ruff-check
17+
name: ruff check
18+
entry: uv run ruff check --force-exclude --fix --exit-non-zero-on-fix
19+
require_serial: true
20+
language: unsupported
21+
types: [python]
22+
23+
- id: local-ruff-format
24+
name: ruff format
25+
entry: uv run ruff format --force-exclude --exit-non-zero-on-format
26+
require_serial: true
27+
language: unsupported
28+
types: [python]

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
-r requirements-tests.txt
44

5-
pre-commit >=2.17.0,<5.0.0
5+
prek>=0.2.24,<1.0.0

0 commit comments

Comments
 (0)