forked from cotes2020/jekyll-theme-chirpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: block invalid pull requests (cotes2020#2010)
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Block Invalid PR | ||
|
||
on: | ||
pull_request_target: | ||
types: [opened, reopened, edited] | ||
|
||
jobs: | ||
check-template: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
pull-requests: write | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Check PR Content | ||
uses: actions/github-script@v7 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const script = require('.github/workflows/scripts/pr-filter.js'); | ||
await script({ github, context }); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function noTypes(markdown) { | ||
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function noDescription(markdown) { | ||
return ( | ||
/## Description/.test(markdown) === false || | ||
/## Description\s*\n\s*## \w+/.test(markdown) || | ||
/## Description\s*\n\s*$/.test(markdown) | ||
); | ||
} | ||
|
||
module.exports = async ({ github, context }) => { | ||
const pr = context.payload.pull_request; | ||
|
||
if (pr.labels.length > 0) { | ||
// Skip if the PR is already labeled (typically created by a deps-bot.) | ||
return; | ||
} | ||
|
||
const body = pr.body === null ? '' : pr.body.trim(); | ||
const markdown = body.replace(/<!--[\s\S]*?-->/g, ''); | ||
|
||
if (body === '' || noTypes(markdown) || noDescription(markdown)) { | ||
await github.rest.pulls.update({ | ||
...context.repo, | ||
pull_number: pr.number, | ||
state: 'closed' | ||
}); | ||
|
||
await github.rest.issues.createComment({ | ||
...context.repo, | ||
issue_number: pr.number, | ||
body: "Oops, it seems you've submitted an invalid pull request. No worries, we'll close it for you." | ||
}); | ||
} | ||
}; |