-
Notifications
You must be signed in to change notification settings - Fork 0
101 lines (92 loc) · 3.14 KB
/
pre-commit.yaml
File metadata and controls
101 lines (92 loc) · 3.14 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
name: pre-commit
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
on:
push:
branches: [dev, main]
pull_request:
branches: [dev, main]
workflow_call:
inputs:
working-directory:
type: string
required: false
default: '.'
jobs:
pre-commit:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref || github.ref_name }}
- name: Set up Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.10'
cache: 'pip'
- uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: >
${{ format('pre-commit-{0}-{1}',
steps.setup-python.outputs.python-version,
hashFiles('.pre-commit-config.yaml')
) }}
- name: Install pre-commit
run: |
pip install --upgrade pip
pip install pre-commit
pre-commit install
- name: Run pre-commit hooks
continue-on-error: true
working-directory: ${{ inputs.working-directory }}
run: |
git fetch --no-tags --depth=1 origin "${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}"
if [ "${{ github.event_name }}" = "pull_request" ]; then
pre-commit run \
--from-ref "origin/${{ github.base_ref }}" \
--to-ref "HEAD" \
--show-diff-on-failure \
--color=always
else
pre-commit run \
--from-ref "HEAD~1" \
--to-ref "HEAD" \
--show-diff-on-failure \
--color=always
fi
- name: Commit if changes
# always() bypasses the error code from pre-commit.
# don't run again if this review has been triggered by github-actions[bot],
# since this would create an action loop.
if: github.event_name == 'pull_request' && github.actor != 'github-actions[bot]'
# Normally github message would include [skip ci] to prevent loops, but since this is the pre-commit fixer, it should be stable.
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
if ! git diff --cached --quiet; then
git commit -m "chore: auto-format code [skip ci]"
git push
fi
- name: Re-run pre-commit hooks
working-directory: ${{ inputs.working-directory }}
run: |
git fetch --no-tags --depth=1 origin "${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}"
if [ "${{ github.event_name }}" = "pull_request" ]; then
pre-commit run \
--from-ref "origin/${{ github.base_ref }}" \
--to-ref "HEAD" \
--show-diff-on-failure \
--color=always
else
pre-commit run \
--from-ref "HEAD~1" \
--to-ref "HEAD" \
--show-diff-on-failure \
--color=always
fi