Github 代码审查
通过分析 git diff、在 PR 上留下内联评论以及执行彻底的推送前审查来审查代码变更。支持 gh CLI,或回退使用 git + 通过 curl 调用的 GitHub REST API。
技能元数据
| 来源 | 捆绑(默认安装) |
| 路径 | skills/github/github-code-review |
| 版本 | 1.1.0 |
| 作者 | Hermes Agent |
| 许可证 | MIT |
| 标签 | GitHub, Code-Review, Pull-Requests, Git, Quality |
| 相关技能 | github-auth, github-pr-workflow |
参考:完整 SKILL.md
以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理看到的指令。
GitHub 代码审查
在推送之前对本地变更进行代码审查,或审查 GitHub 上的开放 PR。此技能的大部分功能使用原生 git — gh/curl 的区别仅在于 PR 级别的交互。
前提条件
- 已通过 GitHub 身份验证(参见
github-auth技能) - 位于 git 仓库中
设置(用于 PR 交互)
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
AUTH="gh"
else
AUTH="git"
if [ -z "$GITHUB_TOKEN" ]; then
if [ -f ~/.hermes/.env ] && grep -q "^GITHUB_TOKEN=" ~/.hermes/.env; then
GITHUB_TOKEN=$(grep "^GITHUB_TOKEN=" ~/.hermes/.env | head -1 | cut -d= -f2 | tr -d '\n\r')
elif grep -q "github.com" ~/.git-credentials 2>/dev/null; then
GITHUB_TOKEN=$(grep "github.com" ~/.git-credentials 2>/dev/null | head -1 | sed 's|https://[^:]*:\([^@]*\)@.*|\1|')
fi
fi
fi
REMOTE_URL=$(git remote get-url origin)
OWNER_REPO=$(echo "$REMOTE_URL" | sed -E 's|.*github\.com[:/]||; s|\.git$||')
OWNER=$(echo "$OWNER_REPO" | cut -d/ -f1)
REPO=$(echo "$OWNER_REPO" | cut -d/ -f2)
1. 审查本地变更(推送前)
这是纯 git 操作 — 在任何地方都有效,无需 API。
获取 Diff
# Staged changes (what would be committed)
git diff --staged
# All changes vs main (what a PR would contain)
git diff main...HEAD
# File names only
git diff main...HEAD --name-only
# Stat summary (insertions/deletions per file)
git diff main...HEAD --stat
审查策略
- 首先了解整体情况:
git diff main...HEAD --stat
git log main..HEAD --oneline
- 逐个文件审查 — 对变更的文件使用
read_file以获取完整上下文,并使用 diff 查看具体变更内容:
git diff main...HEAD -- src/auth/login.py
- 检查常见问题:
# Debug statements, TODOs, console.logs left behind
git diff main...HEAD | grep -n "print(\|console\.log\|TODO\|FIXME\|HACK\|XXX\|debugger"
# Large files accidentally staged
git diff main...HEAD --stat | sort -t'|' -k2 -rn | head -10
# Secrets or credential patterns
git diff main...HEAD | grep -in "password\|secret\|api_key\|token.*=\|private_key"
# Merge conflict markers
git diff main...HEAD | grep -n "<<<<<<\|>>>>>>\|======="
- 向用户呈现结构化的反馈。
审查输出格式
在审查本地变更时,请按以下结构呈现发现结果:
## Code Review Summary
### Critical
- **src/auth.py:45** — SQL injection: user input passed directly to query.
Suggestion: Use parameterized queries.
### Warnings
- **src/models/user.py:23** — Password stored in plaintext. Use bcrypt or argon2.
- **src/api/routes.py:112** — No rate limiting on login endpoint.
### Suggestions
- **src/utils/helpers.py:8** — Duplicates logic in `src/core/utils.py:34`. Consolidate.
- **tests/test_auth.py** — Missing edge case: expired token test.
### Looks Good
- Clean separation of concerns in the middleware layer
- Good test coverage for the happy path
2. 审查 GitHub 上的 Pull Request
查看 PR 详情
使用 gh:
gh pr view 123
gh pr diff 123
gh pr diff 123 --name-only
使用 git + curl:
PR_NUMBER=123
# Get PR details
curl -s \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER \
| python3 -c "
import sys, json
pr = json.load(sys.stdin)
print(f\"Title: {pr['title']}\")
print(f\"Author: {pr['user']['login']}\")
print(f\"Branch: {pr['head']['ref']} -> {pr['base']['ref']}\")
print(f\"State: {pr['state']}\")
print(f\"Body:\n{pr['body']}\")"
# List changed files
curl -s \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER/files \
| python3 -c "
import sys, json
for f in json.load(sys.stdin):
print(f\"{f['status']:10} +{f['additions']:-4} -{f['deletions']:-4} {f['filename']}\")"
将 PR 检出到本地以进行完整审查
这适用于原生 git — 无需 gh:
# Fetch the PR branch and check it out
git fetch origin pull/123/head:pr-123
git checkout pr-123
# Now you can use read_file, search_files, run tests, etc.
# View diff against the base branch
git diff main...pr-123
使用 gh(快捷方式):
gh pr checkout 123
在 PR 上留下评论
通用 PR 评论 — 使用 gh:
gh pr comment 123 --body "Overall looks good, a few suggestions below."
通用 PR 评论 — 使用 curl:
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/issues/$PR_NUMBER/comments \
-d '{"body": "Overall looks good, a few suggestions below."}'
留下内联审查评论
单条内联评论 — 使用 gh(通过 API):
HEAD_SHA=$(gh pr view 123 --json headRefOid --jq '.headRefOid')
gh api repos/$OWNER/$REPO/pulls/123/comments \
--method POST \
-f body="This could be simplified with a list comprehension." \
-f path="src/auth/login.py" \
-f commit_id="$HEAD_SHA" \
-f line=45 \
-f side="RIGHT"
单条内联评论 — 使用 curl:
# Get the head commit SHA
HEAD_SHA=$(curl -s \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER \
| python3 -c "import sys,json; print(json.load(sys.stdin)['head']['sha'])")
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER/comments \
-d "{
\"body\": \"This could be simplified with a list comprehension.\",
\"path\": \"src/auth/login.py\",
\"commit_id\": \"$HEAD_SHA\",
\"line\": 45,
\"side\": \"RIGHT\"
}"
提交正式审查(批准 / 请求变更)
使用 gh:
gh pr review 123 --approve --body "LGTM!"
gh pr review 123 --request-changes --body "See inline comments."
gh pr review 123 --comment --body "Some suggestions, nothing blocking."
使用 curl — 原子化提交多条评论审查:
HEAD_SHA=$(curl -s \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER \
| python3 -c "import sys,json; print(json.load(sys.stdin)['head']['sha'])")
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER/reviews \
-d "{
\"commit_id\": \"$HEAD_SHA\",
\"event\": \"COMMENT\",
\"body\": \"Code review from Hermes Agent\",
\"comments\": [
{\"path\": \"src/auth.py\", \"line\": 45, \"body\": \"Use parameterized queries to prevent SQL injection.\"},
{\"path\": \"src/models/user.py\", \"line\": 23, \"body\": \"Hash passwords with bcrypt before storing.\"},
{\"path\": \"tests/test_auth.py\", \"line\": 1, \"body\": \"Add test for expired token edge case.\"}
]
}"
事件值:"APPROVE"、"REQUEST_CHANGES"、"COMMENT"
line 字段指的是文件新版本中的行号。对于已删除的行,请使用 "side": "LEFT"。
3. 审查清单
在执行代码审查(本地或 PR)时,系统地检查以下内容:
正确性
- 代码是否实现了其声称的功能?
- 是否处理了边界情况(空输入、null、大数据量、并发访问)?
- 是否优雅地处理了错误路径?
安全性
- 无硬编码的秘密、凭证或 API 密钥
- 对用户-facing 输入进行输入验证
- 无 SQL 注入、XSS 或路径遍历漏洞
- 在需要的地方进行身份验证/授权检查
代码质量
- 命名清晰(变量、函数、类)
- 无不必要的复杂性或过早抽象
- DRY 原则 — 无应提取的重复逻辑
- 函数职责单一(专注)
测试
- 新的代码路径是否经过测试?
- 是否覆盖了正常路径和错误情况?
- 测试是否可读且易于维护?
性能
- 无 N+1 查询或不必要的循环
- 在有益的地方使用适当的缓存
- 异步代码路径中无阻塞操作
文档
- 公共 API 是否有文档
- 非显而易见的逻辑是否有注释解释“为什么”
- 如果行为发生变更,README 是否已更新
4. 推送前审查工作流
当用户要求你“审查代码”或“在推送前检查”时:
git diff main...HEAD --stat— 查看变更范围git diff main...HEAD— 阅读完整 diff- 对于每个变更的文件,如果需要更多上下文,请使用
read_file - 应用上述清单
- 以结构化格式呈现发现结果(关键问题 / 警告 / 建议 / 看起来不错)
- 如果发现关键问题,在用户推送之前提供修复建议
5. PR 审查工作流(端到端)
当用户要求你“审查 PR #N”、“查看此 PR”或提供 PR URL 时,请遵循以下步骤:
步骤 1:设置环境
source "${HERMES_HOME:-$HOME/.hermes}/skills/github/github-auth/scripts/gh-env.sh"
# Or run the inline setup block from the top of this skill
步骤 2:收集 PR 上下文
获取 PR 元数据、描述和变更文件列表,以便在深入代码之前了解范围。
使用 gh:
gh pr view 123
gh pr diff 123 --name-only
gh pr checks 123
使用 curl:
PR_NUMBER=123
# PR details (title, author, description, branch)
curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$GH_OWNER/$GH_REPO/pulls/$PR_NUMBER
# Changed files with line counts
curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$GH_OWNER/$GH_REPO/pulls/$PR_NUMBER/files
步骤 3:将 PR 检出到本地
这使你可以完全访问 read_file、search_files 并能够运行测试。
git fetch origin pull/$PR_NUMBER/head:pr-$PR_NUMBER
git checkout pr-$PR_NUMBER
步骤 4:阅读差异并理解变更
# Full diff against the base branch
git diff main...HEAD
# Or file-by-file for large PRs
git diff main...HEAD --name-only
# Then for each file:
git diff main...HEAD -- path/to/file.py
对于每个已更改的文件,使用 read_file 查看变更周围的完整上下文——仅凭差异可能会遗漏只有在周围代码中才能可见的问题。
步骤 5:在本地运行自动化检查(如适用)
# Run tests if there's a test suite
python -m pytest 2>&1 | tail -20
# or: npm test, cargo test, go test ./..., etc.
# Run linter if configured
ruff check . 2>&1 | head -30
# or: eslint, clippy, etc.
步骤 6:应用审查清单(第 3 节)
逐一检查每个类别:正确性、安全性、代码质量、测试、性能、文档。
步骤 7:将审查发布到 GitHub
收集你的发现,并将其作为带有行内评论的正式审查提交。
使用 gh:
# If no issues — approve
gh pr review $PR_NUMBER --approve --body "Reviewed by Hermes Agent. Code looks clean — good test coverage, no security concerns."
# If issues found — request changes with inline comments
gh pr review $PR_NUMBER --request-changes --body "Found a few issues — see inline comments."
使用 curl — 原子化审查,包含多个行内评论:
HEAD_SHA=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$GH_OWNER/$GH_REPO/pulls/$PR_NUMBER \
| python3 -c "import sys,json; print(json.load(sys.stdin)['head']['sha'])")
# Build the review JSON — event is APPROVE, REQUEST_CHANGES, or COMMENT
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$GH_OWNER/$GH_REPO/pulls/$PR_NUMBER/reviews \
-d "{
\"commit_id\": \"$HEAD_SHA\",
\"event\": \"REQUEST_CHANGES\",
\"body\": \"## Hermes Agent Review\n\nFound 2 issues, 1 suggestion. See inline comments.\",
\"comments\": [
{\"path\": \"src/auth.py\", \"line\": 45, \"body\": \"🔴 **Critical:** User input passed directly to SQL query — use parameterized queries.\"},
{\"path\": \"src/models.py\", \"line\": 23, \"body\": \"⚠️ **Warning:** Password stored without hashing.\"},
{\"path\": \"src/utils.py\", \"line\": 8, \"body\": \"💡 **Suggestion:** This duplicates logic in core/utils.py:34.\"}
]
}"
步骤 8:同时发布总结评论
除了行内评论外,还要留下一个顶层总结,以便 PR 作者一目了然地了解全貌。使用 references/review-output-template.md 中的审查输出格式。
使用 gh:
gh pr comment $PR_NUMBER --body "$(cat <<'EOF'
## Code Review Summary
**Verdict: Changes Requested** (2 issues, 1 suggestion)
### 🔴 Critical
- **src/auth.py:45** — SQL injection vulnerability
### ⚠️ Warnings
- **src/models.py:23** — Plaintext password storage
### 💡 Suggestions
- **src/utils.py:8** — Duplicated logic, consider consolidating
### ✅ Looks Good
- Clean API design
- Good error handling in the middleware layer
---
*Reviewed by Hermes Agent*
EOF
)"
步骤 9:清理
git checkout main
git branch -D pr-$PR_NUMBER
决策:批准 vs 请求变更 vs 评论
- 批准 — 没有关键或警告级别的问题,只有轻微建议或完全通过
- 请求变更 — 存在任何需要在合并前修复的关键或警告级别问题
- 评论 — 观察和建议,但没有阻碍性问题(当你不确定或 PR 处于草稿状态时使用)