From 2d4a2d4dce57a5524cd5a241b8847db484b40971 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Sat, 14 Nov 2026 19:33:23 -0600 Subject: [PATCH] Add a GitHub workflow to enforce issue assignment This approach was seen in use in pydantic and is copied here. The workflow is essentially unchanged, but some additional comments are added and the 'name' is converted to all-lowercase (to match our build workflow). Notably, in the original workflow and in this copy, PRs with no associated issues do not auto-close. The supposition is that spam PRs primarily target existing open issues, and we can always revise this in the future. Our thanks to the original author: Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com> --- .../workflows/enforce-issue-assignment.yml | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 .github/workflows/enforce-issue-assignment.yml diff --git a/.github/workflows/enforce-issue-assignment.yml b/.github/workflows/enforce-issue-assignment.yml new file mode 100644 index 00000000..78aaafed --- /dev/null +++ b/.github/workflows/enforce-issue-assignment.yml @@ -0,0 +1,87 @@ +name: enforce issue assignment + +on: + # zizmor: ignore[dangerous-triggers] -- pull_request_target is needed to close PRs + pull_request_target: + types: [opened, edited, reopened] + +permissions: + pull-requests: write + issues: read + +jobs: + # This job checks that PR submitters are assigned to the issues which their PR + # references and auto-closes the PR if they are not assigned. + # + # The check is skipped or passes if any of the following hold: + # + # - the user is an internal collaborator + # - the `skip-assignment-check` label is present + # - there are no referenced issues in the PR + # - the author is assigned *all* of the referenced issues + check-assignment: + if: > + github.event.pull_request.author_association != 'MEMBER' && + github.event.pull_request.author_association != 'OWNER' && + github.event.pull_request.author_association != 'COLLABORATOR' && + !contains(github.event.pull_request.labels.*.name, 'skip-assignment-check') + runs-on: ubuntu-latest + steps: + - name: Check referenced issues and close if unauthorized + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const pr = context.payload.pull_request; + const author = pr.user.login; + + const query = ` + query($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { + closingIssuesReferences(first: 20) { + nodes { + number + assignees(first: 20) { nodes { login } } + } + } + } + } + } + `; + const result = await github.graphql(query, { + owner: context.repo.owner, + repo: context.repo.repo, + number: pr.number, + }); + + const refs = result.repository.pullRequest.closingIssuesReferences.nodes; + if (refs.length === 0) { + core.info('No closing issue references; nothing to enforce.'); + return; + } + + const unauthorized = refs.filter( + issue => !issue.assignees.nodes.some(a => a.login === author) + ); + if (unauthorized.length === 0) { + core.info(`Author ${author} is assigned to all referenced issues.`); + return; + } + + const issueList = unauthorized.map(i => `#${i.number}`).join(', '); + const body = + `@${author} You do not have permission to open a PR without ` + + `being assigned to the referenced issue (${issueList}).`; + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pr.number, + body, + }); + await github.rest.pulls.update({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr.number, + state: 'closed', + });