mirror of
https://github.com/openchamber/openchamber.git
synced 2026-11-23 17:01:19 +08:00
fix(ci): strip HTML comments from PR bodies in one pass, skipping Markdown code (#7306)
Closes #7292
This commit is contained in:
1 parent
cb0ffd0e2f
commit
6e3ea9fbd0
2 files changed
+127
-5
No files matched your search
@@ -85,6 +85,69 @@ const isDocsFile = (file) => {
|
||||
const isTestFile = (file) =>
|
||||
/(^|\/)(__tests__|test|tests)\//.test(file) || /\.(test|spec)\.[^/]+$/.test(file) || /\.snap$/.test(file);
|
||||
|
||||
const FENCE_OPEN = /^ {0,3}(`{3,}(?=[^`\n]*$)|~{3,})/;
|
||||
|
||||
// The text of a PR description a reader sees: each HTML comment replaced by a
|
||||
// space, one pass from left to right, so removing one never joins its
|
||||
// neighbours into another comment, number or image. `<!--` inside Markdown
|
||||
// code (fenced block or code span) is visible text. An unclosed comment that
|
||||
// starts a line hides the rest of the description, as GitHub does; a mid-line
|
||||
// one is shown as text.
|
||||
const stripComments = (text) => {
|
||||
let out = "";
|
||||
let fence = null;
|
||||
let i = 0;
|
||||
while (i < text.length) {
|
||||
if (i === 0 || text[i - 1] === "\n") {
|
||||
const eol = text.indexOf("\n", i);
|
||||
const end = eol === -1 ? text.length : eol + 1;
|
||||
const line = text.slice(i, end).replace(/\r?\n$/, "");
|
||||
if (fence !== null) {
|
||||
if (new RegExp(`^ {0,3}${fence[0]}{${fence.length},}\\s*$`).test(line)) fence = null;
|
||||
out += text.slice(i, end);
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
const open = FENCE_OPEN.exec(line);
|
||||
if (open) {
|
||||
fence = open[1];
|
||||
out += text.slice(i, end);
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (text[i] === "\\") {
|
||||
out += text.slice(i, i + 2);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (text[i] === "`") {
|
||||
const run = /^`+/.exec(text.slice(i))[0];
|
||||
// A code span closes on the same number of backticks within its paragraph,
|
||||
// which a blank line or a line-start comment ends.
|
||||
const closer = new RegExp(`(?<!\`)${run}(?!\`)|\\n[ \\t]*\\n|\\n {0,3}<!--`, "g");
|
||||
closer.lastIndex = i + run.length;
|
||||
const match = closer.exec(text);
|
||||
const spanEnd = match && match[0][0] === "`" ? match.index + run.length : i + run.length;
|
||||
out += text.slice(i, spanEnd);
|
||||
i = spanEnd;
|
||||
continue;
|
||||
}
|
||||
if (text.startsWith("<!--", i)) {
|
||||
// `<!-->` and `<!--->` are complete, empty comments.
|
||||
const close = text.indexOf("-->", i + 2);
|
||||
if (close !== -1 || /^ {0,3}<!--/.test(text.slice(text.lastIndexOf("\n", i - 1) + 1))) {
|
||||
out += " ";
|
||||
i = close === -1 ? text.length : close + 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out += text[i];
|
||||
i += 1;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const readTrustedContributors = (workspace) => {
|
||||
try {
|
||||
return fs
|
||||
@@ -145,7 +208,7 @@ const linkedIssueNumbers = async ({ github, owner, repo, pr }) => {
|
||||
`\\bpart of\\s+(?:${owner}/${repo}#|#|https://github\\.com/${owner}/${repo}/issues/)(\\d+)`,
|
||||
"gi",
|
||||
);
|
||||
for (const match of (pr.body ?? "").matchAll(partOf)) numbers.add(Number(match[1]));
|
||||
for (const match of stripComments(pr.body ?? "").matchAll(partOf)) numbers.add(Number(match[1]));
|
||||
return [...numbers];
|
||||
};
|
||||
|
||||
@@ -229,7 +292,7 @@ const evaluateVisualEvidence = ({ pr, files }) => ({
|
||||
.filter(({ status }) => status !== "removed")
|
||||
.map(({ filename }) => filename)
|
||||
.filter(isEvidenceFile),
|
||||
missingScreenshots: !pr.draft && files.some(({ filename }) => isRenderedUiFile(filename)) && !DESCRIPTION_IMAGE.test(pr.body ?? ""),
|
||||
missingScreenshots: !pr.draft && files.some(({ filename }) => isRenderedUiFile(filename)) && !DESCRIPTION_IMAGE.test(stripComments(pr.body ?? "")),
|
||||
});
|
||||
|
||||
// The runtimes a change has to be run in, by the paths it touches: code that
|
||||
@@ -259,8 +322,6 @@ const RUNTIME_PATHS = [
|
||||
|
||||
const NOT_RUN = /\b(not\s+(yet\s+)?(run|ran|tested|verified|checked|exercised|applicable|available)\b|same\s+code\b|does\s+not\s+run\s+here|untested|unverified|n\/a|(could|can|did|was|were)\s?(not|n['’]t)\s+(be\s+)?(run|ran|test|tested|verify|verified|check|launch|build)|unable\s+to|no\s+access|no\s+(device|simulator|mac)\b)/i;
|
||||
|
||||
const stripComments = (text) => text.replace(/<!--[\s\S]*?-->/g, "");
|
||||
|
||||
const runtimeRow = (body, runtime) => {
|
||||
const escaped = runtime.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const match = new RegExp(`^\\|\\s*${escaped}\\s*\\|(.*)\\|\\s*$`, "im").exec(body);
|
||||
@@ -322,5 +383,6 @@ module.exports = {
|
||||
hasMarkerComment,
|
||||
isMaintainer,
|
||||
isWaived,
|
||||
linkedIssueNumbers,
|
||||
listPullFiles,
|
||||
};
|
||||
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
||||
import { createRequire } from 'node:module';
|
||||
import { test } from 'node:test';
|
||||
|
||||
const { evaluateRuntimeProof } = createRequire(import.meta.url)('../.github/scripts/pr-checks.cjs');
|
||||
const { evaluateRuntimeProof, evaluateVisualEvidence, linkedIssueNumbers } = createRequire(import.meta.url)('../.github/scripts/pr-checks.cjs');
|
||||
|
||||
const body = (rows, known) => `## Affected surfaces\n<!-- not run is banned -->\n| Runtime | What you ran and saw |\n|---|---|\n${Object.entries(rows).map(([k,v])=>`| ${k} | ${v} |`).join('\n')}\n\n## Known problems\n\n${known}\n\n<!-- x -->\n`;
|
||||
const f = (...names) => names.map((filename) => ({ filename, status: 'modified' }));
|
||||
@@ -47,3 +47,63 @@ test('a relay change is run through the relay from a desktop client and from a p
|
||||
assert.deepEqual(run(relay, { Relay: 'Relay-only pairing: desktop app and the iOS Simulator both sent a prompt and streamed the reply' }).unproven, []);
|
||||
assert.deepEqual(run(f('packages/web/server/lib/share-pages/relay-client.js'), {}).unproven, []);
|
||||
});
|
||||
|
||||
test('text GitHub hides inside an HTML comment never counts as the author\'s answer', () => {
|
||||
const electron = f('packages/electron/main.mjs');
|
||||
const evaluate = (text) => evaluateRuntimeProof({ pr: { body: text, draft: false }, files: electron });
|
||||
const row = '| Desktop (Electron) | Ran in Electron on macOS and saw the window open |';
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n${row}\n`).unproven, []);
|
||||
// An unclosed comment hides everything after it.
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n<!-- \n${row}\n`).unproven, ['Desktop (Electron)']);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n<!-- a --> <!-- \n${row}\n`).unproven, ['Desktop (Electron)']);
|
||||
assert.equal(run(f('packages/ui/src/x.ts'), {}, '<!-- - The card can reopen after eviction -->\nNone').knownProblems, false);
|
||||
});
|
||||
|
||||
test('a literal comment marker in visible text does not hide what follows', () => {
|
||||
const electron = f('packages/electron/main.mjs');
|
||||
const evaluate = (text) => evaluateRuntimeProof({ pr: { body: text, draft: false }, files: electron });
|
||||
const row = '| Desktop (Electron) | Ran in Electron on macOS and saw the window open |';
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\nThe parser now skips \`<!--\` markers.\n${row}\n`).unproven, []);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n\`\` a \`<!--\` b \`\`\n${row}\n`).unproven, []);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n\`\`\`html\n<!-- unclosed\n\`\`\`\n${row}\n`).unproven, []);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n~~~\n<!--\n~~~\n${row}\n`).unproven, []);
|
||||
// Removing the inner comment leaves `<!` and `--` that GitHub shows as text.
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n<!<!-- x -->-- \n${row}\n`).unproven, []);
|
||||
// A backtick that never closes is plain text, so the comment after it is real.
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\nit\`s <!-- x --> ok\n${row}\n`).unproven, []);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\nit\`s\n<!-- \n${row}\n`).unproven, ['Desktop (Electron)']);
|
||||
// A line-start comment ends the paragraph, so a backtick inside it closes no span.
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\nit\`s\n<!-- see \`x\n${row}\n-->\n`).unproven, ['Desktop (Electron)']);
|
||||
// An unclosed marker mid-sentence, in a quoted fence or in indented code is text.
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\nWrite <!-- to start one.\n${row}\n`).unproven, []);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n> ~~~html\n> <!-- unclosed\n> ~~~\n\n${row}\n`).unproven, []);
|
||||
assert.deepEqual(evaluate(`## Affected surfaces\n\n <!-- unclosed\n\n${row}\n`).unproven, []);
|
||||
assert.equal(run(f('packages/ui/src/x.ts'), {}, '`<!--` example\n\n- The card can reopen after eviction').knownProblems, true);
|
||||
assert.equal(run(f('packages/ui/src/x.ts'), {}, '```\n<!--\n```\n- The card can reopen after eviction').knownProblems, true);
|
||||
});
|
||||
|
||||
test('a screenshot inside an HTML comment is not a screenshot', () => {
|
||||
const ui = f('packages/ui/src/components/chat/X.tsx');
|
||||
const image = '';
|
||||
const missing = (body) => evaluateVisualEvidence({ pr: { body, draft: false }, files: ui }).missingScreenshots;
|
||||
assert.equal(missing(`## Screenshots\n${image}\n`), false);
|
||||
assert.equal(missing(`## Screenshots\n<!-- ${image} -->\n`), true);
|
||||
assert.equal(missing(`## Screenshots\n<!-- drop images here\n${image}\n`), true);
|
||||
// Removing a comment never makes an image out of the text around it.
|
||||
assert.equal(missing('## Screenshots\n!<!-- note -->[before](https://example.org/image.png)\n'), true);
|
||||
});
|
||||
|
||||
test('a Part of link inside an HTML comment links nothing', async () => {
|
||||
const github = {
|
||||
graphql: async () => ({
|
||||
repository: { pullRequest: { closingIssuesReferences: { nodes: [
|
||||
{ number: 5, repository: { nameWithOwner: 'openchamber/openchamber' } },
|
||||
{ number: 6, repository: { nameWithOwner: 'someone/else' } },
|
||||
] } } },
|
||||
}),
|
||||
};
|
||||
const linked = (body) => linkedIssueNumbers({ github, owner: 'openchamber', repo: 'openchamber', pr: { number: 1, body } });
|
||||
assert.deepEqual(await linked('Part of #7\n<!-- Part of #8 -->\n<!-- e.g. Part of #9'), [5, 7]);
|
||||
assert.deepEqual(await linked('Part of https://github.com/openchamber/openchamber/issues/10'), [5, 10]);
|
||||
assert.deepEqual(await linked('Part of #12<!-- note -->3'), [5, 12]);
|
||||
});
|
||||
Reference in new issue
Block a user