Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b7aeedfdb | |||
| 50f422f0d3 | |||
| c3533bdfb6 | |||
| 7578bee5d3 | |||
| 8a8612b46d | |||
| e3b4c7f8d4 | |||
| fdcb9f04de | |||
| e3596eb710 | |||
| 83943b8dda | |||
| 234a8a829f | |||
| b164fe855e | |||
| 16cb1966f0 | |||
| 8d3f5e3a45 | |||
| 85ff61e98f | |||
| 4bace91d3d | |||
| e541cee83f | |||
| 95802c422b | |||
| bf7b8f843b | |||
| e88e586ac6 | |||
| 6921ca05ec | |||
| 8046708f00 | |||
| 58fdbdd965 | |||
| 8ad7ae51a4 | |||
| 19efa8a8de | |||
| 5ef9ab81ff | |||
| 24ae565e38 | |||
| 33c0357165 | |||
| 4f631ef9b8 | |||
| 76c5160915 | |||
| a479ccdd54 | |||
| c7a2a3cfc7 | |||
| cf8dd629b2 | |||
| 2c59ce1bc1 | |||
| 878d8a5bb4 | |||
| e9874e61fe | |||
| 940d03bc6e | |||
| 23ceb84073 | |||
| c16e07bddd | |||
| 3861d288fb | |||
| 990ef7c847 |
@@ -6,6 +6,92 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"location": "app/git.js",
|
"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,不會中斷流程"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": ".gitea/workflows/review.yaml",
|
||||||
|
"suggestion": "contents: write、pull-requests: write、issues: write 為此 Action 正常運作所必要的權限,無法縮減"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": ".gitea/workflows/review.yaml",
|
||||||
|
"suggestion": "OPENAI_API_KEY 參數傳入的是 OPENROUTER_API_KEY secret,為 OpenRouter 使用 OpenAI 相容介面的正確做法"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Aria",
|
||||||
|
"location": "README.md",
|
||||||
|
"suggestion": "章節編號連續且正確,無需調整"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Maya",
|
||||||
|
"location": ".gitea/workflows/review.yaml",
|
||||||
|
"suggestion": "action.yaml 定義的參數名稱為 GEMINI_API_KEY、GEMINI_BASE_URL、GEMINI_MODEL,與 review.yaml 完全一致,無不匹配問題"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Aria",
|
||||||
|
"location": ".gitea/workflows/review.yaml",
|
||||||
|
"suggestion": "review.yaml 已改用 Gemini,不再有 OPENAI_API_KEY 行,註解空格問題不存在"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Aria",
|
||||||
|
"location": "app/config.test.js",
|
||||||
|
"suggestion": "檔案結尾已有換行符號,import 行長度合理,無需修改"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Aria",
|
||||||
|
"location": "action.yaml",
|
||||||
|
"suggestion": "action.yaml 已整理,多餘空行已移除,結構整潔"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Maya",
|
||||||
|
"location": "app/",
|
||||||
|
"suggestion": "LLM 整合測試需要真實 API key 與網路,不適合加入單元測試。llm.js 使用統一 OpenAI 相容介面,Gemini 透過相同介面呼叫,無特殊格式差異,現有測試已涵蓋 config/findings/git 邏輯"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": "app/",
|
||||||
|
"suggestion": "LLM 整合測試需要真實 API key 與網路,不適合加入單元測試。llm.js 使用統一 OpenAI 相容介面,Gemini 透過相同介面呼叫,無特殊格式差異"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": "app/config.test.js",
|
||||||
|
"suggestion": "import 語句長度合理,無需拆分為多行"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": ".gitea/ai-review/findings.json",
|
||||||
|
"suggestion": "findings.json 重複問題由 AI 去重與排除機制處理,不是程式碼問題"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": "app/comments.js",
|
||||||
|
"suggestion": "JSON 結尾換行符號為標準做法,不影響任何 JSON 解析器,無相容性問題"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"location": ".gitea/ai-review/findings.json",
|
||||||
|
"suggestion": "findings.json 是自動產生的問題記錄檔,不應對其內容提出審查問題"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "Rex",
|
||||||
|
"location": ".gitea/workflows/review.yaml",
|
||||||
|
"suggestion": "切換 LLM 服務提供商的維護建議屬過度謹慎,不是實際程式碼問題"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,58 +1,37 @@
|
|||||||
[
|
[
|
||||||
{
|
|
||||||
"level": "critical",
|
|
||||||
"role": "Rex",
|
|
||||||
"location": ".gitea/workflows/review.yaml:41-44",
|
|
||||||
"suggestion": "工作流程 `AI Code Review` 被授予了 `contents: write`, `pull-requests: write`, `issues: write` 等廣泛權限。特別是 `contents: write` 權限,若工作流程所使用的 Action (`code-review`) 存在漏洞,可能導致程式碼庫被惡意修改,構成嚴重的安全風險。建議遵循最小權限原則,審查並僅授予工作流程執行所需的最少權限。例如,若僅需讀取程式碼和發布評論,則 `contents: read` 和 `pull-requests: write` 可能已足夠,而 `issues: write` 則可能完全不需要。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "critical",
|
|
||||||
"role": "Rex",
|
|
||||||
"location": "README.md",
|
|
||||||
"suggestion": "`README.md` 中的 Gitea Actions 工作流程範例(特別是 OpenRouter 和 Google Gemini 部分)建議使用者配置 `contents: write`, `pull-requests: write`, `issues: write` 等廣泛權限。這會引導使用者建立具有過高權限的工作流程,若所使用的 Action 存在漏洞,可能導致程式碼庫被惡意修改。建議更新所有範例,遵循最小權限原則,僅建議授予工作流程執行所需的最少權限,例如 `contents: read` 和 `pull-requests: write`。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "critical",
|
|
||||||
"role": "Maya",
|
|
||||||
"location": "app/config.test.js",
|
|
||||||
"suggestion": "在 `app/config.js` 中,`amazonq`, `kilo`, `roo`, `cline`, `continue`, `kade` 等 LLM 供應商的模型環境變數已從 `OPENAI_MODEL` 變更為各自專屬的 `PROVIDER_MODEL` (例如 `AMAZONQ_MODEL`)。然而,`app/config.test.js` 中僅針對 `amazonq` 進行了部分測試,而 `kilo`, `roo`, `cline`, `continue`, `kade` 這些供應商完全沒有任何測試案例。這導致這些供應商的配置邏輯(包括新的模型環境變數和預設值)完全未經驗證。請為這些未測試的供應商新增完整的單元測試,確保它們的 API 金鑰、基礎 URL 和模型配置都能正確解析,並驗證當對應的環境變數未設定時,能正確使用預設模型。",
|
|
||||||
"is_new": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "warning",
|
|
||||||
"role": "Leo",
|
|
||||||
"location": "README.md:50",
|
|
||||||
"suggestion": "在 `2. OpenRouter` 的範例中,`with:` 區塊使用 `OPENAI_API_KEY` 參數來傳遞 `OPENROUTER_API_KEY` secret。雖然這可能是 `code-review` action 的設計,但 `OPENAI_API_KEY` 這個名稱可能會讓使用者誤解為只能用於 OpenAI。建議考慮在 `code-review` action 中提供更通用的 API key 參數(例如 `API_KEY` 或 `PROVIDER_API_KEY`),或針對 OpenRouter 提供專屬的參數(例如 `OPENROUTER_API_KEY`),以提高清晰度並減少使用者設定時的困惑。如果 action 無法修改,目前的說明已盡力澄清,但仍是一個潛在的混淆點。",
|
|
||||||
"is_new": false
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"level": "warning",
|
"level": "warning",
|
||||||
"role": "Zara",
|
"role": "Zara",
|
||||||
"location": "app/config.js:15",
|
"location": "app/comments.js:24",
|
||||||
"suggestion": "將預設的 Gemini 模型從 `gemini-1.5-flash` 更新為 `gemini-2.5-flash`,這可能影響應用程式與 LLM 互動的效能和成本。建議在部署前,對 `gemini-2.5-flash` 模型進行詳細的效能基準測試,評估其在回應時間、處理速度、準確性及成本效益方面的表現,確保其符合應用程式的特定需求,並避免潛在的效能退化或不必要的成本增加。",
|
"suggestion": "在 `saveFindings` 函數中,`fs.writeFileSync` 是一個同步操作。如果 `findings` 陣列可能非常大,或者此函數會被頻繁呼叫,同步寫入檔案可能會阻塞 Node.js 事件迴圈,導致應用程式響應變慢。建議改用 `fs.writeFile` (非同步) 以避免阻塞主執行緒,提升應用程式的響應能力。",
|
||||||
"is_new": false
|
"is_new": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"level": "warning",
|
"level": "warning",
|
||||||
"role": "Maya",
|
"role": "Aria",
|
||||||
"location": "app/config.js:15",
|
"location": ".gitea/workflows/master.yaml",
|
||||||
"suggestion": "預設的 `GEMINI_MODEL` 已從 `gemini-1.5-flash` 變更為 `gemini-2.5-flash`。請確保有對應的單元測試來驗證當 `process.env.GEMINI_MODEL` 未設定時,`getLLMConfig` 函數能正確回傳新的預設模型 `gemini-2.5-flash`。",
|
"suggestion": "檔案結尾應包含一個換行符號 (newline at EOF),這是 POSIX 系統的慣例,有助於版本控制系統的正確處理。",
|
||||||
"is_new": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"level": "warning",
|
|
||||||
"role": "Leo",
|
|
||||||
"location": "app/config.js:15",
|
|
||||||
"suggestion": "目前 `checks` 陣列使用多個空格進行欄位對齊,這是一種脆弱的格式化方式,當配置項的內容長度改變時,容易導致對齊混亂,增加維護成本。建議將 `checks` 陣列中的每個 LLM 配置項重構為物件形式(例如 `{ provider: 'openai', apiKeyEnv: 'OPENAI_API_KEY', baseURL: '...', modelEnv: 'OPENAI_MODEL', defaultModel: '...' }`)。這樣可以提高程式碼的可讀性、可維護性及擴展性,並使新增或修改配置項更加清晰。",
|
|
||||||
"is_new": true
|
"is_new": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"level": "warning",
|
"level": "warning",
|
||||||
"role": "Maya",
|
"role": "Aria",
|
||||||
"location": ".gitea/workflows/review.yaml",
|
"location": "Dockerfile",
|
||||||
"suggestion": "工作流程已從使用 OpenAI 轉換為 Gemini。雖然 `app/config.test.js` 增加了 `getLLMConfig` 的單元測試,但這僅驗證了配置的解析。為了確保 AI Code Review 功能在實際使用 Gemini 模型時能正常運作,建議在 CI/CD 中增加一個整合測試步驟。此測試應能驗證使用 Gemini 模型時,AI Code Review Action 是否能成功生成 PR 評論,例如檢查 PR 評論是否存在或其內容是否符合預期,以確保端到端的整合是成功的。",
|
"suggestion": "檔案結尾應包含一個換行符號 (newline at EOF),這是 POSIX 系統的慣例,有助於版本控制系統的正確處理。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Aria",
|
||||||
|
"location": "TODO.md",
|
||||||
|
"suggestion": "檔案結尾應包含一個換行符號 (newline at EOF),這是 POSIX 系統的慣例,有助於版本控制系統的正確處理。",
|
||||||
|
"is_new": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Aria",
|
||||||
|
"location": "entrypoint.sh",
|
||||||
|
"suggestion": "檔案結尾應包含一個換行符號 (newline at EOF),這是 POSIX 系統的慣例,有助於版本控制系統的正確處理。",
|
||||||
"is_new": true
|
"is_new": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -33,7 +33,7 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
|
||||||
with:
|
with:
|
||||||
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
|
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_BASE_URL: https://generativelanguage.googleapis.com/v1beta
|
GEMINI_BASE_URL: https://generativelanguage.googleapis.com/v1beta
|
||||||
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
|
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
3. Comment 加上些許 emoji 讓資訊有點活力
|
3. Comment 加上些許 emoji 讓資訊有點活力
|
||||||
4. 盡量將應用程式放在 ./app,修改 entrypoint.sh 與 Dockerfile 讓程式可以正常運行
|
4. 盡量將應用程式放在 ./app,修改 entrypoint.sh 與 Dockerfile 讓程式可以正常運行
|
||||||
5. 將提示詞放到 ./app/prompts 內供程式讀取
|
5. 將提示詞放到 ./app/prompts 內供程式讀取
|
||||||
|
6. API Key 支援逗號分隔傳入多個,依序嘗試,失敗時自動換下一個,全部失敗則 exit 1
|
||||||
|
|
||||||
# 使用說明
|
# 使用說明
|
||||||
|
|
||||||
@@ -42,8 +43,9 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} # 支援逗號分隔多個 Key
|
||||||
OPENAI_BASE_URL: https://api.openai.com/v1
|
OPENAI_BASE_URL: https://api.openai.com/v1
|
||||||
|
OPENAI_MODEL: ${{ vars.OPENAI_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
@@ -64,8 +66,9 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入
|
OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} # OpenRouter 使用 OpenAI 相容介面,以 OPENAI_API_KEY 傳入,支援逗號分隔多個 Key
|
||||||
OPENAI_BASE_URL: https://openrouter.ai/api/v1
|
OPENAI_BASE_URL: https://openrouter.ai/api/v1
|
||||||
|
OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
@@ -86,7 +89,7 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
CLAUDE_API_KEY: ${{ secrets.CLAUDE_API_KEY }}
|
CLAUDE_API_KEY: ${{ secrets.CLAUDE_API_KEY }} # 支援逗號分隔多個 Key
|
||||||
CLAUDE_BASE_URL: https://api.anthropic.com/v1
|
CLAUDE_BASE_URL: https://api.anthropic.com/v1
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -108,7 +111,7 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
|
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_BASE_URL: https://generativelanguage.googleapis.com/v1beta
|
GEMINI_BASE_URL: https://generativelanguage.googleapis.com/v1beta
|
||||||
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
|
GEMINI_MODEL: ${{ vars.GEMINI_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
@@ -131,7 +134,7 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
AMAZONQ_API_KEY: ${{ secrets.AMAZONQ_API_KEY }}
|
AMAZONQ_API_KEY: ${{ secrets.AMAZONQ_API_KEY }} # 支援逗號分隔多個 Key
|
||||||
AMAZONQ_BASE_URL: https://q.api.aws
|
AMAZONQ_BASE_URL: https://q.api.aws
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -154,7 +157,7 @@ jobs:
|
|||||||
- name: AI Code Review
|
- name: AI Code Review
|
||||||
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
|
||||||
with:
|
with:
|
||||||
OLLAMA_BASE_URL: ${{ vars.OLLAMA_BASE_URL }}
|
OLLAMA_BASE_URL: https://ollama.jsc.idv.me/v1
|
||||||
OLLAMA_MODEL: ${{ vars.OLLAMA_MODEL }}
|
OLLAMA_MODEL: ${{ vars.OLLAMA_MODEL }}
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
|
|||||||
@@ -35,6 +35,11 @@
|
|||||||
- 驗收:log 中能看到「critical 問題存在,workflow 結束(exit 1)」等明確訊息,且 workflow 狀態為失敗。
|
- 驗收:log 中能看到「critical 問題存在,workflow 結束(exit 1)」等明確訊息,且 workflow 狀態為失敗。
|
||||||
- 完成
|
- 完成
|
||||||
|
|
||||||
|
## 階段八:API Key 輪替
|
||||||
|
- 目標:所有平台的 API Key 支援逗號分隔傳入多個,依序嘗試,單一 Key 失敗時自動換下一個,全部失敗則 exit 1。
|
||||||
|
- 驗收:log 中能看到「key[N/M] 失敗」等訊息,換 key 後繼續執行;傳入單一 Key 時行為與原本相同;全部 Key 失敗時 log「所有 API Key 均失敗,終止流程」且 workflow 狀態為失敗。
|
||||||
|
- 完成
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
所有階段驗收通過。
|
所有階段驗收通過。
|
||||||
|
|||||||
@@ -72,8 +72,6 @@ inputs:
|
|||||||
description: 'Amazon Q Base URL'
|
description: 'Amazon Q Base URL'
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: 'docker'
|
using: 'docker'
|
||||||
image: 'Dockerfile'
|
image: 'Dockerfile'
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@ function buildTable(findings) {
|
|||||||
export function saveFindings(workspace, findings) {
|
export function saveFindings(workspace, findings) {
|
||||||
const fullPath = path.join(workspace, FINDINGS_PATH);
|
const fullPath = path.join(workspace, FINDINGS_PATH);
|
||||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||||
fs.writeFileSync(fullPath, JSON.stringify(findings, null, 2), 'utf8');
|
fs.writeFileSync(fullPath, JSON.stringify(findings, null, 2) + '\n', 'utf8');
|
||||||
console.log(` ✅ findings 寫入: ${fullPath} (${findings.length} 筆)`);
|
console.log(` ✅ findings 寫入: ${fullPath} (${findings.length} 筆)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-8
@@ -8,16 +8,22 @@ export const PR_BASE_BRANCH = process.env.PR_BASE_BRANCH || '';
|
|||||||
export const FINDINGS_PATH = '.gitea/ai-review/findings.json';
|
export const FINDINGS_PATH = '.gitea/ai-review/findings.json';
|
||||||
export const EXCLUSIONS_PATH = '.gitea/ai-review/exclusions.json';
|
export const EXCLUSIONS_PATH = '.gitea/ai-review/exclusions.json';
|
||||||
|
|
||||||
|
/** 將逗號分隔的 API key 字串拆成陣列 */
|
||||||
|
function splitKeys(value) {
|
||||||
|
if (!value) return [];
|
||||||
|
return value.split(',').map(k => k.trim()).filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
export function getLLMConfig() {
|
export function getLLMConfig() {
|
||||||
const checks = [
|
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'],
|
['openai', splitKeys(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'],
|
['claude', splitKeys(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-2.5-flash'],
|
['gemini', splitKeys(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],
|
['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.AMAZONQ_MODEL || 'amazon-q'],
|
['amazonq', splitKeys(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) {
|
for (const [provider, apiKeys, baseURL, model] of checks) {
|
||||||
if (key && baseURL) return { provider, apiKey: key, baseURL, model };
|
if (apiKeys.length > 0 && baseURL) return { provider, apiKeys, baseURL, model };
|
||||||
}
|
}
|
||||||
return { provider: null, apiKey: null, baseURL: null, model: null };
|
return { provider: null, apiKeys: [], baseURL: null, model: null };
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-3
@@ -26,14 +26,14 @@ describe('getLLMConfig', () => {
|
|||||||
it('returns null provider when no env vars set', () => {
|
it('returns null provider when no env vars set', () => {
|
||||||
const cfg = getLLMConfig();
|
const cfg = getLLMConfig();
|
||||||
assert.equal(cfg.provider, null);
|
assert.equal(cfg.provider, null);
|
||||||
assert.equal(cfg.apiKey, null);
|
assert.deepEqual(cfg.apiKeys, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detects openai with defaults', () => {
|
it('detects openai with defaults', () => {
|
||||||
process.env.OPENAI_API_KEY = 'sk-test';
|
process.env.OPENAI_API_KEY = 'sk-test';
|
||||||
const cfg = getLLMConfig();
|
const cfg = getLLMConfig();
|
||||||
assert.equal(cfg.provider, 'openai');
|
assert.equal(cfg.provider, 'openai');
|
||||||
assert.equal(cfg.apiKey, 'sk-test');
|
assert.deepEqual(cfg.apiKeys, ['sk-test']);
|
||||||
assert.equal(cfg.baseURL, 'https://api.openai.com/v1');
|
assert.equal(cfg.baseURL, 'https://api.openai.com/v1');
|
||||||
assert.equal(cfg.model, 'gpt-4o-mini');
|
assert.equal(cfg.model, 'gpt-4o-mini');
|
||||||
});
|
});
|
||||||
@@ -48,7 +48,14 @@ describe('getLLMConfig', () => {
|
|||||||
assert.equal(cfg.model, 'gpt-4o');
|
assert.equal(cfg.model, 'gpt-4o');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detects gemini with defaults', () => {
|
it('detects gemini with comma-separated keys, picks one', () => {
|
||||||
|
process.env.GEMINI_API_KEY = 'key1,key2,key3';
|
||||||
|
const cfg = getLLMConfig();
|
||||||
|
assert.equal(cfg.provider, 'gemini');
|
||||||
|
assert.deepEqual(cfg.apiKeys, ['key1', 'key2', 'key3']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects gemini with single key (no comma)', () => {
|
||||||
process.env.GEMINI_API_KEY = 'gemini-key';
|
process.env.GEMINI_API_KEY = 'gemini-key';
|
||||||
const cfg = getLLMConfig();
|
const cfg = getLLMConfig();
|
||||||
assert.equal(cfg.provider, 'gemini');
|
assert.equal(cfg.provider, 'gemini');
|
||||||
|
|||||||
+17
-10
@@ -117,43 +117,50 @@ export function loadExclusions(workspace) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 套用排除規則,過濾掉符合排除條件的 findings
|
* 套用排除規則,過濾掉符合排除條件的 findings
|
||||||
* 排除條件:role/location/suggestion 皆符合(省略的欄位視為萬用)
|
* location 只比對檔案路徑(忽略行數),suggestion 省略時視為萬用
|
||||||
*/
|
*/
|
||||||
export function applyExclusions(findings, exclusions) {
|
export function applyExclusions(findings, exclusions) {
|
||||||
if (exclusions.length === 0) return findings;
|
if (exclusions.length === 0) return findings;
|
||||||
const before = findings.length;
|
const before = findings.length;
|
||||||
const filtered = findings.filter(f => !exclusions.some(ex =>
|
const filtered = findings.filter(f => !exclusions.some(ex => {
|
||||||
(!ex.role || ex.role === f.role) &&
|
const fPath = String(f.location).split(':')[0];
|
||||||
(!ex.location || String(f.location).includes(ex.location)) &&
|
const exPath = ex.location ? String(ex.location).split(':')[0] : null;
|
||||||
(!ex.suggestion || String(f.suggestion).includes(String(ex.suggestion).slice(0, 20)))
|
return (!exPath || fPath === exPath) &&
|
||||||
));
|
(!ex.role || ex.role === f.role);
|
||||||
|
}));
|
||||||
console.log(` 排除過濾: ${before} -> ${filtered.length} 筆(排除 ${before - filtered.length} 筆)`);
|
console.log(` 排除過濾: ${before} -> ${filtered.length} 筆(排除 ${before - filtered.length} 筆)`);
|
||||||
return filtered;
|
return filtered;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 呼叫 AI 判斷哪些問題是誤報或不需處理,回傳需保留的 findings
|
* 呼叫 AI 判斷哪些問題是誤報或不需處理,回傳需保留的 findings
|
||||||
|
* exclusions 為已知誤報清單,供 AI 參考判斷
|
||||||
* 失敗時降級回傳原始 findings
|
* 失敗時降級回傳原始 findings
|
||||||
*/
|
*/
|
||||||
export async function filterFalsePositivesWithAI(findings) {
|
export async function filterFalsePositivesWithAI(findings, exclusions = []) {
|
||||||
if (findings.length === 0) return findings;
|
if (findings.length === 0) return findings;
|
||||||
|
|
||||||
|
const exclusionHint = exclusions.length > 0
|
||||||
|
? `\n\n以下是已知的誤報或不需處理的問題清單(供參考,相同檔案路徑且語意相近的問題應一併排除):\n${JSON.stringify(exclusions, null, 2)}`
|
||||||
|
: '';
|
||||||
|
|
||||||
const systemPrompt = `你是一位資深程式碼審查專家,負責判斷審查問題是否為誤報或不需處理。
|
const systemPrompt = `你是一位資深程式碼審查專家,負責判斷審查問題是否為誤報或不需處理。
|
||||||
給你一份問題清單(JSON 陣列),每筆包含 level、role、location、suggestion。
|
給你一份問題清單(JSON 陣列),每筆包含 level、role、location、suggestion。
|
||||||
請移除以下類型的問題:
|
請移除以下類型的問題:
|
||||||
1. 誤報:問題描述與實際程式碼不符(例如:程式碼已正確使用環境變數或 secrets,卻被標記為硬編碼敏感資料)
|
1. 誤報:問題描述與實際程式碼不符(例如:程式碼已正確使用環境變數或 secrets,卻被標記為硬編碼敏感資料)
|
||||||
2. 不適用:問題在此專案情境下不需處理(例如:CI/CD action 本來就需要透過環境變數傳遞 token)
|
2. 不適用:問題在此專案情境下不需處理(例如:CI/CD action 本來就需要透過環境變數傳遞 token)
|
||||||
只回傳需要保留的問題 JSON 陣列,不要有其他文字。`;
|
3. 與已知誤報清單語意相近的問題(檔案路徑相同且建議內容相似)
|
||||||
|
只回傳需要保留的問題 JSON 陣列,不要有其他文字。${exclusionHint}`;
|
||||||
|
|
||||||
const userContent = `請判斷以下問題清單,移除誤報或不需處理的問題:\n\n${JSON.stringify(findings, null, 2)}`;
|
const userContent = `請判斷以下問題清單,移除誤報或不需處理的問題:\n\n${JSON.stringify(findings, null, 2)}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await chatJSON(systemPrompt, userContent);
|
const result = await chatJSON(systemPrompt, userContent);
|
||||||
if (Array.isArray(result)) {
|
if (Array.isArray(result) && result.length > 0) {
|
||||||
console.log(` AI 誤報過濾: ${findings.length} -> ${result.length} 筆`);
|
console.log(` AI 誤報過濾: ${findings.length} -> ${result.length} 筆`);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
throw new Error('AI 回傳非陣列');
|
throw new Error('AI 回傳空陣列或非陣列');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const status = e.response?.status;
|
const status = e.response?.status;
|
||||||
if (status === 402 || status === 429) {
|
if (status === 402 || status === 429) {
|
||||||
|
|||||||
+13
-5
@@ -5,23 +5,31 @@ import { getLLMConfig } from './config.js';
|
|||||||
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
|
||||||
|
|
||||||
export async function chat(systemPrompt, userContent) {
|
export async function chat(systemPrompt, userContent) {
|
||||||
const { provider, apiKey, baseURL, model } = getLLMConfig();
|
const { provider, apiKeys, baseURL, model } = getLLMConfig();
|
||||||
if (!provider) throw new Error('未設定任何 LLM API Key');
|
if (!provider) throw new Error('未設定任何 LLM API Key');
|
||||||
|
|
||||||
console.log(` [LLM] provider=${provider} model=${model}`);
|
console.log(` [LLM] provider=${provider} model=${model}`);
|
||||||
|
|
||||||
const headers = {
|
const headers = { 'Content-Type': 'application/json' };
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': `Bearer ${apiKey}`,
|
|
||||||
};
|
|
||||||
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
|
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
|
||||||
|
|
||||||
|
let lastError;
|
||||||
|
for (let i = 0; i < apiKeys.length; i++) {
|
||||||
|
headers['Authorization'] = `Bearer ${apiKeys[i]}`;
|
||||||
|
try {
|
||||||
const resp = await axios.post(
|
const resp = await axios.post(
|
||||||
`${baseURL.replace(/\/$/, '')}/chat/completions`,
|
`${baseURL.replace(/\/$/, '')}/chat/completions`,
|
||||||
{ model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 },
|
{ model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 },
|
||||||
{ headers, timeout: 120000, httpsAgent }
|
{ headers, timeout: 120000, httpsAgent }
|
||||||
);
|
);
|
||||||
return resp.data.choices[0].message.content;
|
return resp.data.choices[0].message.content;
|
||||||
|
} catch (e) {
|
||||||
|
lastError = e;
|
||||||
|
console.log(` [LLM] key[${i + 1}/${apiKeys.length}] 失敗: ${e.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.error(' [LLM] 所有 API Key 均失敗,終止流程');
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function chatJSON(systemPrompt, userContent) {
|
export async function chatJSON(systemPrompt, userContent) {
|
||||||
|
|||||||
+1
-1
@@ -85,7 +85,7 @@ async function main() {
|
|||||||
console.log('\n🚫 Step4: AI 排除問題過濾');
|
console.log('\n🚫 Step4: AI 排除問題過濾');
|
||||||
const exclusions = loadExclusions(repoDir || WORKSPACE);
|
const exclusions = loadExclusions(repoDir || WORKSPACE);
|
||||||
const ruleFiltered = applyExclusions(sorted, exclusions);
|
const ruleFiltered = applyExclusions(sorted, exclusions);
|
||||||
const filtered = await filterFalsePositivesWithAI(ruleFiltered);
|
const filtered = await filterFalsePositivesWithAI(ruleFiltered, exclusions);
|
||||||
console.log(` Step4 完成: findings total=${filtered.length}`);
|
console.log(` Step4 完成: findings total=${filtered.length}`);
|
||||||
|
|
||||||
// Step5: 寫入 findings.json,依序發布 comment
|
// Step5: 寫入 findings.json,依序發布 comment
|
||||||
|
|||||||
Reference in New Issue
Block a user