实战

AI 辅助 Code Review:用 Claude Code 让 PR 审查效率提升 3 倍

用 Claude Code 做 AI 辅助代码审查完整指南:Pre-commit Hook 自动检查、PR Review 流程接入、自定义审查规则、与 GitHub Actions 集成、常见审查场景的 Prompt 模板,及人机协作最佳实践。

2026/3/144分钟 阅读ClaudeEagle

代码审查是软件开发中最耗时又最重要的环节。Claude Code 可以做你的第一道审查——发现低级错误、安全漏洞、性能问题,让人工审查专注在架构和业务逻辑。

方式一:本地快速审查

对单个文件或改动做快速检查:

bash
# 审查单个文件
claude -p "Review this file for bugs, security issues, and style problems" < src/auth.py

# 审查 git diff
git diff HEAD~1 | claude -p "Review these changes. Focus on: 1) bugs 2) security 3) missing error handling"

# 审查整个 PR
git diff main...feature-branch | claude -p "Comprehensive code review"

方式二:Pre-commit Hook 自动审查

每次 git commit 前自动让 Claude 检查:

bash
# .git/hooks/pre-commit
#!/bin/bash
STAGED=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(py|js|ts)$')
if [ -z "$STAGED" ]; then exit 0; fi

echo "Running AI code review..."
REVIEW=$(git diff --cached -- $STAGED | claude -p \
  "Review for critical issues only (bugs/security). If OK output PASS. If issues found, describe briefly.")

echo "$REVIEW"
if echo "$REVIEW" | grep -q "PASS"; then
  exit 0
else
  echo "AI review found issues. Fix them or use git commit --no-verify to skip."
  exit 1
fi
bash
chmod +x .git/hooks/pre-commit

方式三:GitHub Actions 自动 PR Review

yaml
# .github/workflows/ai-review.yml
name: AI Code Review
on:
  pull_request:
    types: [opened, synchronize]

jobs:
  ai-review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: {fetch-depth: 0}

      - name: Install Claude Code
        run: npm install -g @anthropic-ai/claude-code

      - name: Run AI Review
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git diff origin/${{ github.base_ref }}...HEAD > /tmp/pr.diff
          REVIEW=$(claude -p "Review this PR diff. Format: severity (high/medium/low), file, line, description" < /tmp/pr.diff)
          gh pr comment ${{ github.event.number }} --body "## AI Review\n\n$REVIEW"

常用审查 Prompt 模板

安全审查

Perform a security review of this code. Look for: - SQL injection vulnerabilities - XSS attack vectors - Authentication/authorization bypass - Sensitive data exposure (passwords, tokens, PII in logs) - Insecure dependencies Rate each issue: CRITICAL / HIGH / MEDIUM / LOW

性能审查

Review for performance issues: - N+1 database query patterns - Missing indexes (based on query patterns) - Unnecessary loops or nested complexity - Memory leaks (unclosed resources, large object retention) - Blocking I/O that should be async Estimate impact: HIGH (user-visible) / MEDIUM / LOW

完整性检查

Check code completeness: - Missing error handling (what happens when X fails?) - Edge cases not covered (empty input, null, overflow) - Missing tests for critical paths - TODO/FIXME comments that should be addressed - Missing documentation for public APIs

在 Claude Code 会话中做审查

bash
cd your-project
claude
Review the changes in the last commit. I want to know: 1. Any bugs or logic errors 2. Security concerns 3. What tests should be added 4. One suggestion to improve code clarity

让 Claude 直接修改问题

You found these issues in the last review: [粘贴审查结果] Please fix all HIGH and CRITICAL issues. For MEDIUM issues, just add TODO comments.

人机协作最佳实践

任务Claude 负责人工负责
语法和风格全部无需
低级 Bug大部分确认修复
安全漏洞发现评估和决策
架构合理性提建议最终决策
业务逻辑正确性无法判断必须人工
代码可读性提建议主要人工

关键原则:Claude 是你的第一轮审查,不是最终审查。安全相关的修改和核心业务逻辑变更,必须人工复核。


来源:Anthropic 官方文档 + GitHub Actions 集成文档

相关文章推荐

实战Claude Code Skills 实战:15 个可直接使用的 SKILL.md 模板(Git/审查/测试/文档/部署/调试)15 个精心设计的开箱即用 SKILL.md 模板:Git 工作流类(Smart Commit/PR Creator/Branch Cleanup);代码审查类(Security Review 含 OWASP 清单/Performance Review N+1 检测);测试类(Test Generator/Coverage Check);文档类(API Doc Generator OpenAPI 格式/Changelog Generator);部署运维类(Pre-deploy Checklist);调试类(Error Analyzer);效率工具类(Code Explainer/Refactor Advisor/Dependency Auditor/Daily Standup Helper)。2026/5/10实战Claude Code 常用工作流大全:新功能开发、重构、测试、代码审查的最佳实践Claude Code 常用工作流大全:6 个经过验证的完整流程——全流程功能开发、代码重构、测试覆盖补全、PR 代码审查、技术债清理、新人 Onboarding,含判断树帮你选对工作流。2026/4/11实战Claude Code 子 Agent 实战:如何用多个 Agent 并行处理复杂任务Claude Code 子 Agent 实战指南:如何用多个独立 Agent 并行处理复杂任务。含 4 个实战示例、自定义 Agent 配置和成本优化建议。2026/4/7实战Claude Code 数据库迁移实战:Alembic、Flyway 与安全迁移策略完全指南(2026)Claude Code 辅助数据库迁移完整实战:Python/SQLAlchemy + Alembic 脚本生成(PostgreSQL CONCURRENTLY不锁表)、Java + Flyway 管理、零停机先加后删四步方案(200万数据拆列)、大表 Online DDL 安全配置、分批数据格式迁移脚本、回滚 SOP 设计,覆盖 PostgreSQL/MySQL 两种数据库。2026/4/1实战Claude Code Kubernetes 部署实战完全指南:AI 辅助 K8s 配置与故障排查(2026)Claude Code 辅助 Kubernetes 部署的完整实战指南:生成生产级 Deployment/HPA/Ingress/RBAC 配置、分析 CrashLoopBackOff/OOMKilled 等 Pod 报错、多环境配置管理(Kustomize/Helm)、GitHub Actions CI/CD 自动部署与回滚,以及 Pod Pending/Service 不可访问的排错 Prompt 模板。2026/3/27实战Claude Code CI/CD 完全集成指南:GitHub Actions 自动化代码审查与测试Claude Code 与 CI/CD 流水线完整集成教程:GitHub Actions 中非交互模式(claude -p)调用、PR 自动代码审查 Workflow、自动测试生成、构建失败时的 AI 诊断、安全扫描集成、Claude API Key 的 Secrets 管理、控制成本的模型选择策略(PR 审查用 Sonnet/失败诊断用 Haiku),以及 GitLab CI 和 Jenkins 的适配方案。2026/3/20