|
| 1 | +name: pre-commit |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - synchronize |
| 8 | + |
| 9 | +env: |
| 10 | + UV_SYSTEM_PYTHON: 1 |
| 11 | + IS_FORK: ${{ github.event.pull_request.head.repo.full_name != github.repository }} |
| 12 | + |
| 13 | +jobs: |
| 14 | + pre-commit: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Dump GitHub context |
| 18 | + env: |
| 19 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 20 | + run: echo "$GITHUB_CONTEXT" |
| 21 | + - uses: actions/checkout@v5 |
| 22 | + name: Checkout PR for own repo |
| 23 | + if: env.IS_FORK == 'false' |
| 24 | + with: |
| 25 | + # To be able to commit it needs more than the last commit |
| 26 | + ref: ${{ github.head_ref }} |
| 27 | + # A token other than the default GITHUB_TOKEN is needed to be able to trigger CI |
| 28 | + token: ${{ secrets.PRE_COMMIT }} |
| 29 | + # pre-commit lite ci needs the default checkout configs to work |
| 30 | + - uses: actions/checkout@v5 |
| 31 | + name: Checkout PR for fork |
| 32 | + if: env.IS_FORK == 'true' |
| 33 | + - name: Set up Python |
| 34 | + uses: actions/setup-python@v6 |
| 35 | + with: |
| 36 | + python-version: "3.14" |
| 37 | + - name: Setup uv |
| 38 | + uses: astral-sh/setup-uv@v7 |
| 39 | + with: |
| 40 | + cache-dependency-glob: | |
| 41 | + requirements**.txt |
| 42 | + pyproject.toml |
| 43 | + uv.lock |
| 44 | + - name: Run pre-commit |
| 45 | + id: precommit |
| 46 | + run: | |
| 47 | + # Fetch the base branch for comparison |
| 48 | + git fetch origin ${{ github.base_ref }} |
| 49 | + uvx pre-commit run --from-ref origin/${{ github.base_ref }} --to-ref HEAD --show-diff-on-failure |
| 50 | + continue-on-error: true |
| 51 | + - name: Commit and push changes |
| 52 | + if: env.IS_FORK == 'false' |
| 53 | + run: | |
| 54 | + git config user.name "github-actions[bot]" |
| 55 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 56 | + git add -A |
| 57 | + if git diff --staged --quiet; then |
| 58 | + echo "No changes to commit" |
| 59 | + else |
| 60 | + git commit -m "🎨 Auto format" |
| 61 | + git push |
| 62 | + fi |
| 63 | + - uses: pre-commit-ci/[email protected] |
| 64 | + if: env.IS_FORK == 'true' |
| 65 | + with: |
| 66 | + msg: 🎨 Auto format |
| 67 | + - name: Error out on pre-commit errors |
| 68 | + if: steps.precommit.outcome == 'failure' |
| 69 | + run: exit 1 |
| 70 | + |
| 71 | + # https://github.com/marketplace/actions/alls-green#why |
| 72 | + alls-green: # This job does nothing and is only used for the branch protection |
| 73 | + if: always() |
| 74 | + needs: |
| 75 | + - pre-commit |
| 76 | + runs-on: ubuntu-latest |
| 77 | + steps: |
| 78 | + - name: Dump GitHub context |
| 79 | + env: |
| 80 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 81 | + run: echo "$GITHUB_CONTEXT" |
| 82 | + - name: Decide whether the needed jobs succeeded or failed |
| 83 | + uses: re-actors/alls-green@release/v1 |
| 84 | + with: |
| 85 | + jobs: ${{ toJSON(needs) }} |
0 commit comments