Files
code-review/README.md
T

8.7 KiB
Raw Blame History

簡介

這是一個 AI Code Review Action。Gitea Workflow 可以使用此 Action 讓 AI 助理根據不同面向分析 Push Request 中變更的內容後,將問題分級 Commnet 到 Push Request 中。

流程(新 Push Request、新 Commit (排除 AI 助理的 Commit) 觸發)

  1. 服務名稱、模型名稱、角色資訊(個性、符合個性的英文名稱、工作內容),Comment 到 Push Request
  2. 每個角色個別分析 Git Diff 的內容產生新問題表格(問題等級、角色名稱、問題位置或行數、修改建議)
  3. 讀取所有未解決的舊問題(問題檔案 .gitea/ai-review/findings.json 存在於使用此 Action 的專案固定位置)加上新問題後,去除重複產生本次 Push Request 的問題表格(PR問題表格)覆蓋問題檔案
  4. 讀取排除問題檔案(.gitea/ai-review/exclusions.json 存在於使用此 Action 的專案固定位置),用來過濾PR問題表格中不需要處理的問題
  5. 從PR問題表格中取出所有舊問題,依照等級排序後 Comment 到 Push Request
  6. 從PR問題表格中取出所有新問題,排除嚴重等級的問題後 Comment 到 Push Request
  7. 從PR問題表格中取出所有新問題,將每個嚴重等級的問題 Comment 到 Push Request
  8. Commit 問題檔案
  9. 如果PR問題表格中有嚴重問題,則不要讓 workflow 執行成功(exit 1)

設計

  1. Gitea 的相關參數如果 inputs 沒有定義,則從 ${{ gitea.* }} 取得
  2. BASE_URL 如果 inputs 沒有定義,則使用預設值
  3. Comment 加上些許 emoji 讓資訊有點活力
  4. 盡量將應用程式放在 ./app,修改 entrypoint.sh 與 Dockerfile 讓程式可以正常運行
  5. 將提示詞放到 ./app/prompts 內供程式讀取
  6. API Key 支援逗號分隔傳入多個,隨機順序各嘗試一次,全部失敗則 exit 1
  7. 讀取 Git Diff 時排除 .gitea/ 資料夾內的所有檔案,避免 AI 分析 workflow 設定等非業務程式碼
  8. 階段七驗證 findings.jsonexclusions.json 是否為合法 JSON 格式,格式錯誤時先嘗試透過 AI 修正內容,再重新驗證;修正後仍不合法才 exit 1;之後才檢查檔案是否存在,不存在則建立並寫入 []
  9. 傳給 AI 的 findings 只保留必要欄位(level、role、location、suggestion),排除 is_new 等內部欄位;system prompt 精簡為指令核心;exclusions hint 只傳 location 與 suggestion,減少 token 用量

使用說明

  1. 在 Gitea 專案中建立 .gitea/workflows 資料夾
  2. .gitea/workflows 資料夾中建立 `ai-review.yaml'
  3. ai-review.yaml 中填入以下內容(選擇一個使用)

權限說明:此 Action 需要 contents: write(寫入 findings.json)、pull-requests: write(發佈 PR comment)、issues: write(發佈 issue comment)三項權限,為正常運作所必要,無法縮減。

1. OpenAI

name: AI
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref }}
  cancel-in-progress: true
on:
  pull_request:
    branches-ignore:
    - master
    types: [opened, synchronize]
jobs:
  code-review:
    name: Code Review
    runs-on: ubuntu
    steps:
    - name: AI Code Review
      uses: https://gitea.jsc.idv.tw/actions/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
      with:
        OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}  # 支援逗號分隔多個 Key
        OPENAI_BASE_URL: https://api.openai.com/v1
        OPENAI_MODEL: ${{ vars.OPENAI_MODEL }}
    permissions:
      contents: write
      pull-requests: write
      issues: write

2. OpenRouter

name: AI
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref }}
  cancel-in-progress: true
on:
  pull_request:
    branches-ignore:
    - master
    types: [opened, synchronize]
jobs:
  code-review:
    name: Code Review
    runs-on: ubuntu
    steps:
    - name: AI Code Review
      uses: https://gitea.jsc.idv.tw/actions/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
      with:
        OPENAI_API_KEY: ${{ secrets.OPENROUTER_API_KEY }},${{ secrets.OPENROUTER_API_KEY_1 }}
        OPENAI_BASE_URL: https://openrouter.ai/api/v1
        OPENAI_MODEL: ${{ vars.OPENROUTER_MODEL }}
    permissions:
      contents: write
      pull-requests: write
      issues: write

3. Anthropic Claude

name: AI
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref }}
  cancel-in-progress: true
on:
  pull_request:
    branches-ignore:
    - master
    types: [opened, synchronize]
jobs:
  code-review:
    name: Code Review
    runs-on: ubuntu
    steps:
    - name: AI Code Review
      uses: https://gitea.jsc.idv.tw/actions/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
      with:
        CLAUDE_API_KEY: ${{ secrets.CLAUDE_API_KEY }}  # 支援逗號分隔多個 Key
        CLAUDE_BASE_URL: https://api.anthropic.com/v1
    permissions:
      contents: write
      pull-requests: write
      issues: write

4. Google Gemini

name: AI
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref }}
  cancel-in-progress: true
on:
  pull_request:
    branches-ignore:
    - master
    types: [opened, synchronize]
jobs:
  code-review:
    name: Code Review
    runs-on: ubuntu
    steps:
    - name: AI Code Review
      uses: https://gitea.jsc.idv.tw/actions/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 }},${{ 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:
      contents: write
      pull-requests: write
      issues: write

5. Amazon Q

name: AI
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref }}
  cancel-in-progress: true
on:
  pull_request:
    branches-ignore:
    - master
    types: [opened, synchronize]
jobs:
  code-review:
    name: Code Review
    runs-on: ubuntu
    steps:
    - name: AI Code Review
      uses: https://gitea.jsc.idv.tw/actions/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
      with:
        AMAZONQ_API_KEY: ${{ secrets.AMAZONQ_API_KEY }}  # 支援逗號分隔多個 Key
        AMAZONQ_BASE_URL: https://q.api.aws
    permissions:
      contents: write
      pull-requests: write
      issues: write

- Ollama

name: AI
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref }}
  cancel-in-progress: true
on:
  pull_request:
    branches-ignore:
    - master
    types: [opened, synchronize]
jobs:
  code-review:
    name: Code Review
    runs-on: ubuntu
    steps:
    - name: AI Code Review
        uses: https://gitea.jsc.idv.tw/actions/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
        with:
          OLLAMA_BASE_URL: https://ollama.jsc.idv.me/v1
          OLLAMA_MODEL: ${{ vars.OLLAMA_MODEL }}
    permissions:
      contents: write
      pull-requests: write
      issues: write

SkillTriage Findings

這份 skill 用來處理收到的 review 問題清單,流程是先合併成單一列表,再依嚴重度排序、重新編號,最後逐項修正或排除誤判。

規則

  1. 先把所有問題合併成一個列表。
  2. 依嚴重度排序:critical -> warning -> info
  3. 重新從 1 開始編號。
  4. 若問題屬於真實缺陷,就直接修正。
  5. 若問題是誤判,就加入 .gitea/ai-review/exclusions.json
  6. 修正時以最小安全變更為原則,必要時補測試。
  7. 修正後要重新確認問題是否仍存在。

使用方式

在 Codex 裡直接用 $triage-findings 觸發,或直接描述你的需求,例如:

  1. 把收到的 review 問題貼給它。
  2. 要求它先合併、排序、重新編號。
  3. 要求它逐項修正真實問題。
  4. 如果某項是誤判,要求它加入 .gitea/ai-review/exclusions.json

例如:

  • 請幫我整理這批 review 問題,合併後依嚴重度排序並重新編號
  • 這些問題裡哪些是誤判?請幫我加入 exclusions
  • 請依照合併後的列表逐項修正問題

適用情境

  • 多張截圖或多段 review 結果需要整併
  • 問題清單需要重新排序與編號
  • 需要把誤判移出主要問題列表
  • 需要依清單逐項修正程式碼並補測試