Skip to content

Commit ef7265c

Browse files
👷 Add pre-commit workflow (#1684)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3b5b92e commit ef7265c

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ jobs:
8181
- name: Install Pydantic v2
8282
if: matrix.pydantic-version == 'pydantic-v2'
8383
run: uv pip install --upgrade "pydantic>=2.0.2,<3.0.0"
84-
- name: Lint
85-
if: matrix.pydantic-version == 'pydantic-v2' && matrix.python-version != '3.8'
86-
run: bash scripts/lint.sh
8784
- run: mkdir coverage
8885
- name: Test
8986
run: bash scripts/test.sh

.pre-commit-config.yaml

Lines changed: 0 additions & 5 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
@@ -20,6 +18,3 @@ repos:
2018
args:
2119
- --fix
2220
- 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

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
-r requirements-tests.txt
44
-r requirements-docs.txt
55

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

0 commit comments

Comments
 (0)