Create check-pr-template.js

This commit is contained in:
William Harrison authored and GitHub committed 2026-08-19 11:48:15 +08:00
1 parent 83a51f6d43
commit bc17404f7c
1 file changed
+76
+76
View File
@@ -0,0 +1,76 @@
const body = process.env.PR_BODY || "";
const REQUIRED_CHECKBOXES = [
"I agree to the [Terms of Service]",
"My file is following the [domain structure]",
"My website is reachable and completed.",
"My website is software development related.",
"My website is not for commercial use.",
"I have provided sufficient contact information in the `owner` key.",
"I have provided a link to my website below.",
];
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function isRequiredCheckboxChecked(text) {
const regex = new RegExp(
`-\\s*\\[[xX]\\]\\s*(?:<!--[^>]*-->\\s*)?${escapeRegExp(text)}`,
"i",
);
return regex.test(body);
}
function getSection(startMarker, endMarker) {
const regex = new RegExp(
`${escapeRegExp(startMarker)}([\\s\\S]*?)${escapeRegExp(endMarker)}`,
"i",
);
const match = body.match(regex);
return match ? match[1].trim() : "";
}
const errors = [];
const uncheckedRequirements = REQUIRED_CHECKBOXES.filter(
(requirement) => !isRequiredCheckboxChecked(requirement),
);
if (uncheckedRequirements.length > 0) {
errors.push(
"The following required checkboxes have not been checked:",
...uncheckedRequirements.map((requirement) => `- ${requirement}`),
);
}
const websitePreview = getSection(
"<!-- WEBSITE_PREVIEW_START -->",
"<!-- WEBSITE_PREVIEW_END -->",
);
if (!websitePreview) {
errors.push("The **Website Preview** section has not been filled out.");
}
const websitePurpose = getSection(
"<!-- WEBSITE_PURPOSE_START -->",
"<!-- WEBSITE_PURPOSE_END -->",
);
if (!websitePurpose) {
errors.push("The **Website Purpose** section has not been filled out.");
}
if (errors.length > 0) {
console.error("PR template validation failed.");
console.error("");
console.error(errors.join("\n"));
process.exit(1);
}
console.log("PR template validation passed.");