跳转至

新建保护分支规则

PUT /api/v5/repos/{owner}/{repo}/branches/setting/new

该接口用于为指定仓库新建保护分支规则,通过设置分支或通配符,并指定可推送代码及可合并 Pull Request 的成员角色,从而控制对应分支的代码变更权限。

访问令牌支持说明

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

是否允许匿名访问

此接口不允许匿名访问

请求参数

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

Request Body:

请求体为 JSON 对象,用于指定保护分支规则所匹配的分支以及成员推送/合并权限。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "wildcard": {
      "description": "分支/通配符",
      "type": "string"
    },
    "pusher": {
      "description": "可推送代码成员。develop:仓库管理员和开发者;admin:仓库管理员;maintainer:维护者;多个角色使用英文分号相隔。指定用户传入用户名,多个用户使用英文分号隔开。空字符串:不允许任何人推送",
      "type": "string"
    },
    "merger": {
      "description": "可合并 Pull Request 成员。develop:仓库管理员和开发者;admin:仓库管理员;maintainer:维护者;多个角色使用英文分号相隔。指定用户传入用户名,多个用户使用英文分号隔开。空字符串:不允许任何人合并",
      "type": "string"
    }
  },
  "required": ["wildcard", "pusher", "merger"]
}

响应参数

该接口成功时返回空的 JSON 对象 {},无其他响应字段。

失败响应

参考文档仅描述了 200 成功响应,未提供失败响应示例;失败时的错误字段名以后端实际返回为准。

HTTP 响应状态码

状态码 说明
200 成功(OK)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 资源不存在(Not found)
429 请求过于频繁(Rate limited)
500 系统内部错误(Internal server error)

请求示例

参数说明

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

cURL

curl -X PUT 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/branches/setting/new?access_token=<token>' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d '{
    "wildcard": "master",
    "pusher": "develop",
    "merger": "develop"
  }'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch(
  "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/branches/setting/new?access_token=<token>",
  {
    method: "PUT",
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      wildcard: "master",
      pusher: "develop",
      merger: "develop"
    })
  }
);

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

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

Python

import requests

resp = requests.put(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/branches/setting/new",
    params={"access_token": "<token>"},
    json={
        "wildcard": "master",
        "pusher": "develop",
        "merger": "develop"
    },
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{}

错误码

错误码 说明
401 未认证或凭证无效(缺少/无效的访问凭证)
403 无权限执行该操作
404 仓库或接口路径不存在
405 请求方法不支持
429 请求过于频繁,触发限流
500 系统内部错误

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