1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-20 14:42:46 +00:00

ci: skip test for invalid PRs (#2013)

This commit is contained in:
Cotes Chung
2024-10-26 16:58:07 +08:00
committed by GitHub
parent 74ed06321c
commit c7f967529c
4 changed files with 36 additions and 22 deletions

View File

@@ -1,30 +1,25 @@
function noTypes(markdown) {
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) {
return false;
}
return true;
function hasTypes(markdown) {
return /## Type of change/.test(markdown) && /-\s*\[x\]/i.test(markdown);
}
function noDescription(markdown) {
function hasDescription(markdown) {
return (
/## Description/.test(markdown) === false ||
/## Description\s*\n\s*## \w+/.test(markdown) ||
/## Description\s*\n\s*$/.test(markdown)
/## Description/.test(markdown) &&
!/## Description\s*\n\s*(##|\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, '');
const action = context.payload.action;
if (body === '' || noTypes(markdown) || noDescription(markdown)) {
const isValid =
pr.labels.length > 0 || // PR create by Dependabot would have labels
(markdown !== '' && hasTypes(markdown) && hasDescription(markdown));
if (!isValid) {
await github.rest.pulls.update({
...context.repo,
pull_number: pr.number,
@@ -34,7 +29,9 @@ module.exports = async ({ github, context }) => {
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."
body: `Oops, it seems you've ${action} an invalid pull request. No worries, we'll close it for you.`
});
}
return isValid;
};