Last active
September 30, 2020 23:39
-
-
Save SaschaMann/a2893d0c005a2ce0739e144353937657 to your computer and use it in GitHub Desktop.
An action that closes all PRs by new contributors and marks them as invalid.
This file contains 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: Stop Hacktoberfest Spam | |
on: | |
pull_request_target: | |
jobs: | |
stop-spam: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check if it's a new contributor | |
# Based on https://github.com/actions/github-script#welcome-a-first-time-contributor | |
id: new-contributor | |
uses: actions/github-script@626af12fe9a53dc2972b48385e7fe7dec79145c9 # 3.0.0 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
result-encoding: string | |
script: | | |
const creator = context.payload.sender.login | |
const opts = github.issues.listForRepo.endpoint.merge({ | |
...context.issue, | |
creator, | |
state: 'all' | |
}) | |
const issues = await github.paginate(opts) | |
for (const issue of issues) { | |
if (issue.number === context.issue.number) { | |
continue | |
} | |
if (issue.pull_request) { | |
return false // Creator is already a contributor. | |
} | |
} | |
return true | |
- name: Post friendly comment | |
if: steps.new-contributor.outputs.result == 'true' | |
uses: actions/github-script@626af12fe9a53dc2972b48385e7fe7dec79145c9 # 3.0.0 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: 'ADD A MESSAGE HERE THAT EXPLAINS WHY THE PR WILL BE AUTOCLOSED.' | |
}) | |
- name: Label as invalid | |
if: steps.new-contributor.outputs.result == 'true' | |
uses: actions/github-script@626af12fe9a53dc2972b48385e7fe7dec79145c9 # 3.0.0 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.issues.addLabels({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
labels: ['invalid'] | |
}) | |
- name: Close PR | |
if: steps.new-contributor.outputs.result == 'true' | |
uses: actions/github-script@626af12fe9a53dc2972b48385e7fe7dec79145c9 # 3.0.0 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.pulls.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.issue.number, | |
state: 'closed' | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment