Check for Spammy PRs #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Check for Spammy PRs | |
| # **What it does**: This action closes low value pull requests in the open-source repository. | |
| # **Why we have it**: We get lots of spam in the open-source repository. | |
| # **Who does it impact**: Open-source contributors. | |
| on: | |
| pull_request_target: | |
| types: [opened] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| jobs: | |
| spammy-pr-check: | |
| name: Label PRs that only delete files or touch a large number of files | |
| if: github.repository == 'github/docs' && github.event_name == 'pull_request_target' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 | |
| with: | |
| github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| script: | | |
| const owner = 'github' | |
| const repo = 'docs' | |
| const pull_number = context.payload.pull_request.number | |
| const { data: files } = await github.rest.pulls.listFiles({ | |
| owner: owner, | |
| repo: repo, | |
| pull_number: pull_number, | |
| }); | |
| const onlyDeletes = files.length > 0 && files.every(f => f.status === 'removed') | |
| const touchesTooMany = files.length > 10 | |
| // Close the PR and add the invalid label | |
| if (onlyDeletes || touchesTooMany) { | |
| await github.rest.issues.update({ | |
| owner: owner, | |
| repo: repo, | |
| issue_number: pull_number, | |
| labels: ['invalid'], | |
| }); | |
| // Comment on the PR | |
| await github.rest.issues.createComment({ | |
| owner: owner, | |
| repo: repo, | |
| issue_number: pull_number, | |
| body: `This pull request may have been opened accidentally. I'm going to close it now, but feel free to check out our [contribution guidelines](https://docs.github.com/en/contributing), or raise a new issue.` | |
| }); | |
| } |