mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-10 10:12:45 +00:00
Remove guide comments workflow (#7830)
This commit is contained in:
18
.github/workflows/remove_guide_comments.yml
vendored
Normal file
18
.github/workflows/remove_guide_comments.yml
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
# Removes guide comments from PRs when opened, so that when we merge them
|
||||
# and reuse the pull request description, the clutter is not left behind
|
||||
name: Remove guide comments
|
||||
on:
|
||||
pull_request_target:
|
||||
types: [opened]
|
||||
jobs:
|
||||
remove_guide_comments:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Remove guide comments
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
script: |
|
||||
const { removeGuideComments } = await import('${{ github.workspace }}/tools/pull_request_hooks/removeGuideComments.js')
|
||||
await removeGuideComments({ github, context })
|
||||
41
tools/pull_request_hooks/removeGuideComments.js
Normal file
41
tools/pull_request_hooks/removeGuideComments.js
Normal file
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user