Compare commits

..

2 Commits

2 changed files with 18 additions and 23 deletions
+2 -1
View File
@@ -29,4 +29,5 @@
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。 每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
每次執行後請貼 log,我會協助 debug。 每次執行後請貼 log,我會協助 debug。
+16 -22
View File
@@ -1,8 +1,11 @@
import { execSync } from 'child_process'; import { spawnSync } from 'child_process';
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js'; import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
function exec(cmd, cwd) { function git(args, cwd) {
return execSync(cmd, { cwd, stdio: 'pipe' }).toString().trim(); 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);
return (result.stdout || '').trim();
} }
/** /**
@@ -10,33 +13,24 @@ function exec(cmd, cwd) {
*/ */
export function commitAndPush(workspace) { export function commitAndPush(workspace) {
const repoDir = `${workspace}/${GITEA_REPOSITORY}`; const repoDir = `${workspace}/${GITEA_REPOSITORY}`;
const remoteUrl = `${GITEA_SERVER_URL.replace(/\/$/, '')}/${GITEA_REPOSITORY}.git` const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '').replace('https://', `https://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`;
.replace('https://', `https://${GITEA_TOKEN}@`);
try { try {
// 設定 git 身份 git(['config', 'user.email', 'ai-review[bot]@gitea'], repoDir);
exec('git config user.email "ai-review[bot]@gitea"', repoDir); git(['config', 'user.name', 'AI Review Bot'], repoDir);
exec('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);
exec(`git fetch origin ${PR_HEAD_BRANCH}`, repoDir);
exec(`git checkout ${PR_HEAD_BRANCH}`, repoDir);
// 確認 findings.json 存在
exec(`git add ${FINDINGS_PATH}`, repoDir);
// 檢查是否有變更
const status = exec('git status --porcelain', repoDir);
if (!status) { if (!status) {
console.log(' findings.json 無變更,跳過 commit'); console.log(' findings.json 無變更,跳過 commit');
return; return;
} }
const commitMsg = 'chore: update ai-review findings [skip ci]'; const out = git(['commit', '-m', 'chore: update ai-review findings [skip ci]'], repoDir);
const commitHash = exec(`git commit -m "${commitMsg}"`, repoDir) const commitHash = out.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown';
.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown'; git(['push', remoteUrl, PR_HEAD_BRANCH], repoDir);
exec(`git push ${remoteUrl} ${PR_HEAD_BRANCH}`, repoDir);
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`); console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
} catch (e) { } catch (e) {
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`); console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);