[英]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
},
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\" }"
}
# ...
script:
- !reference [.commit_comment, script]
- commit_comment "YOUR_MESSAGE"
# ...
ℹ️ Markdown is allowed for the message ℹ️ Markdown 被允许用于消息
Reporter
as role选择Reporter
作为角色api
scope. Select api
scope。 You can add more scopes but this is all we need here您可以添加更多范围,但这就是我们所需要的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
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.