教程:构建 GitHub PR 审查 Agent
问题所在: 你的团队开 PR 的速度比你审查的速度还快。PR 等上好几天才有人看。初级开发者因为没人审查而合入了 bug。你每天早上花时间追赶 diff,而不是专注于开发。
解决方案: 一个全天候监控你仓库的 AI Agent,审查每个新 PR 中的 bug、安全问题和代码质量,并给你发送摘要——这样你只需要把时间花在真正需要人类判断的 PR 上。
你将构建的内容:
┌───────────────────────────────────────────────────────────────────┐
│ │
│ Cron Timer ──▶ Hermes Agent ──▶ GitHub API ──▶ Review │
│ (every 2h) + gh CLI (PR diffs) delivery │
│ + skill (Telegram, │
│ + memory Discord, │
│ local) │
│ │
└───────────────────────────────────────────────────────────────────┘
本指南使用定时任务按计划轮询 PR——不需要服务器或公共端点。可在 NAT 和防火墙后正常工作。
如果你有公共端点,请查看使用 Webhook 自动化 GitHub PR 评论——当 PR 被打开或更新时,GitHub 会立即将事件推送给 Hermes。
前提条件
- 已安装 Hermes Agent — 参见安装指南
- 网关正在运行以支持定时任务:
hermes gateway install # 安装为服务# 或hermes gateway # 在前台运行
- 已安装并认证 GitHub CLI(
gh):# 安装brew install gh # macOSsudo apt install gh # Ubuntu/Debian# 认证gh auth login - 已配置消息平台(可选)— Telegram 或 Discord
使用 deliver: "local" 将审查结果保存到 ~/.hermes/cron/output/。在接入通知之前非常适合用于测试。
步骤 1:验证设置
确保 Hermes 可以访问 GitHub。启动聊天:
hermes
用一个简单命令测试:
Run: gh pr list --repo NousResearch/hermes-agent --state open --limit 3
你应该看到一个开放 PR 的列表。如果成功,就可以继续了。
步骤 2:尝试手动审查
还在聊天中,让 Hermes 审查一个真实的 PR:
Review this pull request. Read the diff, check for bugs, security issues,
and code quality. Be specific about line numbers and quote problematic code.
Run: gh pr diff 3888 --repo NousResearch/hermes-agent
Hermes 将会:
- 执行
gh pr diff获取代码变更 - 读完整个 diff
- 生成包含具体发现的结构化审查
如果你对质量满意,是时候自动化了。
步骤 3:创建审查技能(Skill)
技能为 Hermes 提供跨会话和定时任务持续存在的一致审查规范。没有它,审查质量会参差不齐。
mkdir -p ~/.hermes/skills/code-review
创建 ~/.hermes/skills/code-review/SKILL.md:
---
name: code-review
description: Review pull requests for bugs, security issues, and code quality
---
# Code Review Guidelines
When reviewing a pull request:
## What to Check
1. **Bugs** — Logic errors, off-by-one, null/undefined handling
2. **Security** — Injection, auth bypass, secrets in code, SSRF
3. **Performance** — N+1 queries, unbounded loops, memory leaks
4. **Style** — Naming conventions, dead code, missing error handling
5. **Tests** — Are changes tested? Do tests cover edge cases?
## Output Format
For each finding:
- **File:Line** — exact location
- **Severity** — Critical / Warning / Suggestion
- **What's wrong** — one sentence
- **Fix** — how to fix it
## Rules
- Be specific. Quote the problematic code.
- Don't flag style nitpicks unless they affect readability.
- If the PR looks good, say so. Don't invent problems.
- End with: APPROVE / REQUEST_CHANGES / COMMENT
验证它已加载——启动 hermes,你应该在启动时的技能列表中看到 code-review。
步骤 4:教它你的约定
这才是让审查器真正有用的关键。启动一个会话,告诉 Hermes 你的团队规范:
Remember: In our backend repo, we use Python with FastAPI.
All endpoints must have type annotations and Pydantic models.
We don't allow raw SQL — only SQLAlchemy ORM.
Test files go in tests/ and must use pytest fixtures.
Remember: In our frontend repo, we use TypeScript with React.
No `any` types allowed. All components must have props interfaces.
We use React Query for data fetching, never useEffect for API calls.
这些记忆永久保留——审查器无需每次被告知就会执行你的约定。
步骤 5:创建自动化定时任务
现在把一切串联起来。创建一个每 2 小时运行的定时任务:
hermes cron create "0 */2 * * *" \
"Check for new open PRs and review them.
Repos to monitor:
- myorg/backend-api
- myorg/frontend-app
Steps:
1. Run: gh pr list --repo REPO --state open --limit 5 --json number,title,author,createdAt
2. For each PR created or updated in the last 4 hours:
- Run: gh pr diff NUMBER --repo REPO
- Review the diff using the code-review guidelines
3. Format output as:
## PR Reviews — today
### [repo] #[number]: [title]
**Author:** [name] | **Verdict:** APPROVE/REQUEST_CHANGES/COMMENT
[findings]
If no new PRs found, say: No new PRs to review." \
--name "pr-review" \
--deliver telegram \
--skill code-review
验证任务已调度:
hermes cron list
其他常用调度表达式
| 调度 | 触发时间 |
|---|---|
0 */2 * * * | 每 2 小时 |
0 9,13,17 * * 1-5 | 每天三次,仅工作日 |
0 9 * * 1 | 每周一早上汇总 |
30m | 每 30 分钟(高流量仓库) |
步骤 6:按需运行
不想等到调度时间?手动触发:
hermes cron run pr-review
或在聊天会话中:
/cron run pr-review
进一步扩展
直接在 GitHub 上发布审查
不投递到 Telegram,而是让 Agent 直接在 PR 上评论:
在你的 cron 提示词中添加:
After reviewing, post your review:
- For issues: gh pr review NUMBER --repo REPO --comment --body "YOUR_REVIEW"
- For critical issues: gh pr review NUMBER --repo REPO --request-changes --body "YOUR_REVIEW"
- For clean PRs: gh pr review NUMBER --repo REPO --approve --body "Looks good"
确保 gh 有 repo 权限的令牌。审查将以 gh 认证的用户身份发布。
每周 PR 仪表板
创建所有仓库的周一早间概览:
hermes cron create "0 9 * * 1" \
"Generate a weekly PR dashboard:
- myorg/backend-api
- myorg/frontend-app
- myorg/infra
For each repo show:
1. Open PR count and oldest PR age
2. PRs merged this week
3. Stale PRs (older than 5 days)
4. PRs with no reviewer assigned
Format as a clean summary." \
--name "weekly-dashboard" \
--deliver telegram
多仓库监控
通过在提示词中添加更多仓库来扩大规模。Agent 按顺序处理它们——无需额外设置。
故障排查
"gh: command not found"
网关在精简的环境中运行。确保 gh 在系统 PATH 中,然后重启网关。
审查太泛泛
- 添加
code-review技能(步骤 3) - 通过记忆告诉 Hermes 你的约定(步骤 4)
- 它对你的技术栈了解越多,审查质量越好
定时任务不运行
hermes gateway status # 网关是否在运行?
hermes cron list # 任务是否已启用?
速率限制
GitHub 对认证用户每小时允许 5,000 次 API 请求。每次 PR 审查使用约 3-5 次请求(列表 + diff + 可选评论)。即使每天审查 100 个 PR 也远未达到限制。
下一步
- 基于 Webhook 的 PR 审查 — 当 PR 被打开时立即获得审查(需要公共端点)
- 每日简报机器人 — 将 PR 审查与你的每日新闻简报结合
- 构建插件 — 将审查逻辑封装成可共享的插件
- 配置文件 — 运行一个专用的审查者配置文件,拥有自己的记忆和配置
- 备用提供商 — 确保即使某个提供商下线,审查也能继续运行