Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8aa273b8bd | |||
| 00458d4eb2 | |||
| 894ece033b | |||
| fe2a513fbb | |||
| 2193bdd4d6 | |||
| af51ffacee | |||
| d9c55ca347 | |||
| 21fb9c1d94 | |||
| 607c9b82ea | |||
| 8acea007e7 | |||
| 953951145f | |||
| 1576e783fb | |||
| e017705c64 | |||
| 5f77b83a0f | |||
| da43cb02b0 | |||
| 577a930438 | |||
| 121f66b0b3 | |||
| faa808bb5f | |||
| 07df3ef4a5 | |||
| fc537958ca | |||
| 1c321b7ba2 | |||
| 710cd7308e | |||
| 59978c6fb5 | |||
| 519e04691d | |||
| 5ae0549453 | |||
| 81e38de649 |
+5
-4
@@ -1,8 +1,9 @@
|
||||
FROM node:20-slim
|
||||
FROM alpine
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
git \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN apk add --no-cache bash nodejs npm git \
|
||||
&& node --version \
|
||||
&& npm --version \
|
||||
&& git --version
|
||||
|
||||
WORKDIR /action
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
---
|
||||
|
||||
|
||||
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
|
||||
|
||||
每次執行後請貼 log,我會協助 debug。
|
||||
@@ -0,0 +1,70 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { postComment } from './gitea.js';
|
||||
import { FINDINGS_PATH } from './config.js';
|
||||
|
||||
const LEVEL_EMOJI = { critical: '🔴', warning: '🟡', info: '🔵' };
|
||||
const LEVEL_LABEL = { critical: '嚴重', warning: '警告', info: '建議' };
|
||||
|
||||
function findingRow(f) {
|
||||
return `| ${LEVEL_EMOJI[f.level] || ''} ${LEVEL_LABEL[f.level] || f.level} | ${f.role} | ${f.location} | ${f.suggestion} |`;
|
||||
}
|
||||
|
||||
function buildTable(findings) {
|
||||
const rows = findings.map(findingRow).join('\n');
|
||||
return `| 等級 | 審查員 | 位置 | 建議 |\n|------|--------|------|------|\n${rows}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 寫入 findings.json 到 workspace
|
||||
*/
|
||||
export function saveFindings(workspace, findings) {
|
||||
const fullPath = path.join(workspace, FINDINGS_PATH);
|
||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||
fs.writeFileSync(fullPath, JSON.stringify(findings, null, 2), 'utf8');
|
||||
console.log(` ✅ findings 寫入: ${fullPath} (${findings.length} 筆)`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 發布所有舊問題 comment(一次發布,依等級排序)
|
||||
*/
|
||||
export async function postOldFindingsComment(findings) {
|
||||
const old = findings.filter(f => !f.is_new);
|
||||
if (old.length === 0) {
|
||||
console.log(' 無舊問題,跳過');
|
||||
return;
|
||||
}
|
||||
const body = `## 📋 舊有未解決問題(${old.length} 筆)\n\n${buildTable(old)}`;
|
||||
await postComment(body);
|
||||
console.log(` ✅ 舊問題 comment 發布 (${old.length} 筆)`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 發布新問題中非 critical 的 comment(一次發布)
|
||||
*/
|
||||
export async function postNewNonCriticalComment(findings) {
|
||||
const items = findings.filter(f => f.is_new && f.level !== 'critical');
|
||||
if (items.length === 0) {
|
||||
console.log(' 無新的非嚴重問題,跳過');
|
||||
return;
|
||||
}
|
||||
const body = `## 🔍 新發現問題(${items.length} 筆)\n\n${buildTable(items)}`;
|
||||
await postComment(body);
|
||||
console.log(` ✅ 新問題(非嚴重)comment 發布 (${items.length} 筆)`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 每個新 critical 問題各發一個 comment
|
||||
*/
|
||||
export async function postNewCriticalComments(findings) {
|
||||
const criticals = findings.filter(f => f.is_new && f.level === 'critical');
|
||||
if (criticals.length === 0) {
|
||||
console.log(' 無新的嚴重問題,跳過');
|
||||
return;
|
||||
}
|
||||
for (const f of criticals) {
|
||||
const body = `## 🚨 嚴重問題\n\n| 審查員 | 位置 | 建議 |\n|--------|------|------|\n| ${f.role} | ${f.location} | ${f.suggestion} |`;
|
||||
await postComment(body);
|
||||
console.log(` ✅ 嚴重問題 comment 發布: [${f.role}] ${f.location}`);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import { spawnSync } from 'child_process';
|
||||
import path from 'path';
|
||||
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
||||
|
||||
function git(args, cwd) {
|
||||
const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) throw new Error((result.stderr || result.stdout || '').trim());
|
||||
return (result.stdout || '').trim();
|
||||
}
|
||||
|
||||
export async function commitAndPush(workspace) {
|
||||
const repoDir = path.join(workspace, GITEA_REPOSITORY);
|
||||
const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '')
|
||||
.replace('https://', `https://${GITEA_TOKEN}@`)
|
||||
.replace('http://', `http://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`;
|
||||
|
||||
try {
|
||||
git(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
|
||||
git(['config', 'user.name', 'AI Review Bot'], repoDir);
|
||||
git(['fetch', 'origin', PR_HEAD_BRANCH], repoDir);
|
||||
git(['checkout', PR_HEAD_BRANCH], repoDir);
|
||||
git(['add', FINDINGS_PATH], repoDir);
|
||||
|
||||
const status = git(['status', '--porcelain'], repoDir);
|
||||
if (!status) {
|
||||
console.log(' findings.json 無變更,跳過 commit');
|
||||
return;
|
||||
}
|
||||
|
||||
const out = git(['commit', '-m', 'chore: update ai-review findings [skip ci]'], repoDir);
|
||||
const commitHash = out.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown';
|
||||
git(['push', remoteUrl, PR_HEAD_BRANCH], repoDir);
|
||||
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
|
||||
} catch (e) {
|
||||
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);
|
||||
}
|
||||
}
|
||||
+26
-6
@@ -2,6 +2,8 @@ import { GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH, PR_BASE_BRANCH, getLLMConf
|
||||
import { loadRoles, getRoleIntro } from './roles.js';
|
||||
import { getPRDiff, postComment } from './gitea.js';
|
||||
import { analyzeWithRole, loadOldFindings, mergeFindings, sortByLevel, deduplicateWithAI } from './findings.js';
|
||||
import { saveFindings, postOldFindingsComment, postNewNonCriticalComment, postNewCriticalComments } from './comments.js';
|
||||
import { commitAndPush } from './git.js';
|
||||
|
||||
const WORKSPACE = process.env.GITHUB_WORKSPACE || '/workspace';
|
||||
|
||||
@@ -74,14 +76,32 @@ async function main() {
|
||||
const sorted = sortByLevel(deduped);
|
||||
console.log(` Step3b dedup findings total=${sorted.length} (critical=${sorted.filter(f=>f.level==='critical').length} warning=${sorted.filter(f=>f.level==='warning').length} info=${sorted.filter(f=>f.level==='info').length})`);
|
||||
|
||||
console.log('\n📝 Step4: Findings 寫入與 Comment 發布(待實作)');
|
||||
console.log(' [stub] 寫入 findings.json,發布 comment...');
|
||||
// Step4: 寫入 findings.json,依序發布 comment
|
||||
console.log('\n📝 Step4: Findings 寫入與 Comment 發布');
|
||||
saveFindings(WORKSPACE, sorted);
|
||||
|
||||
console.log('\n💾 Step5: 記憶區 Commit/Push(待實作)');
|
||||
console.log(' [stub] commit & push findings.json...');
|
||||
try {
|
||||
await postOldFindingsComment(sorted);
|
||||
await postNewNonCriticalComment(sorted);
|
||||
await postNewCriticalComments(sorted);
|
||||
console.log(' Step4 完成');
|
||||
} catch (e) {
|
||||
console.log(` ⚠️ comment 發布失敗(繼續執行): ${e.message}`);
|
||||
}
|
||||
|
||||
console.log('\n🚦 Step6: 嚴重問題檢查(待實作)');
|
||||
console.log(' [stub] 檢查 critical findings...');
|
||||
// Step5: commit/push findings.json 到來源分支
|
||||
console.log('\n💾 Step5: 記憶區 Commit/Push');
|
||||
await commitAndPush(WORKSPACE);
|
||||
|
||||
// Step6: 有 critical 問題則 exit 1
|
||||
console.log('\n🚦 Step6: 嚴重問題檢查');
|
||||
const criticalCount = sorted.filter(f => f.level === 'critical').length;
|
||||
if (criticalCount > 0) {
|
||||
console.log(` ❌ 發現 ${criticalCount} 個嚴重問題,workflow 結束(exit 1)`);
|
||||
console.log('='.repeat(60));
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(' ✅ 無嚴重問題');
|
||||
|
||||
console.log('\n✅ Pipeline 完成');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
Reference in New Issue
Block a user