合并请求批准 API

  • 端点/approvals 移除于极狐GitLab 16.0。

配置项目中的所有合并请求的批准。必需为所有端点进行授权。

群组级别的合并请求审批

  • 引入极狐GitLab 16.7,使用名为 approval_group_rules 的功能标志。默认禁用。这是一个实验性功能。
在私有化部署实例上,此功能默认不可用。要想使用,可以让管理员启用功能标志 approval_group_rules。在 JihuLab.com 上,此功能也不可用,因为还未生产就绪。

群组审批规则应用于属于群组项目的所有受保护分支。这是一个实验性功能。

获取群组级别的审批规则Get group-level approval rules

  • 引入于GitLab 16.10。

群组管理员可以使用如下端点请求群组审批规则的信息:

GET /groups/:id/approval_rules

使用 pageper_page pagination 分页参数来限制审批规则列表。

支持的属性:

属性 类型 是否必需 描述
id integer or string Yes ID 或 URL 编码的项目路径

示例请求:

curl --request GET \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/groups/29/approval_rules"

示例响应:

[
  {
    "id": 2,
    "name": "rule1",
    "rule_type": "any_approver",
    "report_type": null,
    "eligible_approvers": [],
    "approvals_required": 3,
    "users": [],
    "groups": [],
    "contains_hidden_groups": false,
    "protected_branches": [],
    "applies_to_all_protected_branches": true
  },
  {
    "id": 3,
    "name": "rule2",
    "rule_type": "code_owner",
    "report_type": null,
    "eligible_approvers": [],
    "approvals_required": 2,
    "users": [],
    "groups": [],
    "contains_hidden_groups": false,
    "protected_branches": [],
    "applies_to_all_protected_branches": true
  },
  {
    "id": 4,
    "name": "rule2",
    "rule_type": "report_approver",
    "report_type": "code_coverage",
    "eligible_approvers": [],
    "approvals_required": 2,
    "users": [],
    "groups": [],
    "contains_hidden_groups": false,
    "protected_branches": [],
    "applies_to_all_protected_branches": true
  }
]

创建群组级别的审批规则

群组管理员可以使用如下端点创建群组级别的审批规则:

POST /groups/:id/approval_rules

支持的属性:

Attribute Type Required Description
id integer or string Yes ID 或 URL 编码的群组路径
approvals_required integer Yes 规则所需的审批数量。
name string Yes 审批规则的名称。
group_ids array No 审批群组的 ID。
rule_type string No 规则类型。any_approverapprovals_required0 时的默认预配置值。其他规则是 其他的规则是 regular(常规的 合并请求审批规则所使用)以及 report_approver。不要使用此字段从 API 来构建审批规则。当极狐GitLab 从配置且启用的合并请求审批策略创建审批规则时,需要使用 report_approver 字段。
user_ids array No 审批人员的 ID。

示例请求:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/groups/29/approval_rules?name=security&approvals_required=2"

示例响应:

{
  "id": 5,
  "name": "security",
  "rule_type": "any_approver",
  "eligible_approvers": [],
  "approvals_required": 2,
  "users": [],
  "groups": [],
  "contains_hidden_groups": false,
  "protected_branches": [
    {
      "id": 5,
      "name": "master",
      "push_access_levels": [
        {
          "id": 5,
          "access_level": 40,
          "access_level_description": "Maintainers",
          "deploy_key_id": null,
          "user_id": null,
          "group_id": null
        }
      ],
      "merge_access_levels": [
        {
          "id": 5,
          "access_level": 40,
          "access_level_description": "Maintainers",
          "user_id": null,
          "group_id": null
        }
      ],
      "allow_force_push": false,
      "unprotect_access_levels": [],
      "code_owner_approval_required": false,
      "inherited": false
    }
  ],
  "applies_to_all_protected_branches": true
}

更新群组级别的审批规则

  • 引入于极狐GitLab 16.10。

群组管理员可以使用如下端点来更新群组级别的审批规则:

PUT /groups/:id/approval_rules/:approval_rule_id

支持的属性:

属性 类型 是否必需 描述
approval_rule_id. integer Yes 审批规则的 ID。
id integer or string Yes ID 或 URL 编码的群组路径
approvals_required string No 此规则需要的审批数量。
group_ids integer No 审核人员的用户 ID。
name string No 审批规则的名称。
rule_type array No 规则类型。any_approverapprovals_required0 时的默认预配置值。其他规则是 其他的规则是 regular(常规的 合并请求审批规则所使用)以及 report_approver。不要使用此字段从 API 来构建审批规则。当极狐GitLab 从配置且启用的合并请求审批策略创建审批规则时,需要使用 report_approver 字段。
user_ids array No 群组审核员 ID。

示例请求:

curl --request PUT \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/groups/29/approval_rules/5?name=security2&approvals_required=1"

Example response:

{
  "id": 5,
  "name": "security2",
  "rule_type": "any_approver",
  "eligible_approvers": [],
  "approvals_required": 1,
  "users": [],
  "groups": [],
  "contains_hidden_groups": false,
  "protected_branches": [
    {
      "id": 5,
      "name": "master",
      "push_access_levels": [
        {
          "id": 5,
          "access_level": 40,
          "access_level_description": "Maintainers",
          "deploy_key_id": null,
          "user_id": null,
          "group_id": null
        }
      ],
      "merge_access_levels": [
        {
          "id": 5,
          "access_level": 40,
          "access_level_description": "Maintainers",
          "user_id": null,
          "group_id": null
        }
      ],
      "allow_force_push": false,
      "unprotect_access_levels": [],
      "code_owner_approval_required": false,
      "inherited": false
    }
  ],
  "applies_to_all_protected_branches": true
}

项目级别的合并请求批准

您可以使用如下端点来请求项目的审批配置:

GET /projects/:id/approvals

支持的属性:

Attribute Type Required Description
id integer or string Yes ID 或 URL 编码的项目路径
{
  "approvers": [], // Deprecated in GitLab 12.3, always returns empty
  "approver_groups": [], // Deprecated in GitLab 12.3, always returns empty
  "approvals_before_merge": 2, // Deprecated in GitLab 12.3, use Approval Rules instead
  "reset_approvals_on_push": true,
  "selective_code_owner_removals": false,
  "disable_overriding_approvers_per_merge_request": false,
  "merge_requests_author_approval": true,
  "merge_requests_disable_committers_approval": false,
  "require_password_to_approve": true, // Deprecated in 16.9, use require_reauthentication_to_approve instead
  "require_reauthentication_to_approve": true
}

更改配置

如果有相关权限,您可以使用以下端点更改批准配置:

POST /projects/:id/approvals

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
approvals_before_merge(已废弃) integer No 合并 MR 之前需要多少批准。已废弃,现在使用批准规则代替
disable_overriding_approvers_per_merge_request boolean No 允许或阻止覆盖每个合并请求的批准者
merge_requests_author_approval boolean No 允许或阻止作者批准自己的合并请求。true 表示作者可以批准自己的合并请求
merge_requests_disable_committers_approval boolean No 允许或阻止提交者批准自己的合并请求
require_password_to_approve boolean No 要求批准者在添加批准前输入密码进行授权
require_reauthentication_to_approve boolean No 在添加规则前,需要审核人员来输入认证。引入于极狐GitLab 17.1。
reset_approvals_on_push boolean No 重置新推送的批准
selective_code_owner_removals boolean No 如果他们的文件发生更改,则重置代码所有者的批准。仅当禁用 reset_approvals_on_push 时才能启用
{
  "approvals_before_merge": 2, // Use Approval Rules instead
  "reset_approvals_on_push": true,
  "selective_code_owner_removals": false,
  "disable_overriding_approvers_per_merge_request": false,
  "merge_requests_author_approval": false,
  "merge_requests_disable_committers_approval": false,
  "require_password_to_approve": true,
  "require_reauthentication_to_approve": true
}

获取项目级别的规则

  • 移动到极狐GitLab 专业版于 13.9。
  • 分页支持引入于极狐GitLab 15.3,功能标志approval_rules_pagination。默认启用。
  • applies_to_all_protected_branches 特性引入于极狐GitLab 15.3。
  • 分页支持普遍可用于极狐GitLab 15.7。功能标志 approval_rules_pagination 已移除。
  • usernames 参数引入于极狐GitLab 15.8。

您可以使用以下端点请求有关项目批准规则的信息:

GET /projects/:id/approval_rules

使用 pageper_page 分页参数限制批准规则列表。

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
[
  {
    "id": 1,
    "name": "security",
    "rule_type": "regular",
    "eligible_approvers": [
      {
        "id": 5,
        "name": "John Doe",
        "username": "jdoe",
        "state": "active",
        "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
        "web_url": "http://localhost/jdoe"
      },
      {
        "id": 50,
        "name": "Group Member 1",
        "username": "group_member_1",
        "state": "active",
        "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
        "web_url": "http://localhost/group_member_1"
      }
    ],
    "approvals_required": 3,
    "users": [
      {
        "id": 5,
        "name": "John Doe",
        "username": "jdoe",
        "state": "active",
        "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
        "web_url": "http://localhost/jdoe"
      }
    ],
    "groups": [
      {
        "id": 5,
        "name": "group1",
        "path": "group1",
        "description": "",
        "visibility": "public",
        "lfs_enabled": false,
        "avatar_url": null,
        "web_url": "http://localhost/groups/group1",
        "request_access_enabled": false,
        "full_name": "group1",
        "full_path": "group1",
        "parent_id": null,
        "ldap_cn": null,
        "ldap_access": null
      }
    ],
    "protected_branches": [
      {
        "id": 1,
        "name": "master",
        "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"
      }
    ],
    "contains_hidden_groups": false
  }
]

获取单个项目级别的规则

  • applies_to_all_protected_branches 属性引入于极狐GitLab 15.3。
  • usernames 参数引入于极狐GitLab 15.8。

您可以使用以下端点请求有关单个项目批准规则的信息:

GET /projects/:id/approval_rules/:approval_rule_id

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
approval_rule_id integer Yes 批准规则 ID
{
  "id": 1,
  "name": "security",
  "rule_type": "regular",
  "eligible_approvers": [
    {
      "id": 5,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    },
    {
      "id": 50,
      "name": "Group Member 1",
      "username": "group_member_1",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/group_member_1"
    }
  ],
  "approvals_required": 3,
  "users": [
    {
      "id": 5,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    }
  ],
  "groups": [
    {
      "id": 5,
      "name": "group1",
      "path": "group1",
      "description": "",
      "visibility": "public",
      "lfs_enabled": false,
      "avatar_url": null,
      "web_url": "http://localhost/groups/group1",
      "request_access_enabled": false,
      "full_name": "group1",
      "full_path": "group1",
      "parent_id": null,
      "ldap_cn": null,
      "ldap_access": null
    }
  ],
  "applies_to_all_protected_branches": false,
  "protected_branches": [
    {
      "id": 1,
      "name": "master",
      "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"
    }
  ],
  "contains_hidden_groups": false
}

创建项目级别的规则

  • 漏洞检查功能移除于极狐GitLab 15.0。
  • applies_to_all_protected_branches 属性引入于极狐GitLab 15.3。
  • usernames 属性引入于极狐GitLab 15.8。

您可以使用以下端点创建项目批准规则。

POST /projects/:id/approval_rules

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
name string Yes 批准规则名称
approvals_required integer Yes 此规则所需要的批准数量
applies_to_all_protected_branches boolean No 规则是否应用于所有受保护的分支。如果设置为 trueprotected_branch_ids 的值将被忽略。默认为 false。引入于极狐itLab 15.3
rule_type string No 规则类型。any_approver 是预配置的默认规则,且 approvals_required0。其他的规则是 regular
user_ids Array No 作为批准者的用户的 ID
group_ids Array No 作为批准者的群组的 ID
protected_branch_ids Array No 应用规则的受保护分支的 ID。要识别 ID,请使用 API
report_type string No 当规则类型为 report_approver 时所需的报告类型。支持的报告类型为 license_scanning(废弃于 15.9)code_coverage
usernames string array No 规则的用户名
{
  "id": 1,
  "name": "security",
  "rule_type": "regular",
  "eligible_approvers": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    },
    {
      "id": 50,
      "name": "Group Member 1",
      "username": "group_member_1",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/group_member_1"
    }
  ],
  "approvals_required": 1,
  "users": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    }
  ],
  "groups": [
    {
      "id": 5,
      "name": "group1",
      "path": "group1",
      "description": "",
      "visibility": "public",
      "lfs_enabled": false,
      "avatar_url": null,
      "web_url": "http://localhost/groups/group1",
      "request_access_enabled": false,
      "full_name": "group1",
      "full_path": "group1",
      "parent_id": null,
      "ldap_cn": null,
      "ldap_access": null
    }
  ],
  "protected_branches": [
    {
      "id": 1,
      "name": "master",
      "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"
    }
  ],
  "contains_hidden_groups": false
}

您可以像下面的方法来增加所需的审核人员数量:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
  --header 'Content-Type: application/json' \
  --data '{"name": "Any name", "rule_type": "any_approver", "approvals_required": 2}'

您可以创建额外的针对于特定用户的规则:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
  --header 'Content-Type: application/json' \
  --data '{"name": "Name of your rule", "approvals_required": 3, "user_ids": [123, 456, 789]}' \
  https://gitlab.example.com/api/v4/projects/<project_id>/approval_rules

更新项目级别的规则

  • 漏洞检查功能移除于极狐GitLab 15.0。
  • applies_to_all_protected_branches 属性引入于极狐GitLab 15.3。
  • usernames 属性引入于极狐GitLab 15.8。

您可以使用以下端点更新项目批准规则:

PUT /projects/:id/approval_rules/:approval_rule_id

重要: 不在 users/groups 参数中的批准者和群组会被移除

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
approval_rule_id integer Yes 批准规则 ID
name string Yes 批准规则名称
approvals_required integer Yes 此规则所需批准的数量
applies_to_all_protected_branches boolean No 规则是否应用到所有的受保护分支。如果设置为 true,忽略 protected_branch_ids 的值。默认为 false。引入于极狐GitLab 15.3
user_ids Array No 作为批准者的用户的 ID
group_ids Array No 作为批准者的群组的 ID
protected_branch_ids Array No 应用规则的受保护分支的 ID。要识别 ID,请使用 API
remove_hidden_groups boolean No 是否应该移除隐藏群组
usernames string array No 规则的用户名称
{
  "id": 1,
  "name": "security",
  "rule_type": "regular",
  "eligible_approvers": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    },
    {
      "id": 50,
      "name": "Group Member 1",
      "username": "group_member_1",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/group_member_1"
    }
  ],
  "approvals_required": 1,
  "users": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    }
  ],
  "groups": [
    {
      "id": 5,
      "name": "group1",
      "path": "group1",
      "description": "",
      "visibility": "public",
      "lfs_enabled": false,
      "avatar_url": null,
      "web_url": "http://localhost/groups/group1",
      "request_access_enabled": false,
      "full_name": "group1",
      "full_path": "group1",
      "parent_id": null,
      "ldap_cn": null,
      "ldap_access": null
    }
  ],
  "protected_branches": [
    {
      "id": 1,
      "name": "master",
      "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"
    }
  ],
  "contains_hidden_groups": false
}

删除项目级别的规则

您可以使用以下端点删除项目批准规则:

DELETE /projects/:id/approval_rules/:approval_rule_id

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
approval_rule_id integer Yes 批准规则 ID

合并请求级别的合并请求批准

对特定合并请求所进行的批准配置。必须对所有端点进行授权。

您可以使用如下端点来请求合并请求审批状态的信息:

GET /projects/:id/merge_requests/:merge_request_iid/approvals

支持的参数:

属性 类型 是否需 描述
id integer or string Yes ID 或 URL 编码的项目路径
merge_request_iid integer Yes 合并请求的 IID。
{
  "id": 5,
  "iid": 5,
  "project_id": 1,
  "title": "Approvals API",
  "description": "Test",
  "state": "opened",
  "created_at": "2016-06-08T00:19:52.638Z",
  "updated_at": "2016-06-08T21:20:42.470Z",
  "merge_status": "cannot_be_merged",
  "approvals_required": 2,
  "approvals_left": 1,
  "approved_by": [
    {
      "user": {
        "name": "Administrator",
        "username": "root",
        "id": 1,
        "state": "active",
        "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon",
        "web_url": "http://localhost:3000/root"
      }
    }
  ]
}

获取合并请求的批准状态

  • 移动到极狐GitLab 专业版于 13.9。

您可以使用以下端点请求有关合并请求批准状态的信息:

GET /projects/:id/merge_requests/:merge_request_iid/approval_state

如果为合并请求创建合并请求级别规则,approval_rules_overwrittentrue;如果没创建,则为 false

这包括有关已批准用户(approved_by)以及规则是否已被批准 (approved)的其他信息。

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
{
  "approval_rules_overwritten": true,
  "rules": [
    {
      "id": 1,
      "name": "Ruby",
      "rule_type": "regular",
      "eligible_approvers": [
        {
          "id": 4,
          "name": "John Doe",
          "username": "jdoe",
          "state": "active",
          "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
          "web_url": "http://localhost/jdoe"
        }
      ],
      "approvals_required": 2,
      "users": [
        {
          "id": 4,
          "name": "John Doe",
          "username": "jdoe",
          "state": "active",
          "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
          "web_url": "http://localhost/jdoe"
        }
      ],
      "groups": [],
      "contains_hidden_groups": false,
      "approved_by": [
        {
          "id": 4,
          "name": "John Doe",
          "username": "jdoe",
          "state": "active",
          "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
          "web_url": "http://localhost/jdoe"
        }
      ],
      "source_rule": null,
      "approved": true,
      "overridden": false
    }
  ]
}

获取合并请求级别规则

  • 分页支持引入于极狐GitLab 15.3,功能标志approval_rules_pagination。默认启用。
  • 分页支持普遍可用于极狐GitLab 15.7。功能标志 approval_rules_pagination 已移除。

您可以使用以下端点请求有关合并请求批准规则的信息:

GET /projects/:id/merge_requests/:merge_request_iid/approval_rules

使用 pageper_page 分页参数限制批准规则列表。

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
[
  {
    "id": 1,
    "name": "security",
    "rule_type": "regular",
    "eligible_approvers": [
      {
        "id": 5,
        "name": "John Doe",
        "username": "jdoe",
        "state": "active",
        "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
        "web_url": "http://localhost/jdoe"
      },
      {
        "id": 50,
        "name": "Group Member 1",
        "username": "group_member_1",
        "state": "active",
        "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
        "web_url": "http://localhost/group_member_1"
      }
    ],
    "approvals_required": 3,
    "source_rule": null,
    "users": [
      {
        "id": 5,
        "name": "John Doe",
        "username": "jdoe",
        "state": "active",
        "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
        "web_url": "http://localhost/jdoe"
      }
    ],
    "groups": [
      {
        "id": 5,
        "name": "group1",
        "path": "group1",
        "description": "",
        "visibility": "public",
        "lfs_enabled": false,
        "avatar_url": null,
        "web_url": "http://localhost/groups/group1",
        "request_access_enabled": false,
        "full_name": "group1",
        "full_path": "group1",
        "parent_id": null,
        "ldap_cn": null,
        "ldap_access": null
      }
    ],
    "contains_hidden_groups": false,
    "overridden": false
  }
]

获取单个合并请求级别规则

您可以使用以下端点请求有关单个合并请求批准规则的信息:

GET /projects/:id/merge_requests/:merge_request_iid/approval_rules/:approval_rule_id

参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
approval_rule_id integer Yes 批准规则 ID
{
  "id": 1,
  "name": "security",
  "rule_type": "regular",
  "eligible_approvers": [
    {
      "id": 5,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    },
    {
      "id": 50,
      "name": "Group Member 1",
      "username": "group_member_1",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/group_member_1"
    }
  ],
  "approvals_required": 3,
  "source_rule": null,
  "users": [
    {
      "id": 5,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    }
  ],
  "groups": [
    {
      "id": 5,
      "name": "group1",
      "path": "group1",
      "description": "",
      "visibility": "public",
      "lfs_enabled": false,
      "avatar_url": null,
      "web_url": "http://localhost/groups/group1",
      "request_access_enabled": false,
      "full_name": "group1",
      "full_path": "group1",
      "parent_id": null,
      "ldap_cn": null,
      "ldap_access": null
    }
  ],
  "contains_hidden_groups": false,
  "overridden": false

创建合并请求级别规则

您可以使用以下端点创建合并请求批准规则:

POST /projects/:id/merge_requests/:merge_request_iid/approval_rules

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
name string Yes 批准规则名称
approvals_required integer Yes 此规则所需的批准数量
approval_project_rule_id integer No 项目级别的批准规则 ID
user_ids Array No 作为批准者的用户的 ID
group_ids Array No 作为批准者的群组的 ID
usernames string array No 规则的用户名称

重要: 如果设置 approval_project_rule_id,会复制项目级别规则的 nameusersgroups。使用 approvals_required 指定的那个。

{
  "id": 1,
  "name": "security",
  "rule_type": "regular",
  "eligible_approvers": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    },
    {
      "id": 50,
      "name": "Group Member 1",
      "username": "group_member_1",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/group_member_1"
    }
  ],
  "approvals_required": 1,
  "source_rule": null,
  "users": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    }
  ],
  "groups": [
    {
      "id": 5,
      "name": "group1",
      "path": "group1",
      "description": "",
      "visibility": "public",
      "lfs_enabled": false,
      "avatar_url": null,
      "web_url": "http://localhost/groups/group1",
      "request_access_enabled": false,
      "full_name": "group1",
      "full_path": "group1",
      "parent_id": null,
      "ldap_cn": null,
      "ldap_access": null
    }
  ],
  "contains_hidden_groups": false,
  "overridden": false
}

更新合并请求级别规则

您可以使用以下端点更新合并请求批准规则:

PUT /projects/:id/merge_requests/:merge_request_iid/approval_rules/:approval_rule_id

此端点会 移除 任何审批者,且群组不在 usersgroups 参数中。

您不能更新 report_approvercode_owner 规则,因为这些都是系统生成的规则。

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
approval_rule_id integer Yes 批准规则 ID
name string No 批准规则名称
approvals_required integer No 此规则所需的批准的数量
user_ids Array No 作为批准者的用户的 ID
group_ids Array No 作为批准者的群组的 ID
remove_hidden_groups boolean No 隐藏群组是否应该移除
usernames string array No 规则的用户名称
{
  "id": 1,
  "name": "security",
  "rule_type": "regular",
  "eligible_approvers": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    },
    {
      "id": 50,
      "name": "Group Member 1",
      "username": "group_member_1",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/group_member_1"
    }
  ],
  "approvals_required": 1,
  "source_rule": null,
  "users": [
    {
      "id": 2,
      "name": "John Doe",
      "username": "jdoe",
      "state": "active",
      "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
      "web_url": "http://localhost/jdoe"
    }
  ],
  "groups": [
    {
      "id": 5,
      "name": "group1",
      "path": "group1",
      "description": "",
      "visibility": "public",
      "lfs_enabled": false,
      "avatar_url": null,
      "web_url": "http://localhost/groups/group1",
      "request_access_enabled": false,
      "full_name": "group1",
      "full_path": "group1",
      "parent_id": null,
      "ldap_cn": null,
      "ldap_access": null
    }
  ],
  "contains_hidden_groups": false,
  "overridden": false
}

删除合并请求级别的规则

您可以使用以下端点删除合并请求批准规则:

DELETE /projects/:id/merge_requests/:merge_request_iid/approval_rules/:approval_rule_id

您不能更新 report_approvercode_owner 规则,因为这些都是系统生成的规则。

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
approval_rule_id integer Yes 批准规则 ID

批准合并请求

如果有相关权限,您可以使用以下端点批准合并请求:

POST /projects/:id/merge_requests/:merge_request_iid/approve

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求 IID
sha string no 合并请求的 HEAD
approval_password string no 当前用户的密码。如果在项目设置中启用了需要用户密码以批准,则必填

sha 参数与接受合并请求的工作方式相同:如果通过,则必须要与要添加的批准的合并请求的当前 HEAD 匹配。如果不匹配,则响应代码为 409

{
  "id": 5,
  "iid": 5,
  "project_id": 1,
  "title": "Approvals API",
  "description": "Test",
  "state": "opened",
  "created_at": "2016-06-08T00:19:52.638Z",
  "updated_at": "2016-06-09T21:32:14.105Z",
  "merge_status": "can_be_merged",
  "approvals_required": 2,
  "approvals_left": 0,
  "approved_by": [
    {
      "user": {
        "name": "Administrator",
        "username": "root",
        "id": 1,
        "state": "active",
        "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon",
        "web_url": "http://localhost:3000/root"
      }
    },
    {
      "user": {
        "name": "Nico Cartwright",
        "username": "ryley",
        "id": 2,
        "state": "active",
        "avatar_url": "http://www.gravatar.com/avatar/cf7ad14b34162a76d593e3affca2adca?s=80\u0026d=identicon",
        "web_url": "http://localhost:3000/ryley"
      }
    }
  ]
}

取消批准合并请求

如果您批准了合并请求,您可以使用以下端点取消批准:

POST /projects/:id/merge_requests/:merge_request_iid/unapprove

支持的参数:

参数 类型 是否必需 描述
id integer or string Yes 项目的 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求的 IID

重置合并请求的批准

清除合并请求的所有批准。

仅适用于基于项目或群组令牌的机器人用户。没有机器人权限的用户会收到 401 Unauthorized 响应。

PUT /projects/:id/merge_requests/:merge_request_iid/reset_approvals
参数 类型 是否必需 描述
id integer or string Yes 经过身份验证的用户拥有的项目 ID 或 URL 编码的路径
merge_request_iid integer Yes 合并请求的内部 ID
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/76/merge_requests/1/reset_approvals"