ByteNoteByteNote

字节笔记本

2026年5月16日

Hermes Agent GitHub PR Review:AI 驱动的代码审查自动化

API中转
¥120

Hermes Agent 是 NousResearch 推出的 AI 智能体框架,支持通过 cron 定时任务和 Webhook 实现自动化工作流。本文将手把手教你构建一个 GitHub PR 自动审查 Agent,让它全天候监控仓库、审查代码质量并推送审查结果,让你专注于真正需要人工判断的 PR。

问题与方案

痛点: 团队开 PR 的速度远超审查速度,PR 排队数天没人看,初级开发者因无人审查而合入 bug,你每天早上都在补看 diff 而不是写代码。

方案: 一个 AI Agent 全天候监控仓库,审查每个新 PR 的 bug、安全问题和代码质量,发送摘要给你——你只需关注真正需要人工判断的 PR。

架构示意:

text
┌───────────────────────────────────────────────────────────────────┐
│                                                                   │
│   Cron Timer  ──▶  Hermes Agent  ──▶  GitHub API  ──▶  Review     │
│   (every 2h)       + gh CLI           (PR diffs)       delivery   │
│                    + skill                             (Telegram, │
│                    + memory                            Discord,   │
│                                                        local)     │
│                                                                   │
└───────────────────────────────────────────────────────────────────┘

本指南使用 cron 定时任务 轮询 PR,无需服务器或公网端点,可在 NAT 和防火墙后运行。

想要实时审查? 如果你有公网端点,可以参考 Webhook 方式的 GitHub PR 自动评论——GitHub 会在 PR 创建或更新时即时推送事件给 Hermes。

前置条件

  • Hermes Agent 已安装 —— 参见 安装指南
  • Gateway 已运行(用于 cron 任务):
    bash
    hermes gateway install   # 安装为服务
    # 或
    hermes gateway           # 前台运行
  • GitHub CLI (gh) 已安装并认证
    bash
    # 安装
    brew install gh        # macOS
    sudo apt install gh    # Ubuntu/Debian
    
    # 认证
    gh auth login
  • 消息通知已配置(可选)—— TelegramDiscord

没有配置消息通知? 使用 deliver: "local" 将审查结果保存到 ~/.hermes/cron/output/,适合在正式接入通知前测试。

第一步:验证环境

确保 Hermes 能访问 GitHub。启动聊天:

bash
hermes

用简单命令测试:

Run: gh pr list --repo NousResearch/hermes-agent --state open --limit 3

你应该能看到开放的 PR 列表。如果正常,说明环境就绪。

第二步:手动审查试运行

在聊天中,让 Hermes 审查一个真实的 PR:

text
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 将:

  1. 执行 gh pr diff 获取代码变更
  2. 通读整个 diff
  3. 生成结构化的审查结果,包含具体发现

如果你对审查质量满意,接下来就把它自动化。

第三步:创建审查 Skill

Skill 为 Hermes 提供跨会话和 cron 运行保持一致的审查规范。没有它,审查质量会波动。

bash
mkdir -p ~/.hermes/skills/code-review

创建 ~/.hermes/skills/code-review/SKILL.md

markdown
---
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,你应该在启动时的 skills 列表中看到 code-review

第四步:教导团队规范

这是让审查真正有用的关键。启动一个会话,教 Hermes 你们团队的标准:

text
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.
text
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.

这些记忆会永久保存——审查者将在无需每次提示的情况下强制执行你们的规范。

第五步:创建自动化 Cron 任务

现在把所有环节串联起来。创建一个每 2 小时运行的 cron 任务:

bash
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

验证任务已创建:

bash
hermes cron list

其他常用调度方案

调度表达式执行时机
0 */2 * * *每 2 小时
0 9,13,17 * * 1-5工作日每天三次
0 9 * * 1每周一早上汇总
30m每 30 分钟(高流量仓库)

第六步:按需手动触发

不想等调度?手动触发:

bash
hermes cron run pr-review

或在聊天会话中:

/cron run pr-review

进阶用法

直接将审查结果发布到 GitHub

除了发送到 Telegram,还可以让 Agent 直接在 PR 上评论:

在 cron 提示词中添加:

text
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 的 token 具有 repo 权限。审查将以 gh 认证的用户身份发布。

每周 PR 仪表盘

创建周一早上的仓库概览:

bash
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"

Gateway 运行在最小环境中。确保 gh 在系统 PATH 中,然后重启 gateway。

审查结果过于笼统

  1. 添加 code-review Skill(第三步)
  2. 通过 memory 教导 Hermes 你的团队规范(第四步)
  3. 你提供的栈信息越丰富,审查质量越高

Cron 任务不运行

bash
hermes gateway status    # Gateway 是否在运行?
hermes cron list         # 任务是否已启用?

API 速率限制

GitHub 认证用户每小时允许 5,000 次 API 请求。每次 PR 审查使用约 3-5 次请求(列表 + diff + 可选评论)。即使每天审查 100 个 PR,也远在限制之内。

下一步

分享: