GitHubPR自动化审查Skill github-pr-review

这个技能用于自动化GitHub拉取请求的审查过程,支持代码差异分析、文件读取、模式搜索和内联评论提交。适用于软件开发中的代码质量检查和持续集成流程。关键词:GitHub、PR审查、代码审查、自动化、DevOps、CI/CD、代码质量、GitHub API。

DevOps 0 次安装 0 次浏览 更新于 3/16/2026

name: github-pr-review description: 审查拉取请求并发布带有代码建议的内联评论。当需要分析PR差异、读取仓库文件、搜索代码模式或提交行级反馈的PR审查时使用。 allowed-tools: Bash(python *)

GitHub PR 审查

认证

重要:凭证由代理层自动注入。请勿检查环境变量中的GITHUB_TOKEN。直接运行脚本;认证是透明处理的。


可用脚本

所有脚本都在.claude/skills/github-pr-review/scripts/目录中。

get_pr_files.py — 获取PR差异和更改的文件

python .claude/skills/github-pr-review/scripts/get_pr_files.py --repo OWNER/REPO --pr NUMBER [--show-patch]

# 示例:
python .claude/skills/github-pr-review/scripts/get_pr_files.py --repo acme/webapp --pr 42
python .claude/skills/github-pr-review/scripts/get_pr_files.py --repo acme/webapp --pr 42 --show-patch

read_file.py — 从仓库读取完整文件

python .claude/skills/github-pr-review/scripts/read_file.py --repo OWNER/REPO --path FILE_PATH [--ref BRANCH]

# 示例:
python .claude/skills/github-pr-review/scripts/read_file.py --repo acme/webapp --path src/components/Checkout.tsx
python .claude/skills/github-pr-review/scripts/read_file.py --repo acme/webapp --path package.json --ref main

search_code.py — 在仓库中搜索代码模式

python .claude/skills/github-pr-review/scripts/search_code.py --query "SEARCH_TERM" --repo OWNER/REPO

# 示例:
python .claude/skills/github-pr-review/scripts/search_code.py --query "trackEvent" --repo acme/webapp
python .claude/skills/github-pr-review/scripts/search_code.py --query "amplitude" --repo acme/webapp
python .claude/skills/github-pr-review/scripts/search_code.py --query "analytics.track" --repo acme/webapp

get_review_context.py — 检查先前的审查(增量模式)

python .claude/skills/github-pr-review/scripts/get_review_context.py --repo OWNER/REPO --pr NUMBER

# 输出告诉你:
# - 是否已经审查过此PR
# - 你最后审查的提交
# - 自那时以来更改了哪些文件(增量)
# - 你之前的内部评论
# - 其他审查者的评论

create_review.py — 提交PR审查并附带内联评论

python .claude/skills/github-pr-review/scripts/create_review.py \
  --repo OWNER/REPO \
  --pr NUMBER \
  --body "审查摘要文本" \
  --comments-file /tmp/review_comments.json \
  --event COMMENT

# --comments-file 是一个JSON数组的内联评论:
# [
#   {
#     "path": "src/components/Checkout.tsx",
#     "line": 42,
#     "body": "考虑跟踪此事件:
```suggestion
trackEvent('checkout_started', { itemCount });
```"
#   }
# ]

PR 审查工作流程

步骤1: 检查先前的审查(始终先做这一步)

python .claude/skills/github-pr-review/scripts/get_review_context.py --repo OWNER/REPO --pr NUMBER

这告诉你是否跳过、进行完整审查或增量审查:

  • “已在当前HEAD处审查过” → 跳过,什么都不做
  • “首次审查” → 完整审查(继续步骤2-5)
  • “自上次审查以来有新提交” → 增量审查(只审查列出的增量文件)

步骤2: 分析PR

# 获取PR详细信息和更改的文件及差异
python .claude/skills/github-pr-review/scripts/get_pr_files.py --repo OWNER/REPO --pr NUMBER --show-patch

步骤3: 读取完整文件上下文(当差异不足时)

# 读取完整文件以理解周围代码
python .claude/skills/github-pr-review/scripts/read_file.py --repo OWNER/REPO --path src/file.tsx

步骤4: 搜索现有模式

# 检查是否已设置分析/跟踪
python .claude/skills/github-pr-review/scripts/search_code.py --query "trackEvent OR analytics.track OR amplitude" --repo OWNER/REPO

步骤5: 编写评论文件并提交审查

# 编写评论JSON(使用写入工具或echo)
# 然后提交审查
python .claude/skills/github-pr-review/scripts/create_review.py \
  --repo OWNER/REPO --pr NUMBER \
  --body "## 遥测审查

找到5个地方应该添加分析事件。" \
  --comments-file /tmp/review_comments.json

内联评论格式

使用GitHub的建议语法来提出具体的代码更改,开发者可以一键接受:

考虑在此处添加遥测事件:
```suggestion
trackEvent('button_clicked', { buttonId: props.id, page: 'checkout' });
handleClick(e);
```

建议块的重要规则:

  • 建议替换指定line号码处的行
  • 包括原始代码加上你的添加(整个替换)
  • 保持建议最小化 — 添加跟踪调用,保持现有代码完整
  • 如果添加一行,在建议中包括原始行加上新行

快速参考

目标 命令
检查先前审查 get_review_context.py --repo X --pr N
获取PR文件 + 差异 get_pr_files.py --repo X --pr N --show-patch
读取文件 read_file.py --repo X --path src/app.tsx
搜索模式 search_code.py --query "amplitude" --repo X
提交审查 create_review.py --repo X --pr N --body "..." --comments-file F
创建分支 create_branch.py --repo X --branch fix/typo
创建/更新文件 create_or_update_file.py --repo X --path f --branch b --message "msg" --file /tmp/c.txt
创建PR create_pull_request.py --repo X --title "Fix" --head fix/typo
设置提交状态 create_commit_status.py --repo X --sha SHA --state success

写入操作

这些脚本允许代理对仓库进行更改:创建分支、提交文件、打开PR和设置提交状态。

create_branch.py — 创建新分支

python .claude/skills/github-pr-review/scripts/create_branch.py --repo OWNER/REPO --branch BRANCH_NAME [--from-branch SOURCE]

# 示例:
python .claude/skills/github-pr-review/scripts/create_branch.py --repo acme/webapp --branch fix/missing-telemetry
python .claude/skills/github-pr-review/scripts/create_branch.py --repo acme/webapp --branch fix/missing-telemetry --from-branch develop

create_or_update_file.py — 在仓库中创建或更新文件

python .claude/skills/github-pr-review/scripts/create_or_update_file.py \
  --repo OWNER/REPO --path FILE_PATH --branch BRANCH --message "提交消息" --file /tmp/content.txt

# 更新现有文件(传递 --sha 从 read_file_with_sha):
python .claude/skills/github-pr-review/scripts/create_or_update_file.py \
  --repo OWNER/REPO --path FILE_PATH --branch BRANCH --message "提交消息" --sha CURRENT_SHA --file /tmp/content.txt

# 通过stdin管道内容:
echo "新内容" | python .claude/skills/github-pr-review/scripts/create_or_update_file.py \
  --repo OWNER/REPO --path FILE_PATH --branch BRANCH --message "提交消息"

create_pull_request.py — 打开拉取请求

python .claude/skills/github-pr-review/scripts/create_pull_request.py \
  --repo OWNER/REPO --title "PR标题" --head BRANCH_NAME [--base main] [--body "描述"]

# 示例:
python .claude/skills/github-pr-review/scripts/create_pull_request.py \
  --repo acme/webapp --title "添加缺失的遥测事件" --head fix/missing-telemetry

python .claude/skills/github-pr-review/scripts/create_pull_request.py \
  --repo acme/webapp --title "添加缺失的遥测事件" --head fix/missing-telemetry \
  --base develop --body-file /tmp/pr_body.md

create_commit_status.py — 设置提交状态(检查)

python .claude/skills/github-pr-review/scripts/create_commit_status.py \
  --repo OWNER/REPO --sha COMMIT_SHA --state STATE [--description "文本"] [--context "标签"] [--target-url URL]

# 有效状态:error, failure, pending, success

# 示例:
python .claude/skills/github-pr-review/scripts/create_commit_status.py \
  --repo acme/webapp --sha abc123 --state success --description "所有检查通过"

python .claude/skills/github-pr-review/scripts/create_commit_status.py \
  --repo acme/webapp --sha abc123 --state failure \
  --description "发现安全问题" --context "IncidentFox/security"

写入工作流程示例:修复和PR

通过GitHub API进行代码更改的典型工作流程:

# 1. 创建分支
python .claude/skills/github-pr-review/scripts/create_branch.py \
  --repo acme/webapp --branch fix/missing-telemetry

# 2. 提交修复(首先将内容写入临时文件,然后传递)
python .claude/skills/github-pr-review/scripts/create_or_update_file.py \
  --repo acme/webapp --path src/analytics.ts --branch fix/missing-telemetry \
  --message "添加缺失的遥测调用" --file /tmp/analytics.ts

# 3. 打开PR
python .claude/skills/github-pr-review/scripts/create_pull_request.py \
  --repo acme/webapp --title "添加缺失的遥测事件" --head fix/missing-telemetry \
  --body "添加结账和支付流程的跟踪事件。"

# 4. 在头部提交上设置状态
python .claude/skills/github-pr-review/scripts/create_commit_status.py \
  --repo acme/webapp --sha abc123def456 --state success \
  --description "遥测审查完成"