Skip to content

Commit 2059d2e

Browse files
👷 Add pre-commit workflow (#2056)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent ac9319d commit 2059d2e

File tree

6 files changed

+146
-133
lines changed

6 files changed

+146
-133
lines changed

.github/workflows/lint-backend.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.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.11"
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: uv sync
53+
working-directory: backend
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+
working-directory: backend
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/lite-action@v1.1.0
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) }}

.pre-commit-config.yaml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ repos:
1717
)$
1818
- id: trailing-whitespace
1919
exclude: ^frontend/src/client/.*
20-
- repo: https://github.com/charliermarsh/ruff-pre-commit
21-
rev: v0.2.2
22-
hooks:
23-
- id: ruff
24-
args:
25-
- --fix
26-
- id: ruff-format
2720
- repo: local
2821
hooks:
2922
- id: local-biome-check
@@ -33,6 +26,16 @@ repos:
3326
types: [text]
3427
files: ^frontend/
3528

36-
ci:
37-
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
38-
autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate
29+
- id: local-ruff-check
30+
name: ruff check
31+
entry: uv run ruff check --force-exclude --fix --exit-non-zero-on-fix
32+
require_serial: true
33+
language: unsupported
34+
types: [python]
35+
36+
- id: local-ruff-format
37+
name: ruff format
38+
entry: uv run ruff format --force-exclude --exit-non-zero-on-format
39+
require_serial: true
40+
language: unsupported
41+
types: [python]

backend/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dev-dependencies = [
2828
"pytest<8.0.0,>=7.4.3",
2929
"mypy<2.0.0,>=1.8.0",
3030
"ruff<1.0.0,>=0.2.2",
31-
"pre-commit<4.0.0,>=3.6.2",
31+
"prek>=0.2.24,<1.0.0",
3232
"types-passlib<2.0.0.0,>=1.7.7.20240106",
3333
"coverage<8.0.0,>=7.4.3",
3434
]

backend/uv.lock

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

0 commit comments

Comments
 (0)