Help us learn about your current experience with the documentation. Take the survey.
受保护包 API
- Tier: Free, Premium, Ultimate
- Offering: GitLab Self-Managed
此 API 管理包的保护规则。
列出包保护规则
从项目中获取包保护规则列表。
GET /api/v4/projects/:id/packages/protection/rules支持的属性:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | 项目 ID 或 URL 编码路径。 |
如果成功,返回 200 和包保护规则列表。
可能返回以下状态码:
200 OK: 包保护规则列表。401 Unauthorized: 访问令牌无效。403 Forbidden: 用户无权列出此项目的包保护规则。404 Not Found: 项目未找到。
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/7/packages/protection/rules"示例响应:
[
{
"id": 1,
"project_id": 7,
"package_name_pattern": "@flightjs/flight-package-0",
"package_type": "npm",
"minimum_access_level_for_delete": "owner",
"minimum_access_level_for_push": "maintainer"
},
{
"id": 2,
"project_id": 7,
"package_name_pattern": "@flightjs/flight-package-1",
"package_type": "npm",
"minimum_access_level_for_delete": "owner",
"minimum_access_level_for_push": "maintainer"
}
]创建包保护规则
为项目创建包保护规则。
POST /api/v4/projects/:id/packages/protection/rules支持的属性:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | 项目 ID 或 URL 编码路径。 |
package_name_pattern |
string | 是 | 受保护规则保护的包名。例如 @my-scope/my-package-*。允许使用通配符 *。 |
package_type |
string | 是 | 受保护规则保护的包类型。例如 npm。 |
minimum_access_level_for_delete |
string | 是 | 删除包所需的最低 GitLab 访问级别。有效值包括 null、owner 或 admin。如果值为 null,默认最低访问级别为 maintainer。当未设置 minimum_access_level_for_push 时必须提供。受名为 packages_protected_packages_delete 的功能标志控制。默认禁用。 |
minimum_access_level_for_push |
string | 是 | 推送包所需的最低 GitLab 访问级别。有效值包括 null、maintainer、owner 或 admin。如果值为 null,默认最低访问级别为 developer。当未设置 minimum_access_level_for_delete 时必须提供。 |
如果成功,返回 201 和创建的包保护规则。
可能返回以下状态码:
201 Created: 包保护规则创建成功。400 Bad Request: 包保护规则无效。401 Unauthorized: 访问令牌无效。403 Forbidden: 用户无权创建包保护规则。404 Not Found: 项目未找到。422 Unprocessable Entity: 包保护规则无法创建,例如因为package_name_pattern已被占用。
示例请求:
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
--url "https://gitlab.example.com/api/v4/projects/7/packages/protection/rules" \
--data '{
"package_name_pattern": "package-name-pattern-*",
"package_type": "npm",
"minimum_access_level_for_delete": "owner",
"minimum_access_level_for_push": "maintainer"
}'更新包保护规则
更新项目的包保护规则。
PATCH /api/v4/projects/:id/packages/protection/rules/:package_protection_rule_id支持的属性:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | 项目 ID 或 URL 编码路径。 |
package_protection_rule_id |
integer | 是 | 要更新的包保护规则 ID。 |
package_name_pattern |
string | 否 | 受保护规则保护的包名。例如 @my-scope/my-package-*。允许使用通配符 *。 |
package_type |
string | 否 | 受保护规则保护的包类型。例如 npm。 |
minimum_access_level_for_delete |
string | 否 | 删除包所需的最低 GitLab 访问级别。有效值包括 null、owner 或 admin。如果值为 null,默认最低访问级别为 maintainer。当未设置 minimum_access_level_for_push 时必须提供。受名为 packages_protected_packages_delete 的功能标志控制。默认禁用。 |
minimum_access_level_for_push |
string | 否 | 推送包所需的最低 GitLab 访问级别。有效值包括 null、maintainer、owner 或 admin。如果值为 null,默认最低访问级别为 developer。当未设置 minimum_access_level_for_delete 时必须提供。 |
如果成功,返回 200 和更新后的包保护规则。
可能返回以下状态码:
200 OK: 包保护规则更新成功。400 Bad Request: 更新无效。401 Unauthorized: 访问令牌无效。403 Forbidden: 用户无权更新包保护规则。404 Not Found: 项目未找到。422 Unprocessable Entity: 包保护规则无法更新,例如因为package_name_pattern已被占用。
示例请求:
curl --request PATCH \
--header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
--url "https://gitlab.example.com/api/v4/projects/7/packages/protection/rules/32" \
--data '{
"package_name_pattern": "new-package-name-pattern-*"
}'删除包保护规则
从项目中删除包保护规则。
DELETE /api/v4/projects/:id/packages/protection/rules/:package_protection_rule_id支持的属性:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id |
integer/string | 是 | 项目 ID 或 URL 编码路径。 |
package_protection_rule_id |
integer | 是 | 要删除的包保护规则 ID。 |
如果成功,返回 204 No Content。
可能返回以下状态码:
204 No Content: 包保护规则删除成功。400 Bad Request:id或package_protection_rule_id缺失或无效。401 Unauthorized: 访问令牌无效。403 Forbidden: 用户无权删除包保护规则。404 Not Found: 项目或包保护规则未找到。
示例请求:
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/7/packages/protection/rules/32"