| 1 |
name: Guard Dependencies |
| 2 |
|
| 3 |
on: |
| 4 |
pull_request_target: # zizmor: ignore[dangerous-triggers] -- This workflow only reads context.payload metadata, never checks out PR code |
| 5 |
branches: [master] |
| 6 |
paths: |
| 7 |
- pyproject.toml |
| 8 |
- uv.lock |
| 9 |
|
| 10 |
permissions: |
| 11 |
contents: read |
| 12 |
issues: write |
| 13 |
pull-requests: write |
| 14 |
|
| 15 |
jobs: |
| 16 |
check-author: |
| 17 |
runs-on: ubuntu-latest |
| 18 |
timeout-minutes: 5 |
| 19 |
steps: |
| 20 |
- name: Check if author is org member or allowed bot |
| 21 |
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 22 |
with: |
| 23 |
script: | |
| 24 |
const pr = context.payload.pull_request; |
| 25 |
const author = pr.user.login; |
| 26 |
const assoc = pr.author_association; |
| 27 |
|
| 28 |
const botAllowlist = new Set(['dependabot[bot]']); |
| 29 |
const orgAuthorAssociations = new Set(['MEMBER', 'OWNER']); |
| 30 |
|
| 31 |
const allowed = |
| 32 |
botAllowlist.has(author) || |
| 33 |
(assoc != null && orgAuthorAssociations.has(assoc)); |
| 34 |
|
| 35 |
if (!allowed) { |
| 36 |
await github.rest.issues.createComment({ |
| 37 |
owner: context.repo.owner, |
| 38 |
repo: context.repo.repo, |
| 39 |
issue_number: context.payload.pull_request.number, |
| 40 |
body: `This PR modifies dependency files (\`pyproject.toml\` or \`uv.lock\`), which is restricted to members of the **${context.repo.owner}** organization on GitHub.\n\nIf you need a dependency change, please [open a discussion](https://github.com/${context.repo.owner}/${context.repo.repo}/discussions/new) describing what you need and why.\n\nClosing this PR automatically.` |
| 41 |
}); |
| 42 |
|
| 43 |
await github.rest.pulls.update({ |
| 44 |
owner: context.repo.owner, |
| 45 |
repo: context.repo.repo, |
| 46 |
pull_number: context.payload.pull_request.number, |
| 47 |
state: 'closed' |
| 48 |
}); |
| 49 |
|
| 50 |
core.setFailed('Dependency changes are restricted to organization members.'); |
| 51 |
} else { |
| 52 |
console.log(`Author ${author} (author_association=${assoc}) is allowed to make dependency changes.`); |
| 53 |
} |