| 1 |
name: pre-commit |
| 2 |
|
| 3 |
on: |
| 4 |
pull_request: |
| 5 |
permissions: {} |
| 6 |
|
| 7 |
env: |
| 8 |
# Forks and Dependabot don't have access to secrets |
| 9 |
HAS_SECRETS: ${{ secrets.PRE_COMMIT != '' }} |
| 10 |
|
| 11 |
jobs: |
| 12 |
pre-commit: |
| 13 |
runs-on: ubuntu-latest |
| 14 |
timeout-minutes: 5 |
| 15 |
steps: |
| 16 |
- name: Dump GitHub context |
| 17 |
env: |
| 18 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 19 |
run: echo "$GITHUB_CONTEXT" |
| 20 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 21 |
name: Checkout PR for own repo |
| 22 |
if: env.HAS_SECRETS == 'true' |
| 23 |
with: |
| 24 |
# To be able to commit it needs to fetch the head of the branch, not the |
| 25 |
# merge commit |
| 26 |
ref: ${{ github.head_ref }} |
| 27 |
# And it needs the full history to be able to compute diffs |
| 28 |
fetch-depth: 0 |
| 29 |
# A token other than the default GITHUB_TOKEN is needed to be able to trigger CI |
| 30 |
token: ${{ secrets.PRE_COMMIT }} # zizmor: ignore[secrets-outside-env] |
| 31 |
persist-credentials: true # Required for `git push` command |
| 32 |
# pre-commit lite ci needs the default checkout configs to work |
| 33 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 34 |
name: Checkout PR for fork |
| 35 |
if: env.HAS_SECRETS == 'false' |
| 36 |
with: |
| 37 |
# To be able to commit it needs the head branch of the PR, the remote one |
| 38 |
ref: ${{ github.event.pull_request.head.sha }} |
| 39 |
fetch-depth: 0 |
| 40 |
persist-credentials: false |
| 41 |
- name: Set up Python |
| 42 |
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 43 |
with: |
| 44 |
python-version-file: ".python-version" |
| 45 |
- name: Setup uv |
| 46 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 47 |
with: |
| 48 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 49 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 50 |
version: "0.11.18" |
| 51 |
cache-dependency-glob: | |
| 52 |
pyproject.toml |
| 53 |
uv.lock |
| 54 |
- name: Install Dependencies |
| 55 |
run: uv sync --locked --extra all |
| 56 |
- name: Run prek - pre-commit |
| 57 |
id: precommit |
| 58 |
run: uv run prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure |
| 59 |
continue-on-error: true |
| 60 |
- name: Commit and push changes |
| 61 |
if: env.HAS_SECRETS == 'true' |
| 62 |
run: | |
| 63 |
git config user.name "github-actions[bot]" |
| 64 |
git config user.email "github-actions[bot]@users.noreply.github.com" |
| 65 |
git add -A |
| 66 |
if git diff --staged --quiet; then |
| 67 |
echo "No changes to commit" |
| 68 |
else |
| 69 |
git commit -m "🎨 Auto format" |
| 70 |
git push |
| 71 |
fi |
| 72 |
- uses: pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0 |
| 73 |
if: env.HAS_SECRETS == 'false' |
| 74 |
with: |
| 75 |
msg: 🎨 Auto format |
| 76 |
- name: Error out on pre-commit errors |
| 77 |
if: steps.precommit.outcome == 'failure' |
| 78 |
run: exit 1 |
| 79 |
|
| 80 |
# https://github.com/marketplace/actions/alls-green#why |
| 81 |
pre-commit-alls-green: # This job does nothing and is only used for the branch protection |
| 82 |
if: always() |
| 83 |
needs: |
| 84 |
- pre-commit |
| 85 |
runs-on: ubuntu-latest |
| 86 |
timeout-minutes: 5 |
| 87 |
steps: |
| 88 |
- name: Dump GitHub context |
| 89 |
env: |
| 90 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 91 |
run: echo "$GITHUB_CONTEXT" |
| 92 |
- name: Decide whether the needed jobs succeeded or failed |
| 93 |
uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # v1.2.2 |
| 94 |
with: |
| 95 |
jobs: ${{ toJSON(needs) }} |