受保护的分支 API

有效的访问级别

访问级别定义在方法 ProtectedRefAccess.allowed_access_levels 中。目前,这些级别是已经确认的:

0  => No access
30 => Developer access
40 => Maintainer access
60 => Admin access

列出受保护的分支

从项目中获取在 UI 中定义的受保护分支的列表。如果设置了通配符,则直接返回通配符而不是与该通配符匹配的分支的确切名称。

GET /projects/:id/protected_branches
参数 类型 是否必需 描述
id integer/string yes 认证用户拥有的 ID 或者项目 URL 编码路径
search string no 要搜索的受保护分支的名称或部分名称
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches"

响应示例:

[
  {
    "id": 1,
    "name": "master",
    "push_access_levels": [
      {
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "merge_access_levels": [
      {
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  {
    "id": 1,
    "name": "release/*",
    "push_access_levels": [
      {
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "merge_access_levels": [
      {
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  ...
]

专业版或更高级别的用户还可以看到 user_idgroup_id 参数:

响应示例:

[
  {
    "id": 1,
    "name": "master",
    "push_access_levels": [
      {
        "access_level": 40,
        "user_id": null,
        "group_id": null,
        "access_level_description": "Maintainers"
      }
    ],
    "merge_access_levels": [
      {
        "access_level": null,
        "user_id": null,
        "group_id": 1234,
        "access_level_description": "Example Merge Group"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  ...
]

获取单个受保护分支或受保护分支通配符

获取单个受保护分支或通配符保护分支。

GET /projects/:id/protected_branches/:name
参数 类型 是否必需 描述
id integer/string yes 认证用户拥有的 ID 或者项目 URL 编码路径
name string yes 分支或通配符的名称
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/master"

响应示例:

{
  "id": 1,
  "name": "master",
  "push_access_levels": [
    {
      "access_level": 40,
      "access_level_description": "Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "access_level": 40,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

专业版或更高级别的用户还可以看到 user_idgroup_id 参数:

响应示例:

{
  "id": 1,
  "name": "master",
  "push_access_levels": [
    {
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "access_level": null,
      "user_id": null,
      "group_id": 1234,
      "access_level_description": "Example Merge Group"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

保护仓库分支

使用受保护分支通配符保护单个存储库分支或多个项目仓库分支。

POST /projects/:id/protected_branches
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&push_access_level=30&merge_access_level=30&unprotect_access_level=40"
参数 类型 是否必需 描述
id integer/string yes 认证用户拥有的 ID 或者项目 URL 编码路径
name string yes 分支或通配符的名称
push_access_level integer no 允许推送的访问级别(默认值:40,维护者角色)
merge_access_level integer no 允许合并的访问级别(默认值:40,维护者角色)
unprotect_access_level integer no 允许取消保护的访问级别(默认值:40,维护者角色)
allow_force_push boolean no 允许所有具有推送权限的用户强制推送(默认:false
allowed_to_push array no 允许推送的访问级别数组,每个级别由这些 Hash 格式定义:{user_id: integer}{group_id: integer},或者 {access_level: integer}
allowed_to_merge array no 允许合并的访问级别数组,每个级别由这些 Hash 格式定义:{user_id: integer}{group_id: integer},或者 {access_level: integer}
allowed_to_unprotect array no 允许取消保护的访问级别数组,每个级别由这些 Hash 格式定义:{user_id: integer}{group_id: integer},或者 {access_level: integer}
code_owner_approval_required boolean no 如果分支在 CODEOWNERS 文件 中,则阻止推送到此分支。(默认值:false

响应示例:

{
  "id": 1,
  "name": "*-stable",
  "push_access_levels": [
    {
      "access_level": 30,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "access_level": 30,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "access_level": 40,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

专业版或更高级别的用户还可以看到 user_idgroup_id 参数:

响应示例:

{
  "id": 1,
  "name": "*-stable",
  "push_access_levels": [
    {
      "access_level": 30,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "access_level": 30,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

具有用户/组级别访问权限的示例

在数组 allowed_to_push / allowed_to_merge / allowed_to_unprotect 中的每一项都应该用这些格式定义:{user_id: integer}{group_id: integer},或者 {access_level: integer}。 每个用户都必须有权访问该项目,并且每个组都必须共享该项目。 这些访问级别允许对受保护的分支访问进行更精细的控制

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&allowed_to_push%5B%5D%5Buser_id%5D=1"

响应示例:

{
  "id": 1,
  "name": "*-stable",
  "push_access_levels": [
    {
      "access_level": null,
      "user_id": 1,
      "group_id": null,
      "access_level_description": "Administrator"
    }
  ],
  "merge_access_levels": [
    {
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

允许推送和允许合并访问的示例

在 13.9 版本中移至专业版。

请求示例:

curl --request POST \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     --header "Content-Type: application/json" \
     --data '{
      "name": "master",
      "allowed_to_push": [{"access_level": 30}],
      "allowed_to_merge": [{
          "access_level": 30
        },{
          "access_level": 40
        }
      ]}'
     "https://gitlab.example.com/api/v4/projects/5/protected_branches"

响应示例:

{
    "id": 5,
    "name": "master",
    "push_access_levels": [
        {
            "access_level": 30,
            "access_level_description": "Developers + Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "merge_access_levels": [
        {
            "access_level": 30,
            "access_level_description": "Developers + Maintainers",
            "user_id": null,
            "group_id": null
        },
        {
            "access_level": 40,
            "access_level_description": "Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "unprotect_access_levels": [
        {
            "access_level": 40,
            "access_level_description": "Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
}

取消保护存储库分支

取消保护给定的受保护分支或者受保护分支通配符。

DELETE /projects/:id/protected_branches/:name
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/*-stable"
参数 类型 是否必需 描述
id integer/string yes 认证用户拥有的 ID 或者项目 URL 编码路径
name string yes 分支的名称

需要代码所有者批准单个分支

更新给定受保护分支的“需要代码所有者批准”选项。

PATCH /projects/:id/protected_branches/:name
curl --request PATCH --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/feature-branch?code_owner_approval_required=true"
参数 类型 是否必需 描述
id integer/string yes 认证用户拥有的 ID 或者项目 URL 编码路径
name string yes 分支的名称
code_owner_approval_required boolean no 如果分支在 CODEOWNERS 文件中,则阻止推送到此分支。(默认值:false