test: cover review edge cases and repair paths

This commit is contained in:
2026-05-13 06:23:45 +00:00
parent 8f413439b3
commit 3f3ead0f08
8 changed files with 243 additions and 41 deletions
+46 -29
View File
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { GITEA_REPOSITORY, 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 } from './gitea.js';
@@ -9,6 +10,42 @@ import { cloneRepo, commitAndPush } from './git.js';
const WORKSPACE = process.env.GITHUB_WORKSPACE || '/workspace';
export function validateAndRepairJsonFile(fullPath, relPath = fullPath) {
if (!fs.existsSync(fullPath)) {
console.log(` ⚠️ ${relPath} 不存在,跳過驗證`);
return true;
}
try {
JSON.parse(fs.readFileSync(fullPath, 'utf8'));
console.log(`${relPath} JSON 格式正確`);
return true;
} catch (e) {
console.error(`${relPath} JSON 格式錯誤: ${e.message},嘗試修正...`);
try {
const backupPath = fullPath + '.bak';
fs.copyFileSync(fullPath, backupPath);
fs.writeFileSync(fullPath, '[]\n', 'utf8');
console.log(`${relPath} 已重置為空陣列(原檔備份至 ${relPath}.bak`);
return true;
} catch (repairErr) {
console.error(`${relPath} 修正失敗: ${repairErr.message}`);
return false;
}
}
}
export function handleCriticalFindings(findings, exitFn = process.exit) {
const criticalCount = findings.filter(f => f.level === 'critical').length;
if (criticalCount > 0) {
console.log(` ❌ 發現 ${criticalCount} 個嚴重問題,workflow 結束(exit 1`);
console.log('='.repeat(60));
exitFn(1);
return true;
}
console.log(' ✅ 無嚴重問題');
return false;
}
async function main() {
console.log('='.repeat(60));
console.log('🚀 Step1: Pipeline 啟動');
@@ -102,24 +139,8 @@ async function main() {
console.log('\n🔎 Step6: JSON 格式驗證');
for (const relPath of [FINDINGS_PATH, EXCLUSIONS_PATH]) {
const fullPath = path.join(repoDir || WORKSPACE, relPath);
if (!fs.existsSync(fullPath)) {
console.log(` ⚠️ ${relPath} 不存在,跳過驗證`);
continue;
}
try {
JSON.parse(fs.readFileSync(fullPath, 'utf8'));
console.log(`${relPath} JSON 格式正確`);
} catch (e) {
console.error(`${relPath} JSON 格式錯誤: ${e.message},嘗試修正...`);
try {
const backupPath = fullPath + '.bak';
fs.copyFileSync(fullPath, backupPath);
fs.writeFileSync(fullPath, '[]\n', 'utf8');
console.log(`${relPath} 已重置為空陣列(原檔備份至 ${relPath}.bak`);
} catch (repairErr) {
console.error(`${relPath} 修正失敗: ${repairErr.message}`);
process.exit(1);
}
if (!validateAndRepairJsonFile(fullPath, relPath)) {
process.exit(1);
}
}
@@ -129,18 +150,14 @@ async function main() {
// Step9: 有 critical 問題則 exit 1
console.log('\n🚦 Step8: 嚴重問題檢查');
const criticalCount = filtered.filter(f => f.level === 'critical').length;
if (criticalCount > 0) {
console.log(` ❌ 發現 ${criticalCount} 個嚴重問題,workflow 結束(exit 1`);
console.log('='.repeat(60));
process.exit(1);
}
console.log(' ✅ 無嚴重問題');
handleCriticalFindings(filtered);
console.log('\n✅ Pipeline 完成');
console.log('='.repeat(60));
}
main().catch(e => {
console.error('❌ Runner failed:', e.message);
process.exit(1);
});
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
main().catch(e => {
console.error('❌ Runner failed:', e.message);
process.exit(1);
});
}