name: Close unrequested pull requests on: pull_request_target: types: [opened, reopened] permissions: # push-level access needed by the collaborator-permission API # otherwise `author_association` is unreliable for private org members. contents: write issues: write pull-requests: write jobs: close: runs-on: ubuntu-24.04 steps: - name: Close pull requests from unapproved contributors uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | const { owner, repo } = context.repo; const pull_number = context.payload.pull_request.number; const issue_number = pull_number; const allowedAssociations = new Set(['OWNER', 'MEMBER', 'COLLABORATOR']); const allowedBots = new Set(['agent-think[bot]', 'dependabot[bot]', 'github-actions[bot]', 'renovate[bot]']); const allowedLabels = new Set(['allow-pr']); const allowedPermissions = new Set(['admin', 'maintain', 'write']); const { data: pullRequest } = await github.rest.pulls.get({ owner, repo, pull_number, }); if (allowedAssociations.has(pullRequest.author_association)) { return; } if (allowedBots.has(pullRequest.user.login)) { return; } const labels = (pullRequest.labels || []).map((label) => label.name); if (labels.some((label) => allowedLabels.has(label))) { return; } // author_association reads CONTRIBUTOR for private org members who get // access through a team, so also check their actual repository permission. try { const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({ owner, repo, username: pullRequest.user.login, }); if (allowedPermissions.has(permission.permission)) { return; } } catch (error) { core.warning(`Could not read repository permission for ${pullRequest.user.login}: ${error.message}`); } const body = [ 'Thanks for your interest in Cloudflare Computer.', '', 'This repository does not accept unsolicited pull requests. Please use one of the accepted contribution paths instead:', '', '- Bug reports, regressions, and fix proposals: https://github.com/cloudflare/computer/issues/new/choose', '- Feature requests, enhancements, and design proposals: https://github.com/cloudflare/computer/discussions', '', 'If a maintainer asked you to open this pull request, they can add the `allow-pr` label and reopen it.' ].join('\n'); await github.rest.issues.createComment({ owner, repo, issue_number, body }); await github.rest.pulls.update({ owner, repo, pull_number, state: 'closed' });