From eae73092ad0791166fb57676513cab1e1b225998 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 11 May 2026 10:53:59 +0000 Subject: [PATCH 1/6] fix: streamline commitAndPush function by removing redundant code and improving error handling --- app/git.js | 51 +++++++++++++++++---------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/app/git.js b/app/git.js index e3e0a2c..87ec0c9 100644 --- a/app/git.js +++ b/app/git.js @@ -15,41 +15,24 @@ export async function commitAndPush(workspace) { .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); + 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 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}`); + } catch (e) { + console.log(` ⚠️ Runner failed: commit/push 失敗: ${e.message}`); } - - 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}`); } From f5cf5950bd51709b7f9597649ee641677f47506e Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 11 May 2026 13:29:31 +0000 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20=E6=94=B9=E7=94=A8=20execSync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/git.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/git.js b/app/git.js index 87ec0c9..b67814a 100644 --- a/app/git.js +++ b/app/git.js @@ -1,12 +1,9 @@ -import { spawnSync } from 'child_process'; +import { execSync } 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(); + return execSync(`git ${args.map(a => `'${a.replace(/'/g, "'\\''")}'`).join(' ')}`, { cwd, encoding: 'utf8' }).trim(); } export async function commitAndPush(workspace) { From d04f4dd2bbf7800baa9697bf1ca696583ed74380 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 11 May 2026 13:47:40 +0000 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=E5=9B=9E=E5=BE=A9=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20spawnSync=20=E5=9F=B7=E8=A1=8C=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/git.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/git.js b/app/git.js index b67814a..87ec0c9 100644 --- a/app/git.js +++ b/app/git.js @@ -1,9 +1,12 @@ -import { execSync } from 'child_process'; +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) { - return execSync(`git ${args.map(a => `'${a.replace(/'/g, "'\\''")}'`).join(' ')}`, { cwd, encoding: 'utf8' }).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 || '').trim()); + return (result.stdout || '').trim(); } export async function commitAndPush(workspace) { From 8a28d1f1ef2dfe236008bb842de2ff67779cfd33 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 11 May 2026 13:50:54 +0000 Subject: [PATCH 4/6] =?UTF-8?q?test:=20=E4=BF=AE=E6=AD=A3=E8=B7=AF?= =?UTF-8?q?=E5=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/git.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/git.js b/app/git.js index 87ec0c9..123178d 100644 --- a/app/git.js +++ b/app/git.js @@ -1,5 +1,4 @@ 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) { @@ -10,7 +9,7 @@ function git(args, cwd) { } export async function commitAndPush(workspace) { - const repoDir = path.join(workspace, GITEA_REPOSITORY); + const repoDir = workspace; const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '') .replace('https://', `https://${GITEA_TOKEN}@`) .replace('http://', `http://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`; From c1f8aa3c72eb659763ab72d05aad6337b6aa0c3e Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 11 May 2026 13:54:44 +0000 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20git=20=E6=93=8D=E4=BD=9C=20clone=20?= =?UTF-8?q?=E7=9A=84=20repo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/git.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/git.js b/app/git.js index 123178d..c9bc837 100644 --- a/app/git.js +++ b/app/git.js @@ -1,4 +1,6 @@ import { spawnSync } from 'child_process'; +import fs from 'fs'; +import path from 'path'; import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js'; function git(args, cwd) { @@ -9,16 +11,28 @@ function git(args, cwd) { } export async function commitAndPush(workspace) { - const repoDir = workspace; const remoteUrl = GITEA_SERVER_URL.replace(/\/$/, '') .replace('https://', `https://${GITEA_TOKEN}@`) .replace('http://', `http://${GITEA_TOKEN}@`) + `/${GITEA_REPOSITORY}.git`; + const repoDir = path.join(workspace, 'repo'); + try { + if (!fs.existsSync(repoDir)) { + git(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace); + } + 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); + + // 將 findings.json 從 workspace 複製到 clone 的 repo + const srcFindings = path.join(workspace, FINDINGS_PATH); + const destFindings = path.join(repoDir, FINDINGS_PATH); + fs.mkdirSync(path.dirname(destFindings), { recursive: true }); + fs.copyFileSync(srcFindings, destFindings); + git(['add', FINDINGS_PATH], repoDir); const status = git(['status', '--porcelain'], repoDir); From d7336dbe6c657f57641916655664766f291c5631 Mon Sep 17 00:00:00 2001 From: AI Review Bot Date: Mon, 11 May 2026 13:55:46 +0000 Subject: [PATCH 6/6] chore: update ai-review findings [skip ci] --- .gitea/ai-review/findings.json | 114 +++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 .gitea/ai-review/findings.json diff --git a/.gitea/ai-review/findings.json b/.gitea/ai-review/findings.json new file mode 100644 index 0000000..d38cfc7 --- /dev/null +++ b/.gitea/ai-review/findings.json @@ -0,0 +1,114 @@ +[ + { + "level": "critical", + "role": "Rex", + "location": "app/git.js:11", + "suggestion": "GITEA_TOKEN 直接嵌入 URL 中,可能導致憑證洩漏。建議使用環境變數或安全的憑證管理方式來處理敏感資訊。", + "is_new": true + }, + { + "level": "critical", + "role": "Maya", + "location": "app/git.js:1", + "suggestion": "缺少對 commitAndPush 函數的單元測試,應該為其添加測試以確保其正確性。", + "is_new": true + }, + { + "level": "warning", + "role": "Leo", + "location": "app/git.js:11", + "suggestion": "建議將 git 函式的實作細節封裝到一個獨立的模組中,以提高模組化和可重用性。", + "is_new": true + }, + { + "level": "warning", + "role": "Leo", + "location": "app/git.js:41", + "suggestion": "建議在 try-catch 區塊中增加更詳細的錯誤處理,以便於未來的除錯和維護。", + "is_new": true + }, + { + "level": "warning", + "role": "Zara", + "location": "app/git.js:25", + "suggestion": "在使用 fs.existsSync 檢查目錄是否存在時,應考慮使用非同步方法以避免阻塞事件循環。", + "is_new": true + }, + { + "level": "warning", + "role": "Zara", + "location": "app/git.js:29", + "suggestion": "在 git clone 時使用 --depth=1 可能會導致未來需要完整歷史紀錄時的性能問題,建議根據實際需求調整。", + "is_new": true + }, + { + "level": "warning", + "role": "Rex", + "location": "app/git.js:11", + "suggestion": "在使用 fs.copyFileSync 時,未檢查目標檔案是否存在,可能會覆蓋重要資料。建議在複製之前檢查檔案是否存在。", + "is_new": true + }, + { + "level": "warning", + "role": "Maya", + "location": "app/git.js:11", + "suggestion": "在 commitAndPush 函數中,對於 git 操作的錯誤處理不夠完善,應該添加更多的測試來驗證不同情況下的行為。", + "is_new": true + }, + { + "level": "info", + "role": "Leo", + "location": "app/git.js:5", + "suggestion": "建議為函式添加 JSDoc 註解,以提高文件完整性和可讀性。", + "is_new": true + }, + { + "level": "info", + "role": "Leo", + "location": "app/git.js:10", + "suggestion": "建議將常數如 'repo' 提取為變數,以提高可讀性和可維護性。", + "is_new": true + }, + { + "level": "info", + "role": "Zara", + "location": "app/git.js:45", + "suggestion": "考慮使用 async/await 來處理 fs.copyFileSync,以提高可讀性和錯誤處理能力。", + "is_new": true + }, + { + "level": "info", + "role": "Aria", + "location": "app/git.js:12", + "suggestion": "考慮將常量 GITEA_SERVER_URL、GITEA_REPOSITORY、GITEA_TOKEN、PR_HEAD_BRANCH 和 FINDINGS_PATH 的引入放在一起,以保持一致性。", + "is_new": true + }, + { + "level": "info", + "role": "Aria", + "location": "app/git.js:41", + "suggestion": "在 console.log 的訊息中,考慮使用更具描述性的文字來提高可讀性。", + "is_new": true + }, + { + "level": "info", + "role": "Aria", + "location": "app/git.js:43", + "suggestion": "考慮將錯誤處理的 console.log 訊息翻譯成英文,以保持一致性。", + "is_new": true + }, + { + "level": "info", + "role": "Maya", + "location": "app/git.js:11", + "suggestion": "建議為 git 函數添加測試,以確保其在不同參數下的行為正確。", + "is_new": true + }, + { + "level": "info", + "role": "Maya", + "location": "app/git.js:11", + "suggestion": "建議對 repoDir 的存在性檢查進行單元測試,以確保在不存在時能正確執行 clone 操作。", + "is_new": true + } +] \ No newline at end of file