Skip to content

CNB 后端 API 速查手册

本文档基于 CNB OpenAPI(https://api.cnb.cool/swagger.json)整理,涵盖所有可用的后端相关端点。

基础信息

  • API 地址https://api.cnb.cool
  • 鉴权方式Authorization: Bearer {token}
  • 返回格式Accept: application/jsonapplication/vnd.cnb.api+json
  • 仓库路径{org}/{repo},例如 cnbnn/my-project

端点列表

文件与内容

方法端点说明备注
GET/{repo}/-/git/contents列出根目录文件返回 entries 数组
GET/{repo}/-/git/contents/{path}列出指定目录或获取文件信息path 支持多级,如 data/users.json
GET/{repo}/-/git/raw/{ref}/{path}获取文件原始内容ref 可以是分支名、tag 名或 commit SHA
POST/{repo}/-/git/blobs创建 Blob(数据块)不产生 commit,返回 SHA
POST/{repo}/-/upload/files获取文件上传 URL返回 upload_url,需 PUT 上传
POST/{repo}/-/upload/imgs获取图片上传 URL返回 upload_url,需 PUT 上传
DELETE/{repo}/-/files/{filePath}删除已上传的附件仅限 UploadFiles 上传的

Commit 与分支

方法端点说明
GET/{repo}/-/git/head获取默认分支
GET/{repo}/-/git/commits获取 commit 列表
GET/{repo}/-/git/commits/{ref}获取指定 commit 详情
GET/{repo}/-/git/compare/{base_head}比较两个 ref 的差异(base..head 格式)
GET/{repo}/-/git/branches列出所有分支
POST/{repo}/-/git/branches创建新分支
GET/{repo}/-/git/branches/{branch}获取指定分支信息
DELETE/{repo}/-/git/branches/{branch}删除分支
GET/{repo}/-/git/tags列出所有 Tag
POST/{repo}/-/git/tags创建 Tag
GET/{repo}/-/git/tags/{tag}获取指定 Tag 信息
DELETE/{repo}/-/git/tags/{tag}删除 Tag

Issue 系统(当作数据表使用)

方法端点说明
GET/{repo}/-/issues查询 Issue 列表
POST/{repo}/-/issues创建 Issue
GET/{repo}/-/issues/{number}获取指定 Issue
PATCH/{repo}/-/issues/{number}更新 Issue
GET/{repo}/-/issues/{number}/labels获取 Issue 标签
POST/{repo}/-/issues/{number}/labels添加标签
DELETE/{repo}/-/issues/{number}/labels/{name}删除标签
GET/{repo}/-/issues/{number}/comments获取 Issue 评论
POST/{repo}/-/issues/{number}/comments创建评论
GET/{repo}/-/labels获取仓库标签列表
POST/{repo}/-/labels创建标签

仓库信息

方法端点说明
GET/{repo}获取仓库详细信息
PATCH/{repo}更新仓库信息(简介、站点、主题、许可证)
DELETE/{repo}删除仓库
GET/{slug}/-/repos列出组织/用户下的仓库
POST/{slug}/-/repos在组织/用户下创建仓库

Release(版本发布)

方法端点说明
GET/{repo}/-/releases列出 Release
POST/{repo}/-/releases创建 Release
GET/{repo}/-/releases/latest获取最新 Release
GET/{repo}/-/releases/tags/{tag}按 Tag 获取 Release

仓库动态

方法端点说明
GET/events/{repo}/-/{date}获取仓库动态

组织

方法端点说明
GET/user/groups获取当前用户加入的组织列表
GET/user/repos获取有权限的仓库列表
POST/groups创建新组织

请求/响应格式

通用请求头

http
Authorization: Bearer {token}
Accept: application/json
Content-Type: application/json

GET /-/git/contents 响应

json
{
  "type": "tree",
  "path": "",
  "sha": "abc123...",
  "entries": [
    {
      "type": "blob",
      "path": "README.md",
      "name": "README.md",
      "sha": "def456...",
      "size": 1234
    },
    {
      "type": "tree",
      "path": "docs",
      "name": "docs",
      "sha": "ghi789...",
      "size": 0
    }
  ]
}

GET /-/git/contents/{file} 响应

json
{
  "type": "blob",
  "path": "data/config.json",
  "name": "config.json",
  "sha": "abc123...",
  "size": 256,
  "encoding": "base64",
  "content": "eyJuYW1lIjoi..."
}

POST /-/git/blobs 请求与响应

请求:

json
{
  "content": "文件内容",
  "encoding": "utf-8"
}

响应:

json
{
  "sha": "3f3571faa3b7454a3ce63e71ef5c909b1b58766a"
}

POST /-/issues 请求与响应

请求:

json
{
  "title": "Issue 标题",
  "body": "Issue 内容(支持 Markdown)",
  "labels": ["bug", "urgent"]
}

POST /{slug}/-/repos(创建仓库)请求

json
{
  "name": "my-repo",
  "description": "仓库描述",
  "visibility": "public"
}

visibility 可选值:publicprivatesecret

错误码

errcode说明
5资源不存在
7Token 权限不足
16用户未登录
10009路径冲突(仓库已存在)

Git Push 写入

完整的数据写入(产生 commit)只能通过 Git Push:

bash
# Git remote URL 格式(无需 .git 后缀)
https://{username}:{token}@cnb.cool/{org}/{repo}

# 示例
git clone https://user:TOKEN@cnb.cool/myorg/myrepo
cd myrepo

# 写入数据
echo '{"users":[]}' > data/users.json
git add data/users.json
git commit -m "feat: init users data"
git push origin main

注意:CNB Git URL 不需要 .git 后缀。

由云锦鸿维护