fix(ai-review): 對齊建問題模式留言順序與命名
This commit is contained in:
+29
-48
@@ -117,31 +117,11 @@ function commitFindings({ cwd, ctx, files, result }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* AI code review 主流程:依固定 10 步驟執行多角色審查,回傳 process exit code。
|
||||
* AI code review 主流程:編排多角色審查、發布審查結果,並回傳 process exit code。
|
||||
*
|
||||
* 流程概要(步驟 2~10 描述一般模式;建問題模式差異見末段):
|
||||
* 1. 快速回報 — 最新 commit 若為 ai-review-bot 的結果 commit([success]/[failure]),直接回報 0/1 不重審;
|
||||
* 2. 將 PR 既有舊留言標記為解決(跳過本回合留言;建問題模式不執行此步)——
|
||||
* 此步延後到「本回合審查已成功產生結果、即將發布問題留言前」才執行,避免工具偵測/diff/
|
||||
* 攻防裁決任一失敗時舊結果先被清掉卻沒有新結果(一般模式);
|
||||
* 3. 偵測 AI 工具(antigravity/codex/claude)並留言;
|
||||
* 4. 讀 .reviewignore、整理 git diff 並留言(無可審查變更時:留言+保存空 findings,
|
||||
* 一般模式 commit success、建問題模式略過 commit,回傳 0);
|
||||
* 5–6. 攻擊方登場留言、每位攻擊方一個 sub agent 並行找問題;
|
||||
* 7–8. 防守方登場留言、裁決誤報後排序並保存 findings JSON,
|
||||
* 並以 appendExclusions 把誤判/重複問題回寫 .gitea/ai-review/exclusions.json;
|
||||
* 9. 嚴重問題逐條掛在程式碼行上留言;
|
||||
* 10. 警告+建議彙整為單一表格留言;
|
||||
* 建問題模式(input: create-issue):不執行步驟 2、不觸碰 PR 既有留言;步驟 3~10 的所有留言
|
||||
* 改發到追蹤 issue(工具/diff/角色留言先暫存,確定有保留問題後先挑好標籤、連同標籤一次建立 issue
|
||||
* 並寫入暫存留言,嚴重問題與警告+建議再逐條發到該 issue,讓每條問題都能被個別回覆);
|
||||
* 無保留問題或無可審查變更則不建 issue、PR 也完全不留言(靜默通過);
|
||||
* 收束時在 PR 回貼 issue 連結形成雙向關聯,
|
||||
* 並「僅在有嚴重問題時」讓 PR 相依於該 issue(addIssueDependency,issue 關閉前 PR 無法合併;
|
||||
* 需 repo 啟用問題相依功能)——僅有警告/建議時 issue 仍建立供追蹤,但不阻擋合併;
|
||||
* 收尾:組 filesToCommit —— 一般模式 commit findings 檔(+有變更的 exclusions.json)、
|
||||
* 建問題模式只 commit exclusions.json、無檔案可 commit 時略過;
|
||||
* commit 訊息帶結果標記(success=無嚴重問題、failure=有嚴重問題)。
|
||||
* 一般模式會把審查情境、嚴重問題與警告/建議發布到 PR,並在成功產生本回合結果後才把舊留言標為過時。
|
||||
* 建問題模式會把審查情境與每條 finding 發到追蹤 issue;沒有保留 finding 時不建立 issue、PR 也不留言。
|
||||
* 嚴重 finding 會寫入 failure 結果 commit,警告與建議只建立追蹤資訊,不直接阻擋合併。
|
||||
*
|
||||
* @returns {Promise<number>} process exit code:本輪「審查」一律回傳 0(不因嚴重問題直接讓檢查失敗——
|
||||
* 失敗改由推出的 `[ai-review-bot][failure]` 結果 commit,於下一輪在步驟 1 讀 commit 訊息時回報);
|
||||
@@ -182,14 +162,14 @@ async function main() {
|
||||
|
||||
// 本回合(一般模式)發出的 PR 留言 id:resolveOldComments 標註過時時要跳過這些。
|
||||
const currentRunCommentIds = new Set();
|
||||
// 建問題模式:issue 於「確定有保留問題」後才建立;在那之前的情境留言(工具/diff/角色)
|
||||
// 先暫存於 issueBuffer,建立 issue 後一次寫入。
|
||||
const issueBuffer = [];
|
||||
let issue = null;
|
||||
// 建問題模式:追蹤 issue 於「確定有保留問題」後才建立;在那之前的情境留言(工具/diff/角色)
|
||||
// 先暫存於 pendingIssueCommentBodies,建立 issue 後一次寫入。
|
||||
const pendingIssueCommentBodies = [];
|
||||
let trackingIssue = null;
|
||||
/**
|
||||
* 發布一則審查留言。依模式決定去向:
|
||||
* - 一般模式:發到 PR,並記錄留言 id 供 `resolveOldComments` 排除。
|
||||
* - 建問題模式:issue 已建立時發到 issue;尚未建立時先暫存到 `issueBuffer`。
|
||||
* - 建問題模式:追蹤 issue 已建立時發到 issue;尚未建立時先暫存到 `pendingIssueCommentBodies`。
|
||||
*
|
||||
* @param {string} body 要發布的 Markdown 留言內容。
|
||||
* @returns {Promise<Object|null>} 一般模式、或建問題模式且 issue 已建立時回傳 Gitea 留言物件;
|
||||
@@ -200,8 +180,8 @@ async function main() {
|
||||
*/
|
||||
const queueOrPostComment = async (body) => {
|
||||
if (ctx.createIssue) {
|
||||
if (issue) return gitea.createCommentOnIssue(ctx, issue.number, body);
|
||||
issueBuffer.push(body);
|
||||
if (trackingIssue) return gitea.createCommentOnIssue(ctx, trackingIssue.number, body);
|
||||
pendingIssueCommentBodies.push(body);
|
||||
return null;
|
||||
}
|
||||
const created = await gitea.createIssueComment(ctx, body);
|
||||
@@ -210,24 +190,25 @@ async function main() {
|
||||
};
|
||||
/**
|
||||
* 建問題模式:建立追蹤 issue(標題=PR 標題、本文=PR 描述+回溯 PR 的引言,連同挑好的標籤一次建立),
|
||||
* 並把 `issueBuffer` 內暫存的情境留言依流程順序寫入 issue;設定閉包變數 `issue` 供後續留言直接發到 issue。
|
||||
* 並把 `pendingIssueCommentBodies` 內暫存的情境留言依流程順序寫入 issue;
|
||||
* 設定閉包變數 `trackingIssue` 供後續留言直接發到 issue。
|
||||
* 僅於「確定有保留問題」時呼叫一次。標籤於建立時一次帶入,省去「先建空標籤 issue 再補掛」的多餘 API 往返。
|
||||
*
|
||||
* @param {number[]} [labelIds] - 建立 issue 時要一併掛上的標籤 id 陣列(由 `review.selectLabels` 事先挑選);
|
||||
* 空陣列或省略時不掛任何標籤(`gitea.createIssue` 對空陣列不帶 labels 欄位)。
|
||||
* @returns {Promise<void>} 無回傳值;結果反映在閉包變數 `issue` 與 issue 留言。
|
||||
* @returns {Promise<void>} 無回傳值;結果反映在閉包變數 `trackingIssue` 與 issue 留言。
|
||||
*/
|
||||
const createIssueAndFlushBufferedComments = async (labelIds = []) => {
|
||||
issue = await gitea.createIssue(ctx, {
|
||||
trackingIssue = await gitea.createIssue(ctx, {
|
||||
title: ctx.prTitle || `AI Code Review:PR #${ctx.prNumber}`,
|
||||
body: templates.issueBody({ prNumber: ctx.prNumber, prBody: ctx.prBody }),
|
||||
labels: labelIds,
|
||||
});
|
||||
log('建問題', 'INF', `已建立追蹤 issue #${issue.number},寫入 ${issueBuffer.length} 則情境留言。`);
|
||||
for (const body of issueBuffer) {
|
||||
await gitea.createCommentOnIssue(ctx, issue.number, body);
|
||||
log('建問題', 'INF', `已建立追蹤 issue #${trackingIssue.number},寫入 ${pendingIssueCommentBodies.length} 則情境留言。`);
|
||||
for (const body of pendingIssueCommentBodies) {
|
||||
await gitea.createCommentOnIssue(ctx, trackingIssue.number, body);
|
||||
}
|
||||
issueBuffer.length = 0;
|
||||
pendingIssueCommentBodies.length = 0;
|
||||
};
|
||||
|
||||
// ── 步驟 2:延後執行 ───────────────────────────────────────────────────
|
||||
@@ -355,7 +336,7 @@ async function main() {
|
||||
// ── 步驟 9:嚴重問題留言(一般模式掛在 PR 程式碼行上;建問題模式逐條發到 issue)─
|
||||
if (severe.length > 0) {
|
||||
if (ctx.createIssue) {
|
||||
await review.postSevereToIssue({ ctx, gitea, issueNumber: issue.number, severe });
|
||||
await review.postSevereToIssue({ ctx, gitea, issueNumber: trackingIssue.number, severe });
|
||||
} else {
|
||||
await review.postSevereComments({ ctx, gitea, severe, cwd });
|
||||
}
|
||||
@@ -365,7 +346,7 @@ async function main() {
|
||||
// 建問題模式逐條發到 issue,讓每條問題都能被個別回覆。 ──
|
||||
if (others.length > 0) {
|
||||
if (ctx.createIssue) {
|
||||
await review.postOthersToIssue({ ctx, gitea, issueNumber: issue.number, others });
|
||||
await review.postOthersToIssue({ ctx, gitea, issueNumber: trackingIssue.number, others });
|
||||
} else {
|
||||
await queueOrPostComment(templates.othersComment(others));
|
||||
log('步驟10', 'INF', `警告+建議表格留言已發布(${others.length} 條)。`);
|
||||
@@ -374,12 +355,12 @@ async function main() {
|
||||
|
||||
// ── 建問題模式收束:在 PR 回貼 issue 連結(雙向關聯);僅在有嚴重問題時才讓 PR 相依於該 issue ─
|
||||
// 標籤已於建立 issue 時一次帶入(見上方 selectLabels → createIssueAndFlushBufferedComments),此處不再補掛。
|
||||
if (ctx.createIssue && issue) {
|
||||
if (ctx.createIssue && trackingIssue) {
|
||||
await gitea.createIssueComment(
|
||||
ctx,
|
||||
templates.issueLinkComment({
|
||||
issueNumber: issue.number,
|
||||
issueUrl: issue.html_url,
|
||||
templates.prIssueLinkComment({
|
||||
issueNumber: trackingIssue.number,
|
||||
issueUrl: trackingIssue.html_url,
|
||||
severeCount: severe.length,
|
||||
otherCount: others.length,
|
||||
}),
|
||||
@@ -388,15 +369,15 @@ async function main() {
|
||||
// 僅有警告/建議時,issue 仍建立供追蹤,但不掛相依、不阻擋 PR 合併。
|
||||
if (severe.length > 0) {
|
||||
try {
|
||||
await gitea.addIssueDependency(ctx, ctx.prNumber, issue.number);
|
||||
log('建問題', 'INF', `有嚴重問題:已將 PR #${ctx.prNumber} 設為相依於 issue #${issue.number},issue 關閉前無法合併。`);
|
||||
await gitea.addIssueDependency(ctx, ctx.prNumber, trackingIssue.number);
|
||||
log('建問題', 'INF', `有嚴重問題:已將 PR #${ctx.prNumber} 設為相依於 issue #${trackingIssue.number},issue 關閉前無法合併。`);
|
||||
} catch (err) {
|
||||
log('建問題', 'WRN', `設定 PR 相依失敗(可能未啟用「問題相依」功能):${err.message}。`);
|
||||
}
|
||||
} else {
|
||||
log('建問題', 'INF', `無嚴重問題(僅警告/建議):issue #${issue.number} 僅供追蹤,不阻擋 PR 合併。`);
|
||||
log('建問題', 'INF', `無嚴重問題(僅警告/建議):issue #${trackingIssue.number} 僅供追蹤,不阻擋 PR 合併。`);
|
||||
}
|
||||
log('建問題', 'INF', `issue #${issue.number} 已寫入審查內容,並在 PR 回貼連結。`);
|
||||
log('建問題', 'INF', `issue #${trackingIssue.number} 已寫入審查內容,並在 PR 回貼連結。`);
|
||||
}
|
||||
|
||||
// ── 收尾:commit 並 push(success=無嚴重問題、failure=有嚴重問題)───────
|
||||
|
||||
Reference in New Issue
Block a user