|
1 | 1 | name: Lint Check (pre-commit) |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
| 4 | + # We have to use pull_request_target here as pull_request does not grant |
| 5 | + # enough permission for reviewdog |
| 6 | + pull_request_target: |
5 | 7 | push: |
6 | 8 |
|
7 | 9 | jobs: |
8 | | - pre-commit: |
| 10 | + pre-commit-push: |
| 11 | + name: Pre-Commit check on Push |
9 | 12 | runs-on: ubuntu-latest |
10 | | - name: pre-commit |
| 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/[email protected] |
| 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 | + |
11 | 35 | permissions: |
12 | 36 | contents: read |
13 | 37 | checks: write |
14 | 38 | issues: write |
15 | 39 | pull-requests: write |
16 | 40 |
|
17 | 41 | steps: |
18 | | - - name: Checkout code |
| 42 | + - name: Checkout repository |
19 | 43 | uses: actions/checkout@v4 |
20 | 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 | + |
21 | 52 | - name: Set up Python |
22 | 53 | uses: actions/setup-python@v5 |
23 | 54 | with: |
24 | 55 | python-version: 3.13 |
25 | 56 |
|
| 57 | + # we only lint on the changed file in PR. |
26 | 58 | - name: Get Changed Files |
27 | 59 | id: changed-files |
28 | 60 | uses: tj-actions/changed-files@v45 |
29 | 61 |
|
30 | 62 | # See: |
31 | 63 | # https://github.com/tj-actions/changed-files?tab=readme-ov-file#using-local-git-directory- |
32 | 64 | - uses: pre-commit/[email protected] |
| 65 | + id: run-pre-commit |
33 | 66 | with: |
34 | 67 | extra_args: --files ${{ steps.changed-files.outputs.all_changed_files }} |
35 | | - continue-on-error: true |
36 | 68 |
|
| 69 | + # Review dog posts the suggested change from pre-commit to the pr. |
37 | 70 | - name: suggester / pre-commit |
38 | | - if: ${{ github.event_name == 'pull_request' }} |
39 | 71 | uses: reviewdog/action-suggester@v1 |
| 72 | + if: ${{ failure() && steps.run-pre-commit.conclusion == 'failure' }} |
40 | 73 | with: |
41 | 74 | tool_name: pre-commit |
42 | 75 | level: warning |
|
0 commit comments