{{< details >}}

  • Tier: 基础版,专业版,旗舰版
  • Offering: JihuLab.com, 私有化部署

{{< /details >}}

当你审查代码时,建议提供了一种可以直接应用的具体更改的方式。有关更多信息,请参阅建议更改

你可以通过此 API 在合并请求讨论中以编程方式创建和应用代码建议。对建议的每次 API 调用都必须进行认证。

创建建议

要通过 API 创建建议,请使用讨论 API 在合并请求差异中创建一个新线程。建议的格式为:

```suggestion:-3+0
example text
```

应用建议

在合并请求中应用建议的补丁。用户必须至少具有开发者角色才能执行此操作。

PUT /suggestions/:id/apply
属性 类型 必需 描述
id integer 建议的 ID
commit_message string 使用自定义提交消息替代默认生成的消息或项目的默认消息
curl --request PUT \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/suggestions/5/apply"

示例响应:

{
  "id": 5,
  "from_line": 10,
  "to_line": 10,
  "applicable": true,
  "applied": false,
  "from_content": "This is an example\n",
  "to_content": "This is an example\n"
}

应用多个建议

PUT /suggestions/batch_apply
属性 类型 必需 描述
ids integer 建议的 ID
commit_message string 使用自定义提交消息替代默认生成的消息或项目的默认消息
curl --request PUT \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --header 'Content-Type: application/json' \
  --data '{"ids": [5, 6]}' \
  --url "https://gitlab.example.com/api/v4/suggestions/batch_apply"

示例响应:

[
  {
    "id": 5,
    "from_line": 10,
    "to_line": 10,
    "applicable": true,
    "applied": false,
    "from_content": "This is an example\n",
    "to_content": "This is an example\n"
  }
  {
    "id": 6,
    "from_line": 19,
    "to_line": 19,
    "applicable": true,
    "applied": false,
    "from_content": "This is another example\n",
    "to_content": "This is another example\n"
  }
 ]