Compare commits

..

20 Commits

Author SHA1 Message Date
jiantw83 5ef9ab81ff fix: enhance suggestions in exclusions.json for clarity and accuracy; update filterFalsePositivesWithAI to accept exclusions 2026-05-12 04:20:35 +00:00
AI Review Bot 24ae565e38 chore: update ai-review findings [skip ci] 2026-05-12 04:06:08 +00:00
jiantw83 33c0357165 fix: update suggestions in exclusions.json for clarity and accuracy 2026-05-12 04:05:49 +00:00
AI Review Bot 4f631ef9b8 chore: update ai-review findings [skip ci] 2026-05-12 03:48:43 +00:00
jiantw83 76c5160915 fix: add additional suggestions for AI review exclusions in exclusions.json 2026-05-12 03:42:57 +00:00
AI Review Bot a479ccdd54 chore: update ai-review findings [skip ci] 2026-05-12 03:37:41 +00:00
jiantw83 c7a2a3cfc7 fix: update AI Code Review step to use OpenRouter API instead of OpenAI 2026-05-12 03:28:17 +00:00
jiantw83 cf8dd629b2 fix: update AI Code Review step to use OpenAI API instead of Gemini 2026-05-12 03:25:39 +00:00
jiantw83 2c59ce1bc1 fix: enhance error handling in filterFalsePositivesWithAI to check for empty array response 2026-05-12 03:23:44 +00:00
jiantw83 878d8a5bb4 fix: update OLLAMA_BASE_URL to a specific endpoint in README.md 2026-05-12 03:23:44 +00:00
AI Review Bot e9874e61fe chore: update ai-review findings [skip ci] 2026-05-12 03:19:47 +00:00
jiantw83 940d03bc6e fix: add suggestion for necessary permissions in README.md for Action functionality 2026-05-12 03:18:04 +00:00
jiantw83 23ceb84073 fix: add OPENAI_MODEL variable to OpenAI and OpenRouter job configurations 2026-05-12 03:17:06 +00:00
AI Review Bot c16e07bddd chore: update ai-review findings [skip ci] 2026-05-12 03:11:29 +00:00
jiantw83 66a75f135f refactor: remove unused API keys and configurations from action and config files 2026-05-12 03:08:08 +00:00
AI Review Bot d73d360051 chore: update ai-review findings [skip ci] 2026-05-12 03:07:13 +00:00
jiantw83 4b382b4183 fix: update README for OpenRouter API compatibility and add tests for LLM configuration 2026-05-12 03:04:12 +00:00
AI Review Bot f8e24844e8 chore: update ai-review findings [skip ci] 2026-05-12 03:01:32 +00:00
jiantw83 c2e56e4bb2 fix: update GEMINI model version in configuration and workflows 2026-05-12 02:59:22 +00:00
jiantw83 774b78d84e fix: update AI Code Review to use GEMINI API and base URL 2026-05-12 02:56:34 +00:00
9 changed files with 394 additions and 104 deletions
+18 -1
View File
@@ -6,6 +6,23 @@
},
{
"location": "app/git.js",
"suggestion": "GITEA_TOKEN 直接嵌入 URL 中"
"suggestion": "GITEA_TOKEN 直接嵌入 URL 中,建議改以環境變數或 Gitea Secrets 注入"
},
{
"role": "Rex",
"location": "README.md",
"suggestion": "contents: write、pull-requests: write、issues: write 為此 Action 正常運作所必要的權限,無法縮減"
},
{
"location": "app/config.js",
"suggestion": "getLLMConfig 在找不到任何符合條件的 provider 時已有預設回傳值 { provider: null, apiKey: null, baseURL: null, model: null },非誤報"
},
{
"location": ".gitea/ai-review/exclusions.json",
"suggestion": "exclusions.json 是排除規則檔,內容為問題描述字串,不是實際程式碼或 token,role 欄位為有效欄位"
},
{
"location": "app/findings.js",
"suggestion": "filterFalsePositivesWithAI 拋出的 Error 會被 catch 攔截並降級回傳原始 findings,不會中斷流程"
}
]
+247 -1
View File
@@ -1 +1,247 @@
[]
[
{
"level": "critical",
"role": "Aria",
"location": ".gitea/ai-review/exclusions.json",
"suggestion": "新增的條目包含 `role` 欄位,但目前的 JSON schema 只接受 `location` 與 `suggestion`,此欄位會導致驗證失敗,請移除或更新 schema。",
"is_new": false
},
{
"level": "critical",
"role": "Aria",
"location": ".gitea/ai-review/exclusions.json:12",
"suggestion": "移除 `role` 欄位,JSON schema 只接受 `location` 與 `suggestion`,保留這兩個欄位即可,避免驗證失敗。",
"is_new": false
},
{
"level": "warning",
"role": "Leo",
"location": "app/config.js:5",
"suggestion": "目前使用硬編碼的 `checks` 陣列來管理所有 LLM provider,未來若要新增或移除 provider 必須直接修改程式碼,易產生重複與維護負擔。建議將 provider 設定抽離至外部 JSON/YAML 檔或使用可擴充的資料結構,並在程式中載入,提升模組化與可維護性。",
"is_new": false
},
{
"level": "warning",
"role": "Rex",
"location": ".gitea/workflows/review.yaml:33",
"suggestion": "工作流程目前授予 contents、pullrequests、issues 三項 write 權限,過於寬鬆。建議依實際需求僅授予 read 或最小必要的 write 權限,以降低被濫用的風險。",
"is_new": false
},
{
"level": "warning",
"role": "Rex",
"location": ".gitea/workflows/review.yaml:35",
"suggestion": "將 OPENAI_API_KEY 參數改為使用正確的 secret 名稱(如 OPENROUTER_API_KEY)時,請確保工作流程文件中不會同時暴露兩個不同的 secret 名稱,以免因名稱錯誤導致金鑰未傳入或意外洩漏。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": ".gitea/workflows/review.yaml:33",
"suggestion": "在 `OPENAI_API_KEY` 後的註解前應保留一個空格,以符合常見的 YAML 註解風格:`... ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入`。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "README.md",
"suggestion": "文件中章節編號不連續(例如 `### 2. OpenRouter` 後直接跳到 `### 3. Anthropic Claude`),建議重新編號或使用一致的標題層級,以提升可讀性與維護性。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "app/config.js",
"suggestion": "`checks` 陣列的每一行過長,超過 120 個字元,建議拆成多行並對齊欄位,以符合程式碼可讀性與行長限制的慣例。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "app/config.js",
"suggestion": "`amazonq` 那一行的空格對齊與其他項目不一致,請統一使用單一個空格分隔欄位,或使用對齊工具保持列的垂直對齊。",
"is_new": false
},
{
"level": "warning",
"role": "Leo",
"location": "app/config.js:12",
"suggestion": "目前使用硬編碼的 `checks` 陣列管理所有 LLM provider,未來若要新增或移除 provider 必須直接修改程式碼,易產生重複與維護負擔。建議將 provider 設定抽離至外部 JSON/YAML 檔或使用可擴充的資料結構,於程式中載入,以提升模組化與可維護性。",
"is_new": false
},
{
"level": "warning",
"role": "Zara",
"location": "app/config.js:12",
"suggestion": "改用映射表(Map)或物件儲存 provider 設定,並以 O(1) 方式查找符合條件的項目,減少每次呼叫時的線性掃描成本。",
"is_new": false
},
{
"level": "warning",
"role": "Rex",
"location": ".gitea/workflows/review.yaml:33-35",
"suggestion": "工作流程目前授予 contents、pullrequests、issues 三項 write 權限過於寬鬆。建議依實際需求僅授予 read 或最小必要的 write 權限,降低被濫用的風險。",
"is_new": false
},
{
"level": "warning",
"role": "Rex",
"location": ".gitea/workflows/review.yaml:33",
"suggestion": "將 OPENAI_API_KEY 參數改為使用正確的 secret 名稱(如 OPENROUTER_API_KEY)時,請確保工作流程檔案中不會同時暴露兩個不同的 secret 名稱,以免因名稱錯誤導致金鑰未傳入或意外洩漏。",
"is_new": false
},
{
"level": "warning",
"role": "Rex",
"location": "app/config.js:12",
"suggestion": "在判斷 API 金鑰是否有效時,應先檢查 key 是否為非空字串,避免空字串被誤認為有效金鑰而發送請求。可改為 `if (key && key.trim() && baseURL) …`。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": ".gitea/ai-review/exclusions.json",
"suggestion": "檔案最後缺少換行符號,請在檔案結尾加入一個空白換行,以符合 POSIX 標準。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": ".gitea/ai-review/findings.json",
"suggestion": "檔案最後缺少換行符號,請在檔案結尾加入一個空白換行。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "app/config.js:12-13",
"suggestion": "`checks` 陣列每一行超過 120 個字元,請拆成多行並對齊欄位,以符合常見的行長限制。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "app/config.js:13",
"suggestion": "`amazonq` 那一行的空格對齊與其他項目不一致,請統一使用單一個空格分隔欄位或使用對齊工具保持列的垂直對齊。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "app/config.test.js",
"suggestion": "檔案最後缺少換行符號,請在檔案結尾加入空白換行。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "README.md:28",
"suggestion": "文件中章節編號不連續(例如 `### 2. OpenRouter` 後直接跳到 `### 3. Anthropic Claude`),請重新編號或使用自動編號方式,保持編號連續。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "README.md:42",
"suggestion": "標題層級使用不一致,部分章節使用 `### 1.`、`### 2.`,而後面的章節直接跳到 `### 3.`,建議統一使用相同層級的 Markdown 標題,並在每個標題後留一個空行以提升可讀性。",
"is_new": false
},
{
"level": "warning",
"role": "Aria",
"location": "action.yaml:72-99",
"suggestion": "大量移除的輸入欄位留下多行空白與註解,請整理檔案結構,移除不必要的空行與註解,保持檔案整潔。",
"is_new": false
},
{
"level": "info",
"role": "Zara",
"location": "app/config.js",
"suggestion": "對於未設定 API 金鑰的 provider(如 ollama),可提前返回快取的預設設定,避免每次呼叫都進行條件檢查。",
"is_new": false
},
{
"level": "info",
"role": "Rex",
"location": "README.md:28",
"suggestion": "文件中列出的權限說明應與實際 workflow 中的 permissions 保持一致,並提醒使用者僅在必要時授予 write 權限,避免在 CI/CD 環境中過度授權。",
"is_new": false
},
{
"level": "info",
"role": "Rex",
"location": "app/config.js:12",
"suggestion": "在取得 LLM 設定時,若 key 為空字串仍會被視為有效。建議在判斷前先檢查 key 是否為非空字串,以防止意外使用空的 API 金鑰發送請求。",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": "app/config.test.js",
"suggestion": "匯入語句過長,建議改寫為多行匯入,例如:\n```js\nimport {\n describe,\n it,\n beforeEach,\n afterEach\n} from 'node:test';\n```",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": "app/config.test.js",
"suggestion": "`ENV_KEYS` 陣列過長,建議分行列舉,每行放置一個環境變數,以提升可讀性。",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": "app/findings.js",
"suggestion": "錯誤訊息 `AI 回傳空陣列或非陣列` 可簡化為 `AI 回傳的結果不是有效的非空陣列`,讓訊息更清晰。",
"is_new": false
},
{
"level": "info",
"role": "Leo",
"location": "app/config.js:12",
"suggestion": "`checks` 陣列的每一行過長,超過 120 個字元,建議拆成多行並對齊欄位,以符合程式碼可讀性與行長限制的慣例。",
"is_new": false
},
{
"level": "info",
"role": "Leo",
"location": "app/findings.js:151",
"suggestion": "函式內使用 `console.log` 輸出過濾結果,可能在正式環境產生過多日誌。建議改以 logger 並提供可設定的 log level,或在非除錯模式下移除此輸出。",
"is_new": false
},
{
"level": "info",
"role": "Leo",
"location": "app/config.test.js:1",
"suggestion": "匯入語句過長,建議改寫為多行匯入,以提升可讀性,例如:\n```js\nimport {\n describe,\n it,\n beforeEach,\n afterEach\n} from 'node:test';\n```",
"is_new": false
},
{
"level": "info",
"role": "Leo",
"location": "app/config.test.js:9",
"suggestion": "`ENV_KEYS` 陣列過長,建議分行列舉,每行放置一個環境變數,以提升可讀性與維護性。",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": "app/config.test.js:1",
"suggestion": "匯入語句過長,建議改寫為多行匯入,例如:\n```js\nimport {\n describe,\n it,\n beforeEach,\n afterEach\n} from 'node:test';\n```",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": "app/config.test.js:7",
"suggestion": "`ENV_KEYS` 陣列過長,建議每行放置一個環境變數,分行列舉以提升可讀性。",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": ".gitea/workflows/review.yaml:33",
"suggestion": "在 `OPENAI_API_KEY` 後的註解前保留一個空格,以符合常見的 YAML 註解風格:`... ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入`。",
"is_new": false
}
]
+3 -2
View File
@@ -33,8 +33,9 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
with:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_BASE_URL: https://api.openai.com/v1
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入
OPENAI_BASE_URL: https://openrouter.ai/api/v1
OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
permissions:
contents: write
pull-requests: write
+5 -24
View File
@@ -44,6 +44,7 @@ jobs:
with:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_BASE_URL: https://api.openai.com/v1
OPENAI_MODEL: ${{ vars.OPENAI_MODEL }}
permissions:
contents: write
pull-requests: write
@@ -64,8 +65,9 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
with:
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入
OPENAI_BASE_URL: https://openrouter.ai/api/v1
OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
permissions:
contents: write
pull-requests: write
@@ -110,6 +112,7 @@ jobs:
with:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GEMINI_BASE_URL: https://generativelanguage.googleapis.com/v1beta
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
permissions:
contents: write
pull-requests: write
@@ -138,28 +141,6 @@ jobs:
issues: write
```
### 6. SonarQube
```yaml
name: AI
on:
pull_request:
types: [opened, synchronize]
jobs:
code-review:
name: 'Code Review'
runs-on: ubuntu
steps:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
with:
SONARQUBE_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
SONARQUBE_URL: https://sonarqube.example.com
permissions:
contents: write
pull-requests: write
issues: write
```
### - Ollama
```yaml
@@ -175,7 +156,7 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
with:
OLLAMA_BASE_URL: ${{ vars.OLLAMA_BASE_URL }}
OLLAMA_BASE_URL: https://ollama.jsc.idv.me/v1
OLLAMA_MODEL: ${{ vars.OLLAMA_MODEL }}
permissions:
contents: write
-58
View File
@@ -72,53 +72,7 @@ inputs:
description: 'Amazon Q Base URL'
required: false
# SonarQube
SONARQUBE_TOKEN:
description: 'SonarQube Token'
required: false
SONARQUBE_URL:
description: 'SonarQube URL'
required: false
# Kilo Code
KILO_API_KEY:
description: 'Kilo Code API Key'
required: false
KILO_BASE_URL:
description: 'Kilo Code Base URL'
required: false
# Roo Code
ROO_API_KEY:
description: 'Roo Code API Key'
required: false
ROO_BASE_URL:
description: 'Roo Code Base URL'
required: false
# Cline
CLINE_API_KEY:
description: 'Cline API Key'
required: false
CLINE_BASE_URL:
description: 'Cline Base URL'
required: false
# Continue
CONTINUE_API_KEY:
description: 'Continue API Key'
required: false
CONTINUE_BASE_URL:
description: 'Continue Base URL'
required: false
# Kade
KADE_API_KEY:
description: 'Kade API Key'
required: false
KADE_BASE_URL:
description: 'Kade Base URL'
required: false
runs:
using: 'docker'
@@ -145,15 +99,3 @@ runs:
OLLAMA_MODEL: ${{ inputs.OLLAMA_MODEL }}
AMAZONQ_API_KEY: ${{ inputs.AMAZONQ_API_KEY }}
AMAZONQ_BASE_URL: ${{ inputs.AMAZONQ_BASE_URL }}
SONARQUBE_TOKEN: ${{ inputs.SONARQUBE_TOKEN }}
SONARQUBE_URL: ${{ inputs.SONARQUBE_URL }}
KILO_API_KEY: ${{ inputs.KILO_API_KEY }}
KILO_BASE_URL: ${{ inputs.KILO_BASE_URL }}
ROO_API_KEY: ${{ inputs.ROO_API_KEY }}
ROO_BASE_URL: ${{ inputs.ROO_BASE_URL }}
CLINE_API_KEY: ${{ inputs.CLINE_API_KEY }}
CLINE_BASE_URL: ${{ inputs.CLINE_BASE_URL }}
CONTINUE_API_KEY: ${{ inputs.CONTINUE_API_KEY }}
CONTINUE_BASE_URL: ${{ inputs.CONTINUE_BASE_URL }}
KADE_API_KEY: ${{ inputs.KADE_API_KEY }}
KADE_BASE_URL: ${{ inputs.KADE_BASE_URL }}
+2 -7
View File
@@ -12,14 +12,9 @@ export function getLLMConfig() {
const checks = [
['openai', process.env.OPENAI_API_KEY, process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1', process.env.OPENAI_MODEL || 'gpt-4o-mini'],
['claude', process.env.CLAUDE_API_KEY, process.env.CLAUDE_BASE_URL || 'https://api.anthropic.com/v1', process.env.CLAUDE_MODEL || 'claude-3-haiku-20240307'],
['gemini', process.env.GEMINI_API_KEY, process.env.GEMINI_BASE_URL || 'https://generativelanguage.googleapis.com/v1beta', process.env.GEMINI_MODEL || 'gemini-1.5-flash'],
['gemini', process.env.GEMINI_API_KEY, process.env.GEMINI_BASE_URL || 'https://generativelanguage.googleapis.com/v1beta', process.env.GEMINI_MODEL || 'gemini-2.5-flash'],
['ollama', 'ollama', process.env.OLLAMA_BASE_URL, process.env.OLLAMA_MODEL],
['amazonq', process.env.AMAZONQ_API_KEY, process.env.AMAZONQ_BASE_URL || 'https://q.api.aws', process.env.OPENAI_MODEL || 'amazon-q'],
['kilo', process.env.KILO_API_KEY, process.env.KILO_BASE_URL || 'https://api.kilocode.com/v1', process.env.OPENAI_MODEL || 'kilo-default'],
['roo', process.env.ROO_API_KEY, process.env.ROO_BASE_URL || 'https://api.roocode.com/v1', process.env.OPENAI_MODEL || 'roo-default'],
['cline', process.env.CLINE_API_KEY, process.env.CLINE_BASE_URL || 'https://api.cline.dev/v1', process.env.OPENAI_MODEL || 'cline-default'],
['continue', process.env.CONTINUE_API_KEY, process.env.CONTINUE_BASE_URL || 'https://api.continue.dev/v1', process.env.OPENAI_MODEL || 'continue-default'],
['kade', process.env.KADE_API_KEY, process.env.KADE_BASE_URL || 'https://api.kade.dev/v1', process.env.OPENAI_MODEL || 'kade-default'],
['amazonq', process.env.AMAZONQ_API_KEY, process.env.AMAZONQ_BASE_URL || 'https://q.api.aws', process.env.AMAZONQ_MODEL || 'amazon-q'],
];
for (const [provider, key, baseURL, model] of checks) {
if (key && baseURL) return { provider, apiKey: key, baseURL, model };
+101
View File
@@ -0,0 +1,101 @@
import { describe, it, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { getLLMConfig } from './config.js';
const ENV_KEYS = [
'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL',
'CLAUDE_API_KEY', 'CLAUDE_BASE_URL', 'CLAUDE_MODEL',
'GEMINI_API_KEY', 'GEMINI_BASE_URL', 'GEMINI_MODEL',
'OLLAMA_BASE_URL', 'OLLAMA_MODEL',
'AMAZONQ_API_KEY', 'AMAZONQ_BASE_URL', 'AMAZONQ_MODEL',
];
let saved = {};
beforeEach(() => {
saved = {};
for (const k of ENV_KEYS) { saved[k] = process.env[k]; delete process.env[k]; }
});
afterEach(() => {
for (const k of ENV_KEYS) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
});
describe('getLLMConfig', () => {
it('returns null provider when no env vars set', () => {
const cfg = getLLMConfig();
assert.equal(cfg.provider, null);
assert.equal(cfg.apiKey, null);
});
it('detects openai with defaults', () => {
process.env.OPENAI_API_KEY = 'sk-test';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'openai');
assert.equal(cfg.apiKey, 'sk-test');
assert.equal(cfg.baseURL, 'https://api.openai.com/v1');
assert.equal(cfg.model, 'gpt-4o-mini');
});
it('detects openai with custom base url and model', () => {
process.env.OPENAI_API_KEY = 'sk-test';
process.env.OPENAI_BASE_URL = 'https://openrouter.ai/api/v1';
process.env.OPENAI_MODEL = 'gpt-4o';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'openai');
assert.equal(cfg.baseURL, 'https://openrouter.ai/api/v1');
assert.equal(cfg.model, 'gpt-4o');
});
it('detects gemini with defaults', () => {
process.env.GEMINI_API_KEY = 'gemini-key';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'gemini');
assert.equal(cfg.model, 'gemini-2.5-flash');
});
it('detects gemini with custom model', () => {
process.env.GEMINI_API_KEY = 'gemini-key';
process.env.GEMINI_MODEL = 'gemini-2.0-flash';
const cfg = getLLMConfig();
assert.equal(cfg.model, 'gemini-2.0-flash');
});
it('detects claude with defaults', () => {
process.env.CLAUDE_API_KEY = 'claude-key';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'claude');
assert.equal(cfg.model, 'claude-3-haiku-20240307');
});
it('detects amazonq with its own model env', () => {
process.env.AMAZONQ_API_KEY = 'aq-key';
process.env.AMAZONQ_MODEL = 'my-amazon-model';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'amazonq');
assert.equal(cfg.model, 'my-amazon-model');
});
it('openai takes priority over gemini when both set', () => {
process.env.OPENAI_API_KEY = 'sk-test';
process.env.GEMINI_API_KEY = 'gemini-key';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'openai');
});
it('empty string api key is treated as not set', () => {
process.env.OPENAI_API_KEY = '';
process.env.GEMINI_API_KEY = 'gemini-key';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'gemini');
});
it('detects ollama without api key', () => {
process.env.OLLAMA_BASE_URL = 'http://localhost:11434';
process.env.OLLAMA_MODEL = 'llama3';
const cfg = getLLMConfig();
assert.equal(cfg.provider, 'ollama');
assert.equal(cfg.model, 'llama3');
});
});
+17 -10
View File
@@ -117,43 +117,50 @@ export function loadExclusions(workspace) {
/**
* 套用排除規則,過濾掉符合排除條件的 findings
* 排除條件:role/location/suggestion 皆符合(省略的欄位視為萬用
* location 只比對檔案路徑(忽略行數),suggestion 省略時視為萬用
*/
export function applyExclusions(findings, exclusions) {
if (exclusions.length === 0) return findings;
const before = findings.length;
const filtered = findings.filter(f => !exclusions.some(ex =>
(!ex.role || ex.role === f.role) &&
(!ex.location || String(f.location).includes(ex.location)) &&
(!ex.suggestion || String(f.suggestion).includes(String(ex.suggestion).slice(0, 20)))
));
const filtered = findings.filter(f => !exclusions.some(ex => {
const fPath = String(f.location).split(':')[0];
const exPath = ex.location ? String(ex.location).split(':')[0] : null;
return (!exPath || fPath === exPath) &&
(!ex.role || ex.role === f.role);
}));
console.log(` 排除過濾: ${before} -> ${filtered.length} 筆(排除 ${before - filtered.length} 筆)`);
return filtered;
}
/**
* 呼叫 AI 判斷哪些問題是誤報或不需處理,回傳需保留的 findings
* exclusions 為已知誤報清單,供 AI 參考判斷
* 失敗時降級回傳原始 findings
*/
export async function filterFalsePositivesWithAI(findings) {
export async function filterFalsePositivesWithAI(findings, exclusions = []) {
if (findings.length === 0) return findings;
const exclusionHint = exclusions.length > 0
? `\n\n以下是已知的誤報或不需處理的問題清單(供參考,相同檔案路徑且語意相近的問題應一併排除):\n${JSON.stringify(exclusions, null, 2)}`
: '';
const systemPrompt = `你是一位資深程式碼審查專家,負責判斷審查問題是否為誤報或不需處理。
給你一份問題清單(JSON 陣列),每筆包含 level、role、location、suggestion。
請移除以下類型的問題:
1. 誤報:問題描述與實際程式碼不符(例如:程式碼已正確使用環境變數或 secrets,卻被標記為硬編碼敏感資料)
2. 不適用:問題在此專案情境下不需處理(例如:CI/CD action 本來就需要透過環境變數傳遞 token)
只回傳需要保留的問題 JSON 陣列,不要有其他文字。`;
3. 與已知誤報清單語意相近的問題(檔案路徑相同且建議內容相似)
只回傳需要保留的問題 JSON 陣列,不要有其他文字。${exclusionHint}`;
const userContent = `請判斷以下問題清單,移除誤報或不需處理的問題:\n\n${JSON.stringify(findings, null, 2)}`;
try {
const result = await chatJSON(systemPrompt, userContent);
if (Array.isArray(result)) {
if (Array.isArray(result) && result.length > 0) {
console.log(` AI 誤報過濾: ${findings.length} -> ${result.length}`);
return result;
}
throw new Error('AI 回傳非陣列');
throw new Error('AI 回傳空陣列或非陣列');
} catch (e) {
const status = e.response?.status;
if (status === 402 || status === 429) {
+1 -1
View File
@@ -85,7 +85,7 @@ async function main() {
console.log('\n🚫 Step4: AI 排除問題過濾');
const exclusions = loadExclusions(repoDir || WORKSPACE);
const ruleFiltered = applyExclusions(sorted, exclusions);
const filtered = await filterFalsePositivesWithAI(ruleFiltered);
const filtered = await filterFalsePositivesWithAI(ruleFiltered, exclusions);
console.log(` Step4 完成: findings total=${filtered.length}`);
// Step5: 寫入 findings.json,依序發布 comment