关于 github-code-review
github-code-review 是供开发者检查本地改动或 GitHub 拉取请求的代码审查技能。它用结构化清单指导审查,并可通过 gh 或 GitHub API 发布评论或提交正式审查决定。
主要功能
- 01审查本地 Git 改动
- 02审查 GitHub 拉取请求
- 03使用正确性与安全清单
- 04发布行内及摘要评论
- 05提交正式审查决定
- 06支持 gh 或 git 与 curl
适用场景
- 01推送前检查改动
- 02审查拉取请求
- 03在 PR 中留下行内反馈
- 04批准或请求修改
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ nousresearch/ hermes-agent --skill github-code-review安装后在 Claude Code 中输入/github-code-review确认已加载。
附带文件references/review-output-template.md
详细介绍
GitHub 代码审查
在推送前审查本地更改,或审查 GitHub 上已打开的 PR。本技能大部分操作使用纯 git——仅 PR 级别的交互需要区分 gh 和 curl。
前提条件
- 已通过 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 _hermes_env="${HERMES_HOME:-$HOME/.hermes}/.env"; [ -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=$(uv run python "${HERMES_HOME:-$HOME/.hermes}/skills/github/github-auth/scripts/git-credential-token.py")
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。
获取差异
# 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查看完整上下文,并通过差异了解具体更改:
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 \
| python -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 \
| python -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 \
| python -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 \
| python -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 密钥
- 对面向用户的输入进行了验证
- 没有 SQL 注入、XSS 或路径遍历
- 在需要的地方执行身份验证 / 授权检查
代码质量
- 命名清晰(变量、函数、类)
- 没有不必要的复杂性或过早抽象
- DRY — 没有应当提取的重复逻辑
- 函数职责明确(单一职责)
测试
- 是否测试了新增的代码路径?
- 是否覆盖了正常情况和错误情况?
- 测试是否易于阅读和维护?
性能
- 没有 N+1 查询或不必要的循环
- 在有益的情况下使用了适当的缓存
- 异步代码路径中没有阻塞操作
文档
- 为公开 API 编写了文档
- 对于不明显的逻辑,注释说明了“原因”
- 行为发生更改时更新了 README
4. 推送前审查工作流
当用户要求你“审查代码”或“推送前检查”时:
git diff main...HEAD --stat— 查看更改范围git diff main...HEAD— 阅读完整差异- 对每个更改的文件,如需更多上下文,则使用
read_file - 应用上述清单
- 按结构化格式呈现发现(Critical / Warnings / Suggestions / Looks Good)
- 如果发现严重问题,主动提出在用户推送前修复
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 \
| python -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
决策:批准、请求修改还是发表评论
- 批准 — 没有严重或警告级别的问题,只有一些小建议或没有问题
- 请求修改 — 存在任何应在合并前修复的严重或警告级别问题
- 发表评论 — 提出观察和建议,但没有阻碍合并的问题(不确定或 PR 处于草稿状态时使用)