简体   繁体   English

GitLab API 将内联评论发布到合并请求

[英]GitLab API to post inline comment to Merge Request

Using GitLab API it is possible to post a comment to a commit "on a particular line of a particular file" (see here ).使用 GitLab API 可以在“特定文件的特定行”上对提交发表评论(参见此处)。

On Merge Request in order to add comments it's required to use the notes resource (see here ) but the note object does not seem to contain any parameter to comment on a particular line.在合并请求上,为了添加注释,需要使用注释资源(请参见此处),但注释object 似乎不包含任何参数来评论特定行。

However from the GitLab UI I'm able to add inline comments to a Merge Request in the Changes tab but when I call the API and look at the corresponding note object created from my inline comment there is nothing about the inline, it is only a regular note object without any line or line_type parameter...但是,从 GitLab UI 我可以在“更改”选项卡中向合并请求添加内联注释,但是当我调用 API 并查看相应的注释ZA8CFDE6331BD59EB2AC96F8911C4B6666Z常规注释object 没有任何 line 或 line_type 参数...

Anyone knows how to use the GitLab API to add inline comments to a Merge Request?任何人都知道如何使用 GitLab API 在合并请求中添加内联注释?

The notes API is used to only add comments to the Merge Request. notes API仅用于向合并请求添加注释。

In order to add inline comments to the source code, you must use this other API endpoint: 为了向源代码添加内联注释,您必须使用此其他API端点:

https://docs.gitlab.com/ce/api/commits.html#post-comment-to-commit https://docs.gitlab.com/ce/api/commits.html#post-comment-to-commit

But this API sets the comment in the commit list. 但是这个API在提交列表中设置了注释。 You will not see the comment anywhere in the "Merge Request" page. 您不会在“合并请求”页面中的任何位置看到评论。

In order to add inline comments for merge requests, there is Discussions API: https://docs.gitlab.com/ce/api/discussions.html 为了添加合并请求的内联注释,有Discussions API: https//docs.gitlab.com/ce/api/discussions.html

Each discussion can contain a position in the code, like that: 每个讨论都可以在代码中包含一个位置,如下所示:

    "position": {
      "base_sha": "b5d6e7b1613fca24d250fa8e5bc7bcc3dd6002ef",
      "start_sha": "7c9c2ead8a320fb7ba0b4e234bd9529a2614e306",
      "head_sha": "4803c71e6b1833ca72b8b26ef2ecd5adc8a38031",
      "old_path": "package.json",
      "new_path": "package.json",
      "position_type": "text",
      "old_line": 27,
      "new_line": 27
    },

Code代码

functions.yml

.commit_comment:
  script:
    - export GITLAB_TOKEN="PROJECT_ACCESS_TOKEN"
    - |
      commit_comment() {
        curl --location --request POST "https://gitlab.com/api/v4/projects/$CI_MERGE_REQUEST_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --header "Content-Type: application/json" --data-raw "{ \"body\": \"$1\" }"
      }

How to Use?如何使用?

# ...
script:
    - !reference [.commit_comment, script]
    - commit_comment "YOUR_MESSAGE"
# ...

ℹ️ Markdown is allowed for the message ℹ️ Markdown 被允许用于消息

Generating Token生成令牌

  • Go to Your Project > Settings > Access Tokens Go 到您的项目 > 设置 > 访问令牌
  • Enter a Token name输入令牌名称
  • Choose Reporter as role选择Reporter作为角色
  • Select the api scope. Select api scope。 You can add more scopes but this is all we need here您可以添加更多范围,但这就是我们所需要的
  • Generate the token生成令牌

Ps Here, I have created a function to use with multiple jobs but you also directly use the curl command anywhere directly by replacing $1 with your message. Ps 在这里,我创建了一个 function 用于多个作业,但您也可以通过将$1替换为您的消息直接在任何地方直接使用curl命令。

Ref 参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GitLab 的合并请求讨论中的“提交评论”和“立即添加评论”有什么区别? - What's the difference between "Submit review" and "Add comment now" in GitLab's merge request discussions? Gitlab 合并请求何时批准 - Gitlab Merge Request When To Approve 在 GitLab 中为合并请求设置默认审阅者 - Set default reviewers for merge request in GitLab Gitlab CI 根据合并请求自动运行管道 - Gitlab CI automatical run pipeline on merge request 接受合并请求后禁用 gitlab 管道 - disable gitlab pipeline after merge request is accepted 为什么 Gitlab 在您批准合并请求时会自动将目标分支合并到源分支? 如何禁用此行为? - Why Gitlab automatically merge target branch into source branch when you approve a merge request? How to disable this behaviour? 从使用 Gitlab Web 编辑器删除的合并请求中删除跟踪文件 - Remove tracked file from Merge Request that was deleted using Gitlab Web Editor 为什么我在 Gitlab 版本 14.95 中尝试推送合并请求命令时看到“未知开关”错误? - Why am I seeing an 'unknown switch' error when attempting to push a merge request command in Gitlab version 14.95? 使用 Bash 和 cURL 通过 CI/CD 管道进入时生成 GitLab 合并请求 - Generate GitLab Merge Request when a push comes in via CI/CD Pipeline using Bash and cURL 如何对 Gitlab URL 进行反向工程以指向合并请求中的某个代码行号 - How to reverse engineer a Gitlab URL to point to a certain code line number in a Merge Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM