Compare commits

...

22 Commits

Author SHA1 Message Date
jiantw83 69624a542e fix: refactor commitAndPush function to improve clarity and maintainability 2026-05-11 10:50:16 +00:00
jiantw83 8aa273b8bd fix: add bash to Dockerfile dependencies 2026-05-11 10:46:58 +00:00
jiantw83 00458d4eb2 fix: switch Dockerfile base image to alpine and install dependencies using apk 2026-05-11 10:43:58 +00:00
jiantw83 894ece033b fix: Dockerfile 加入 git 安裝驗證,git.js 改回 git binary 2026-05-11 10:34:23 +00:00
jiantw83 fe2a513fbb fix: 修正 findings.json 路徑重複問題 2026-05-11 10:31:43 +00:00
jiantw83 2193bdd4d6 fix: 改回 Gitea API commit,修正 URL encode 與 JSON.stringify 2026-05-11 10:30:07 +00:00
jiantw83 af51ffacee fix: 換用 node:20 完整版(內建 git,不需要 apt-get) 2026-05-11 10:24:24 +00:00
jiantw83 d9c55ca347 chore: add newline at end of TODO.md for better formatting 2026-05-11 10:18:34 +00:00
jiantw83 21fb9c1d94 fix: 改回 git commit/push,強制 Dockerfile rebuild 確保 git binary 存在 2026-05-11 10:15:36 +00:00
jiantw83 607c9b82ea debug: log content_len,改用 JSON.stringify 2026-05-11 10:13:57 +00:00
jiantw83 8acea007e7 chore: remove duplicate log assistance note in TODO.md 2026-05-11 10:12:47 +00:00
jiantw83 953951145f chore: remove test findings 2026-05-11 10:11:56 +00:00
jiantw83 1576e783fb test 2026-05-11 10:11:40 +00:00
jiantw83 e017705c64 chore: remove test findings 2026-05-11 10:10:27 +00:00
jiantw83 5f77b83a0f debug: commitFile 加上詳細 log 2026-05-11 10:09:16 +00:00
jiantw83 da43cb02b0 chore: update ai-review findings [skip ci] 2026-05-11 10:08:57 +00:00
AI Review Bot 577a930438 chore: update ai-review findings [skip ci] 2026-05-11 10:08:26 +00:00
jiantw83 121f66b0b3 debug: git.js 加上參數 log 2026-05-11 10:06:28 +00:00
jiantw83 faa808bb5f test update 2026-05-11 10:06:05 +00:00
jiantw83 07df3ef4a5 test 2026-05-11 10:05:57 +00:00
jiantw83 fc537958ca debug: commit/push 失敗時顯示詳細錯誤 2026-05-11 10:01:40 +00:00
jiantw83 1c321b7ba2 fix: commitAndPush 加上 await 2026-05-11 10:00:08 +00:00
5 changed files with 61 additions and 62 deletions
+5 -4
View File
@@ -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
+1 -1
View File
@@ -27,7 +27,7 @@
---
每個階段都會加上明確的 log,並確保即使部分功能未完成也能降級執行、不會中斷 pipeline。
每次執行後請貼 log,我會協助 debug。
+52 -20
View File
@@ -1,23 +1,55 @@
import fs from 'fs';
import { spawnSync } from 'child_process';
import path from 'path';
import { commitFile } from './gitea.js';
import { FINDINGS_PATH } from './config.js';
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
/**
* 透過 Gitea API 將 findings.json push 到來源分支(不需要 git binary)
*/
export async function commitAndPush(workspace) {
try {
const fullPath = path.join(workspace, FINDINGS_PATH);
const content = fs.readFileSync(fullPath, 'utf8');
const result = await commitFile(
FINDINGS_PATH,
content,
'chore: update ai-review findings [skip ci]'
);
const commitHash = result.commit?.sha?.slice(0, 7) || 'unknown';
console.log(` ✅ persisted findings commit=${commitHash} push=${process.env.PR_HEAD_BRANCH}`);
} catch (e) {
console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`);
}
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}`);
// }
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}`);
}
+1 -35
View File
@@ -1,6 +1,6 @@
import axios from 'axios';
import https from 'https';
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH } from './config.js';
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER } from './config.js';
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
@@ -15,37 +15,3 @@ export async function postComment(body) {
const resp = await axios.post(api(`/repos/${GITEA_REPOSITORY}/issues/${PR_NUMBER}/comments`), { body }, { headers: headers(), timeout: 30000, httpsAgent });
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
View File
@@ -91,7 +91,7 @@ async function main() {
// Step5: commit/push findings.json 到來源分支
console.log('\n💾 Step5: 記憶區 Commit/Push');
commitAndPush(WORKSPACE);
await commitAndPush(WORKSPACE);
// Step6: 有 critical 問題則 exit 1
console.log('\n🚦 Step6: 嚴重問題檢查');