autofix

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CodeRabbit Autofix

CodeRabbit 自动修复工具

Fetch CodeRabbit review comments for your current branch's PR and fix them interactively or in batch.
拉取当前分支对应PR的CodeRabbit评审意见,支持交互式或批量修复。

Prerequisites

前置条件

Required Tools

所需工具

  • gh
    (GitHub CLI) - Installation guide
  • git
Verify:
gh auth status
  • gh
    (GitHub CLI) - 安装指南
  • git
验证命令:
gh auth status

Required State

所需状态

  • Git repo on GitHub
  • Current branch has open PR
  • PR reviewed by CodeRabbit bot (
    coderabbitai
    ,
    coderabbit[bot]
    ,
    coderabbitai[bot]
    )
  • GitHub上的Git仓库
  • 当前分支存在未关闭的PR
  • PR已被CodeRabbit机器人评审(
    coderabbitai
    coderabbit[bot]
    coderabbitai[bot]

Workflow

工作流

Step 0: Load Repository Instructions (
AGENTS.md
)

步骤0:加载仓库说明(
AGENTS.md

Before any autofix actions, search for
AGENTS.md
in the current repository and load applicable instructions.
  • If found, follow its build/lint/test/commit guidance throughout the run.
  • If not found, continue with default workflow.
执行任何自动修复操作前,先在当前仓库搜索
AGENTS.md
并加载适用的说明。
  • 若找到该文件:全程遵循其中的构建/代码检查/测试/提交指引
  • 若未找到:继续使用默认工作流

Step 1: Check Code Push Status

步骤1:检查代码推送状态

Check:
git status
+ check for unpushed commits
If uncommitted changes:
  • Warn: "⚠️ Uncommitted changes won't be in CodeRabbit review"
  • Ask: "Commit and push first?" → If yes: wait for user action, then continue
If unpushed commits:
  • Warn: "⚠️ N unpushed commits. CodeRabbit hasn't reviewed them"
  • Ask: "Push now?" → If yes:
    git push
    , inform "CodeRabbit will review in ~5 min", EXIT skill
Otherwise: Proceed to Step 2
检查命令:
git status
+ 检查未推送的提交
如果存在未提交的变更:
  • 警告:"⚠️ 未提交的变更不会包含在CodeRabbit评审中"
  • 询问:"是否先提交并推送?" → 若选择是:等待用户操作后继续
如果存在未推送的提交:
  • 警告:"⚠️ 存在N个未推送的提交,CodeRabbit尚未对其进行评审"
  • 询问:"是否现在推送?" → 若选择是:执行
    git push
    ,提示"CodeRabbit将在约5分钟内完成评审",退出skill
其他情况:进入步骤2

Step 2: Find Open PR

步骤2:查找未关闭的PR

bash
gh pr list --head $(git branch --show-current) --state open --json number,title
If no PR: Ask "Create PR?" → If yes: create PR (see github.md § 5), inform "Run skill again in ~5 min", EXIT
bash
gh pr list --head $(git branch --show-current) --state open --json number,title
如果不存在PR: 询问"是否创建PR?" → 若选择是:创建PR(参见github.md § 5),提示"约5分钟后再重新运行本skill",退出

Step 3: Fetch Unresolved CodeRabbit Threads

步骤3:拉取未解决的CodeRabbit讨论线程

Fetch PR review threads (see github.md § 2):
  • Threads:
    gh api graphql ... pullRequest.reviewThreads ...
    (see github.md § 2)
Filter to:
  • unresolved threads only (
    isResolved == false
    )
  • threads started by CodeRabbit bot (
    coderabbitai
    ,
    coderabbit[bot]
    ,
    coderabbitai[bot]
    )
If review in progress: Check for "Come back again in a few minutes" message → Inform "⏳ Review in progress, try again in a few minutes", EXIT
If no unresolved CodeRabbit threads: Inform "No unresolved CodeRabbit review threads found", EXIT
For each selected thread:
  • Extract issue metadata from root comment
拉取PR评审讨论线程(参见github.md § 2):
  • 线程拉取命令:
    gh api graphql ... pullRequest.reviewThreads ...
    (参见github.md § 2
过滤条件:
  • 仅未解决的线程(
    isResolved == false
  • 仅由CodeRabbit机器人发起的线程(
    coderabbitai
    coderabbit[bot]
    coderabbitai[bot]
如果评审正在进行中: 检查是否存在"Come back again in a few minutes"消息 → 提示"⏳ 评审正在进行中,请几分钟后再试",退出
如果没有未解决的CodeRabbit讨论线程: 提示"未找到未解决的CodeRabbit评审讨论线程",退出
对每个选中的线程: 从根评论中提取问题元数据

Step 4: Parse and Display Issues

步骤4:解析并展示问题

Extract from each comment:
  1. Header:
    _([^_]+)_ \| _([^_]+)_
    → Issue type | Severity
  2. Description: Main body text
  3. Agent prompt: Content in
    <details><summary>🤖 Prompt for AI Agents</summary>
    (this is the fix instruction)
    • If missing, use description as fallback
  4. Location: File path and line numbers
Map severity:
  • 🔴 Critical/High → CRITICAL (action required)
  • 🟠 Medium → HIGH (review recommended)
  • 🟡 Minor/Low → MEDIUM (review recommended)
  • 🟢 Info/Suggestion → LOW (optional)
  • 🔒 Security → Treat as high priority
Display in CodeRabbit's original order (already severity-ordered):
CodeRabbit Issues for PR #123: [PR Title]

| # | Severity | Issue Title | Location & Details | Type | Action |
|---|----------|-------------|-------------------|------|--------|
| 1 | 🔴 CRITICAL | Insecure authentication check | src/auth/service.py:42<br>Authorization logic inverted | 🐛 Bug 🔒 Security | Fix |
| 2 | 🟠 HIGH | Database query not awaited | src/db/repository.py:89<br>Async call missing await | 🐛 Bug | Fix |
从每条评论中提取:
  1. 标题:
    _([^_]+)_ \| _([^_]+)_
    → 问题类型 | 严重程度
  2. 描述: 正文内容
  3. Agent提示词:
    <details><summary>🤖 Prompt for AI Agents</summary>
    标签内的内容(即修复指令)
    • 若不存在该部分,使用描述作为替代
  4. 位置: 文件路径和行号
严重程度映射:
  • 🔴 严重/高 → CRITICAL(必须处理)
  • 🟠 中 → HIGH(建议评审)
  • 🟡 次要/低 → MEDIUM(建议评审)
  • 🟢 信息/建议 → LOW(可选)
  • 🔒 安全 → 按高优先级处理
按CodeRabbit原始顺序展示(已按严重程度排序):
CodeRabbit Issues for PR #123: [PR Title]

| # | Severity | Issue Title | Location & Details | Type | Action |
|---|----------|-------------|-------------------|------|--------|
| 1 | 🔴 CRITICAL | Insecure authentication check | src/auth/service.py:42<br>Authorization logic inverted | 🐛 Bug 🔒 Security | Fix |
| 2 | 🟠 HIGH | Database query not awaited | src/db/repository.py:89<br>Async call missing await | 🐛 Bug | Fix |

Step 5: Ask User for Fix Preference

步骤5:询问用户修复偏好

Use AskUserQuestion:
  • 🔍 "Review each issue" - Manual review and approval (recommended)
  • ⚡ "Auto-fix all" - Apply all "Fix" issues without approval
  • ❌ "Cancel" - Exit
Route based on choice:
  • Review → Step 5
  • Auto-fix → Step 6
  • Cancel → EXIT
使用AskUserQuestion:
  • 🔍 "逐个评审问题" - 手动评审并确认(推荐)
  • ⚡ "自动修复全部" - 无需确认,应用所有标记为"Fix"的问题
  • ❌ "取消" - 退出
根据选择跳转:
  • 逐个评审 → 步骤6
  • 自动修复 → 步骤7
  • 取消 → 退出

Step 6: Manual Review Mode

步骤6:手动评审模式

For each "Fix" issue (CRITICAL first):
  1. Read relevant files
  2. Execute CodeRabbit's agent prompt as direct instruction (from "🤖 Prompt for AI Agents" section)
  3. Calculate proposed fix (DO NOT apply yet)
  4. Show fix and ask approval in ONE step:
    • Issue title + location
    • CodeRabbit's agent prompt (so user can verify)
    • Current code
    • Proposed diff
    • AskUserQuestion: ✅ Apply fix | ⏭️ Defer | 🔧 Modify
If "Apply fix":
  • Apply with Edit tool
  • Track changed files for a single consolidated commit after all fixes
  • Confirm: "✅ Fix applied and commented"
If "Defer":
  • Ask for reason (AskUserQuestion)
  • Move to next
If "Modify":
  • Inform user can make changes manually
  • Move to next
对每个标记为"Fix"的问题(严重程度最高的优先):
  1. 读取相关文件
  2. 直接执行CodeRabbit的Agent提示词指令(来自"🤖 Prompt for AI Agents"部分)
  3. 生成修复方案(暂不应用)
  4. 单步展示修复方案并询问确认:
    • 问题标题 + 位置
    • CodeRabbit的Agent提示词(方便用户验证)
    • 当前代码
    • 建议的diff变更
    • AskUserQuestion: ✅ 应用修复 | ⏭️ 延后处理 | 🔧 手动修改
若选择"应用修复":
  • 使用Edit工具应用变更
  • 跟踪修改的文件,待所有修复完成后统一提交
  • 确认提示:"✅ 修复已应用并添加评论"
若选择"延后处理":
  • 询问延后原因(AskUserQuestion)
  • 跳转到下一个问题
若选择"手动修改":
  • 提示用户可手动修改内容
  • 跳转到下一个问题

Step 7: Auto-Fix Mode

步骤7:自动修复模式

For each "Fix" issue (CRITICAL first):
  1. Read relevant files
  2. Execute CodeRabbit's agent prompt as direct instruction
  3. Apply fix with Edit tool
  4. Track changed files for one consolidated commit
  5. Report:
    Fixed: [Issue Title] at
    [Location]
    Agent prompt: [prompt used]
After all fixes, display summary of fixed/skipped issues.
对每个标记为"Fix"的问题(严重程度最高的优先):
  1. 读取相关文件
  2. 直接执行CodeRabbit的Agent提示词指令
  3. 使用Edit工具应用修复
  4. 跟踪修改的文件,待所有修复完成后统一提交
  5. 输出报告:
    已修复:[问题标题] 位于
    [位置]
    Agent提示词: [使用的提示词]
所有修复完成后,展示已修复/已跳过问题的汇总。

Step 8: Create Single Consolidated Commit

步骤8:创建统一提交

If any fixes were applied:
bash
git add <all-changed-files>
git commit -m "fix: apply CodeRabbit auto-fixes"
Use one commit for all applied fixes in this run.
如果应用了任何修复:
bash
git add <all-changed-files>
git commit -m "fix: apply CodeRabbit auto-fixes"
本次运行的所有修复合并为一个提交。

Step 9: Prompt Build/Lint Before Push

步骤9:推送前提示执行构建/代码检查

If a consolidated commit was created:
  • Prompt user interactively to run validation before push (recommended, not required).
  • Remind the user of the
    AGENTS.md
    instructions already loaded in Step 0 (if present).
  • If user agrees, run the requested checks and report results.
如果已创建统一提交:
  • 交互式提示用户在推送前运行验证(推荐,非强制)
  • 提醒用户步骤0中加载的
    AGENTS.md
    说明(如果存在)
  • 如果用户同意,运行请求的检查并输出结果

Step 10: Push Changes

步骤10:推送变更

If a consolidated commit was created:
  • Ask: "Push changes?" → If yes:
    git push
If all deferred (no commit): Skip this step.
如果已创建统一提交:
  • 询问:"是否推送变更?" → 若选择是:执行
    git push
如果所有问题都选择延后处理(无提交):跳过本步骤

Step 11: Post Summary

步骤11:发布总结

REQUIRED after all issues reviewed:
bash
gh pr comment <pr-number> --body "$(cat <<'EOF'
所有问题评审完成后必须执行:
bash
gh pr comment <pr-number> --body "$(cat <<'EOF'

Fixes Applied Successfully

修复应用成功

Fixed <file-count> file(s) based on <issue-count> unresolved review comment(s).
Files modified:
  • path/to/file-a.ts
  • path/to/file-b.ts
Commit:
<commit-sha>
The latest autofix changes are on the
<branch-name>
branch.
EOF )"

See [github.md § 3](./github.md#3-post-summary-comment) for details.

Optionally react to CodeRabbit's main comment with 👍.
基于<issue-count>条未解决评审意见,共修复<file-count>个文件。
修改的文件:
  • path/to/file-a.ts
  • path/to/file-b.ts
提交哈希:
<commit-sha>
最新的自动修复变更已提交到
<branch-name>
分支。
EOF )"

详情参见[github.md § 3](./github.md#3-post-summary-comment)

可选操作:给CodeRabbit的主评论点👍。

Key Notes

注意要点

  • Follow agent prompts literally - The "🤖 Prompt for AI Agents" section IS the fix specification
  • One approval per fix - Show context + diff + AskUserQuestion in single message (manual mode)
  • Preserve issue titles - Use CodeRabbit's exact titles, don't paraphrase
  • Preserve ordering - Display issues in CodeRabbit's original order
  • Do not post per-issue replies - Keep the workflow summary-comment only
  • 严格遵循Agent提示词 - "🤖 Prompt for AI Agents"部分的内容就是修复规范
  • 每个修复单独确认 - 手动模式下单条消息展示上下文+diff+询问确认
  • 保留问题原始标题 - 直接使用CodeRabbit的原始标题,不要改写
  • 保留原始顺序 - 按CodeRabbit输出的原始顺序展示问题
  • 不要逐条回复问题 - 仅发布工作流总结评论即可