Compare commits

..

15 Commits

Author SHA1 Message Date
jiantw83 ea64c5f063 Merge pull request 'feat: 使用 kiro 重構 ai code review' (#85) from feat/refactor/main into develop
Reviewed-on: jiantw83/code-review#85
2026-05-12 10:06:38 +00:00
AI Review Bot 931481179a chore: update ai-review findings [skip ci] 2026-05-12 10:02:45 +00:00
jiantw83 52fa3acf18 feat: update GEMINI_BASE_URL to v1beta for improved API compatibility 2026-05-12 09:58:43 +00:00
jiantw83 c751a53d43 feat: enhance exclusions.json with new suggestions and refactor roles.js for dynamic path resolution 2026-05-12 09:57:33 +00:00
jiantw83 2aba414d36 feat: update GEMINI_BASE_URL to use v1 endpoint in workflow and documentation 2026-05-12 09:52:42 +00:00
AI Review Bot d565b79feb chore: update ai-review findings [skip ci] 2026-05-12 09:50:51 +00:00
jiantw83 81d5e3ff13 feat: update exclusions.json suggestions and refactor Dockerfile for improved npm installation 2026-05-12 09:40:56 +00:00
jiantw83 1ccc2cd560 feat: add GITEA_SKIP_TLS_VERIFY support for skipping SSL/TLS verification in Gitea integration 2026-05-12 09:37:51 +00:00
AI Review Bot c815c30088 chore: update ai-review findings [skip ci] 2026-05-12 09:31:01 +00:00
jiantw83 91816c700e Merge pull request 'feat: support multiple API keys for LLM providers, allowing automatic key rotation on failure' (#84) from feat/refactor/mulit_api_key into feat/refactor/main
Reviewed-on: jiantw83/code-review#84
2026-05-12 09:25:46 +00:00
AI Review Bot d9acf3b0b7 chore: update ai-review findings [skip ci] 2026-05-12 09:25:14 +00:00
jiantw83 9650162a67 feat: extend GEMINI_API_KEY support to include additional keys and update suggestions in exclusions.json 2026-05-12 09:21:32 +00:00
AI Review Bot 3fa5504e9a chore: update ai-review findings [skip ci] 2026-05-12 09:13:49 +00:00
jiantw83 b6aa37201a feat: add test for handling comma-only API key in getLLMConfig 2026-05-12 09:01:54 +00:00
AI Review Bot a296c594d3 chore: update ai-review findings [skip ci] 2026-05-12 08:45:24 +00:00
14 changed files with 154 additions and 36 deletions
+55
View File
@@ -148,5 +148,60 @@
"role": "Leo",
"location": "app/llm.test.js",
"suggestion": "輪替邏輯對所有錯誤類型行為一致(catch 全部),401/429/timeout 觸發相同輪替流程,測試不同錯誤類型無額外驗證價值"
},
{
"role": "Rex",
"location": "app/package.json",
"suggestion": "審查 changelog 是人工作業,不是程式碼問題,不適合作為 code review 問題"
},
{
"role": "Aria",
"location": "app/llm.js",
"suggestion": "此 action 為 CLI 工具,process.exit(1) 是設計意圖讓 CI/CD workflow 失敗。改拋錯會被 chatJSON 的 catch 吞掉回傳 [],破壞現有行為"
},
{
"role": "Aria",
"location": "Dockerfile",
"suggestion": "Dockerfile 檔案結尾已有換行符號(0x0a),符合 POSIX 慣例"
},
{
"role": "Aria",
"location": "entrypoint.sh",
"suggestion": "entrypoint.sh 檔案結尾已有換行符號(0x0a),符合 POSIX 慣例"
},
{
"role": "Maya",
"location": "app/main.js",
"suggestion": "main.js 整合測試需要真實 Gitea API、LLM API、git 操作,不適合單元測試。各模組已有獨立單元測試覆蓋"
},
{
"role": "Maya",
"location": "app/comments.js",
"suggestion": "comments.js 的 buildTable 為簡單字串拼接,postComment 已透過 gitea.js mock 間接測試,補測試效益低"
},
{
"role": "Maya",
"location": "app/roles.js",
"suggestion": "roles.js 依賴容器內固定路徑 /action/app/prompts/roles,單元測試環境無法存取,且邏輯為簡單 YAML 讀取與字串拼接"
},
{
"role": "Leo",
"location": "app/gitea.js",
"suggestion": "gitea.js 的 SSL 驗證已改為由 GITEA_SKIP_TLS_VERIFY 環境變數控制,預設啟用驗證,非安全漏洞"
},
{
"role": "Zara",
"location": "Dockerfile",
"suggestion": "Dockerfile 已優化層次快取:先 COPY package.json 再 npm install,最後才 COPY 其餘檔案"
},
{
"role": "Aria",
"location": "app/package.json",
"suggestion": "test 腳本已改為 node --test *.test.js,在 app/ 目錄下執行可自動發現所有測試檔案"
},
{
"role": "Zara",
"location": "app/main.js",
"suggestion": "deduplicateWithAI 和 filterFalsePositivesWithAI 為循序依賴流程(去重後才能過濾),無法平行化"
}
]
+4 -18
View File
@@ -1,23 +1,9 @@
[
{
"level": "warning",
"role": "Aria",
"location": ".gitea/workflows/master.yaml",
"suggestion": "檔案結尾應包含一個換行符號 (newline at EOF),這是 POSIX 系統的慣例,有助於版本控制系統的正確處理。",
"is_new": false
},
{
"level": "warning",
"role": "Leo",
"location": "app/llm.test.js",
"suggestion": "根據 `TODO.md` 的驗收標準,API Key 輪替失敗時應輸出特定的日誌訊息。目前的單元測試雖然驗證了 `process.exit(1)` 的調用,但並未對 `console.log` 和 `console.error` 的輸出進行模擬和斷言。建議使用 `mock.method(console, 'log', ...)` 和 `mock.method(console, 'error', ...)` 來捕獲並驗證這些重要的日誌訊息,以確保系統在 API Key 輪替失敗時能提供清晰的診斷資訊,這對長期維護和問題排查至關重要。",
"is_new": true
},
{
"level": "warning",
"role": "Leo",
"location": "app/llm.test.js",
"suggestion": "針對 API Key 輪替的錯誤處理,`TODO.md` 驗收標準中明確提到「模擬不同類型的 API 錯誤(例如 401 Unauthorized, 429 Too Many Requests, 網路超時等)」。目前的測試僅使用 `new Error('fail')` 進行通用錯誤模擬。建議擴展測試案例,模擬 `axios` 拋出帶有特定 HTTP 狀態碼(如 401, 429)的錯誤,以及模擬網路超時(例如 `axios.isAxiosError` 且 `e.code === 'ECONNABORTED'`),以確保 API Key 輪替機制在面對各種實際的 API 錯誤時都能穩健運作,這有助於提高程式碼的健壯性和可維護性。",
"level": "info",
"role": "Rex",
"location": "action.yaml",
"suggestion": "此 Action 需要 `contents: write`、`pull-requests: write` 和 `issues: write` 權限。這些權限對於 Action 的正常運作是必要的(例如寫入 findings.json、發布評論),但屬於較廣泛的權限。建議在文件或使用說明中明確指出這些權限的需求及其潛在影響,確保使用者了解並接受。",
"is_new": true
}
]
+1 -1
View File
@@ -33,7 +33,7 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
with:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }},${{ secrets.GEMINI_API_KEY_1 }},${{ secrets.GEMINI_API_KEY_2 }},${{ secrets.GEMINI_API_KEY_3 }},${{ secrets.GEMINI_API_KEY_4 }},${{ secrets.GEMINI_API_KEY_5 }},${{ secrets.GEMINI_API_KEY_6 }},${{ secrets.GEMINI_API_KEY_7 }},${{ secrets.GEMINI_API_KEY_8 }},${{ secrets.GEMINI_API_KEY_9 }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }},${{ secrets.GEMINI_API_KEY_1 }},${{ secrets.GEMINI_API_KEY_2 }},${{ secrets.GEMINI_API_KEY_3 }},${{ secrets.GEMINI_API_KEY_4 }},${{ secrets.GEMINI_API_KEY_5 }},${{ secrets.GEMINI_API_KEY_6 }},${{ secrets.GEMINI_API_KEY_7 }},${{ secrets.GEMINI_API_KEY_8 }},${{ secrets.GEMINI_API_KEY_9 }},${{ secrets.GEMINI_API_KEY_10 }},${{ secrets.GEMINI_API_KEY_11 }},${{ secrets.GEMINI_API_KEY_12 }},${{ secrets.GEMINI_API_KEY_13 }},${{ secrets.GEMINI_API_KEY_14 }},${{ secrets.GEMINI_API_KEY_15 }},${{ secrets.GEMINI_API_KEY_16 }},${{ secrets.GEMINI_API_KEY_17 }},${{ secrets.GEMINI_API_KEY_18 }},${{ secrets.GEMINI_API_KEY_19 }}
GEMINI_BASE_URL: https://generativelanguage.googleapis.com/v1beta
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
permissions:
+5 -4
View File
@@ -1,4 +1,4 @@
FROM alpine
FROM alpine:3.20
RUN apk add --no-cache bash nodejs npm git \
&& node --version \
@@ -7,10 +7,11 @@ RUN apk add --no-cache bash nodejs npm git \
WORKDIR /action
COPY app/package.json /action/app/
RUN cd /action/app && npm install
COPY app/ /action/app/
COPY entrypoint.sh /entrypoint.sh
RUN cd /action/app && npm install && \
chmod +x /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
+1 -1
View File
@@ -111,7 +111,7 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
with:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }},${{ secrets.GEMINI_API_KEY_1 }},${{ secrets.GEMINI_API_KEY_2 }},${{ secrets.GEMINI_API_KEY_3 }},${{ secrets.GEMINI_API_KEY_4 }},${{ secrets.GEMINI_API_KEY_5 }},${{ secrets.GEMINI_API_KEY_6 }},${{ secrets.GEMINI_API_KEY_7 }},${{ secrets.GEMINI_API_KEY_8 }},${{ secrets.GEMINI_API_KEY_9 }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }},${{ secrets.GEMINI_API_KEY_1 }},${{ secrets.GEMINI_API_KEY_2 }},${{ secrets.GEMINI_API_KEY_3 }},${{ secrets.GEMINI_API_KEY_4 }},${{ secrets.GEMINI_API_KEY_5 }},${{ secrets.GEMINI_API_KEY_6 }},${{ secrets.GEMINI_API_KEY_7 }},${{ secrets.GEMINI_API_KEY_8 }},${{ secrets.GEMINI_API_KEY_9 }},${{ secrets.GEMINI_API_KEY_10 }},${{ secrets.GEMINI_API_KEY_11 }},${{ secrets.GEMINI_API_KEY_12 }},${{ secrets.GEMINI_API_KEY_13 }},${{ secrets.GEMINI_API_KEY_14 }},${{ secrets.GEMINI_API_KEY_15 }},${{ secrets.GEMINI_API_KEY_16 }},${{ secrets.GEMINI_API_KEY_17 }},${{ secrets.GEMINI_API_KEY_18 }},${{ secrets.GEMINI_API_KEY_19 }}
GEMINI_BASE_URL: https://generativelanguage.googleapis.com/v1beta
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
permissions:
+5
View File
@@ -12,6 +12,10 @@ inputs:
GITEA_REPOSITORY:
description: 'Gitea Repository (owner/repo)'
required: false
GITEA_SKIP_TLS_VERIFY:
description: '跳過 Gitea SSL/TLS 憑證驗證(自簽憑證時使用)'
required: false
default: 'false'
PR_NUMBER:
description: 'Pull Request Number'
required: false
@@ -80,6 +84,7 @@ runs:
GITEA_TOKEN: ${{ inputs.GITEA_TOKEN || secrets.GITEA_TOKEN }}
GITEA_SERVER_URL: ${{ inputs.GITEA_SERVER_URL || gitea.server_url }}
GITEA_REPOSITORY: ${{ inputs.GITEA_REPOSITORY || gitea.repository }}
GITEA_SKIP_TLS_VERIFY: ${{ inputs.GITEA_SKIP_TLS_VERIFY }}
PR_NUMBER: ${{ inputs.PR_NUMBER || gitea.event.pull_request.number }}
PR_HEAD_BRANCH: ${{ inputs.PR_HEAD_BRANCH || gitea.event.pull_request.head.ref }}
PR_BASE_BRANCH: ${{ inputs.PR_BASE_BRANCH || gitea.event.pull_request.base.ref }}
+1
View File
@@ -1,6 +1,7 @@
export const GITEA_TOKEN = process.env.GITEA_TOKEN || '';
export const GITEA_SERVER_URL = process.env.GITEA_SERVER_URL || 'https://gitea.com';
export const GITEA_REPOSITORY = process.env.GITEA_REPOSITORY || '';
export const GITEA_SKIP_TLS_VERIFY = process.env.GITEA_SKIP_TLS_VERIFY === 'true';
export const PR_NUMBER = process.env.PR_NUMBER || '';
export const PR_HEAD_BRANCH = process.env.PR_HEAD_BRANCH || '';
export const PR_BASE_BRANCH = process.env.PR_BASE_BRANCH || '';
+7
View File
@@ -105,4 +105,11 @@ describe('getLLMConfig', () => {
assert.equal(cfg.provider, 'ollama');
assert.equal(cfg.model, 'llama3');
});
it('comma-only api key is treated as not set', () => {
process.env.OPENAI_API_KEY = ',,,';
const cfg = getLLMConfig();
assert.equal(cfg.provider, null);
assert.deepEqual(cfg.apiKeys, []);
});
});
+2 -2
View File
@@ -1,8 +1,8 @@
import axios from 'axios';
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, GITEA_SKIP_TLS_VERIFY, PR_NUMBER } from './config.js';
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
const httpsAgent = GITEA_SKIP_TLS_VERIFY ? new https.Agent({ rejectUnauthorized: false }) : undefined;
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`;
+64
View File
@@ -0,0 +1,64 @@
import { describe, it, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import axios from 'axios';
// gitea.js reads env vars at module load time (ESM cache), so we test
// the actual values baked in at import time and verify behavior via axios mocks.
afterEach(() => mock.restoreAll());
describe('gitea', async () => {
const { getPRDiff, postComment } = await import('./gitea.js');
it('getPRDiff calls Gitea diff API with Authorization header', async () => {
let capturedUrl, capturedOpts;
mock.method(axios, 'get', async (url, opts) => {
capturedUrl = url;
capturedOpts = opts;
return { data: 'diff content' };
});
const result = await getPRDiff();
assert.equal(result, 'diff content');
assert.ok(capturedUrl.includes('/api/v1/repos/'));
assert.ok(capturedUrl.endsWith('.diff'));
assert.ok(capturedOpts.headers['Authorization'].startsWith('token '));
assert.equal(capturedOpts.headers['Content-Type'], 'application/json');
});
it('postComment calls Gitea issues comments API with body', async () => {
let capturedUrl, capturedBody, capturedOpts;
mock.method(axios, 'post', async (url, body, opts) => {
capturedUrl = url;
capturedBody = body;
capturedOpts = opts;
return { data: { id: 1 } };
});
const result = await postComment('hello world');
assert.deepEqual(result, { id: 1 });
assert.ok(capturedUrl.includes('/api/v1/repos/'));
assert.ok(capturedUrl.endsWith('/comments'));
assert.equal(capturedBody.body, 'hello world');
assert.ok(capturedOpts.headers['Authorization'].startsWith('token '));
});
it('does not set httpsAgent by default (GITEA_SKIP_TLS_VERIFY not true)', async () => {
let capturedOpts;
mock.method(axios, 'get', async (_url, opts) => {
capturedOpts = opts;
return { data: '' };
});
await getPRDiff();
// httpsAgent is undefined when GITEA_SKIP_TLS_VERIFY !== 'true'
assert.equal(capturedOpts.httpsAgent, undefined);
});
it('getPRDiff propagates axios errors', async () => {
mock.method(axios, 'get', async () => { throw new Error('network error'); });
await assert.rejects(() => getPRDiff(), /network error/);
});
it('postComment propagates axios errors', async () => {
mock.method(axios, 'post', async () => { throw new Error('api error'); });
await assert.rejects(() => postComment('test'), /api error/);
});
});
-2
View File
@@ -11,7 +11,6 @@ export async function chat(systemPrompt, userContent) {
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
const shuffled = [...apiKeys].sort(() => Math.random() - 0.5);
let lastError;
for (let i = 0; i < shuffled.length; i++) {
if (provider !== 'ollama') headers['Authorization'] = `Bearer ${shuffled[i]}`;
try {
@@ -22,7 +21,6 @@ export async function chat(systemPrompt, userContent) {
);
return resp.data.choices[0].message.content;
} catch (e) {
lastError = e;
console.log(` [LLM] key[${i + 1}/${shuffled.length}] 失敗: ${e.message}`);
}
}
+6 -6
View File
@@ -52,13 +52,13 @@ async function main() {
// Step2: 各角色分析 diff 產生新 findings
console.log('\n📊 Step2: Findings 產生');
const results = await Promise.allSettled(roles.map(role => analyzeWithRole(role, diff)));
const newFindings = [];
for (const role of roles) {
try {
const found = await analyzeWithRole(role, diff);
newFindings.push(...found);
} catch (e) {
console.log(` ⚠️ [${role.name}] 分析失敗(跳過): ${e.message}`);
for (let i = 0; i < results.length; i++) {
if (results[i].status === 'fulfilled') {
newFindings.push(...results[i].value);
} else {
console.log(` ⚠️ [${roles[i].name}] 分析失敗(跳過): ${results[i].reason?.message}`);
}
}
console.log(` Step2 完成: 新 findings 總計 ${newFindings.length}`);
+1 -1
View File
@@ -3,7 +3,7 @@
"version": "1.0.0",
"type": "module",
"scripts": {
"test": "node --test git.test.js config.test.js llm.test.js"
"test": "node --test *.test.js"
},
"dependencies": {
"axios": "^1.6.7",
+2 -1
View File
@@ -1,8 +1,9 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import yaml from 'js-yaml';
const ROLES_DIR = '/action/app/prompts/roles';
const ROLES_DIR = path.join(fileURLToPath(import.meta.url), '..', 'prompts', 'roles');
export function loadRoles() {
return fs.readdirSync(ROLES_DIR)