受保护分支 API
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
使用此 API 来管理您仓库的受保护分支。
GitLab Premium 和 GitLab Ultimate 支持对分支推送提供更细粒度的保护。管理员可以只允许部署密钥修改和推送到受保护分支,而不是特定用户。
有效的访问级别
ProtectedRefAccess.allowed_access_levels 方法定义了以下访问级别:
0: 无访问权限30: 开发者角色40: 维护者角色60: 管理员
列出受保护分支
从项目中获取受保护分支列表,这些分支在 UI 中定义。如果设置了通配符,则返回匹配该通配符的分支的确切名称。
GET /projects/:id/protected_branches| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer or string | yes | 项目 ID 或项目的 URL 编码路径 |
search |
string | no | 要搜索的受保护分支的名称或名称的一部分 |
在以下示例中,项目 ID 为 5。
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/5/protected_branches"以下示例响应包括:
- ID 为
100和101的两个受保护分支。 - ID 为
1001、1002和1003的push_access_levels。 - ID 为
2001和2002的merge_access_levels。
[
{
"id": 100,
"name": "main",
"push_access_levels": [
{
"id": 1001,
"access_level": 40,
"access_level_description": "Maintainers"
},
{
"id": 1002,
"access_level": 40,
"access_level_description": "Deploy key",
"deploy_key_id": 1
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
},
{
"id": 101,
"name": "release/*",
"push_access_levels": [
{
"id": 1003,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 2002,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
},
...
]GitLab Premium 或 Ultimate 用户还会看到 user_id、group_id 和 inherited 参数。如果存在 inherited 参数,则表示该设置是从项目的组继承的。
以下示例响应包括:
- ID 为
100的一个受保护分支。 - ID 为
1001和1002的push_access_levels。 - ID 为
2001的merge_access_levels。
[
{
"id": 101,
"name": "main",
"push_access_levels": [
{
"id": 1001,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
},
{
"id": 1002,
"access_level": 40,
"access_level_description": "Deploy key",
"deploy_key_id": 1,
"user_id": null,
"group_id": null
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": null,
"user_id": null,
"group_id": 1234,
"access_level_description": "Example Merge Group"
}
],
"allow_force_push":false,
"code_owner_approval_required": false,
"inherited": true
},
...
]获取单个受保护分支或通配符受保护分支
获取单个受保护分支或通配符受保护分支。
GET /projects/:id/protected_branches/:name| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer or string | yes | 项目 ID 或项目的 URL 编码路径 |
name |
string | yes | 分支名称或通配符 |
在以下示例中,项目 ID 为 5,分支名称为 main:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/5/protected_branches/main"示例响应:
{
"id": 101,
"name": "main",
"push_access_levels": [
{
"id": 1001,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}GitLab Premium 或 Ultimate 用户还会看到 user_id 和 group_id 参数:
示例响应:
{
"id": 101,
"name": "main",
"push_access_levels": [
{
"id": 1001,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 2001,
"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| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer or string | yes | 项目 ID 或项目的 URL 编码路径。 |
name |
string | yes | 分支名称或通配符。 |
allow_force_push |
boolean | no | 启用时,可以推送到此分支的成员也可以强制推送。(默认:false) |
allowed_to_merge |
array | no | 合并访问级别的数组,每个由哈希描述,形式为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。仅限 Premium 和 Ultimate。 |
allowed_to_push |
array | no | 推送访问级别的数组,每个由哈希描述,形式为 {user_id: integer}、{group_id: integer}、{deploy_key_id: integer} 或 {access_level: integer}。仅限 Premium 和 Ultimate。 |
allowed_to_unprotect |
array | no | 取消保护访问级别的数组,每个由哈希描述,形式为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。访问级别 No access 不适用于此字段。仅限 Premium 和 Ultimate。 |
code_owner_approval_required |
boolean | no | 如果此分支与 CODEOWNERS 文件中的项目匹配,则阻止推送到此分支。(默认:false)仅限 Premium 和 Ultimate。 |
merge_access_level |
integer | no | 允许合并的访问级别。(默认:40,维护者角色) |
push_access_level |
integer | no | 允许推送的访问级别。(默认:40,维护者角色) |
unprotect_access_level |
integer | no | 允许取消保护的访问级别。(默认:40,维护者角色) |
在以下示例中,项目 ID 为 5,分支名称为 *-stable。
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "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 为
101的受保护分支。 - ID 为
1001的push_access_levels。 - ID 为
2001的merge_access_levels。 - ID 为
3001的unprotect_access_levels。
{
"id": 101,
"name": "*-stable",
"push_access_levels": [
{
"id": 1001,
"access_level": 30,
"access_level_description": "Developers + Maintainers"
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": 30,
"access_level_description": "Developers + Maintainers"
}
],
"unprotect_access_levels": [
{
"id": 3001,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}GitLab Premium 或 Ultimate 用户还会看到 user_id 和 group_id 参数:
以下示例响应包括:
- ID 为
101的受保护分支。 - ID 为
1001的push_access_levels。 - ID 为
2001的merge_access_levels。 - ID 为
3001的unprotect_access_levels。
{
"id": 1,
"name": "*-stable",
"push_access_levels": [
{
"id": 1001,
"access_level": 30,
"user_id": null,
"group_id": null,
"access_level_description": "Developers + Maintainers"
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": 30,
"user_id": null,
"group_id": null,
"access_level_description": "Developers + Maintainers"
}
],
"unprotect_access_levels": [
{
"id": 3001,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}用户推送访问和组合并访问示例
- Tier: Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
allowed_to_push / allowed_to_merge / allowed_to_unprotect 数组中的元素应采用 {user_id: integer}、{group_id: integer} 或 {access_level: integer} 的形式。
每个用户必须有权访问项目,每个组必须共享此项目。
这些访问级别允许对受保护分支访问进行更细粒度的控制。
有关更多信息,请参阅配置组权限。
以下示例请求创建一个具有用户推送访问和组合并访问的受保护分支。
user_id 为 2,group_id 为 3。
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&allowed_to_push%5B%5D%5Buser_id%5D=2&allowed_to_merge%5B%5D%5Bgroup_id%5D=3"以下示例响应包括:
- ID 为
101的受保护分支。 - ID 为
1001的push_access_levels。 - ID 为
2001的merge_access_levels。 - ID 为
3001的unprotect_access_levels。
{
"id": 101,
"name": "*-stable",
"push_access_levels": [
{
"id": 1001,
"access_level": null,
"user_id": 2,
"group_id": null,
"access_level_description": "Administrator"
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": null
"user_id": null,
"group_id": 3,
"access_level_description": "Example Merge Group"
}
],
"unprotect_access_levels": [
{
"id": 3001,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}部署密钥访问示例
- Tier: Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
allowed_to_push 数组中的元素应采用 {user_id: integer}、{group_id: integer}、{deploy_key_id: integer} 或 {access_level: integer} 的形式。
部署密钥必须为您的项目启用,并且必须具有对您项目仓库的写入权限。
有关其他要求,请参阅允许部署密钥推送到受保护分支。
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&allowed_to_push[][deploy_key_id]=1"以下示例响应包括:
- ID 为
101的受保护分支。 - ID 为
1001的push_access_levels。 - ID 为
2001的merge_access_levels。 - ID 为
3001的unprotect_access_levels。
{
"id": 101,
"name": "*-stable",
"push_access_levels": [
{
"id": 1001,
"access_level": null,
"user_id": null,
"group_id": null,
"deploy_key_id": 1,
"access_level_description": "Deploy"
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"unprotect_access_levels": [
{
"id": 3001,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}允许推送和允许合并访问示例
- Tier: Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
示例请求:
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/projects/5/protected_branches"以下示例响应包括:
- ID 为
105的受保护分支。 - ID 为
1001的push_access_levels。 - ID 为
2001和2002的merge_access_levels。 - ID 为
3001的unprotect_access_levels。
{
"id": 105,
"name": "main",
"push_access_levels": [
{
"id": 1001,
"access_level": 30,
"access_level_description": "Developers + Maintainers",
"user_id": null,
"group_id": null
}
],
"merge_access_levels": [
{
"id": 2001,
"access_level": 30,
"access_level_description": "Developers + Maintainers",
"user_id": null,
"group_id": null
},
{
"id": 2002,
"access_level": 40,
"access_level_description": "Maintainers",
"user_id": null,
"group_id": null
}
],
"unprotect_access_levels": [
{
"id": 3001,
"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| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer or string | yes | 项目 ID 或项目的 URL 编码路径 |
name |
string | yes | 分支名称 |
在以下示例中,项目 ID 为 5,分支名称为 *-stable。
curl --request DELETE \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/5/protected_branches/*-stable"更新受保护分支
更新受保护分支。
PATCH /projects/:id/protected_branches/:name| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer or string | yes | 项目 ID 或项目的 URL 编码路径。 |
name |
string | yes | 分支名称或通配符。 |
allow_force_push |
boolean | no | 启用时,可以推送到此分支的成员也可以强制推送。 |
allowed_to_merge |
array | no | 合并访问级别的数组,每个由哈希描述,形式为 {user_id: integer}、{group_id: integer} 或 {access_level: integer}。仅限 Premium 和 Ultimate。 |
allowed_to_push |
array | no | 推送访问级别的数组,每个由哈希描述,形式为 {user_id: integer}、{group_id: integer}、{deploy_key_id: integer} 或 {access_level: integer}。仅限 Premium 和 Ultimate。 |
allowed_to_unprotect |
array | no | 取消保护访问级别的数组,每个由哈希描述,形式为 {user_id: integer}、{group_id: integer}、{access_level: integer} 或 {id: integer, _destroy: true} 来销毁现有的访问级别。访问级别 No access 不适用于此字段。仅限 Premium 和 Ultimate。 |
code_owner_approval_required |
boolean | no | 如果此分支与 CODEOWNERS 文件中的项目匹配,则阻止推送到此分支。仅限 Premium 和 Ultimate。 |
在以下示例中,项目 ID 为 5,分支名称为 feature-branch。
curl --request PATCH \
--header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/5/protected_branches/feature-branch?allow_force_push=true&code_owner_approval_required=true"allowed_to_push、allowed_to_merge 和 allowed_to_unprotect 数组中的元素应为 user_id、group_id 或 access_level 之一,并采用 {user_id: integer}、{group_id: integer} 或 {access_level: integer} 的形式。
allowed_to_push 包含一个额外的元素 deploy_key_id,采用 {deploy_key_id: integer} 的形式。
要更新:
user_id: 确保更新的用户有权访问项目。您还必须在相应的哈希中传递access_level的id。group_id: 确保更新的组共享此项目。 您还必须在相应的哈希中传递access_level的id。deploy_key_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/projects/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/projects/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/projects/22034114/protected_branches/main"示例响应:
{
"name": "main",
"push_access_levels": []
}