ci: keep published PR reviews concise
This commit is contained in:
1 parent
836d91cdbc
commit
dd32e28f4b
2 files changed
+33
-18
No files matched your search
@@ -8,7 +8,8 @@ const MAX_FILES = 300;
|
||||
const MAX_DIFF_LENGTH = 400_000;
|
||||
const MAX_FILE_LENGTH = 36_000;
|
||||
const CI_WAIT_MS = 240_000;
|
||||
const MAX_FINDINGS = 8;
|
||||
const MAX_FINDINGS = 5;
|
||||
const MAX_PENDING_CHECKS = 3;
|
||||
const HUNK = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/;
|
||||
const REVIEW_SECTIONS = {
|
||||
value: "合并价值与功能完整性",
|
||||
@@ -111,12 +112,13 @@ const instructions = [
|
||||
"5. 结合该 PR 当前提交的 CI 记录核对已执行的关键流程,列出尚需人工或真机实测的用例,每条写清入口、旧数据条件或预期结果。只承认输入中明确通过的步骤。",
|
||||
"CI 记录对应 PR 的 head 提交。若主线同一流程已有新改动,或 PR 修改了 CI 本身,检查合入当前主线还需要哪些验证。",
|
||||
"最后用实际证据给出合并建议。缺少关键测试、主线对照或重要文件时标明限制。存在阻塞问题时指出合并前必须解决的事项。",
|
||||
"仅输出 JSON 对象,包含 value、upgrade、ui、implementation 四个简体中文短段落,decision、reason、pending_checks 和 findings。",
|
||||
"decision 为 merge、changes、verify 或 decline,reason 用一句话给出依据。pending_checks 为最多 5 条待实测项目,全部有证据时为空数组。无行内发现时 findings 为空数组。",
|
||||
"只返回一个 JSON 对象,不加前言、后记或 Markdown。包含 value、upgrade、ui、implementation 四项简体中文短结论,以及 decision、reason、pending_checks 和 findings。",
|
||||
"四项短结论每项最多 75 字。decision 为 merge、changes、verify 或 decline,reason 用一句话给出依据,最多 110 字。",
|
||||
"pending_checks 最多 3 条,每条最多 75 字。全部有证据时返回空数组。无行内发现时 findings 为空数组。",
|
||||
"行内发现只报告有证据的可操作问题,避免格式偏好、猜测或可执行的攻击步骤。",
|
||||
"每条包含 path、side、line、priority、body。side 为 RIGHT(新增)或 LEFT(删除)。",
|
||||
"line 使用 diff hunk 对应的文件行号,只能锚定到改动行。priority 为 P1、P2 或 P3。",
|
||||
"最多 8 条。body 简体中文,简述触发条件、影响及建议,使用纯文本,不抄录代码或外部链接。",
|
||||
"行内发现按影响从大到小排列,最多 5 条。body 每条最多 130 字,简述触发条件、影响及建议,使用纯文本,不抄录代码或外部链接。",
|
||||
].join("\n");
|
||||
|
||||
async function reviewDiffs(context, includeDesignGuidelines) {
|
||||
@@ -146,7 +148,8 @@ async function reviewDiffs(context, includeDesignGuidelines) {
|
||||
},
|
||||
},
|
||||
agent: {
|
||||
"pr-review": { mode: "primary", steps: 1, prompt: instructions + "\n\n" + guidelines },
|
||||
// OpenCode injects a prose summary instruction on the last configured step.
|
||||
"pr-review": { mode: "primary", steps: 2, prompt: guidelines + "\n\n" + instructions },
|
||||
},
|
||||
permission: { "*": "deny" },
|
||||
share: "disabled",
|
||||
@@ -217,13 +220,7 @@ async function reviewDiffs(context, includeDesignGuidelines) {
|
||||
throw new Error("OpenCode did not return a complete text-only review " + JSON.stringify(diagnostic));
|
||||
}
|
||||
const answer = text.map((event) => event.part.text).join("\n");
|
||||
let decoded;
|
||||
try {
|
||||
const content = answer.trim().replace(/^```(?:json)?\s*\n([\s\S]*?)\n```$/i, "$1");
|
||||
decoded = JSON.parse(content);
|
||||
} catch {
|
||||
throw new Error("LLM review is not valid JSON");
|
||||
}
|
||||
const decoded = parseReviewJSON(answer);
|
||||
if (!Object.keys(REVIEW_SECTIONS).every((key) => typeof decoded?.[key] === "string" && decoded[key].trim())
|
||||
|| typeof decoded.decision !== "string" || !Object.hasOwn(DECISIONS, decoded.decision)
|
||||
|| typeof decoded.reason !== "string" || !decoded.reason.trim()
|
||||
@@ -232,6 +229,24 @@ async function reviewDiffs(context, includeDesignGuidelines) {
|
||||
return decoded;
|
||||
}
|
||||
|
||||
function parseReviewJSON(answer) {
|
||||
const fenced = [...answer.matchAll(/```(?:json)?[^\S\n]*\n([\s\S]*?)\n```/gi)].map((match) => match[1]);
|
||||
const object = answer.slice(answer.indexOf("{"), answer.lastIndexOf("}") + 1);
|
||||
for (const candidate of [answer, ...fenced, object]) {
|
||||
try {
|
||||
return JSON.parse(candidate);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
throw new Error("LLM review is not valid JSON (" + answer.length + " text characters)");
|
||||
}
|
||||
|
||||
function shortText(value, limit) {
|
||||
const characters = Array.from(value.trim());
|
||||
return characters.length > limit ? characters.slice(0, limit - 1).join("") + "…" : characters.join("");
|
||||
}
|
||||
|
||||
function plainMarkdown(value) {
|
||||
return value
|
||||
.replace(/[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]+/g, " ")
|
||||
@@ -256,7 +271,7 @@ function inlineComments(findings, locations) {
|
||||
seen.add(key);
|
||||
comments.push({
|
||||
path, side, line,
|
||||
body: "**[" + finding.priority + "]** " + plainMarkdown(finding.body.trim().slice(0, 900)),
|
||||
body: "**[" + finding.priority + "]** " + plainMarkdown(shortText(finding.body, 130)),
|
||||
});
|
||||
}
|
||||
return comments;
|
||||
@@ -345,7 +360,7 @@ function reviewSummary(assessment, ci, incomplete, mainMoved, mergeable) {
|
||||
const lines = [];
|
||||
if (assessment) {
|
||||
for (const [key, label] of Object.entries(REVIEW_SECTIONS)) {
|
||||
lines.push("- **" + label + "** " + plainMarkdown(assessment[key].trim().slice(0, 1000)));
|
||||
lines.push("- **" + label + "** " + plainMarkdown(shortText(assessment[key], 75)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,14 +371,14 @@ function reviewSummary(assessment, ci, incomplete, mainMoved, mergeable) {
|
||||
lines.push("- " + plainMarkdown(step.name) + " · " + plainMarkdown(step.conclusion ?? "未执行"));
|
||||
}
|
||||
|
||||
const pending = (assessment?.pending_checks ?? []).map((check) => check.trim()).filter(Boolean).slice(0, 5);
|
||||
const pending = (assessment?.pending_checks ?? []).map((check) => check.trim()).filter(Boolean).slice(0, MAX_PENDING_CHECKS);
|
||||
if (pending.length) {
|
||||
lines.push("\n**待实测**");
|
||||
for (const check of pending) lines.push("- " + plainMarkdown(check.slice(0, 800)));
|
||||
for (const check of pending) lines.push("- " + plainMarkdown(shortText(check, 75)));
|
||||
}
|
||||
|
||||
let decision = assessment?.decision ?? "verify";
|
||||
let reason = assessment?.reason.trim().slice(0, 800) ?? "没有可审核的文本差异。";
|
||||
let reason = assessment ? shortText(assessment.reason, 110) : "没有可审核的文本差异。";
|
||||
const hasTestEvidence = ci.conclusion === "success" && ci.steps?.some((step) => step.conclusion === "success");
|
||||
if (mergeable === false) {
|
||||
decision = "changes";
|
||||
|
||||
Reference in new issue
Block a user