Remove guide comments action (#70739)

Removes all default comments after a PR is opened (and only when it is opened).

Means that I can turn on the pr body merge option without keeping all the cruft
This commit is contained in:
Mothblocks
2022-10-30 01:46:48 -07:00
committed by GitHub
parent 79e1ef4302
commit acd034f967
2 changed files with 54 additions and 0 deletions
@@ -0,0 +1,36 @@
import fs from "fs";
const REGEX_COMMENT = /<!--.+?-->/g;
// Make sure we only remove default comments
const comments = [];
for (const match of fs
.readFileSync(".github/PULL_REQUEST_TEMPLATE.md", { encoding: "utf8" })
.matchAll(REGEX_COMMENT)) {
comments.push(match[0]);
}
function escapeRegex(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
}
export async function removeGuideComments({ github, context }) {
let newBody = context.payload.pull_request.body;
for (const comment of comments) {
newBody = newBody.replace(
new RegExp(`^\\s*${escapeRegex(comment)}\\s*`, "gm"),
"\n"
);
}
if (newBody !== context.payload.pull_request.body) {
await github.rest.pulls.update({
pull_number: context.payload.pull_request.number,
repo: context.repo.repo,
owner: context.repo.owner,
body: newBody,
});
}
}