mcpField
U
using-gh-cli

using-gh-cli

github.com/trailofbits开发7.9k212 次安装今天更新
代码仓库

关于 using-gh-cli

using-gh-cli 是一项使用经过认证的 `gh` 命令行处理 GitHub 任务的技能,涵盖仓库浏览、API 访问及 PR、Issue、Release 和 Actions 操作。

主要功能

  1. 01经过认证的 GitHub 访问
  2. 02克隆仓库以浏览代码
  3. 03管理 PR 和 Issue
  4. 04查询 GitHub API 数据
  5. 05查看 Release 和 Actions

适用场景

  1. 01阅读 GitHub 仓库文件
  2. 02查看或创建 PR
  3. 03获取仓库元数据或 API 结果
  4. 04检查工作流运行和日志

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/trailofbits/skills --skill using-gh-cli

安装后在 Claude Code 中输入/using-gh-cli确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
actions.md
api.md
issues.md
pull-requests.md
releases.md
repos-and-files.md

详细介绍

使用 GitHub CLI(gh)

适用场景

  • 浏览或阅读 GitHub 仓库中的代码——克隆仓库,然后使用 Read/Glob/Grep
  • 查看或创建拉取请求、议题、发布版本或 gist
  • 获取仓库元数据或任何 GitHub API 数据
  • 与 GitHub Actions 交互(运行记录、工作流)
  • 任何涉及 GitHub,且原本可能会使用 curl、wget 或 WebFetch 的任务

不适用场景

  • 非 GitHub URL(使用 WebFetch 或 curl)
  • 托管在 GitHub Pages(*.github.io)上的公开网页内容——这些是普通网站
  • 本地 git 操作(git commit、git push)——直接使用 git

核心原则

访问 GitHub URL 时,始终使用 gh,不要使用 curl、wget 或 WebFetch。 gh CLI 会自动使用用户经过身份验证的令牌,因此它:

  • 可用于私有仓库
  • 避免触发 GitHub API 速率限制(未认证:每小时 60 次请求;已认证:每小时 5,000 次请求)
  • 能正确处理分页
  • 提供结构化输出和筛选功能

浏览仓库代码

要读取或浏览 GitHub 仓库中的文件,请先将其克隆到本地,然后使用常规文件工具(Read、Glob、Grep)。这比通过 API 逐个获取文件快得多,也更自然。

# Clone to a session-scoped temp directory (cleaned up automatically on session end)
clonedir="$TMPDIR/gh-clones-${CLAUDE_SESSION_ID}"
mkdir -p "$clonedir"
gh repo clone owner/repo "$clonedir/repo" -- --depth 1

克隆后,使用 Explore Agent(通过 subagent_type=Explore 的 Task 工具)调查克隆的仓库。Explore Agent 可高效地使用 Read、Glob 和 Grep 搜索整个克隆仓库——比逐个调用这些工具好得多:

Task(subagent_type="Explore", prompt="In $clonedir/repo/, find how authentication is implemented")

对于已熟悉的克隆仓库中的定向查找,直接使用 Read/Glob/Grep。

  • gh repo clone 使用用户经过身份验证的令牌——可用于私有仓库
  • --depth 1 可加快克隆速度(仅获取最新提交)
  • 无需清理——SessionEnd 钩子会自动移除克隆目录
  • 仅在无需克隆、想快速查找单个文件时使用 gh api

快速入门

# View a repo
gh repo view owner/repo

# List and view PRs
gh pr list --repo owner/repo
gh pr view 123 --repo owner/repo

# List and view issues
gh issue list --repo owner/repo
gh issue view 456 --repo owner/repo

# Call any REST API endpoint
gh api repos/owner/repo/contents/README.md

# Call with pagination and jq filtering
gh api repos/owner/repo/pulls --paginate --jq '.[].title'

常用模式

不要使用改用
对 github.com/owner/repo 使用 WebFetchgh repo view owner/repo
对 blob/tree URL 使用 WebFetch使用 gh repo clone 克隆,然后使用 Read/Glob/Grep
对 raw.githubusercontent.com/... 使用 WebFetch使用 gh repo clone 克隆,然后使用 Read
对 api.github.com/... 使用 WebFetchgh api <endpoint>
curl https://api.github.com/...gh api <endpoint>
使用 -H "Authorization: token ..." 的 curlgh api <endpoint>(自动进行身份验证)
使用 wget 下载发布资产gh release download --repo owner/repo

参考

  • 拉取请求 — 列出、查看、创建、合并和审查 PR
  • 议题 — 列出、查看、创建、关闭议题并发表评论
  • 仓库和文件 — 查看仓库、浏览文件、克隆仓库
  • API — 原始 REST/GraphQL 访问、分页、jq 筛选
  • 发布版本 — 列出、创建和下载发布版本
  • Actions — 查看运行记录、触发工作流、检查日志

评论

支持 Markdown