Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Fix the CodeQL alerts, withhold approval-sounding notes, add a maintainer dry run

Nguyen Xuan An6e1e2238b772
diff --git a/.github/workflows/claude-triage.yml b/.github/workflows/claude-triage.yml
index dead68f7..00c2f613 100644
--- a/.github/workflows/claude-triage.yml
+++ b/.github/workflows/claude-triage.yml
@@ -18,6 +18,10 @@ name: Claude triage
# The action then scrubs secrets from the environment of Claude's subprocesses (kept on below).
# Cost and abuse: one run per item (opened or ready for review only), at most 3 items per author and 40 runs per day, the
# gate runs again before anything is posted, and the repository variable TRIAGE_ENABLED=false pauses it.
+# Dry run: a maintainer can start this workflow by hand (Actions tab, Run workflow) with the number of an issue or pull
+# request. It runs the same gate and the same Claude step, then prints the comment and labels it would post to the job
+# log and the step summary, and posts nothing and adds no label. Only people with write access can start a
+# workflow_dispatch run, so this is not a new way in for a stranger. docs/maintainers.md says how to use it.
# Setup: the repository secret CLAUDE_CODE_OAUTH_TOKEN, created with `claude setup-token`. Without it the job only logs
# that and exits.
@@ -26,9 +30,15 @@ on:
types: [opened]
pull_request_target:
types: [opened, ready_for_review]
+ workflow_dispatch:
+ inputs:
+ number:
+ description: Issue or pull request number to try the first pass on (posts nothing)
+ required: true
+ type: string
concurrency:
- group: claude-triage-${{ github.event.pull_request.number || github.event.issue.number }}
+ group: claude-triage-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.number }}
cancel-in-progress: true
jobs:
@@ -73,6 +83,7 @@ jobs:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPOSITORY: ${{ github.repository }}
EVENT_NAME: ${{ github.event_name }}
+ INPUT_NUMBER: ${{ github.event.inputs.number }}
DIFF_LIMIT_BYTES: '61440'
BODY_LIMIT_BYTES: '16384'
LINKED_LIMIT_BYTES: '8192'
@@ -81,14 +92,24 @@ jobs:
mkdir -p pr-data
scratch="$RUNNER_TEMP/triage-scratch"
mkdir -p "$scratch"
- number=$(jq -r '(.pull_request // .issue).number' "$GITHUB_EVENT_PATH")
- jq -r '(.pull_request // .issue).title' "$GITHUB_EVENT_PATH" > pr-data/title.txt
- jq -r '(.pull_request // .issue).body // ""' "$GITHUB_EVENT_PATH" > "$scratch/body.md"
+ # A dry run has only a number, so the item is read from the API into the same shape as an event payload.
+ event_file="$GITHUB_EVENT_PATH"
+ if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
+ if ! printf '%s' "$INPUT_NUMBER" | grep -qE '^[1-9][0-9]{0,8}$'; then
+ echo "The input number must be an issue or pull request number such as 123."
+ exit 1
+ fi
+ event_file="$scratch/event.json"
+ gh api "repos/$REPOSITORY/issues/$INPUT_NUMBER" | jq '{issue: .}' > "$event_file"
+ fi
+ number=$(jq -r '(.pull_request // .issue).number' "$event_file")
+ jq -r '(.pull_request // .issue).title' "$event_file" > pr-data/title.txt
+ jq -r '(.pull_request // .issue).body // ""' "$event_file" > "$scratch/body.md"
head -c "$BODY_LIMIT_BYTES" "$scratch/body.md" > pr-data/body.md
- jq -r '(.pull_request // .issue).user.login' "$GITHUB_EVENT_PATH" > pr-data/author.txt
- jq -r '[(.pull_request // .issue).labels[].name] | join(", ")' "$GITHUB_EVENT_PATH" > pr-data/labels.txt
+ jq -r '(.pull_request // .issue).user.login' "$event_file" > pr-data/author.txt
+ jq -r '[(.pull_request // .issue).labels[].name] | join(", ")' "$event_file" > pr-data/labels.txt
- if [ "$EVENT_NAME" = "pull_request_target" ]; then
+ if jq -e '(.pull_request != null) or (.issue.pull_request != null)' "$event_file" > /dev/null; then
echo "pull request" > pr-data/kind.txt
gh api --paginate "repos/$REPOSITORY/pulls/$number/files?per_page=100" \
--jq '.[] | [.filename, .status, .additions, .deletions] | @tsv' > "$scratch/files.tsv"
diff --git a/docs/maintainers.md b/docs/maintainers.md
index f7a29005..eff1fb69 100644
--- a/docs/maintainers.md
+++ b/docs/maintainers.md
@@ -41,6 +41,14 @@ Without the secret the job logs one line and does nothing. The workflow takes no
Each run uses the subscription behind that token, with the Sonnet model and at most 6 turns, so a run is small but not free. The body is cut at 16 KB, the diff at 60 KB and a linked issue at 8 KB. The limits above cap a day's spend.
+### Trying it without posting
+
+To see what the first pass would say about an item, open the Actions tab, pick "Claude triage", choose "Run workflow" and enter the issue or pull request number. The run applies the same gate and the same Claude step, then prints the comment and the labels it would add to the job log and the step summary. It posts no comment and adds no label. Only people with write access can start it. The gate still skips maintainer-authored items, drafts and items a maintainer has touched; the reason is in the "Decide whether the first pass applies" step. An earlier bot comment does not stop a dry run.
+
+### Approval wording
+
+The comment is quoted under a fixed header that says it is not a review, an approval or a security clearance. If Claude's text uses a word or phrase such as "safe", "secure", "approved", "LGTM", "ready to merge", "good to merge", "looks good", "no security issues" or "verified" (`APPROVAL_PHRASES` in `scripts/triage-post-comment.mjs`), the text is withheld and the comment only says that a maintainer will review the item.
+
### Turning it off
Disable the "Claude triage" workflow in the Actions tab, delete the secret, or delete the workflow file. Any of these stops it at once.
diff --git a/scripts/triage-apply-labels.mjs b/scripts/triage-apply-labels.mjs
index 115f77ae..a59ac84a 100644
--- a/scripts/triage-apply-labels.mjs
+++ b/scripts/triage-apply-labels.mjs
@@ -1,9 +1,10 @@
// Applies the labels Claude suggested during the automated first pass, and only those on a fixed allowlist.
// This step has no Claude secret and can add labels only: it never removes one, closes anything or edits text.
// No label here acts as a stop signal or a gate (hold:*, status:*), so a hijacked Claude cannot park an item.
-import { readFileSync } from 'node:fs';
+import { appendFileSync, readFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
+import { isDryRun, loadTriageItem } from './triage-load-item.mjs';
export const LABEL_ALLOWLIST = Object.freeze([
'needs-triage',
@@ -32,7 +33,7 @@ export function filterLabels(suggested, existing = []) {
function main() {
const payload = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'));
- const item = payload.pull_request ?? payload.issue;
+ const { number, item } = loadTriageItem(payload, process.env.GITHUB_REPOSITORY);
let suggested = [];
try {
suggested = JSON.parse(process.env.TRIAGE_OUTPUT).labels;
@@ -45,8 +46,14 @@ function main() {
console.log('no labels to add');
return;
}
+ if (isDryRun()) {
+ const line = `Dry run: would add labels: ${toAdd.join(', ')}. Nothing was added.`;
+ console.log(line);
+ if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY, `\n${line}\n`);
+ return;
+ }
const body = JSON.stringify({ labels: toAdd });
- execFileSync('gh', ['api', '--method', 'POST', `repos/${process.env.GITHUB_REPOSITORY}/issues/${item.number}/labels`, '--input', '-'], { input: body });
+ execFileSync('gh', ['api', '--method', 'POST', `repos/${process.env.GITHUB_REPOSITORY}/issues/${number}/labels`, '--input', '-'], { input: body });
console.log(`added labels: ${toAdd.join(', ')}`);
}
diff --git a/scripts/triage-load-item.mjs b/scripts/triage-load-item.mjs
new file mode 100644
index 00000000..0243dd7c
--- /dev/null
+++ b/scripts/triage-load-item.mjs
@@ -0,0 +1,33 @@
+// Finds the issue or pull request a triage run is about. On a normal event it is in the payload. On a maintainer's
+// manual dry run (workflow_dispatch) only a number is given, so it is read from the API.
+import { execFileSync } from 'node:child_process';
+
+export function isDryRun(eventName = process.env.GITHUB_EVENT_NAME) {
+ return eventName === 'workflow_dispatch';
+}
+
+export function parseDispatchNumber(value) {
+ const text = String(value ?? '').trim();
+ if (!/^[1-9][0-9]{0,8}$/.test(text)) {
+ throw new Error(`The dispatch input "number" must be an issue or pull request number such as 123, got "${text}".`);
+ }
+ return Number(text);
+}
+
+function callApi(args) {
+ return JSON.parse(execFileSync('gh', ['api', ...args], { encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 }));
+}
+
+// Returns { number, item, isPullRequest }. item is the pulls API object for a pull request, else the issues object.
+export function loadTriageItem(payload, repository, fetchJson = callApi) {
+ if (payload.inputs && !payload.pull_request && !payload.issue) {
+ const number = parseDispatchNumber(payload.inputs.number);
+ const issue = fetchJson([`repos/${repository}/issues/${number}`]);
+ const isPullRequest = Boolean(issue.pull_request);
+ const item = isPullRequest ? fetchJson([`repos/${repository}/pulls/${number}`]) : issue;
+ return { number, item, isPullRequest };
+ }
+ const number = (payload.pull_request ?? payload.issue).number;
+ const item = payload.pull_request ? fetchJson([`repos/${repository}/pulls/${number}`]) : fetchJson([`repos/${repository}/issues/${number}`]);
+ return { number, item, isPullRequest: Boolean(payload.pull_request) };
+}
diff --git a/scripts/triage-post-comment.mjs b/scripts/triage-post-comment.mjs
index ef1ed673..f5bac792 100644
--- a/scripts/triage-post-comment.mjs
+++ b/scripts/triage-post-comment.mjs
@@ -1,15 +1,17 @@
// Posts Claude's first-pass note as one sticky comment. Claude has no write tool: it returns text, and this step,
// which holds no Claude secret in its prompt path, cleans the text and posts or updates the single comment carrying a
// hidden marker.
-import { readFileSync } from 'node:fs';
+import { appendFileSync, readFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
+import { isDryRun, loadTriageItem } from './triage-load-item.mjs';
export const COMMENT_MARKER = '<!-- claude-triage -->';
export const BOT_LOGIN = 'github-actions[bot]';
export const MAX_COMMENT_LENGTH = 6000;
const HEADER = '**Automated first pass. This is not a review, an approval or a security clearance.**';
const FOOTER = '_Automated first pass by Claude. It can be wrong. A maintainer will pick this up, and this note stops updating then._';
+const REVIEW_NOTE = 'A maintainer will review this item.';
const WITHHELD_NOTE = 'The automated note was withheld because it contained text that looked like a credential. A maintainer will look at this item.';
const SECRET_PATTERNS = [
@@ -23,6 +25,40 @@ const SECRET_PATTERNS = [
const ALLOWED_URL = /^https:\/\/(github\.com\/codepawl\/67\.js|67js\.codepawl\.com)([/?#][^\s)]*)?$/i;
const MAX_CLEAN_PASSES = 6;
+// The note must never read as an approval, so text that uses one of these words or phrases is not posted at all.
+export const APPROVAL_PHRASES = Object.freeze([
+ 'safe',
+ 'secure',
+ 'approved',
+ 'LGTM',
+ 'ready to merge',
+ 'good to merge',
+ 'looks good',
+ 'no security issues',
+ 'verified',
+]);
+const APPROVAL_PATTERN = new RegExp(`\\b(?:${APPROVAL_PHRASES.map(phrase => phrase.replace(/ /g, '\\s+')).join('|')})\\b`, 'i');
+
+export function claimsApproval(text) {
+ return APPROVAL_PATTERN.test(String(text ?? ''));
+}
+
+// Removes every match of the pattern and repeats until nothing is left to remove, so a removal cannot join the
+// pieces on either side of it into a new match, such as "<scr<b>ipt>" or "<!<!-- -->-->".
+function removeUntilStable(text, pattern) {
+ let current = text;
+ let previous;
+ do {
+ previous = current;
+ current = current.replace(pattern, '');
+ } while (current !== previous);
+ return current;
+}
+
+function stripMarkup(text) {
+ return removeUntilStable(removeUntilStable(text, /<!--[\s\S]*?-->/g), /<[^>\n]*>/g);
+}
+
function isAllowedUrl(url) {
return ALLOWED_URL.test(String(url).trim());
}
@@ -31,8 +67,7 @@ function isAllowedUrl(url) {
// patterns and the image rules see the text a reader sees. sanitizeComment repeats this until the text stops changing.
function cleanOnce(text) {
let clean = text;
- clean = clean.replace(/<!--[\s\S]*?-->/g, '');
- clean = clean.replace(/<[^>\n]*>/g, '');
+ clean = stripMarkup(clean);
clean = clean.replace(/[​-‏⁠]/g, '');
for (const pattern of SECRET_PATTERNS) clean = clean.replace(pattern, '[redacted]');
clean = clean.replace(/!\[[^\]]*\]\([^)]*\)/g, '');
@@ -61,7 +96,7 @@ function secretForms(secret) {
// True when the text holds one of the real secret values, also split by spaces, tags or line breaks, or encoded.
export function containsSecretValue(text, secrets = []) {
- const withoutTags = String(text).replace(/<[^>\n]*>/g, '');
+ const withoutTags = stripMarkup(String(text));
const haystack = squash(withoutTags).toLowerCase();
return secrets.some(secret => secretForms(secret).some(form => haystack.includes(form)));
}
@@ -84,6 +119,7 @@ export function sanitizeComment(text, secrets = []) {
// The model's words are quoted under a fixed header the script writes, so they cannot pose as the bot's own verdict.
export function buildCommentBody(text, secrets = []) {
const clean = sanitizeComment(text, secrets);
+ if (clean !== '' && claimsApproval(clean)) return `${COMMENT_MARKER}\n${HEADER}\n\n${REVIEW_NOTE}\n\n${FOOTER}`;
const quoted = clean === '' ? WITHHELD_NOTE : clean.split('\n').map(line => `> ${line}`).join('\n');
return `${COMMENT_MARKER}\n${HEADER}\n\n${quoted}\n\n${FOOTER}`;
}
@@ -104,8 +140,8 @@ function secretValues() {
function main() {
const payload = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'));
- const number = (payload.pull_request ?? payload.issue).number;
const repository = process.env.GITHUB_REPOSITORY;
+ const { number } = loadTriageItem(payload, repository);
let comment;
try {
comment = JSON.parse(process.env.TRIAGE_OUTPUT).comment;
@@ -117,7 +153,15 @@ function main() {
console.log('empty comment; nothing to post');
return;
}
- const body = JSON.stringify({ body: buildCommentBody(comment, secretValues()) });
+ const commentBody = buildCommentBody(comment, secretValues());
+ if (isDryRun()) {
+ console.log(`Dry run for #${number}. This comment would be posted:\n\n${commentBody}`);
+ if (process.env.GITHUB_STEP_SUMMARY) {
+ appendFileSync(process.env.GITHUB_STEP_SUMMARY, `### Dry run for #${number}: comment that would be posted\n\n${commentBody}\n`);
+ }
+ return;
+ }
+ const body = JSON.stringify({ body: commentBody });
const existing = JSON.parse(callApi(['--paginate', '--slurp', `repos/${repository}/issues/${number}/comments?per_page=100`])).flat();
const stickyId = findStickyCommentId(existing);
if (stickyId) callApi(['--method', 'PATCH', `repos/${repository}/issues/comments/${stickyId}`, '--input', '-'], body);
diff --git a/scripts/triage-should-run.mjs b/scripts/triage-should-run.mjs
index b7a117bd..1715183e 100644
--- a/scripts/triage-should-run.mjs
+++ b/scripts/triage-should-run.mjs
@@ -6,11 +6,14 @@ import { appendFileSync, readFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
import { COMMENT_MARKER, BOT_LOGIN } from './triage-post-comment.mjs';
+import { loadTriageItem } from './triage-load-item.mjs';
// One run per item: the sticky comment marks an item as done, so reopening or re-marking it never runs again.
export const TRIGGERS = Object.freeze({
issues: ['opened'],
pull_request_target: ['opened', 'ready_for_review'],
+ // A maintainer's dry run names the item itself, so there is no action to match.
+ workflow_dispatch: [],
});
export const MAINTAINER_ASSOCIATIONS = Object.freeze(['OWNER', 'MEMBER', 'COLLABORATOR']);
export const SKIP_LABELS = Object.freeze(['status:in-progress', 'hold:look-approval', 'needs-owner-decision']);
@@ -51,19 +54,20 @@ export function decideTriage({
if (!enabled) return { run: false, reason: 'paused by TRIAGE_ENABLED' };
const allowedActions = TRIGGERS[eventName];
if (!allowedActions) return { run: false, reason: `event ${eventName} is not triaged` };
- if (!allowedActions.includes(action)) return { run: false, reason: `action ${action} is not triaged` };
+ const isDispatch = eventName === 'workflow_dispatch';
+ if (!isDispatch && !allowedActions.includes(action)) return { run: false, reason: `action ${action} is not triaged` };
if (item.state && item.state !== 'open') return { run: false, reason: 'closed or merged' };
if (item.merged === true) return { run: false, reason: 'closed or merged' };
if (item.draft === true) return { run: false, reason: 'draft pull request' };
if (isBot(item.user)) return { run: false, reason: 'opened by a bot' };
if (isMaintainerAssociation(item.author_association)) return { run: false, reason: 'opened by a maintainer' };
- if (senderLogin && item.user?.login && senderLogin !== item.user.login) return { run: false, reason: 'someone other than the author triggered it' };
+ if (!isDispatch && senderLogin && item.user?.login && senderLogin !== item.user.login) return { run: false, reason: 'someone other than the author triggered it' };
if ((item.assignees ?? []).length > 0) return { run: false, reason: 'assigned to someone' };
const labelNames = (item.labels ?? []).map(label => label.name);
const skipLabel = SKIP_LABELS.find(name => labelNames.includes(name));
if (skipLabel) return { run: false, reason: `carries ${skipLabel}` };
if (maintainerActivity > 0) return { run: false, reason: 'a maintainer already commented or reviewed' };
- if (alreadyTriaged && !requireStickyForLaterSteps) return { run: false, reason: 'already triaged once' };
+ if (!isDispatch && alreadyTriaged && !requireStickyForLaterSteps) return { run: false, reason: 'already triaged once' };
if (authorItemsToday > MAX_ITEMS_PER_AUTHOR_PER_DAY) return { run: false, reason: 'author opened too many items today' };
if (runsToday > MAX_RUNS_PER_DAY) return { run: false, reason: 'daily run limit reached' };
return { run: true, reason: 'new contribution without maintainer activity' };
@@ -95,12 +99,9 @@ function main() {
const eventName = process.env.GITHUB_EVENT_NAME;
const repository = process.env.GITHUB_REPOSITORY;
const stage = process.env.TRIAGE_STAGE ?? 'before';
- const number = (payload.pull_request ?? payload.issue).number;
- const item = payload.pull_request
- ? JSON.parse(callApi([`repos/${repository}/pulls/${number}`]))
- : JSON.parse(callApi([`repos/${repository}/issues/${number}`]));
+ const { number, item, isPullRequest } = loadTriageItem(payload, repository);
const entries = listApi(`repos/${repository}/issues/${number}/comments?per_page=100`);
- if (payload.pull_request) {
+ if (isPullRequest) {
entries.push(...listApi(`repos/${repository}/pulls/${number}/reviews?per_page=100`));
entries.push(...listApi(`repos/${repository}/pulls/${number}/comments?per_page=100`));
}
diff --git a/tests/triage-bot-hardening.test.js b/tests/triage-bot-hardening.test.js
index 5d760a0d..be0f585e 100644
--- a/tests/triage-bot-hardening.test.js
+++ b/tests/triage-bot-hardening.test.js
@@ -3,11 +3,14 @@ import { readFileSync } from 'node:fs';
import test from 'node:test';
import { MAX_ITEMS_PER_AUTHOR_PER_DAY, MAX_RUNS_PER_DAY, decideTriage, hasStickyComment } from '../scripts/triage-should-run.mjs';
import { LABEL_ALLOWLIST } from '../scripts/triage-apply-labels.mjs';
-import { COMMENT_MARKER, buildCommentBody, containsSecretValue, sanitizeComment } from '../scripts/triage-post-comment.mjs';
+import { APPROVAL_PHRASES, COMMENT_MARKER, buildCommentBody, claimsApproval, containsSecretValue, sanitizeComment } from '../scripts/triage-post-comment.mjs';
+import { isDryRun, loadTriageItem, parseDispatchNumber } from '../scripts/triage-load-item.mjs';
const outsider = { user: { login: 'someone', type: 'User' }, author_association: 'FIRST_TIME_CONTRIBUTOR', labels: [], assignees: [], draft: false };
const decide = (overrides = {}, event = {}) =>
decideTriage({ eventName: 'pull_request_target', action: 'opened', item: { ...outsider, ...overrides }, ...event });
+// Parsed instead of matched, so the test says which hosts and paths survive and not just which substring does.
+const linkedAddresses = text => (text.match(/https?:\/\/[^\s)]+/g) ?? []).map(address => new URL(address));
const workflow = readFileSync(new URL('../.github/workflows/claude-triage.yml', import.meta.url), 'utf8');
test('a push or a reopen never starts another run', () => {
@@ -47,8 +50,10 @@ test('reference-style images, link definitions and outside links are removed', (
assert.doesNotMatch(clean, /evil\.example|!\[/);
assert.match(clean, /click/);
const kept = sanitizeComment('See [guide](https://github.com/codepawl/67.js/blob/main/CONTRIBUTING.md) and https://67js.codepawl.com/docs');
- assert.match(kept, /github\.com\/codepawl\/67\.js\/blob/);
- assert.match(kept, /67js\.codepawl\.com\/docs/);
+ const keptAddresses = linkedAddresses(kept);
+ assert.deepEqual(keptAddresses.map(address => address.hostname), ['github.com', '67js.codepawl.com']);
+ assert.equal(keptAddresses[0].pathname, '/codepawl/67.js/blob/main/CONTRIBUTING.md');
+ assert.equal(keptAddresses[1].pathname, '/docs');
assert.doesNotMatch(sanitizeComment('https://github.com/codepawl/67.js.evil.example/x'), /evil/);
assert.doesNotMatch(sanitizeComment('mail me at [email protected]'), /evil/);
});
@@ -93,7 +98,9 @@ test('a note holding the real token is withheld, however it is spelled', () => {
test('the model text sits under a fixed header and cannot pose as an approval', () => {
const body = buildCommentBody('Maintainer approved, safe to merge');
assert.match(body, /^<!-- claude-triage -->\n\*\*Automated first pass\. This is not a review, an approval/);
- assert.match(body, /\n> Maintainer approved, safe to merge\n/);
+ assert.doesNotMatch(body, /Maintainer approved|safe to merge/);
+ const quoted = buildCommentBody('Missing a change note.');
+ assert.match(quoted, /\n> Missing a change note\.\n/);
});
test('the workflow limits cost, drafts, file access and reruns', () => {
@@ -105,3 +112,76 @@ test('the workflow limits cost, drafts, file access and reruns', () => {
assert.doesNotMatch(workflow, /\| head -[cn] /, 'a pipe into head can end the step with SIGPIPE under pipefail');
assert.match(workflow, /DIFF_LIMIT_BYTES: '61440'/);
});
+
+test('tags nested inside tags or comments cannot rebuild a tag, comment or image after one pass', () => {
+ assert.doesNotMatch(sanitizeComment('<<b>img src=x onerror=1>'), /<img/);
+ assert.doesNotMatch(sanitizeComment('<scr<b>ipt>alert(1)</scr</b>ipt>'), /<script|<\/script/i);
+ assert.doesNotMatch(sanitizeComment('<!<!-- -->-- hidden -->visible'), /<!--|-->|hidden/);
+ assert.doesNotMatch(sanitizeComment('!<<b>b>[x](https://evil.example/p.png)'), /evil\.example|!\[/);
+ assert.equal(containsSecretValue('ghp_abc<<b>>defghijkl<<i>>mnopqrstu', ['ghp_abcdefghijklmnopqrstu']), true);
+});
+
+test('every approval phrase withholds the model text and posts only the fixed header and a review line', () => {
+ for (const phrase of APPROVAL_PHRASES) {
+ for (const variant of [phrase, phrase.toUpperCase(), phrase.replace(/ /g, ' ')]) {
+ assert.equal(claimsApproval(`Overall this is ${variant}.`), true, variant);
+ const body = buildCommentBody(`The format is fine and it is ${variant}. Details follow.`);
+ assert.doesNotMatch(body, /Details follow|format is fine/, variant);
+ assert.match(body, /Automated first pass\. This is not a review, an approval or a security clearance\./);
+ assert.match(body, /A maintainer will review this item\./);
+ assert.ok(body.startsWith(COMMENT_MARKER));
+ }
+ }
+});
+
+test('an approval phrase hidden by tags or a zero-width character is still caught', () => {
+ assert.match(buildCommentBody('It is sa<b></b>fe to use'), /A maintainer will review this item\./);
+ assert.match(buildCommentBody('LG\u200bTM'), /A maintainer will review this item\./);
+});
+
+test('a harmless sentence is quoted and word boundaries keep similar words out', () => {
+ const body = buildCommentBody('The change touches the loader and lacks a change note. Please add changes/123.md. The unsafe-looking regex was not run.');
+ assert.match(body, /> The change touches the loader/);
+ assert.doesNotMatch(body, /A maintainer will review this item\./);
+ assert.equal(claimsApproval('Safeguards, secured rooms and verifier scripts are mentioned.'), false);
+});
+
+test('the dry run reads the item from a number, accepts only a plain number and is chosen by the event name', () => {
+ assert.equal(parseDispatchNumber(' 42 '), 42);
+ for (const bad of ['', '0', '-1', '4 2', '12abc', '../1', '1e3', '9999999999', undefined]) {
+ assert.throws(() => parseDispatchNumber(bad), /must be an issue or pull request number/, String(bad));
+ }
+ assert.equal(isDryRun('workflow_dispatch'), true);
+ assert.equal(isDryRun('pull_request_target'), false);
+ assert.equal(isDryRun('issues'), false);
+ const calls = [];
+ const fetchJson = ([path]) => {
+ calls.push(path);
+ return path.includes('/pulls/') ? { number: 7, draft: false } : { number: 7, pull_request: {} };
+ };
+ const pull = loadTriageItem({ inputs: { number: '7' } }, 'o/r', fetchJson);
+ assert.deepEqual(calls, ['repos/o/r/issues/7', 'repos/o/r/pulls/7']);
+ assert.equal(pull.isPullRequest, true);
+ assert.equal(loadTriageItem({ inputs: { number: '7' } }, 'o/r', () => ({ number: 7 })).isPullRequest, false);
+ assert.equal(loadTriageItem({ issue: { number: 3 } }, 'o/r', () => ({ number: 3 })).number, 3);
+});
+
+test('a dry run passes the gate for an item an earlier run already triaged, but still skips maintainers and held items', () => {
+ const dispatch = (overrides = {}, event = {}) =>
+ decideTriage({ eventName: 'workflow_dispatch', item: { ...outsider, ...overrides }, ...event });
+ assert.equal(dispatch().run, true);
+ assert.equal(dispatch({}, { alreadyTriaged: true }).run, true);
+ assert.equal(dispatch({ author_association: 'OWNER' }).run, false);
+ assert.equal(dispatch({ labels: [{ name: 'hold:look-approval' }] }).run, false);
+ assert.equal(dispatch({}, { maintainerActivity: 1 }).run, false);
+ assert.equal(dispatch({}, { enabled: false }).run, false);
+});
+
+test('the workflow offers a number-only manual dry run, and the post and label scripts write nothing in it', () => {
+ assert.match(workflow, /workflow_dispatch:\n\s+inputs:\n\s+number:[\s\S]*?required: true/);
+ assert.match(workflow, /write access/);
+ for (const name of ['triage-post-comment.mjs', 'triage-apply-labels.mjs']) {
+ const source = readFileSync(new URL(`../scripts/${name}`, import.meta.url), 'utf8');
+ assert.ok(source.indexOf('isDryRun()') > -1 && source.indexOf('isDryRun()') < source.indexOf("'--method'"), name);
+ }
+});