Skip to content

Commit b92fe66

Browse files
👷 Add pre-commit GitHub Action workflow (#413)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b0b441c commit b92fe66

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

.github/workflows/pre-commit.yml

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

.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

0 commit comments

Comments
 (0)