55permissions : {}
66
77env :
8- # Forks and Dependabot don't have access to secrets
9- HAS_SECRETS : ${{ secrets.PRE_COMMIT != '' }}
8+ CAN_PUSH : ${{ github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' }}
109
1110jobs :
1211 pre-commit :
1312 runs-on : ubuntu-latest
13+ permissions :
14+ contents : read
15+ id-token : write
1416 timeout-minutes : 5
1517 steps :
1618 - name : Dump GitHub context
@@ -19,20 +21,18 @@ jobs:
1921 run : echo "$GITHUB_CONTEXT"
2022 - uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2123 name : Checkout PR for own repo
22- if : env.HAS_SECRETS == 'true'
24+ if : env.CAN_PUSH == 'true'
2325 with :
2426 # To be able to commit it needs to fetch the head of the branch, not the
2527 # merge commit
2628 ref : ${{ github.head_ref }}
2729 # And it needs the full history to be able to compute diffs
2830 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
31+ persist-credentials : false
3232 # pre-commit lite ci needs the default checkout configs to work
3333 - uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
3434 name : Checkout PR for fork
35- if : env.HAS_SECRETS == 'false'
35+ if : env.CAN_PUSH == 'false'
3636 with :
3737 # To be able to commit it needs the head branch of the PR, the remote one
3838 ref : ${{ github.event.pull_request.head.sha }}
@@ -57,11 +57,26 @@ jobs:
5757 id : precommit
5858 run : uv run prek run --from-ref origin/${GITHUB_BASE_REF} --to-ref HEAD --show-diff-on-failure
5959 continue-on-error : true
60+ - name : Check for changes
61+ id : changes
62+ run : |
63+ if [[ -n "$(git status --porcelain)" ]]; then
64+ echo "changed=true" >> "$GITHUB_OUTPUT"
65+ else
66+ echo "changed=false" >> "$GITHUB_OUTPUT"
67+ fi
68+ - name : Get PR Push token
69+ id : pr-push
70+ if : env.CAN_PUSH == 'true' && steps.changes.outputs.changed == 'true'
71+ uses : tiangolo/pr-push@ff4e51a433de4c22bbf90597e069e8247b9203d2 # 0.0.1
6072 - name : Commit and push changes
61- if : env.HAS_SECRETS == 'true'
73+ if : env.CAN_PUSH == 'true' && steps.changes.outputs.changed == 'true'
74+ env :
75+ PR_PUSH_TOKEN : ${{ steps.pr-push.outputs.token }}
6276 run : |
6377 git config user.name "github-actions[bot]"
6478 git config user.email "github-actions[bot]@users.noreply.github.com"
79+ git remote set-url origin "https://x-access-token:${PR_PUSH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
6580 git add -A
6681 if git diff --staged --quiet; then
6782 echo "No changes to commit"
7085 git push
7186 fi
7287 - uses : pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0
73- if : env.HAS_SECRETS == 'false'
88+ if : env.CAN_PUSH == 'false'
7489 with :
7590 msg : 🎨 Auto format
7691 - name : Error out on pre-commit errors
0 commit comments