Skip to content

Instantly share code, notes, and snippets.

@SaschaMann
Last active September 30, 2020 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaschaMann/a2893d0c005a2ce0739e144353937657 to your computer and use it in GitHub Desktop.
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.
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