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