{{< details >}}
- Tier: 专业版, 旗舰版
- Offering: 私有化部署
{{< /details >}}
{{< history >}}
- 引入于极狐GitLab 15.9,使用名为
group_protected_branches
的功能标志。默认禁用。 - 在极狐GitLab 15.11 中,功能标志
group_protected_branches
重命名为allow_protected_branches_for_group
。 - 在极狐GitLab 17.6 中 GA。功能标志
group_protected_branches
被移除。
{{< /history >}}
使用群组的受保护分支 API 来管理受保护分支规则。它提供了列出、创建、更新和删除适用于群组所属项目的受保护分支规则的端点。
{{< alert type=”warning” >}}
群组的受保护分支设置仅限于顶级群组。
{{< /alert >}}
有效访问级别
访问级别在 ProtectedRefAccess.allowed_access_levels
方法中定义。这些级别包括:
0 => 无访问
30 => 开发者访问
40 => 维护者访问
60 => 管理员访问
列出受保护分支
从群组中获取受保护分支的列表。如果设置了通配符,则返回匹配该通配符的分支的确切名称。
GET /groups/:id/protected_branches
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
整数或字符串 | 是 | 群组的 ID 或 URL 编码路径。 |
search |
字符串 | 否 | 要搜索的受保护分支的名称或部分名称。 |
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches"
示例响应:
[
{
"id": 1,
"name": "main",
"push_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": 1234,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": 1234,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
},
{
"id": 1,
"name": "release/*",
"push_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": 1234,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
},
...
]
获取单个受保护分支或通配符受保护分支
获取单个受保护分支或通配符受保护分支。
GET /groups/:id/protected_branches/:name
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
整数或字符串 | 是 | 群组的 ID 或 URL 编码路径。 |
name |
字符串 | 是 | 分支或通配符的名称。 |
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches/main"
示例响应:
{
"id": 1,
"name": "main",
"push_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"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 /groups/:id/protected_branches
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches?name=*-stable&push_access_level=30&merge_access_level=30&unprotect_access_level=40"
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
整数或字符串 | 是 | 群组的 ID 或 URL 编码路径。 |
name |
字符串 | 是 | 分支或通配符的名称。 |
allow_force_push |
布尔值 | 否 | 允许具有推送访问权限的所有用户进行强制推送。默认值:false 。 |
allowed_to_merge |
数组 | 否 | 允许合并的访问级别数组,每个由 {user_id: integer} 、{group_id: integer} 或 {access_level: integer} 形式的哈希描述。 |
allowed_to_push |
数组 | 否 | 允许推送的访问级别数组,每个由 {user_id: integer} 、{group_id: integer} 或 {access_level: integer} 形式的哈希描述。 |
allowed_to_unprotect |
数组 | 否 | 允许取消保护的访问级别数组,每个由 {user_id: integer} 、{group_id: integer} 或 {access_level: integer} 形式的哈希描述。 |
code_owner_approval_required |
布尔值 | 否 | 如果该分支匹配 CODEOWNERS 文件 中的项目,则阻止推送。默认值:false 。 |
merge_access_level |
整数 | 否 | 允许合并的访问级别。默认值:40 ,维护者角色。 |
push_access_level |
整数 | 否 | 允许推送的访问级别。默认值:40 ,维护者角色。 |
unprotect_access_level |
整数 | 否 | 允许取消保护的访问级别。默认值:40 ,维护者角色。 |
示例响应:
{
"id": 1,
"name": "*-stable",
"push_access_levels": [
{
"id": 1,
"access_level": 30,
"user_id": null,
"group_id": null,
"access_level_description": "Developers + Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 30,
"user_id": null,
"group_id": null,
"access_level_description": "Developers + Maintainers"
}
],
"unprotect_access_levels": [
{
"id": 1,
"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>" \
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches?name=*-stable&allowed_to_push%5B%5D%5Buser_id%5D=1"
示例响应:
{
"id": 1,
"name": "*-stable",
"push_access_levels": [
{
"id": 1,
"access_level": null,
"user_id": 1,
"group_id": null,
"access_level_description": "Administrator"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"unprotect_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}
允许推送和允许合并访问示例
示例请求:
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
--data '{
"name": "main",
"allowed_to_push": [{"access_level": 30}],
"allowed_to_merge": [{
"access_level": 30
},{
"access_level": 40
}
]}'
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches"
示例响应:
{
"id": 5,
"name": "main",
"push_access_levels": [
{
"id": 1,
"access_level": 30,
"access_level_description": "Developers + Maintainers",
"user_id": null,
"group_id": null
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 30,
"access_level_description": "Developers + Maintainers",
"user_id": null,
"group_id": null
},
{
"id": 2,
"access_level": 40,
"access_level_description": "Maintainers",
"user_id": null,
"group_id": null
}
],
"unprotect_access_levels": [
{
"id": 1,
"access_level": 40,
"access_level_description": "Maintainers",
"user_id": null,
"group_id": null
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}
取消保护仓库分支
取消保护给定的受保护分支或通配符受保护分支。
DELETE /groups/:id/protected_branches/:name
curl --request DELETE \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches/*-stable"
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
整数或字符串 | 是 | 群组的 ID 或 URL 编码路径。 |
name |
字符串 | 是 | 分支的名称。 |
示例响应:
{
"name": "main",
"push_access_levels": [
{
"id": 12,
"access_level": 40,
"access_level_description": "Maintainers",
"user_id": null,
"group_id": null
}
]
}
更新受保护分支
更新受保护分支。
PATCH /groups/:id/protected_branches/:name
curl --request PATCH \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/5/protected_branches/feature-branch?allow_force_push=true&code_owner_approval_required=true"
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
整数或字符串 | 是 | 群组的 ID 或 URL 编码路径。 |
name |
字符串 | 是 | 分支的名称。 |
allow_force_push |
布尔值 | 否 | 启用后,能够推送到此分支的成员也可以进行强制推送。 |
allowed_to_push |
数组 | 否 | 推送访问级别数组,每个由哈希描述。 |
allowed_to_merge |
数组 | 否 | 合并访问级别数组,每个由哈希描述。 |
allowed_to_unprotect |
数组 | 否 | 取消保护访问级别数组,每个由哈希描述。 |
code_owner_approval_required |
布尔值 | 否 | 如果该分支匹配 CODEOWNERS 文件 中的项目,则阻止推送。默认值:false 。 |
allowed_to_push
、allowed_to_merge
和 allowed_to_unprotect
数组中的元素应该:
- 是
user_id
、group_id
或access_level
之一。 - 采用
{user_id: integer}
、{group_id: integer}
或{access_level: integer}
的形式。
要更新:
-
user_id
: 确保更新的用户有权访问项目。还必须传递各自哈希中的access_level
的id
。 -
group_id
: 确保更新的群组共享此项目。 还必须传递各自哈希中的access_level
的id
。
要删除:
- 必须传递
_destroy
设置为true
。参见以下示例。
示例:创建 push_access_level
记录
curl --header 'Content-Type: application/json' --request PATCH \
--data '{"allowed_to_push": [{access_level: 40}]}' \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/22034114/protected_branches/main"
示例响应:
{
"name": "main",
"push_access_levels": [
{
"id": 12,
"access_level": 40,
"access_level_description": "Maintainers",
"user_id": null,
"group_id": null
}
]
}
示例:更新 push_access_level
记录
curl --header 'Content-Type: application/json' --request PATCH \
--data '{"allowed_to_push": [{"id": 12, "access_level": 0}]' \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/22034114/protected_branches/main"
示例响应:
{
"name": "main",
"push_access_levels": [
{
"id": 12,
"access_level": 0,
"access_level_description": "No One",
"user_id": null,
"group_id": null
}
]
}
示例:删除 push_access_level
记录
curl --header 'Content-Type: application/json' --request PATCH \
--data '{"allowed_to_push": [{"id": 12, "_destroy": true}]}' \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/groups/22034114/protected_branches/main"
示例响应:
{
"name": "main",
"push_access_levels": []
}