Help us learn about your current experience with the documentation. Take the survey.

代码建议 API

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

在代码审查时,建议(suggestions)提供了一种方式,可以直接提出并应用具体的更改。更多信息,请参阅 Suggest changes

您可以使用此 API,在合并请求(merge request)的讨论中以编程方式创建和应用代码建议。每次调用建议 API 都必须进行身份验证。

创建建议

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

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

应用建议

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

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"
  }
 ]