From b1f6ff5862cf7c717b71f387936bbb9d464d0426 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Fri, 4 Nov 2022 18:55:52 +0100 Subject: [PATCH] [MIRROR] Null-checks PR Body in removeGuideComments [MDB IGNORE] (#17361) Null-checks PR Body in removeGuideComments Co-authored-by: san7890 --- .../pull_request_hooks/removeGuideComments.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tools/pull_request_hooks/removeGuideComments.js diff --git a/tools/pull_request_hooks/removeGuideComments.js b/tools/pull_request_hooks/removeGuideComments.js new file mode 100644 index 00000000000..c7cdcd1ca67 --- /dev/null +++ b/tools/pull_request_hooks/removeGuideComments.js @@ -0,0 +1,41 @@ +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; + + if (!newBody) { + console.log("PR body is empty, skipping..."); + return; + } + + 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, + }); + } +}