Skip to content

Commit 55f7c36

Browse files
👷 Add pre-commit workflow (#135)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 91ff85e commit 55f7c36

5 files changed

Lines changed: 126 additions & 166 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
pyproject.toml
49+
uv.lock
50+
- name: Run prek - pre-commit
51+
id: precommit
52+
run: uvx prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure
53+
continue-on-error: true
54+
- name: Commit and push changes
55+
if: env.HAS_SECRETS == 'true'
56+
run: |
57+
git config user.name "github-actions[bot]"
58+
git config user.email "github-actions[bot]@users.noreply.github.com"
59+
git add -A
60+
if git diff --staged --quiet; then
61+
echo "No changes to commit"
62+
else
63+
git commit -m "🎨 Auto format"
64+
git push
65+
fi
66+
- uses: pre-commit-ci/lite-action@v1.1.0
67+
if: env.HAS_SECRETS == 'false'
68+
with:
69+
msg: 🎨 Auto format
70+
- name: Error out on pre-commit errors
71+
if: steps.precommit.outcome == 'failure'
72+
run: exit 1
73+
74+
# https://github.com/marketplace/actions/alls-green#why
75+
pre-commit-alls-green: # This job does nothing and is only used for the branch protection
76+
if: always()
77+
needs:
78+
- pre-commit
79+
runs-on: ubuntu-latest
80+
steps:
81+
- name: Dump GitHub context
82+
env:
83+
GITHUB_CONTEXT: ${{ toJson(github) }}
84+
run: echo "$GITHUB_CONTEXT"
85+
- name: Decide whether the needed jobs succeeded or failed
86+
uses: re-actors/alls-green@release/v1
87+
with:
88+
jobs: ${{ toJSON(needs) }}

‎.github/workflows/test.yml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ jobs:
5353
limit-access-to-actor: true
5454
- name: Install Dependencies
5555
run: uv sync --locked --all-extras --dev
56-
- name: Lint
57-
run: uv run --no-sync scripts/lint.sh
5856
- run: mkdir coverage
5957
- name: Test
6058
run: uv run --no-sync 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: v4.6.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.12.0
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]

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ standard = ["uvicorn[standard] >= 0.15.0"]
4444

4545
[dependency-groups]
4646
dev = [
47-
"pre-commit>=2.17.0,<5.0.0",
47+
"prek>=0.2.24,<1.0.0",
4848
"pytest>=4.4.0,<9.0.0",
4949
"coverage[toml]>=6.2,<8.0",
5050
"mypy==1.14.1",

0 commit comments

Comments
 (0)