Skip to content

Commit d5b8c7c

Browse files
committed
🔒 Replace pre-commit PAT with PR Push
1 parent d506ea4 commit d5b8c7c

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎.github/pr-push.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
workflows:
2+
- .github/workflows/pre-commit.yml

‎.github/workflows/pre-commit.yml‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ name: pre-commit
22

33
on:
44
pull_request:
5-
permissions:
6-
contents: read
5+
permissions: {}
76

87
env:
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

1210
jobs:
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"
@@ -77,7 +91,7 @@ jobs:
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

Comments
 (0)