Files
Bubberstation/tools/pull_request_hooks/removeGuideComments.js
san7890 cf59b0bba1 Null-checks PR Body in removeGuideComments (#71043)
This is what #71041 was supposed to be.

## About The Pull Request
Hey there,

I was looking at #71028, and I noticed that this workflow failed because
oranges left the PR body blank. I think that's silly, so let's just
include an early return so the whole thing doesn't throw.

This is the error:


![image](https://user-images.githubusercontent.com/34697715/199852746-24b2ed53-442d-4c40-b81d-902236183328.png)
via:
https://github.com/tgstation/tgstation/actions/runs/3382875608/jobs/5618241641

Here's an example PR where I fixed it (on my fork, just expand the
"Remove guide comments" step):
https://github.com/san7890/bruhstation/actions/runs/3389994395/jobs/5633658300

## Why It's Good For The Game

It hurts my heart to see checks red out in trivial situations like this.
## Changelog
Literally nothing here concerns players.

Ignore the commit history I was testing this PR the wrong way and was
effectively gaslighting myself (we're good now though)
2022-11-04 01:26:43 -07:00

42 lines
970 B
JavaScript

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