Skip to content

Commit 8190f3c

Browse files
👷 Add pre-commit workflow (#28)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 71f2e14 commit 8190f3c

5 files changed

Lines changed: 151 additions & 13 deletions

File tree

.github/workflows/pre-commit.yml

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

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ jobs:
7070
limit-access-to-actor: true
7171
- name: Install Dependencies
7272
run: uv pip install -r requirements-tests.txt
73-
- name: Lint
74-
run: bash scripts/lint.sh
7573
- run: mkdir coverage
7674
- name: Test
7775
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.13.3
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@ source-includes = [
104104
"requirements*.txt",
105105
"scripts/",
106106
]
107+
108+
[dependency-groups]
109+
dev = [
110+
"prek>=0.2.24,<1.0.0",
111+
]

uv.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)