feat: report ai review commit status

This commit is contained in:
2026-05-15 14:39:15 +00:00
parent 93c3d0ca66
commit 4fd9a22aa0
4 changed files with 63 additions and 4 deletions
+24 -2
View File
@@ -1,13 +1,22 @@
import path from 'path';
import { GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH, PR_BASE_BRANCH, getLLMConfig, FINDINGS_PATH, EXCLUSIONS_PATH } from './config.js';
import { GITEA_REPOSITORY, GITEA_SERVER_URL, PR_NUMBER, PR_HEAD_BRANCH, PR_BASE_BRANCH, getLLMConfig, FINDINGS_PATH, EXCLUSIONS_PATH } from './config.js';
import { loadRoles, getRoleIntro } from './roles.js';
import { getPRDiff, postComment, shouldSkipBotCommit } from './gitea.js';
import { getPRDiff, postComment, shouldSkipBotCommit, setCommitStatus } from './gitea.js';
import { analyzeWithRole, loadOldFindings, mergeFindings, sortByLevel, deduplicateWithAI, loadExclusions, applyExclusions, filterFalsePositivesWithAI } from './findings.js';
import { saveFindings, postOldFindingsComment, postNewNonCriticalComment, postNewCriticalComments } from './comments.js';
import { cloneRepo, commitAndPush, getRepoState } from './git.js';
import { validateJSONArrayFile, ensureJSONArrayFileExists } from './json.js';
const WORKSPACE = process.env.GITHUB_WORKSPACE || '/workspace';
const REVIEW_STATUS_CONTEXT = 'ai-review/critical';
async function updateReviewStatus(sha, criticalCount) {
const state = criticalCount > 0 ? 'failure' : 'success';
const description = criticalCount > 0
? `found ${criticalCount} critical issue${criticalCount === 1 ? '' : 's'}`
: 'no critical issues found';
await setCommitStatus(sha, state, description, REVIEW_STATUS_CONTEXT, `${GITEA_SERVER_URL.replace(/\/$/, '')}/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}`);
}
async function main() {
console.log('='.repeat(60));
@@ -17,6 +26,17 @@ async function main() {
if (await shouldSkipBotCommit()) {
console.log(' 🤖 偵測到 [ai-review-bot] 自動提交,直接完成 action');
let criticalCount = 0;
try {
const repoDir = cloneRepo(WORKSPACE);
const findings = loadOldFindings(repoDir || WORKSPACE);
criticalCount = findings.filter(f => f.level === 'critical').length;
console.log(` 🔎 bot-check: current findings critical=${criticalCount}`);
await updateReviewStatus(process.env.PR_HEAD_SHA || process.env.GITHUB_SHA, criticalCount);
} catch (e) {
console.error(` ❌ bot-check: 無法回報 status: ${e.message}`);
process.exit(1);
}
console.log('='.repeat(60));
process.exit(0);
}
@@ -42,6 +62,7 @@ async function main() {
if (!diff.trim()) {
console.log(' ⚠️ diff 為空,無需審查');
await updateReviewStatus(process.env.PR_HEAD_SHA || process.env.GITHUB_SHA, 0);
process.exit(0);
}
@@ -133,6 +154,7 @@ async function main() {
// Step9: 有 critical 問題則 exit 1
console.log('\n🚦 Step8: 嚴重問題檢查');
const criticalCount = filtered.filter(f => f.level === 'critical').length;
await updateReviewStatus(process.env.PR_HEAD_SHA || process.env.GITHUB_SHA, criticalCount);
if (criticalCount > 0) {
console.log(` ❌ 發現 ${criticalCount} 個嚴重問題,workflow 結束(exit 1`);
console.log('='.repeat(60));