用 Claude Code 做 GitHub PR 治理:从自动 Review 到安全审查的完整方案
TL;DR:Claude Code Action 接入 GitHub Actions 实现 PR 自动审查。本文给出 5 种自动化方案(全量 Review / 关键路径专项 / 外部贡献者 / OWASP 安全审查 / 自定义清单),含完整 YAML 配置和费用估算。
目录
- PR 治理的问题出在哪里
- Claude Code Action 是什么
- 安装:5 分钟快速上手
- 方案一:全量自动 PR Review
- 方案二:关键路径专项 Review
- 方案三:外部贡献者严格审查
- 方案四:安全聚焦 Review(OWASP 对齐)
- 方案五:自定义 Review 清单
- 人机边界:哪些留给人做
- API 费用估算
- 常见问题
- 相关资料
PR 治理的问题出在哪里
大多数团队的 PR Review 有两个慢性病:
第一,等待。Senior engineer 忙,Review 排队,一个 PR 从提交到合并等 24–48 小时是常态。新功能的交付节奏被 Review 队列拖住。
第二,不一致。同样的问题,不同的 reviewer 有不同的标准——有人在意命名规范,有人关注安全漏洞,有人注重测试覆盖率。没有人能每次都全面检查所有维度。
Stack Overflow 2025 开发者调查显示,84% 的开发者已在日常工作中使用 AI 工具,但大多数只是用 AI 生成代码,没有把 AI 嵌入 Review 流程。把 Claude Code 接入 GitHub Actions,可以在 PR 提交后几分钟内完成客观性检查——不等人,不漏项。
这篇文章把常用的 5 个 PR 治理方案走一遍,每个都附完整的 workflow YAML,可以直接用。
Claude Code Action 是什么
claude-code-action 是 Anthropic 官方发布的 GitHub Action,让 Claude Code 直接参与 PR 和 Issue 的工作流。
核心能力:
- 自动 Code Review:分析 PR diff,给出改进建议,支持 inline 注释到具体代码行
- 交互式问答:在 PR 评论里 @claude,Claude 回复代码相关问题
- 代码修改:Claude 可以直接实现简单的 fix、重构、新功能
- 进度追踪:用 checkbox 实时显示 review 进度
运行机制:Action 在你的 GitHub runner 上执行,API 请求发到你指定的 Anthropic API 端点(可以是 Anthropic 直连、AWS Bedrock、Google Vertex AI,或第三方网关)。
安装:5 分钟快速上手
最快的方式是通过 Claude Code 终端安装:
# 在项目根目录打开 Claude Code,运行
/install-github-app这个命令会引导你完成 GitHub App 安装和 Secrets 配置。
注意:需要仓库管理员权限。如果你用 API 网关(如 CodeGateway)而不是 Anthropic 直连,参考手动配置方式。
手动配置(网关用户):
- 在仓库 Settings → Secrets and variables → Actions 里添加: -
ANTHROPIC_API_KEY:你的 API Key - (可选)ANTHROPIC_BASE_URL:https://api.codegateway.dev/v1 - 在
.github/workflows/目录下创建 workflow 文件(见下面各方案) - Workflow 需要的权限:
permissions:
contents: read
pull-requests: write
id-token: write方案一:全量自动 PR Review
适用场景:对每一个 PR 自动触发 Review,适合主干开发模式或所有 PR 都需要规范检查的团队。
name: Claude Auto Review
on:
pull_request:
types: [opened, synchronize, ready_for_review]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
track_progress: true
prompt: |
REPO: ${{ github.repository }}
PR NUMBER: ${{ github.event.pull_request.number }}
Review this pull request with focus on:
- Code quality and best practices
- Potential bugs and edge cases
- Security implications
- Test coverage gaps
Use inline comments for specific code issues.
Use `gh pr comment` for overall summary.
claude_args: |
--allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)"track_progress: true 会在 PR 里显示一条进度评论,随着 Review 进行实时更新 checkbox——比较直观,能让团队看到 AI 正在工作。
触发时机:opened(新建 PR)、synchronize(有新 commit)、ready_for_review(从草稿转为正式 PR)。
方案二:关键路径专项 Review
适用场景:不对所有 PR 做全量 Review,只在关键文件变更时触发——比如认证模块、支付逻辑、API 接口定义。
name: Review Critical Paths
on:
pull_request:
types: [opened, synchronize]
paths:
- "src/auth/**"
- "src/api/**"
- "src/payment/**"
- "config/security.yml"
jobs:
critical-review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: |
REPO: ${{ github.repository }}
PR NUMBER: ${{ github.event.pull_request.number }}
This PR modifies critical authentication, API, or payment files.
Focus your review on:
- Authentication and authorization flows
- Input validation and data sanitization
- Potential SQL injection or XSS vulnerabilities
- API contract changes that may break consumers
- Payment logic correctness and idempotency
Note: The PR branch is already checked out.
claude_args: |
--allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*)"paths: 过滤器让 Claude 只在真正重要的文件改动时出现,不刷低价值 PR。
方案三:外部贡献者严格审查
适用场景:开源项目或接受外部 PR 的仓库。对首次贡献者施加更高的审查标准,减少人工 Review 负担同时保证质量。
name: External Contributor Review
on:
pull_request:
types: [opened, synchronize]
jobs:
external-review:
# 只对首次贡献者触发
if: github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: |
REPO: ${{ github.repository }}
PR NUMBER: ${{ github.event.pull_request.number }}
CONTRIBUTOR: ${{ github.event.pull_request.user.login }}
This is a first-time contribution from @${{ github.event.pull_request.user.login }}.
Please provide a comprehensive review:
- Code quality and adherence to project conventions
- Tests: are new behaviors covered?
- Documentation: public APIs documented?
- Breaking changes clearly noted?
- License and attribution (no copy-pasted code without attribution)
Be constructive and welcoming. Explain the "why" behind suggestions
so the contributor learns, not just "change this."
claude_args: |
--allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)"注意 prompt 里加了"be constructive and welcoming"——对外部贡献者的反馈语气很重要,Claude 会遵守。
方案四:安全聚焦 Review(OWASP 对齐)
适用场景:有安全合规要求的项目,或希望每个 PR 都经过 OWASP Top 10 扫描的团队。
name: Security-Focused Review
on:
pull_request:
types: [opened, synchronize]
jobs:
security-review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: |
REPO: ${{ github.repository }}
PR NUMBER: ${{ github.event.pull_request.number }}
Perform a security-focused code review aligned with OWASP Top 10:
1. Injection (SQL, NoSQL, command injection)
2. Broken Authentication (session management, token handling)
3. Sensitive Data Exposure (unencrypted PII, secrets in code)
4. Security Misconfiguration (default credentials, verbose errors)
5. Cross-Site Scripting (reflected, stored, DOM-based)
6. Insecure Deserialization
7. Known Vulnerable Dependencies (flag any new imports)
8. Insufficient Logging (missing audit trails for sensitive ops)
Format findings as:
- [CRITICAL]: Must fix before merge
- [HIGH]: Should fix before merge
- [MEDIUM]: Fix in follow-up PR
- [INFO]: Note for awareness, no action required
claude_args: |
--allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*)"给 Claude 明确的严重级别(CRITICAL / HIGH / MEDIUM / INFO)让输出格式一致,便于 reviewer 快速判断哪些必须处理。
方案五:自定义 Review 清单
适用场景:团队有特定的 Review 规范,需要 Claude 按清单逐项检查。
name: Team Review Checklist
on:
pull_request:
types: [opened, synchronize]
jobs:
checklist-review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
id-token: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: |
REPO: ${{ github.repository }}
PR NUMBER: ${{ github.event.pull_request.number }}
Review against our team checklist and mark each item:
✅ Pass | ❌ Fail (with specific location) | ⚠️ Needs attention
CHECKLIST:
[ ] Functions under 50 lines; complex logic extracted
[ ] No direct DB queries outside repository layer
[ ] New endpoints have rate limiting
[ ] Error messages don't expose stack traces in prod
[ ] New env variables documented in .env.example
[ ] Migrations are reversible (down migration exists)
[ ] API responses follow existing schema conventions
[ ] No hardcoded secrets or API keys
Post the checklist result as a PR comment with pass/fail for each item.
Add inline comments on specific lines that fail.
claude_args: |
--allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)"这个方案的输出格式是结构化清单,Review 结果可以存档、统计,也方便在 PR 合并前做门控(Gate)。
人机边界:哪些留给人做
把 Claude 接入 PR 流程,不是让人退场——是让人聚焦在机器做不好的判断上。
工作类型 | Claude 负责 | 人负责 |
|---|---|---|
代码风格 / 格式 / 命名 | ✅ | 不需要人 |
常见安全漏洞(SQL 注入、XSS 等) | ✅ | 人决定优先级 |
测试覆盖率检查 | ✅ | 人判断哪些边界需要覆盖 |
技术债识别 | ✅ | 人决定是否此刻处理 |
业务逻辑正确性 | ❌ | 人 — 需要理解 PRD |
架构一致性 | ❌ | 人 — 需要知道系统演进方向 |
合并决策 | ❌ | 人 — 最终 approve 和 merge |
跨团队影响评估 | ❌ | 人 — 依赖上下文信息 |
关键原则:Claude 生成的 Review 注释需要人确认,不能绕过人直接 merge。Claude 是"第一个 reviewer",不是"唯一 reviewer"。
API 费用估算
以 Sonnet 4.6 为基准(input $3/1M tokens,output $15/1M tokens):
PR 规模 | 预估 tokens | 单次 review 费用(直连) | 通过 CodeGateway(1.4x 倍率) |
|---|---|---|---|
小型(< 200 行 diff) | ~8K | ~$0.03 | ~$0.04 |
中型(200–800 行 diff) | ~20K | ~$0.08 | ~$0.11 |
大型(800–2000 行 diff) | ~50K | ~$0.20 | ~$0.28 |
按每天 10 个中型 PR 计算,月费用约 $24–$33——通常低于一个 senior engineer 一小时的时间成本。
建议:对超大 PR(> 2000 行)先考虑是否应该拆分,再考虑用 AI review。大 PR 本身就是 review 效率的杀手,AI 也一样。
常见问题
Q:Claude 的 review 结果准确吗?会不会产生太多误报?
初期会有误报,尤其是对业务规则的判断(Claude 不知道你的 PRD)。降低误报的方法是把 prompt 写精确——告诉 Claude 你的项目用了哪些约定、哪些规则是已知豁免的。运行 2–3 周后根据实际误报情况迭代 prompt,准确率会收敛。
Q:Claude 能直接修复 review 发现的问题吗?
可以,但建议谨慎开放。在 claude_args 里放开 Bash(git commit:*) 权限后,Claude 可以直接提交 fix commit 到 PR 分支。更安全的做法是:Claude 识别问题 + 建议修复代码,人来决定是否接受,再 apply。
Q:能不能让 Claude 只 review 特定语言的文件?
可以,在 paths: 过滤器里指定扩展名:
paths:
- "**/*.py"
- "**/*.ts"Q:用 API 网关(CodeGateway)而不是 Anthropic 直连,Action 能正常工作吗?
可以。在 Secrets 里额外添加 ANTHROPIC_BASE_URL,然后在 workflow 里传入:
env:
ANTHROPIC_BASE_URL: ${{ secrets.ANTHROPIC_BASE_URL }}claude-code-action 会用这个环境变量覆盖默认端点。
Q:多个 commit push 到同一个 PR,会触发多次 review 吗?
会。synchronize 触发器在每次 push 时都会触发。如果 push 很频繁,可以改成只在 ready_for_review 时触发,或用 concurrency 设置取消前一次未完成的 review:
concurrency:
group: review-${{ github.event.pull_request.number }}
cancel-in-progress: trueQ:可以在私有仓库里使用吗?
可以,没有限制。API 调用走你自己的 Key,Claude 通过 gh CLI 访问 PR 内容,用的是 GitHub Actions 自带的 GITHUB_TOKEN,权限范围可控。
相关资料
- Claude Code 快速配置指南 — Claude Code 命令行工具接入
- Claude Code Auto Mode 详解 — Auto Mode 的工作原理
- Codex CLI vs Claude Code 深度对比 — 两个 AI 编程工具的选择
- Claude API Rate Limits 详解 — 大规模 CI/CD 使用的限流策略
参考资料
- anthropics/claude-code-action GitHub 仓库(github.com/anthropics/claude-code-action)
- Stack Overflow Developer Survey 2025
- Anthropic Claude Code 文档(2026)
Anthropic 官方实践对照
参考 Anthropic 在《How Claude Code Works in Large Codebases》一文里总结的大型代码库实战经验,以下做法可以和上面 5 种自动化方案叠加:
- 路径绑定的 review skill(叠加方案二:关键路径专项)—— Anthropic 建议为不同子目录绑定专属 skill,比如支付服务团队把 deployment skill 限定在
services/payments/。同理,可以把 OWASP 安全审查 skill 仅作用于auth/、payments/路径,其他路径走通用 review,避免噪音。 - 分离探索 agent 与编辑 agent(叠加方案五:自定义清单)—— 用一个只读 subagent 先生成 PR 涉及子系统的 map,再让主 agent 基于这个 map 做 review。Claude Code Action 里写成两个 step:先
claude-code map,再claude-code review --context map.md。 - stop hook 持续迭代 CLAUDE.md —— session 结束时跑一个 stop hook,让它反思本次 review 中发现的新模式,自动 propose 更新到仓库根的 CLAUDE.md。团队的 review 标准会随每次 PR 演进,而不是停留在初始版本。
更多企业级实践,看 Anthropic 官方博客:How Claude Code Works in Large Codebases。
