本教程演示如何在 GitHub Actions 中使用 Cursor CLI 设置代码评审。该工作流程会分析 pull requests、定位问题,并以评论的形式发布反馈。
对于大多数用户,我们推荐使用 Bugbot。Bugbot 提供托管式的自动化代码评审,无需任何设置。采用 CLI 的方式更适合探索功能与进行高级自定义。
自动化代码审查演示:在拉取请求中显示内联评论

配置身份验证

在 GitHub Actions 中设置你的 API 密钥和仓库密钥,以对 Cursor CLI 进行身份验证。

设置代理权限

创建一个配置文件来控制代理能执行的操作,避免发生比如推送代码或创建 pull request 这类非预期操作。 在仓库根目录创建 .cursor/cli.json
{
  "permissions": {
    "deny": [
      "Shell(git push)",
      "Shell(gh pr create)",
      "Write(**)"
    ]
  }
}
这个配置允许代理读取文件并使用 GitHub CLI 发表评论,但会阻止它对你的仓库做任何更改。更多配置项见权限参考

构建 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 失败设置额外的工作流
  • 为不同分支配置不同的审查等级
  • 和团队现有的代码评审流程集成
  • 针对不同文件类型或目录自定义 agent 的行为