-
Notifications
You must be signed in to change notification settings - Fork 2
75 lines (61 loc) · 2.7 KB
/
pre-commit.yml
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
# GitHub action to check if pre-commit has been run. Runs from .pre-commit-config.yaml, where the pre-commit actions are.
name: run-pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
permissions:
pull-requests: write
if: ${{ github.actor != 'dependabot[bot]' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ secrets.PAT }}
- uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Install pre-commit
run: pip install pre-commit
- name: Run pre-commit
id: pre_commit
continue-on-error: true
run: |
if pre-commit run --color always --all-files; then
echo "Pre-commit check passed"
echo "pre_commit_failed=0" >> $GITHUB_OUTPUT
else
echo "Pre-commit check failed"
echo "pre_commit_failed=1" >> $GITHUB_OUTPUT
exit 1
fi
# Have this step before commit in case the PR is from a fork. In this case, we want the
# add-pr-comment to fail, because it makes it means that the contributer is directed here,
# and are given the informative error message, instead of directed to a "could not commit error message".
- uses: mshick/add-pr-comment@v2
if: ${{ steps.pre_commit.outputs.pre_commit_failed == 1 && github.event_name == 'pull_request' }}
id: add_comment
with:
message: |
Looks like some formatting rules failed.
✨ The action has attempted automatic fixes ✨
If any were succesful, they were committed to the branch.
We suggest using `git pull --rebase` to apply them locally.
If some errors could not be fixed automatically, you can:
🏎️ Get results locally by running `pre-commit run --all-files`
🕵️ Examine the results in the `Run pre-commit` section of this workflow `pre-commit`
We also strongly recommend setting up the `ruff` and `black` extensions to auto-format on save in your chosen editor.
- name: Commit formatting
if: ${{ steps.pre_commit.outputs.pre_commit_failed == 1 && github.event_name == 'pull_request' }}
run: |
git config user.name github-actions
git config user.email [email protected]
git commit -am "style: linting"
git push --no-verify
- name: Fail workflow
if: ${{ steps.pre_commit.outputs.pre_commit_failed == 1 && github.event_name == 'pull_request' }}
run: exit 1