跳转至

创建分支

POST /api/v5/repos/{owner}/{repo}/branches

该接口用于在指定仓库中创建新分支,可指定分支起点、新分支名称及描述信息,创建成功后返回新分支的名称、描述、指向的提交信息,以及是否为默认分支或保护分支等属性。

访问令牌支持说明

  • AtomGit 用户访问令牌(AtomGit user access tokens)

是否允许匿名访问

此接口不允许匿名访问

请求参数

参数名 位置 类型 必填 说明
owner path string 是 仓库所属空间地址(企业、组织或个人的地址path)
repo path string 是 仓库路径(path)
access_token query string 是 用户授权码

Request Body:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "refs": {
      "description": "起点名称",
      "type": "string",
      "default": "master"
    },
    "branch_name": {
      "description": "新创建的分支名称",
      "type": "string"
    },
    "description": {
      "description": "分支描述信息",
      "type": "string"
    }
  },
  "required": ["refs", "branch_name"]
}

响应参数

字段 类型 必返 可为空 说明
name string 是 否 新创建的分支名称
description string 是 否 分支描述信息
commit object 是 否 分支所指向的提交信息
default_branch boolean 是 否 是否为默认分支
protected boolean 是 否 是否为保护分支

commit 对象

字段 类型 必返 可为空 说明
commit.commit object 是 否 提交内容
commit.sha string 是 否 提交的 SHA 值
commit.url string 是 否 提交详情链接

commit.commit 对象

字段 类型 必返 可为空 说明
commit.commit.author object 是 否 作者信息
commit.commit.committer object 是 否 提交者信息
commit.commit.message string 是 否 提交信息

commit.commit.author 对象

字段 类型 必返 可为空 说明
commit.commit.author.name string 是 否 作者名称
commit.commit.author.date string 是 否 作者提交时间
commit.commit.author.email string 是 否 作者邮箱

commit.commit.committer 对象

字段 类型 必返 可为空 说明
commit.commit.committer.name string 是 否 提交者名称
commit.commit.committer.date string 是 否 提交时间
commit.commit.committer.email string 是 否 提交者邮箱

失败响应

400 参数错误

{
  "error": "invalid_request",
  "message": "参数 branch_name 不能为空",
  "field": "branch_name"
}

401 未认证

{
  "error": "unauthorized",
  "message": "缺少或无效的访问令牌"
}

错误字段名以后端实际返回为准。

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误(Bad Request)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 资源不存在(Not Found)
500 系统内部错误(Internal Server Error)

请求示例

参数说明

<owner> 替换为仓库所属空间地址(企业、组织或个人的地址path); <repo> 替换为仓库路径(path); <token> 替换为你的 AtomGit 用户访问令牌。

cURL

curl -X POST 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/branches?access_token=<token>' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d '{
  "refs": "master",
  "branch_name": "feature/test-branch",
  "description": "测试分支"
}'

JavaScript

const response = await fetch(
  "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/branches?access_token=<token>",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json",
    },
    body: JSON.stringify({
      refs: "master",
      branch_name: "feature/test-branch",
      description: "测试分支",
    }),
  }
);

if (!response.ok) {
  throw new Error(`请求失败: ${response.status}`);
}

const data = await response.json();
console.log(data);

Python

import requests

resp = requests.post(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/branches",
    params={"access_token": "<token>"},
    headers={
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
    json={
        "refs": "master",
        "branch_name": "feature/test-branch",
        "description": "测试分支",
    },
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "name": "gitcode",
  "description": "测试分支",
  "commit": {
    "commit": {
      "author": {
        "name": "dengmengmian",
        "date": "2024-09-10T08:29:18Z",
        "email": "dengmengmian@noreply.atomgit.com"
      },
      "committer": {
        "name": "dengmengmian",
        "date": "2024-09-10T08:29:18Z",
        "email": "dengmengmian@noreply.atomgit.com"
      },
      "message": "test"
    },
    "sha": "1af35823b8bbcaf68776cd6cb0ecbf88cfdec905",
    "url": "https://api.atomgit.com/api/v5/repos/dengmengmian/test03/commits/1af35823b8bbcaf68776cd6cb0ecbf88cfdec905"
  },
  "default_branch": false,
  "protected": false
}

错误码

错误码 说明
400 参数错误或请求无法解析
401 未认证或凭证无效(缺少/无效的访问凭证)
403 无权限执行该操作
404 请求路径或资源不存在
500 系统内部错误

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc