-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (87 loc) · 2.92 KB
/
Copy pathpre-commit.yml
File metadata and controls
104 lines (87 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name: Prek Hooks
on:
pull_request:
branches:
- master
- main
push:
branches:
- master
- main
permissions:
contents: read
jobs:
prek:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout PR head (for auto-commit)
if: github.event_name == 'pull_request'
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Checkout repository
if: github.event_name != 'pull_request'
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: "uv.lock"
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: uv sync --group dev
- name: Run prek hooks (allow autofixes)
id: prek
shell: bash
run: |
set +e
uv run prek run --all-files
exit_code=$?
echo "exit_code=${exit_code}" >> "$GITHUB_OUTPUT"
if git diff --quiet; then
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
exit 0
- name: Commit and push prek autofixes
if: |
steps.prek.outputs.changed == 'true' &&
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.full_name == github.repository &&
github.actor != 'github-actions[bot]'
shell: bash
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "chore: apply prek autofixes"
git push
- name: Fail if prek changed files (can't auto-push)
if: |
steps.prek.outputs.changed == 'true' &&
(github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository)
shell: bash
run: |
echo "prek modified files but this workflow can't push to the branch (fork PR or missing permissions)."
echo "Please run: uv run prek run --all-files and commit the results."
git status --porcelain
exit 1
- name: Re-run prek hooks after auto-commit
if: |
steps.prek.outputs.changed == 'true' &&
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.full_name == github.repository &&
github.actor != 'github-actions[bot]'
run: uv run prek run --all-files
- name: Fail on non-fixable prek errors
if: steps.prek.outputs.changed == 'false' && steps.prek.outputs.exit_code != '0'
shell: bash
run: exit 1