Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc537958ca | |||
| 1c321b7ba2 | |||
| 710cd7308e | |||
| 59978c6fb5 | |||
| 519e04691d |
@@ -29,4 +29,5 @@
|
|||||||
|
|
||||||
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
|
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
|
||||||
|
|
||||||
每次執行後請貼 log,我會協助 debug。
|
每次執行後請貼 log,我會協助 debug。
|
||||||
|
|
||||||
|
|||||||
+17
-37
@@ -1,44 +1,24 @@
|
|||||||
import { execSync } from 'child_process';
|
import fs from 'fs';
|
||||||
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
import path from 'path';
|
||||||
|
import { commitFile } from './gitea.js';
|
||||||
function exec(cmd, cwd) {
|
import { FINDINGS_PATH } from './config.js';
|
||||||
return execSync(cmd, { cwd, stdio: 'pipe' }).toString().trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Commit findings.json 並 push 到 PR 來源分支
|
* 透過 Gitea API 將 findings.json push 到來源分支(不需要 git binary)
|
||||||
*/
|
*/
|
||||||
export function commitAndPush(workspace) {
|
export async function commitAndPush(workspace) {
|
||||||
const repoDir = `${workspace}/${GITEA_REPOSITORY}`;
|
|
||||||
const remoteUrl = `${GITEA_SERVER_URL.replace(/\/$/, '')}/${GITEA_REPOSITORY}.git`
|
|
||||||
.replace('https://', `https://${GITEA_TOKEN}@`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 設定 git 身份
|
const fullPath = path.join(workspace, FINDINGS_PATH);
|
||||||
exec('git config user.email "ai-review[bot]@gitea"', repoDir);
|
const content = fs.readFileSync(fullPath, 'utf8');
|
||||||
exec('git config user.name "AI Review Bot"', repoDir);
|
const result = await commitFile(
|
||||||
|
FINDINGS_PATH,
|
||||||
// 切換到來源分支
|
content,
|
||||||
exec(`git fetch origin ${PR_HEAD_BRANCH}`, repoDir);
|
'chore: update ai-review findings [skip ci]'
|
||||||
exec(`git checkout ${PR_HEAD_BRANCH}`, repoDir);
|
);
|
||||||
|
const commitHash = result.commit?.sha?.slice(0, 7) || 'unknown';
|
||||||
// 確認 findings.json 存在
|
console.log(` ✅ persisted findings commit=${commitHash} push=${process.env.PR_HEAD_BRANCH}`);
|
||||||
exec(`git add ${FINDINGS_PATH}`, repoDir);
|
|
||||||
|
|
||||||
// 檢查是否有變更
|
|
||||||
const status = exec('git status --porcelain', repoDir);
|
|
||||||
if (!status) {
|
|
||||||
console.log(' findings.json 無變更,跳過 commit');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const commitMsg = 'chore: update ai-review findings [skip ci]';
|
|
||||||
const commitHash = exec(`git commit -m "${commitMsg}"`, repoDir)
|
|
||||||
.match(/\[.+ ([a-f0-9]+)\]/)?.[1] || 'unknown';
|
|
||||||
|
|
||||||
exec(`git push ${remoteUrl} ${PR_HEAD_BRANCH}`, repoDir);
|
|
||||||
console.log(` ✅ persisted findings commit=${commitHash} push=${PR_HEAD_BRANCH}`);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);
|
const detail = e.response?.data ? JSON.stringify(e.response.data) : e.message;
|
||||||
|
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.response?.status || ''} ${detail}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-1
@@ -1,6 +1,6 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import https from 'https';
|
import https from 'https';
|
||||||
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER } from './config.js';
|
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH } from './config.js';
|
||||||
|
|
||||||
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
||||||
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
|
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
|
||||||
@@ -15,3 +15,37 @@ export async function postComment(body) {
|
|||||||
const resp = await axios.post(api(`/repos/${GITEA_REPOSITORY}/issues/${PR_NUMBER}/comments`), { body }, { headers: headers(), timeout: 30000, httpsAgent });
|
const resp = await axios.post(api(`/repos/${GITEA_REPOSITORY}/issues/${PR_NUMBER}/comments`), { body }, { headers: headers(), timeout: 30000, httpsAgent });
|
||||||
return resp.data;
|
return resp.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 透過 Gitea API 建立或更新檔案(不需要 git binary)
|
||||||
|
*/
|
||||||
|
export async function commitFile(filePath, content, message) {
|
||||||
|
const encoded = Buffer.from(content).toString('base64');
|
||||||
|
const url = api(`/repos/${GITEA_REPOSITORY}/contents/${filePath}`);
|
||||||
|
|
||||||
|
// 先嘗試取得現有檔案的 SHA
|
||||||
|
let sha;
|
||||||
|
try {
|
||||||
|
const existing = await axios.get(`${url}?ref=${PR_HEAD_BRANCH}`, { headers: headers(), httpsAgent, timeout: 15000 });
|
||||||
|
sha = existing.data.sha;
|
||||||
|
} catch {
|
||||||
|
sha = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
message,
|
||||||
|
content: encoded,
|
||||||
|
branch: PR_HEAD_BRANCH,
|
||||||
|
...(sha ? { sha } : {}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const resp = await axios.request({
|
||||||
|
method: sha ? 'put' : 'post',
|
||||||
|
url,
|
||||||
|
headers: headers(),
|
||||||
|
httpsAgent,
|
||||||
|
timeout: 30000,
|
||||||
|
data: payload,
|
||||||
|
});
|
||||||
|
return resp.data;
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -91,7 +91,7 @@ async function main() {
|
|||||||
|
|
||||||
// Step5: commit/push findings.json 到來源分支
|
// Step5: commit/push findings.json 到來源分支
|
||||||
console.log('\n💾 Step5: 記憶區 Commit/Push');
|
console.log('\n💾 Step5: 記憶區 Commit/Push');
|
||||||
commitAndPush(WORKSPACE);
|
await commitAndPush(WORKSPACE);
|
||||||
|
|
||||||
// Step6: 有 critical 問題則 exit 1
|
// Step6: 有 critical 問題則 exit 1
|
||||||
console.log('\n🚦 Step6: 嚴重問題檢查');
|
console.log('\n🚦 Step6: 嚴重問題檢查');
|
||||||
|
|||||||
Reference in New Issue
Block a user