用途:为智能体提供调用 GitHub CLI(gh)的参考模式,帮助在非交互环境中稳定获取结构化数据。
主要能力与用法: 非交互策略:gh 在非 TTY 下会自动跳过分页器、去除 ANSI 颜色并快速报错而非提示,无需设置 GH_PAGER 或 --no-pager(该标志不存在)。 结构化输出:加 --json field1,field2 获取结构化数据;只写 --json 不带字段可列出全部可用字段;用 --jq '<expr>' 过滤;用 --template '<go-template>' 生成文本,注意 --template/-T 在部分命令(如 gh pr create -T、gh issue create -T)与正文模板标志冲突,使用前查 --help。 分页:list 类命令默认上限通常为 30,用 -L N 调整;gh issue list / gh pr list 不通过 --json 暴露 totalCount,需真实总数时用 gh api graphql 查询;原始 API 调用用 gh api --paginate <path>,可配合 --jq 与 --slurp。 仓库定位:gh 从当前目录的 git remote 推断仓库,用 --repo OWNER/REPO(-R)覆盖。 search 与 list:gh search issues|prs|code|repos|commits|users 使用搜索索引,支持完整搜索语法,每个限定符须作为独立 token,不能整体加引号;多词自由文本才加引号。跨仓库或按作者/标签过滤优先用 search。机器人以 GitHub App 身份提交,--author dependabot 无效,应使用 --app dependabot 或 --author "dependabot[bot]"。gh search issues 还支持 --search-type <lexical|semantic|hybrid>(仅 GHEC、仅 issues)。 Issue 类型与关系:gh issue create 支持 --type、--parent、--blocked-by、--blocking;gh issue edit 支持增删类型、父级、子问题及阻塞关系;gh issue list --type 可按类型过滤;--json 字段包括 issueType、parent、subIssues、subIssuesSummary、blockedBy、blocking,其中 subIssues、blockedBy、blocking 为 {"nodes": [...], "totalCount": N} 结构且节点有上限(subIssues 100,blockedBy/blocking 50)。GHES 需 3.17+ 支持类型与子问题,3.19+ 支持阻塞关系。 Discussions:gh discussion 为预览命令集,含 list、view、create、edit、comment 子命令,支持 --json/--jq/--template(仅 list 与 view)。 读取文件与目录:gh repo read-file 与 gh repo read-dir 为预览命令,可经 API 读取仓库内容而无需克隆,支持 --repo 与 --ref。 兜底:--json 未暴露的数据可用 gh api,例如 PR 评审线程评论 gh api repos/{owner}/{repo}/pulls/{n}/comments,或 gh api graphql -f query='...' -F var=value。 认证:gh auth status 显示活动主机、用户及所用环境变量,支持 --json。 其他:gh pr checkout <n> 切换分支,--worktree <path> 检出到 git worktree;支持 NO_COLOR、CLICOLOR_FORCE、GH_FORCE_TTY。
限制:discussion 与 read-file/read-dir 为预览命令,可能变更;部分功能仅限 GHEC 或特定 GHES 版本;节点列表存在截断,需对比 totalCount。