@@ -2,16 +2,17 @@ name: pre-commit
22
33on :
44 pull_request :
5- permissions :
6- contents : read
5+ permissions : {}
76
87env :
9- # Forks and Dependabot don't have access to secrets
10- HAS_SECRETS : ${{ secrets.PRE_COMMIT != '' }}
8+ CAN_PUSH : ${{ github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' }}
119
1210jobs :
1311 pre-commit :
1412 runs-on : ubuntu-latest
13+ permissions :
14+ contents : read
15+ id-token : write
1516 timeout-minutes : 5
1617 steps :
1718 - name : Dump GitHub context
@@ -20,20 +21,18 @@ jobs:
2021 run : echo "$GITHUB_CONTEXT"
2122 - uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2223 name : Checkout PR for own repo
23- if : env.HAS_SECRETS == 'true'
24+ if : env.CAN_PUSH == 'true'
2425 with :
2526 # To be able to commit it needs to fetch the head of the branch, not the
2627 # merge commit
2728 ref : ${{ github.head_ref }}
2829 # And it needs the full history to be able to compute diffs
2930 fetch-depth : 0
30- # A token other than the default GITHUB_TOKEN is needed to be able to trigger CI
31- token : ${{ secrets.PRE_COMMIT }} # zizmor: ignore[secrets-outside-env]
32- persist-credentials : true # Required for `git push` command
31+ persist-credentials : false
3332 # pre-commit lite ci needs the default checkout configs to work
3433 - uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
3534 name : Checkout PR for fork
36- if : env.HAS_SECRETS == 'false'
35+ if : env.CAN_PUSH == 'false'
3736 with :
3837 # To be able to commit it needs the head branch of the PR, the remote one
3938 ref : ${{ github.event.pull_request.head.sha }}
@@ -64,11 +63,26 @@ jobs:
6463 id : precommit
6564 run : uv run prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure
6665 continue-on-error : true
66+ - name : Check for changes
67+ id : changes
68+ run : |
69+ if [[ -n "$(git status --porcelain)" ]]; then
70+ echo "changed=true" >> "$GITHUB_OUTPUT"
71+ else
72+ echo "changed=false" >> "$GITHUB_OUTPUT"
73+ fi
74+ - name : Get PR Push token
75+ id : pr-push
76+ if : env.CAN_PUSH == 'true' && steps.changes.outputs.changed == 'true'
77+ uses : tiangolo/pr-push@ff4e51a433de4c22bbf90597e069e8247b9203d2 # 0.0.1
6778 - name : Commit and push changes
68- if : env.HAS_SECRETS == 'true'
79+ if : env.CAN_PUSH == 'true' && steps.changes.outputs.changed == 'true'
80+ env :
81+ PR_PUSH_TOKEN : ${{ steps.pr-push.outputs.token }}
6982 run : |
7083 git config user.name "github-actions[bot]"
7184 git config user.email "github-actions[bot]@users.noreply.github.com"
85+ git remote set-url origin "https://x-access-token:${PR_PUSH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
7286 git add -A
7387 if git diff --staged --quiet; then
7488 echo "No changes to commit"
7791 git push
7892 fi
7993 - uses : pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0
80- if : env.HAS_SECRETS == 'false'
94+ if : env.CAN_PUSH == 'false'
8195 with :
8296 msg : 🎨 Auto format and update with pre-commit
8397 - name : Error out on pre-commit errors
0 commit comments