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