API 参考 · 11
Git 仓库统一接口(/api/git-repo)
设计:技能/第三方访问 Git 仓库资源(Issue/PR/内容/分支等)时,统一走 grape 的 Git 仓库 API。
⚠️ grape/api/repos是平台业务接口(托管/同步/成员/部署 URL 等,见 02-repos.md);本接口/api/git-repo是 Git 仓库透传(Issue/PR/内容/分支等由 gitcode/github/gitlab 提供)。
不是 GitCode 代理:grape 侧定义中性的 Git 资源契约(入参/语义 provider 无关),平台差异由
服务端vcs/providers/(§git-repo-provider-neutral-interface-design.md 查表翻译)收敛;backend 由 tokenrepo_key前缀区分。
契约 provider 无关:入参与语义对调用方统一,平台差异由服务端适配,调用方不感知(本文不再描述各 provider 实现细节)。
认证:get_current_user_or_api_key(契约收录)+ 体内_repo_token强制 session。
scope(x-token-scope):全部 =session_only——仅tk_会话 token(agent 专用)可用;
grape_API Key / Web JWT → 403。写操作需授权。
Base:{{SERVER_URL}}/api/git-repo
出参当前为 gitcode 平台实际结构(github 逐步归一中,raw兜底保留)。
CLI:grape git-repo <子资源>(如grape git-repo branches对应GET /api/git-repo/{owner}/{repo}/branches;
owner/repo由 CLI 参数传入,命令名 ≠ 路径,见 README「CLI 命令映射」)。
后端区分(Git Provider 判定)
Git provider 由认证 token 的 repo_key 前缀派生(gitcode: / github:),不接受 X-Git-Provider / provider 参数
(单一信息源,见 docs/weekly-02/git-repo-provider-from-token-design.md):
token(tk_)repo_key = gitcode:owner/repo → GitCode 后端
token(tk_)repo_key = github:owner/repo → GitHub 后端
| provider | 内部实现(src/vcs/providers/ 注册表自动派生,新增 provider 只加模块) |
|---|---|
gitcode |
https://gitcode.com/api/v5(原生 _req)+ providers/gitcode.py |
github |
https://api.github.com(原生 _github_req)+ providers/github.py(已接入;仅少数语义差异逐步归一) |
gitlab |
未来:新增 vcs/providers/gitlab.py(API_BASE/request/token/MAP)+ 注册 |
通用的 Content-Type: application/json(有 body 时);分页 ?page=1&per_page=20(max 100)。
1. 仓库信息
GET /api/git-repo/{owner}/{repo}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo repo --owner {owner} --repo {repo}(operation_id=gitrepo_repo,命令名 ≠ 路径,见 README「CLI 命令映射」)。
入参
| 位置 | 参数 | 类型 | 说明 |
|---|---|---|---|
| Path | owner |
string | 仓库属主 |
| Path | repo |
string | 仓库名 |
返回(200):仓库对象
{
"id": 10489208,
"name": "hello",
"full_name": "Jason7602/hello",
"description": "...",
"default_branch": "main",
"html_url": "https://gitcode.com/Jason7602/hello",
"clone_url": "...",
"visibility_level": 20,
"created_at": "..."
}
错误:401 / 403 / 404(仓库不存在或无权)。
2. 分支
GET /api/git-repo/{owner}/{repo}/branches
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo branches --owner {owner} --repo {repo}(operation_id=gitrepo_branches)。
入参:Path {owner}/{repo}(provider 由 token 派生,不传);Query ?per_page=50(技能用)。
返回(200):分支对象数组
[
{"name": "main", "protected": false, "commit": {"id": "sha", "message": "..."}},
{"name": "feat/issue-7", "protected": false}
]
错误:401 / 403 / 404。
3. 文件内容
GET /api/git-repo/{owner}/{repo}/tree?ref={branch}&depth={single|recursive}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo tree --owner {owner} --repo {repo} --ref {branch} --depth single|recursive(operation_id=gitrepo_tree)。
入参
| 参数 | 类型 | 说明 |
|---|---|---|
ref |
string(Query) | 分支/引用;空 → 自动解析默认分支 |
depth |
string(Query) | single(单层,默认) / recursive(递归,仅 GitHub) |
返回(200):底层 git/trees/{ref} 结构
{"tree": [
{"type": "tree", "name": "configs", "path": "configs", "mode": "040000", "sha": "..."},
{"type": "blob", "name": "README.md", "path": "README.md", "mode": "100644", "sha": "..."}
]}
平台差异:GitCode 单层可用(tree+blob);
depth=recursive在 GitCode 明确不支持(返回破碎树,
丢失 blob),会返回 400 提示;GitHub 原生支持递归(git/trees/{sha}?recursive=1)。
错误:401 / 403 / 400(GitCode 递归)/ 404。
GET /api/git-repo/{owner}/{repo}/contents — 根目录列表
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo contents --owner {owner} --repo {repo}(operation_id=gitrepo_contents_root)。无尾斜杠的根路由(
gitrepo_contents_root),等价contents/{path}的空 path:返回根目录条目数组。
返回(200):条目数组(同目录列表结构):
[{"type":"dir","name":"configs","path":"configs","sha":"..."},
{"type":"file","name":"README.md","path":"README.md","sha":"..."}]
GET /api/git-repo/{owner}/{repo}/contents/{path}?ref={branch}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo contents --owner {owner} --repo {repo} --path {path} --ref {branch}(operation_id=gitrepo_contents)。
入参
| 位置 | 参数 | 类型 | 说明 |
|---|---|---|---|
| Path | owner/repo/path |
string | 路径。空/省略 → 根目录列表;目录 → 该目录条目列表;文件 → 单文件 base64 内容(技能多用 CODEOWNERS) |
| Query | ref |
string | 分支/引用(技能用 ref=codeowner) |
返回(200):
- 目录/根 → 条目数组(一接口枚举仓库结构,QA/开发 agent 一步拿目录树):
json
[{"type":"dir","name":"configs","path":"configs","sha":"..."},
{"type":"file","name":"README.md","path":"README.md","sha":"..."}]
- 文件 → 文件对象(base64 content):
json
{"name": "CODEOWNERS", "path": "CODEOWNERS", "size": 123,
"sha": "sha", "content": "<base64>", "encoding": "base64",
"type": "file", "download_url": "..."}
根目录列表:
GET /api/git-repo/{owner}/{repo}/contents(无尾斜杠的根路由
gitrepo_contents_root)或/contents?ref=请求根目录,平台返回条目数组。
错误:401 / 403 / 404(路径不存在)。
提示:路径不存在时(尤其根/目录探测),可先取根目录列表确认仓库结构,避免盲目探测吃 404。
4. 提交历史
GET /api/git-repo/{owner}/{repo}/commits
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo commits --owner {owner} --repo {repo}(operation_id=gitrepo_commits)。
入参:Path owner/repo;Query ?per_page=(可选)、?sha=(可选,分支名/tag/commit sha;省略 = 默认分支)。
版本要求(Issue #2227):--sha 需要 grape-cli >= 0.3.3 的构建。版本号相同也可能是
不同构建(离线 wheel 过旧),以能力探测为准:
grape git-repo commits --help | grep -- --sha # 有输出 = 支持
python3 scripts/check_grape_cli_drift.py # 漂移守卫:缺失时 exit 1,无 grape 时 SKIP
降级核验(部署线 CLI 无 --sha 时):不要静默改用默认分支猜测,按下面组合旁证:
# ① PR 目标分支/head sha(pulls get 返回 base/head 的 ref 与 sha)
grape git-repo pulls get --owner {owner} --repo {repo} --number <PR> --json-out
# ② 指定 ref/view 下的文件内容(确认改动已落在目标分支)
grape git-repo contents --owner {owner} --repo {repo} --path <file> --ref <branch>
# ③ 本地 worktree 的提交历史旁证(自基线所在 ref 起)
git log origin/<branch> --oneline -30
返回(200):提交对象数组
[{"sha": "...", "message": "...", "author": {"name": "x", "email": "x@x"},
"committer": {"name": "x"}, "date": "2026-..."}]
错误:401 / 403 / 404。
5. 标签
GET /api/git-repo/{owner}/{repo}/labels
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo labels --owner {owner} --repo {repo}(operation_id=gitrepo_labels_list)。
返回(200):标签数组 [{"id":1,"name":"bug","color":"#ff0000","description":""}]
POST /api/git-repo/{owner}/{repo}/labels
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo labels create --owner {owner} --repo {repo} --json '{"name":"bug","color":"d73a4a"}'(operation_id=gitrepo_labels_create)。
Body(form-encoded 或 JSON):name(必填)、color(如 d73a4a,不带 # 的 6 位 hex;带 # 前缀会被自动去除)
返回(201):创建后的标签对象。错误:400(缺 name)/ 409(已存在)/ 403。
6. 里程碑
GET /api/git-repo/{owner}/{repo}/milestones?state=all
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo milestones --owner {owner} --repo {repo} --state all(operation_id=gitrepo_milestones)。
Query:state(open/closed/all,技能用 all)
返回(200):里程碑数组 [{"id":3,"number":850012,"title":"v1.0","state":"open","description":"","due_on":null,"open_issues":3,"closed_issues":1,"created_at":"..."}]
Issue #1005:上游里程碑对象通常只有
number(如850012),真实id体现在url尾部
(…/milestones/3→id=3)。本端点(以及 milestones create/update)统一补出非空id;
issues update --milestone_id优先取id,同时兼容number。
错误:401 / 403 / 404。
7. Issues
GET /api/git-repo/{owner}/{repo}/issues
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo issues --owner {owner} --repo {repo} --state open(operation_id=gitrepo_issues_list)。
入参:Query ?state=open&page=1&per_page=50(state: open/closed/all)
返回(200):Issue 对象数组
[{"id": 4312271, "number": 5, "title": "bug: ...", "state": "opened",
"body": "desc", "labels": [{"name":"bug"}],
"assignee": {"username":"Jason7602"}, "assignees": [...],
"closed_at": null, "created_at": "2026-...", "milestone": null}]
POST /api/git-repo/{owner}/{repo}/issues
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo issues create --owner {owner} --repo {repo} --json '{"title":"...","body":"...","assignee":"..."}'(operation_id=gitrepo_issues_create)。
入参 Body(中性,provider 无关):
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
title |
string | ✅ | Issue 标题 |
body |
string | ✅ | Issue 正文 |
assignee |
string | 否 | 负责人平台登录用户名(login,如 Jason7602;非昵称/数字 id),服务端适配到各平台 |
服务端按 provider 归一(vcs/providers):gitcode 收 {title,body,assignee};github 收 {title,body,assignees}。
发布前一致性断言(Issue #2225 P2):新建 Issue 无目标号可比,比对正文首个
Issue #N主语锚点与当前会话任务 Issue;不一致返回 409(error_code=ISSUE_ASSERT_MISMATCH)
且不产生线上写入。正文无锚点放行,不阻断正常发布。该断言无放行参数;如确需为其他 Issue 建单,请在目标 Issue 自己的会话(或用户 JWT / API Key 身份)下发起。
返回(201):创建后的 Issue 对象(同 GET 单个结构)。
GET /api/git-repo/{owner}/{repo}/issues/{number}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo issues get --owner {owner} --repo {repo} --number {number}(operation_id=gitrepo_issues_get)。
返回(200):单个 Issue 对象(结构同上)。错误:404。
PATCH /api/git-repo/{owner}/{repo}/issues/{number}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo issues update --owner {owner} --repo {repo} --number {number} --json '{...}'(operation_id=gitrepo_issues_update)。
入参 Body(JSON,中性):
| 字段 | 类型 | 说明 |
|---|---|---|
labels |
string/array | 标签 |
assignee |
string | 责任人 |
milestone_id |
int | 里程碑 id(优先,取 milestones list 的 id);兼容历史 number 传参。GitCode 下游解析后映射为上游 milestone 参数(#640/#1005) |
state |
string | Issue 状态迁移(对齐 GitHub state_event):仅接受 "reopen" / "close",其他取值后端返回 400 |
body |
string | Issue 正文(空值忽略,不影响其他字段) |
返回(200):更新后的 Issue 对象。错误:400/404/403/409/422/502。
发布前一致性断言(Issue #2225):带
body更新时,目标 Issue 号与正文主语Issue #N
必须与当前会话任务 Issue 一致;不一致返回 409(error_code=ISSUE_ASSERT_MISMATCH)且
不产生线上写入。
元数据类更新(labels/assignee/milestone/state)不做标签一致性拦截。里程碑(#640 / #1005 / #1076):
- GitCode:milestone_id服务端映射为上游milestone(int);PATCH 必填repo+title由服务端自动补齐
(title 缺失时 GET 回取,回取失败/为空返回 502),仅传milestone_id也不会被上游 400 拒绝。
- 取值口径(#1005):milestone_id优先按id匹配(milestones list的id,如3),
未命中再按number匹配(如850012);解析后以目标里程碑的上游number写入 PATCH。
- 失败语义(不再笼统 502):
- 400:可访问里程碑列表中找不到该id/number(不存在或无权限查看),解析失败不会发出 PATCH;
- 403:当前身份无权限(读取里程碑或写 Issue 时上游返回 403);
- 422:上游 PATCH 返回 200 但回读milestone为空 / number 与解析目标不一致(上游忽略),
并回滚为请求前的 milestone、回读确认;若回滚被上游静默忽略,错误文案标注「原值未恢复」。
- 5xx 仅保留上游真实故障:预校验/回读时上游不可用、网关或连接失败仍返回 5xx。
- 成功时响应回显milestone(issues get同样回显上游 milestone 字段,并补id供 QA 断言)。
- GitHub:milestone_id语义不变(透传上游同名参数)。
POST /api/git-repo/{owner}/{repo}/issues/{number}/close
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo issues close --owner {owner} --repo {repo} --number {number}(operation_id=gitrepo_issues_close)。
关闭 Issue(对外关闭入口,供 grape-cli / agent 使用;无 Body)。平台适配由服务端完成,调用方无需区分 provider。
返回(200):关闭后的 Issue 对象(state 为 closed)。
错误:401 / 403 / 404(Issue 不存在)/ 502(平台关闭未生效或 token 解析失败)。
GET/POST /api/git-repo/{owner}/{repo}/issues/{number}/comments
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo issues comments list --owner {owner} --repo {repo} --number {number}/issues comments create ... --json '{"body":"..."}'(operation_id=gitrepo_issues_comments_list/gitrepo_issues_comments_create)。
- GET:返回评论数组
[{"id":1,"body":"...","user":{"username":"x"},"created_at":"..."}] - POST:Body
{body}(必填)→ 返回(201)创建后的评论对象。
发布前一致性断言(Issue #2225 P1):会话任务可确定时,评论目标 Issue 与正文首个
Issue #N主语锚点必须与当前任务一致;不一致返回 409(ISSUE_ASSERT_MISMATCH)
且不产生线上写入。讨论区 chat 会话代发与用户 JWT / API Key 不受影响。该断言无放行参数;确需在其他 Issue 评论区发言时,请在目标 Issue 自己的会话(或用户 JWT / API Key 身份)下发起。
8. Pull Requests
GET /api/git-repo/{owner}/{repo}/pulls
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls --owner {owner} --repo {repo}(operation_id=gitrepo_pulls_list)。
返回(200):PR 对象数组
[{"id": 1, "number": 1, "title": "feat: #7 ...", "state": "opened",
"head": {"ref": "feat/issue-7"}, "base": {"ref": "main"},
"body": "...", "merged": false, "mergeable": true, "user": {"username":"x"},
"created_at": "...", "html_url": "..."}]
POST /api/git-repo/{owner}/{repo}/pulls
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls create --owner {owner} --repo {repo} --json '{...}'(operation_id=gitrepo_pulls_create)。
入参 Body(JSON,中性):
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
title |
string | ✅ | PR 标题(如 feat: #7 标题) |
head |
string | ✅ | 源分支(push 的分支,如 feat/issue-7) |
base |
string | ✅ | 目标分支(main/master) |
body |
string | 否 | PR 正文 |
related_issue_nums |
array[int] | 否 | 关联 Issue 号 [7, 8] |
auto_close_issues |
bool | 否 | true=合并后自动关闭关联 Issue |
关联 Issue 的关闭由服务端按 provider 适配(gitcode 原生支持;github 自动在 body 追加
Closes #N)。
发布前一致性断言(Issue #2225):标题首个#N、正文主语Issue #N、related_issue_nums
必须包含当前会话任务 Issue;不一致返回 409(ISSUE_ASSERT_MISMATCH)且不产生线上写入。该断言无放行参数。
返回(201):创建的 PR 对象。错误:400(缺 head/base)/ 404 / 403 / 409。
GET /api/git-repo/{owner}/{repo}/pulls/{number}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls get --owner {owner} --repo {repo} --number {number}(operation_id=gitrepo_pulls_get)。
返回(200):单个 PR 对象(结构同上)。错误:404。
Issue #2324 附加字段 merge_readiness:统一合并判定结果(QA hit 判定与 pulls merge 前置检查共用
同一实现),不改变存量字段:
"merge_readiness": {
"ready": false,
"verdict": "not_mergeable", // ready | not_mergeable | pending_review | unknown
"label": "待评审/不可合入", // 可合入 | 待评审/不可合入 | 无法判定
"summary": "待评审/不可合入:can_merge_check 未通过(...);approval_reviewers 为空(待指定评审人)",
"missing": [{"code": "can_merge_check", "message": "..."}],
"warnings": ["..."],
"fields": {"mergeable": true, "can_merge_check": false, "...": "..."}
}
组合判定口径(GitCode v5 实测层级):mergeable && can_merge_check &&
approval_reviewers_required_passed;仅当
mergeable_state.merge_request_switch.approval_required_reviewers_count >= 1 时才纳入评审人校验。
None/缺失字段显式视为不可合入并告警,禁止退化为「只看 mergeable」。
| mergeable | can_merge_check | reviewers_required_passed | required_count | approval_reviewers | 判定 |
|---|---|---|---|---|---|
| true | true | true | 1 | 非空 | 可合入 |
| true | true | true | 0 | 空 | 可合入(未开启评审人要求) |
| true | false | false | 1 | 空 | 待评审/不可合入:can_merge_check 未通过 + 待指定评审人 |
| true | true | false | 1 | 非空(pending) | 待评审/不可合入:必需评审人未满足 |
| true | true | false | 1 | 空 | 待评审/不可合入:待指定评审人 |
| false / 缺失 | * | * | * | * | 待评审/不可合入(缺失项告警) |
字段层级:
mergeable/can_merge_check在顶层;approval_reviewers_required_passed在
mergeable_state;approval_required_reviewers_count在mergeable_state.merge_request_switch。
非 GitCode provider(mergeable_state为字符串枚举)返回verdict=unknown,合入路径按原流程继续。
PATCH /api/git-repo/{owner}/{repo}/pulls/{number}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls update --owner {owner} --repo {repo} --number {number} --json '{...}'(operation_id=gitrepo_pulls_update)。
入参 Body(JSON):title / body / labels(按需更新字段)。
发布前一致性断言(Issue #2225):标题首个
#N/ 正文主语Issue #N与当前会话任务
Issue 不一致 → 409(ISSUE_ASSERT_MISMATCH),不产生线上写入。该断言无放行参数;确需更新其他 Issue/PR 时,请在目标 Issue 自己的会话(或用户 JWT / API Key 身份)下发起。
返回(200):更新后的 PR 对象。错误:409。
PUT /api/git-repo/{owner}/{repo}/pulls/{number}/merge
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls merge --owner {owner} --repo {repo} --number {number}(operation_id=gitrepo_pulls_merge)。
返回(200):{"merged": true, "sha": "..."}。错误:409(不可合并)。
Issue #2324 合入前置检查:合入前按统一判定(与 pulls get 的 merge_readiness 同一实现)校验
mergeable && can_merge_check && approval_reviewers_required_passed,不满足时 409:
{"detail": {"code": 409,
"message": "merge blocked by merge readiness gate (Issue #2324): 待评审/不可合入:...",
"gate": {"phase": "pre_merge_readiness", "verdict": "pending_review",
"missing": [{"code": "reviewers_not_assigned", "message": "approval_reviewers 为空(待指定评审人)"}],
"warnings": []}}}
GET/POST /api/git-repo/{owner}/{repo}/pulls/{number}/comments
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls comments list --owner {owner} --repo {repo} --number {number}/pulls comments create ... --json '{"body":"LGTM"}'(operation_id=gitrepo_pulls_comments_list/gitrepo_pulls_comments_create;create 仅接受body,不支持path/line等行内定位字段)。
- GET:返回 PR 行内评论数组
[{"id":1,"body":"LGTM","path":"a.py","line":5,"user":{"username":"x"},"created_at":"..."}] - POST:Body
{body}(必填)→ 返回(201)评论对象。 - PUT /pulls/{number}/comments/{discussion_id}:标记 resolved → 更新后的评论对象。
发布前一致性断言(Issue #2225 P1):PR 评论正文首个
Issue #N主语锚点与当前会话
任务不一致(疑似暂存被覆写)返回 409(ISSUE_ASSERT_MISMATCH)且不产生线上写入。
PR 号与任务 Issue 号属不同号段空间,不做目标号比对;讨论区 chat 会话代发与用户
JWT / API Key 不受影响。
PATCH/DELETE /api/git-repo/{owner}/{repo}/pulls/{number}/comments/{comment_id}
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls comments update --owner {owner} --repo {repo} --number {number} --comment_id {id} --json '{"body":"修正后的内容"}'/grape git-repo pulls comments delete --owner {owner} --repo {repo} --number {number} --comment_id {id}(operation_id=gitrepo_pulls_comments_update/gitrepo_pulls_comments_delete;comment_id 取 list 返回的评论 id,上游全局唯一,PR number 仅作上下文)。
- PATCH:Body
{body}(必填)→ 透传PATCH /pulls/comments/{id}改写评论正文 → 返回更新后的评论对象。 - Issue #623 契约说明:
--comment_id支持数字 note_id(推荐,list 响应中的id)与 create 响应返回的十六进制id(兼容形态,服务端自动映射为 note_id)——直接回用 create 响应主键不再 422;其余非法形态返回 422(明确错误信息)。十六进制识别规则:全 hex 字符且含 a-f 字母(纯数字串按 note_id 直传,向后兼容)。 - DELETE:透传
DELETE /pulls/comments/{id}删除评论(误发评论自纠,Issue #563;delete 的 comment_id 仍为数字 note_id)。 - create --json @file 读取失败:CLI ≥0.3.3 已显式报错
cannot read --json file '...'并以非零码退出,不再静默。
GET /api/git-repo/{owner}/{repo}/pulls/{number}/files
scope(x-token-scope):session_only(仅
tk_会话 token;API Key / JWT → 403)
CLI:grape git-repo pulls files --owner {owner} --repo {repo} --number {number}(operation_id=gitrepo_pulls_files)。
返回(200):变更文件数组
[{"filename": "src/a.py", "status": "modified", "additions": 10, "deletions": 2,
"patch": "@@ ...", "raw_url": "..."}]
通用错误(全接口一致)
detail.message英文;格式见README.md「错误响应统一格式」。
| 码 | 含义(message 英文) | body |
|---|---|---|
| 401 | Unauthorized(缺失/无效 API Key) | {"detail":{"code":401,"message":"Not logged in"}} |
| 403 | Forbidden(写操作/白名单外 path/未知 provider) | {"detail":{"code":403,"message":"Forbidden"}} |
| 404 | Not Found(资源不存在/无权) | {"detail":{"code":404,"message":"Not Found"}} |
| 400 | Bad Request(参数缺失/校验失败) | {"detail":{"code":400,"message":"Invalid parameter"}} |
| 502 | Bad Gateway(平台 token 解析失败) | {"detail":{"code":502,"message":"Git repository token resolve failed"}} |
| 429 | Too Many Requests(平台限流 400/min,4000/hour) | 透传 |
技能迁移后:
gitcode-api-reference.md中api.gitcode.com/api/v5→{{SERVER_URL}}/api/git-repo/...;认证$BOT_GITCODE_TOKEN→$GRAPE_API_TOKEN。
9. CLI 使用样例(每接口一条)
语法:
grape git-repo <资源> [<动作>] --owner O --repo R [--flag ...] [--json '{...}'] [--json-out];
provider 由会话 token 派生,无需传。具体子命令/flag 以grape git-repo <资源> --help为准,以下为典型用法。
| 接口 | CLI 样例 |
|---|---|
| GET仓库 | grape git-repo repo --owner Jason7602 --repo hello |
| GET/branches | grape git-repo branches --owner Jason7602 --repo hello |
| GET/tree | grape git-repo tree --owner Jason7602 --repo hello --ref main --depth single |
| GET/contents 根 | grape git-repo contents --owner Jason7602 --repo hello |
| GET/contents/{path} | grape git-repo contents --owner Jason7602 --repo hello --path CODEOWNERS --ref codeowner |
| GET/commits | grape git-repo commits --owner Jason7602 --repo hello --per-page 10(按分支/SHA:--sha weekly-11,需 grape-cli >= 0.3.3) |
| GET/labels | grape git-repo labels --owner Jason7602 --repo hello |
| POST/labels | grape git-repo labels create --owner Jason7602 --repo hello --json '{"name":"bug","color":"d73a4a"}' |
| GET/milestones | grape git-repo milestones --owner Jason7602 --repo hello --state all |
| GET/issues | grape git-repo issues --owner Jason7602 --repo hello --state open |
| POST/issues | grape git-repo issues create --owner Jason7602 --repo hello --json '{"title":"bug: 列表为空","body":"复现步骤...","assignee":"Jason7602"}' |
| GET/issues/{number} | grape git-repo issues get --owner Jason7602 --repo hello --number 5 |
| PATCH/issues/{number} | grape git-repo issues update --owner Jason7602 --repo hello --number 5 --json '{"state":"reopen","labels":"bug"}' |
| POST/issues/{number}/close | grape git-repo issues close --owner Jason7602 --repo hello --number 5 |
| GET/issues comments | grape git-repo issues comments list --owner Jason7602 --repo hello --number 5 |
| POST/issues comments | grape git-repo issues comments create --owner Jason7602 --repo hello --number 5 --json '{"body":"已定位原因"}' |
| GET/pulls | grape git-repo pulls --owner Jason7602 --repo hello |
| POST/pulls | grape git-repo pulls create --owner Jason7602 --repo hello --json '{"title":"feat: #7","head":"feat/7","base":"main","related_issue_nums":[7],"auto_close_issues":true}' --json-out |
| GET/pulls/{number} | grape git-repo pulls get --owner Jason7602 --repo hello --number 7 |
| PATCH/pulls/{number} | grape git-repo pulls update --owner Jason7602 --repo hello --number 7 --json '{"title":"feat: #7 (v2)","labels":"lgtm"}' |
| PUT/pulls/{number}/merge | grape git-repo pulls merge --owner Jason7602 --repo hello --number 7 |
| GET/POST pulls comments | grape git-repo pulls comments list --owner Jason7602 --repo hello --number 7 / ... comments create ... --json '{"body":"LGTM","path":"a.py","line":5}' |
| GET/pulls/{number}/files | grape git-repo pulls files --owner Jason7602 --repo hello --number 7 |
| DELETE/pulls/{number}/issues/{issue_number} | grape git-repo pulls issues unlink --owner Jason7602 --repo hello --number 7 --issue-number 5 |
PR body 提及 Issue 号的规避写法(Issue #728)
pulls create 在调用方未显式传 related_issue_nums 时,会从 body 自动提取 Issue 号建立 PR↔Issue 关联。提取前有上下文过滤:代码块(``` 围栏)、行内反引号、引用块(> 行)内的 #N 属非语义语境,不会被捕获;正文自然语言 #N 与 Closes # / Related # / 关联 # 显式写法正常关联。
- 想在 body 里提及但不关联某个 Issue:把编号放进代码块或用反引号包裹,如
`#123`。 - 想确保一定关联:正文显式写
Closes #N/Related #N/关联 #N,或 create 时直接传related_issue_nums(显式参数优先,不受过滤影响)。 - 已误建立的关联可用 unlink 命令解除(幂等,重复调用安全):
bash grape git-repo pulls issues unlink --owner Jason7602 --repo hello --number 7 --issue-number 5 # {"unlinked": true, "pr_number": 7, "issue_number": 5, "remaining_issue_nums": [...]}
关联不存在时返回unlinked=false(幂等 no-op);GitHub 通道无原生 PR↔Issue 关联存储,恒为 no-op。