ci: block invalid pull requests (#2010)

This commit is contained in:
Cotes Chung 2024-10-25 19:48:11 +08:00 committed by GitHub
parent d4f7f39ece
commit 74ed06321c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 0 deletions

22
.github/workflows/pr-filter.yml vendored Normal file
View file

@ -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 });

40
.github/workflows/scripts/pr-filter.js vendored Normal file
View file

@ -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."
});
}
};