9

Using GitLab API it is possible to post a comment to a commit "on a particular line of a particular file" (see here).

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.

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...

Anyone knows how to use the GitLab API to add inline comments to a Merge Request ?

stour
  • 171
  • 3
  • 4

4 Answers4

3

In order to add inline comments for merge requests, there is 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
    },
Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • 1
    not sure what you were after. I was looking for a way to log information on the build status in the MR (QG status to be specific). Your post clearly gave me what I was looking for. Kudos – avi.elkharrat Jan 03 '20 at 12:48
  • 2
    When I try to use a `position` like that I get this error: `{"message":"400 (Bad request) \"Note {:line_code=\u003e[\"can't be blank\", \"must be a valid line code\"]}\" not given"}` and I can't for the life of me seem to find how to construct the mysterious `line_code` that appear here and there. Any pointers? – oskarth Nov 19 '20 at 17:53
  • i'm having a similar issue @oskarth, I asked about it in a separate issue: https://stackoverflow.com/questions/65926187/what-is-a-gitlab-line-code-as-referenced-when-creating-a-new-merge-request-threa – brianc Jan 27 '21 at 19:39
1

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

Generating Token

  • Go to Your Project > Settings > Access Tokens
  • Enter a Token name
  • Choose Reporter as role
  • Select the api scope. You can add more scopes but this is all we need here
  • Generate the token

P.s. 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.

Ref

Utsav Barnwal
  • 985
  • 11
  • 14
0

The notes API is used to only add comments to the Merge Request.

In order to add inline comments to the source code, you must use this other API endpoint:

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

But this API sets the comment in the commit list. You will not see the comment anywhere in the "Merge Request" page.

veladan
  • 365
  • 1
  • 4
  • 11
0

I am using the python gitlab module and this is the format to add an inline comment. Please note that adding comments will fail If the line is not modified.

import gitlab
gl = gitlab.Gitlab(ci_server_url, oauth_token)
merge_request = gl.projects.get(project_id).mergerequests.get(merge_request_iid)

position = {
    "base_sha": merge_request.diff_refs["base_sha"],
    "head_sha": merge_request.diff_refs["head_sha"],
    "start_sha": merge_request.diff_refs["start_sha"],
    "new_path": "1.py",
    "new_line": 18,
    'position_type': 'text'
}

disc = merge_request.discussions.create({"body": "This is a comment on line 18", "position": position})
Be Champzz
  • 391
  • 3
  • 6