Protected branches API

原文:https://docs.gitlab.com/ee/api/protected_branches.html

Protected branches API

注意:此功能是在 GitLab 9.5 中引入的

有效的访问级别

访问级别在ProtectedRefAccess.allowed_access_levels方法中定义. 当前,这些级别被认可:

  1. 0 => No access
  2. 30 => Developer access
  3. 40 => Maintainer access
  4. 60 => Admin access

List protected branches

从项目中获取受保护分支的列表.

  1. GET /projects/:id/protected_branches
Attribute Type Required Description
id integer/string yes 经过身份验证的用户拥有的项目的 ID 或URL 编码路径
search string no 要搜索的受保护分支的名称或名称的一部分
  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches"

响应示例:

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

使用 GitLab Starter,Bronze 或更高版本的用户还将看到user_idgroup_id参数:

响应示例:

  1. [ { "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" } ], "code_owner_approval_required": "false" }, ... ]

Get a single protected branch or wildcard protected branch

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

  1. GET /projects/:id/protected_branches/:name
Attribute Type Required Description
id integer/string yes 经过身份验证的用户拥有的项目的 ID 或URL 编码路径
name string yes 分支或通配符的名称
  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/master"

响应示例:

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

使用 GitLab Starter,Bronze 或更高版本的用户还将看到user_idgroup_id参数:

响应示例:

  1. { "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" } ], "code_owner_approval_required": "false" }

Protect repository branches

使用通配符保护的分支来保护单个存储库分支或几个项目存储库分支.

  1. POST /projects/:id/protected_branches
  1. 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"
Attribute Type Required Description
id integer/string yes 经过身份验证的用户拥有的项目的 ID 或URL 编码路径
name string yes 分支或通配符的名称
push_access_level string no 允许推送的访问级别(默认值: 40 ,维护者访问级别)
merge_access_level string no 允许合并的访问级别(默认值: 40 ,维护者访问级别)
unprotect_access_level string no 允许取消保护的访问级别(默认值: 40 ,维护者访问级别)
allowed_to_push array no 允许推送的访问级别数组,每个访问级别由一个哈希表描述
allowed_to_merge array no 允许合并的访问级别数组,每个访问级别由哈希描述
allowed_to_unprotect array no 允许取消保护的访问级别数组,每个访问级别由一个哈希表描述
code_owner_approval_required boolean no 如果它与CODEOWNERS文件中的项目匹配,则阻止推送到此分支. (默认值:false)

响应示例:

  1. { "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" } ], "code_owner_approval_required": "false" }

使用 GitLab Starter,Bronze 或更高版本的用户还将看到user_idgroup_id参数:

响应示例:

  1. { "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" } ], "code_owner_approval_required": "false" }

Example with user / group level access

allowed_to_push / allowed_to_merge / allowed_to_unprotect数组中的元素应采用{user_id: integer}{group_id: integer}{access_level: integer} . 每个用户必须有权访问该项目,并且每个组都必须共享该项目 . 这些访问级别允许对受保护的分支访问进行更精细的控制,并在 GitLab 10.3 EE 中将其添加到 API 中 .

  1. 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"

响应示例:

  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" } ], "code_owner_approval_required": "false" }

Unprotect repository branches

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

  1. DELETE /projects/:id/protected_branches/:name
  1. curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/*-stable"
Attribute Type Required Description
id integer/string yes 经过身份验证的用户拥有的项目的 ID 或URL 编码路径
name string yes 分支名称

Require code owner approvals for a single branch

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

  1. PATCH /projects/:id/protected_branches/:name
  1. curl --request PATCH --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/feature-branch"
Attribute Type Required Description
id integer/string yes 经过身份验证的用户拥有的项目的 ID 或URL 编码路径
name string yes 分支名称
code_owner_approval_required boolean no 如果它与CODEOWNERS文件中的项目匹配,则阻止推送到此分支. (默认值:false)