本教學示範如何在 GitHub Actions 中使用 Cursor CLI 設定程式碼審查。此工作流程會分析 Pull Request、找出問題,並以留言的方式發布回饋。
對多數使用者,我們建議改用 Bugbot。Bugbot 提供託管的自動化程式碼審查,無需設定。這種 CLI 作法適合探索功能與進階自訂。
自動化程式碼審查進行中,於 Pull Request 中顯示行內註解

設定驗證

在 GitHub Actions 中設定你的 API 金鑰和儲存庫機密來讓 Cursor CLI 完成驗證。

設定代理權限

建立設定檔來控管代理可以執行的動作,避免發生像推送程式碼或建立 pull request 這類非預期的操作。 在你的儲存庫根目錄建立 .cursor/cli.json
{
  "permissions": {
    "deny": [
      "Shell(git push)",
      "Shell(gh pr create)",
      "Write(**)"
    ]
  }
}
這個設定允許代理讀取檔案並使用 GitHub CLI 發表留言,但會阻止它對你的儲存庫進行任何變更。更多設定選項請參考 permissions reference

建立 GitHub Actions 工作流程

現在我們來一步步建立這個工作流程。

設定工作流程觸發條件

建立 .github/workflows/cursor-code-review.yml,並設定在 pull request 事件時執行:
name: Cursor Code Review

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  code-review:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    
    steps:

取出儲存庫

加入取出步驟以存取 pull request 的程式碼:
- name: Checkout repository
  uses: actions/checkout@v4
  with:
    fetch-depth: 0
    ref: ${{ github.event.pull_request.head.sha }}

安裝 Cursor CLI

加入 CLI 安裝步驟:
- name: Install Cursor CLI
  run: |
    curl https://cursor.com/install -fsS | bash
    echo "$HOME/.cursor/bin" >> $GITHUB_PATH

設定審查代理

在實作完整的審查步驟之前,先理解我們的審查提示結構。以下說明代理應有的行為: 目標: 我們要代理審查目前的 PR diff,只標註明確且高嚴重性的問題,僅在變更的行留下非常精簡的行內評論(1–2 句),最後附上簡短摘要,以維持良好的訊噪比。 格式: 評論要短而精準。為了便於掃讀會使用表情符號,並在最後附上一則高層級的整體審查摘要。 提交: 審查完成後,代理要根據審查結果附上一則簡短評論。代理應提交一次審查,包含行內評論與精煉摘要。 邊界情況: 我們需要處理:
  • 既有評論已被解決:當問題已被處理時,代理應標示為已解決
  • 避免重複:若相似回饋已存在於相同或相鄰行,代理應略過不再留言
最終提示: 完整提示結合以上行為要求,產出聚焦且可行的回饋 現在來實作審查代理步驟:
- name: Perform code review
  env:
    CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
    GH_TOKEN: ${{ github.token }}
  run: |
    cursor-agent --force --model "$MODEL" --output-format=text --print "You are operating in a GitHub Actions runner performing automated code review. The gh CLI is available and authenticated via GH_TOKEN. You may comment on pull requests.
    
    Context:
    - Repo: ${{ github.repository }}
    - PR Number: ${{ github.event.pull_request.number }}
    - PR Head SHA: ${{ github.event.pull_request.head.sha }}
    - PR Base SHA: ${{ github.event.pull_request.base.sha }}
    
    Objectives:
    1) Re-check existing review comments and reply resolved when addressed
    2) Review the current PR diff and flag only clear, high-severity issues
    3) Leave very short inline comments (1-2 sentences) on changed lines only and a brief summary at the end
    
    Procedure:
    - Get existing comments: gh pr view --json comments
    - Get diff: gh pr diff
    - If a previously reported issue appears fixed by nearby changes, reply: ✅ This issue appears to be resolved by the recent changes
    - Avoid duplicates: skip if similar feedback already exists on or near the same lines
    
    Commenting rules:
    - Max 10 inline comments total; prioritize the most critical issues
    - One issue per comment; place on the exact changed line
    - Natural tone, specific and actionable; do not mention automated or high-confidence
    - Use emojis: 🚨 Critical 🔒 Security ⚡ Performance ⚠️ Logic ✅ Resolved ✨ Improvement
    
    Submission:
    - Submit one review containing inline comments plus a concise summary
    - Use only: gh pr review --comment
    - Do not use: gh pr review --approve or --request-changes"
.
├── .cursor/
│   └── cli.json
├── .github/
│   └── workflows/
│       └── cursor-code-review.yml

測試你的審查員

建立一個測試 pull request,確認工作流程正常運作,且代理會張貼帶有表情符號回饋的審查留言。
Pull request 顯示在特定程式行的自動化審查留言,包含表情符號與行內回饋

下一步

你現在已經有一個可運作的自動化程式碼審查系統。可以考慮以下強化項目:
  • 設定額外的工作流程來修復 CI 失敗
  • 為不同分支設定不同的審查層級
  • 與你團隊既有的程式碼審查流程整合
  • 針對不同檔案類型或目錄自訂代理的行為