Skip to content

Commit 79c42cc

Browse files
committed
Added a pre-commit GitHub Action.
1 parent e92d55e commit 79c42cc

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

.github/workflows/pre-commit.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Lint Check (pre-commit)
2+
3+
on:
4+
# We have to use pull_request_target here as pull_request does not grant
5+
# enough permission for reviewdog
6+
pull_request_target:
7+
push: [precommit]
8+
9+
jobs:
10+
pre-commit-push:
11+
name: Pre-Commit check on Push
12+
runs-on: ubuntu-latest
13+
if: ${{ github.event_name == 'push' }}
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: 3.13
23+
24+
# We wish to run pre-commit on all files instead of the changes
25+
# only made in the push commit.
26+
#
27+
# So linting error persists when there's formatting problem.
28+
- uses: pre-commit/action@v3.0.1
29+
30+
pre-commit-pr:
31+
name: Pre-Commit check on PR
32+
runs-on: ubuntu-latest
33+
if: ${{ github.event_name == 'pull_request_target' }}
34+
35+
permissions:
36+
contents: read
37+
checks: write
38+
issues: write
39+
pull-requests: write
40+
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@v4
44+
45+
# pull_request_target checkout the base of the repo
46+
# We need to checkout the actual pr to lint the changes.
47+
- name: Checkout pr
48+
run: gh pr checkout ${{ github.event.number }}
49+
env:
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
52+
- name: Set up Python
53+
uses: actions/setup-python@v5
54+
with:
55+
python-version: 3.13
56+
57+
# we only lint on the changed file in PR.
58+
- name: Get Changed Files
59+
id: changed-files
60+
uses: tj-actions/changed-files@v45
61+
62+
# See:
63+
# https://github.com/tj-actions/changed-files?tab=readme-ov-file#using-local-git-directory-
64+
- uses: pre-commit/action@v3.0.1
65+
id: run-pre-commit
66+
with:
67+
extra_args: --files ${{ steps.changed-files.outputs.all_changed_files }}
68+
69+
# Review dog posts the suggested change from pre-commit to the pr.
70+
- name: suggester / pre-commit
71+
uses: reviewdog/action-suggester@v1
72+
if: ${{ failure() && steps.run-pre-commit.conclusion == 'failure' }}
73+
with:
74+
tool_name: pre-commit
75+
level: warning
76+
reviewdog_flags: "-fail-level=error"

0 commit comments

Comments
 (0)