Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/qwen-code-pr-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ jobs:
echo "PR #${PR_NUMBER} is ${PR_STATE}; skipping acknowledgement." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
ACK_BODY="<!-- qwen-review-ack -->_Qwen Code review request accepted. Review is queued in [workflow run](${RUN_URL})._"
# Blank line after the marker, or none of the prose below renders. A
# line opening with `<!--` starts an HTML block that runs to the line
# containing the closing delimiter, and the REST of that line stays
# inside it — so gluing the text on shipped it as raw source with a
# dead link. Verified against GitHub's own renderer.
ACK_BODY="$(printf '<!-- qwen-review-ack -->\n\n_Qwen Code review request accepted. Review is queued in [workflow run](%s)._' "$RUN_URL")"
EXISTING_ACK_ID="$(
# -F would otherwise make gh api default to POST.
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
Expand Down
108 changes: 108 additions & 0 deletions scripts/tests/qwen-pr-review-workflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2274,3 +2274,111 @@ describe('command shape matching', () => {
expect(stripCr).toBeGreaterThan(firstLine);
});
});

describe('bot comment markers', () => {
// A line that opens with `<!--` starts an HTML block, and that block runs to
// the line holding the closing delimiter INCLUSIVE — the rest of that line
// stays inside it and is never parsed as Markdown. The queued-ack comment
// glued its prose straight onto the marker and reached every PR as raw
// source with a dead link. Measured through GitHub's own renderer
// (POST /markdown, mode=gfm): marker+text -> 0 <a>/0 <em>; marker+"\n"+text
// and marker+"\n\n"+text -> 1 <a>/1 <em>.
//
// The rule keys off ONE thing: a marker that opens a string literal, and
// what remains inside that literal after it. That is what distinguishes
// BUILDING a comment body from merely REFERENCING a marker — `jq
// contains("<!-- m -->")` and `printf '<!-- m -->' "$VAR"` leave nothing
// after the marker and are fine, while `="<!-- m -->prose"` does not. The
// scan is bounded to the marker's physical line: a glued body is by
// definition on that line, and searching past it would couple the guard to
// unrelated quotes elsewhere in the file — a doc example quoting an unclosed
// `--body "<!-- m -->` would break the moment any later line gained a `"`.
//
// Known gaps, stated rather than papered over: bodies split across printf
// arguments (`printf '%s%s' '<!-- m -->' 'prose'`) — detecting them means
// modelling which literal is the format string, and every cheap
// approximation flagged the legitimate `printf '<!-- m -->' "$VAR"` form;
// bodies assembled across statements or files (one `echo` per line into a
// `--body-file`); markers at physical line start (heredocs, YAML block
// scalars); markers mid-literal that only land at a rendered line start
// after `\n` expansion (`printf 'x\n<!-- m -->prose'`); multi-line literals
// whose closing quote sits on a later line; and a line-wrapped printf whose
// format opens with the marker (the `\n` exemption reads one physical
// line). All are latent — no workflow uses these shapes today.
const dir = '.github/workflows';
const files = readdirSync(dir).filter((f) => /\.ya?ml$/.test(f));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The new guard pastes the identical workflow-directory scan idiom already present 130 lines above in this file (describe('workflow expression length') at lines 2178-2179) — two independent enumerations of .github/workflows with the same \.ya?ml$ filter.

Failure scenario: any future change to the scan — a new workflow-file extension, excluding a subdirectory, or the dir moving — must be applied in both places; applying it in one silently leaves the two guards scanning different file sets with no failure signal and no way to notice.

Suggested change
const dir = '.github/workflows';
const files = readdirSync(dir).filter((f) => /\.ya?ml$/.test(f));
const workflowFiles = readdirSync('.github/workflows').filter((f) => /\.ya?ml$/.test(f));
中文说明

新守卫把本文件 130 行之前已有的工作流目录扫描惯用法又粘贴了一遍(workflow expression length describe 块,2178-2179 行)——两处各自枚举 .github/workflows 且使用相同的 \.ya?ml$ 过滤。失败场景:将来任何对扫描范围的改动(新增扩展名、排除子目录、目录迁移)都必须在两处同时进行;只改一处会让两个守卫静默扫描不同的文件集合,没有任何失败信号。建议把枚举提升到模块作用域(第 23 行已有的 const workflow = readFileSync(...) 旁),两个 describe 共用。

— DeepSeek/deepseek-v4-flash via Qwen Code /review (v0.21.8)


it('never glues prose onto a comment marker, in any workflow', () => {
expect(files.length).toBeGreaterThan(0);
const offenders = [];
for (const file of files) {
const text = readFileSync(join(dir, file), 'utf8');
// `[^>\n]` and not `[^>]`: a `<!--` inside a nearby comment would
// otherwise let one match span lines, swallow the real marker, and get
// discarded by the comment skip below — the guard would then pass with
// the very regression present.
const re = /<!--[^>\n]*-->/g;
let m;
while ((m = re.exec(text)) !== null) {
const lineStart = text.lastIndexOf('\n', m.index) + 1;
const prefix = text.slice(lineStart, m.index);
// Prose in a YAML or shell comment never reaches a comment body.
if (/^\s*#/.test(prefix)) continue;
const quote = m.index === lineStart ? null : text[m.index - 1];
// Only a marker that OPENS a string literal can be building a body.
if (quote !== "'" && quote !== '"') continue;
const rest = text.slice(m.index + m[0].length);
const lineEnd = rest.indexOf('\n');
const line = lineEnd === -1 ? rest : rest.slice(0, lineEnd);
const end = line.indexOf(quote);
// No closing quote on this line means either the marker ends the line
// inside a multi-line literal (a real newline separates the body,
// which renders) or the quote is prose in a doc example — neither
// glues anything ON the marker's line.
if (end === -1) continue;
let glued = line.slice(0, end);
if (glued === '') {
// The literal ended at the marker — but an adjacent literal on the
// same line concatenates onto it at runtime, and so does an
// unquoted `$(…)` (its output is invisible to this scan).
const after = line.slice(end + 1);
if (after[0] === "'" || after[0] === '"') {
const q2 = after[0];
const e2 = after.slice(1).indexOf(q2);
glued = e2 === -1 ? '' : after.slice(1, 1 + e2);
} else if (after[0] === '$' && after[1] === '(') {
glued = after;
}
}
if (glued === '') continue;
// The two-character `\n` escape separates only where the shell
// expands it: a printf format string or ANSI-C `$'…'` opened at the
// END of the prefix — an unrelated printf earlier on the same line
// must not bless a plain double-quoted assignment, where `\n` stays
// a literal backslash-n and the prose stays on the marker's line.
if (glued.startsWith('\\n') && /printf\s+['"]$|\$'$/.test(prefix)) {
continue;
}
offenders.push(
`${file}: ${text.slice(m.index, m.index + 56).split('\n')[0]}`,
);
}
}
expect(offenders).toEqual([]);
});

it('pins the workflow-run URL into the ack printf', () => {
// bash printf with a leftover argument and no conversion spec exits 0
// under `set -euo pipefail` and emits `[workflow run]()`, so nothing
// else catches a dropped `%s` or `"$RUN_URL"` on the ack line.
const text = readFileSync(join(dir, 'qwen-code-pr-review.yml'), 'utf8');
const ackLine = text
.split('\n')
.find(
(l) => l.includes('printf') && l.includes('<!-- qwen-review-ack -->'),
);
expect(ackLine).toBeDefined();
expect(ackLine).toContain('%s');
expect(ackLine).toContain('"$RUN_URL"');
Comment on lines +2400 to +2402

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The URL-pin test asserts %s and "$RUN_URL" merely co-exist on the ack line, not that the URL is woven into the markdown link — a displaced %s or marker-as-argument refactor passes both tests and re-ships the dead-link comment this PR fixes.

Failure scenario: a future edit rewrites the ack line to ACK_BODY="$(printf '%s%s' '<!-- qwen-review-ack -->' "$RUN_URL")" (marker moved out of the format into its own argument — the natural refactor, since $RUN_URL is already an argument) or moves %s outside the link parens ([workflow run]%s._). Both forms keep %s and "$RUN_URL" somewhere on the line, so the two toContain assertions pass. Real bash confirms the output renders [workflow run]https://… with no parens / the URL glued onto the marker line — GitHub's HTML block swallows the line, the comment ships as raw source with a dead link again, and CI stays green.

Suggested change
expect(ackLine).toBeDefined();
expect(ackLine).toContain('%s');
expect(ackLine).toContain('"$RUN_URL"');
expect(ackLine).toContain('[workflow run](%s)');
中文说明

URL 固定测试只断言 %s 与 "$RUN_URL" 同时出现在 ack 行上,并未断言 URL 真的织入了 markdown 链接——一旦 %s 被移出链接括号、或标记被改成 printf 的独立参数,两个 toContain 断言仍然通过,死链评论会再次发出。失败场景:把 ack 行改写为 printf '%s%s' '<!-- qwen-review-ack -->' "$RUN_URL",或把 %s 移出括号。bash 实测输出为 [workflow run]https://…(无括号)或 URL 直接粘在标记行——GitHub 的 HTML block 吞掉整行,评论以原始文本发出且链接失效,CI 依旧全绿。建议改为断言链接形状,例如 expect(ackLine).toContain('[workflow run](%s)')。

— DeepSeek/deepseek-v4-flash via Qwen Code /review (v0.21.8)

});
});
Loading