Multiline comment on merge request with GitLab REST API

142 views Asked by At

I'm trying to post a comment that spans multiple lines of the changes of a merge request on gitlab, using the gitlab REST API. So far I've managed to make the comment spanning multiple lines using the following JSON body:

{
  "position": {
    "base_sha": "4af5230bd8894e9e56a5343537fd0f5dd1d3341b",
    "start_sha": "4af5230bd8894e9e56a5343537fd0f5dd1d3341b",
    "head_sha": "7e5df9ede4766b0c33509881055d00be94debdae",
    "new_path": "test.php",
    "old_path": "test.php",
    "position_type": "text",
    "new_line": 20,
    "line_range": {
      "start": {
        "line_code": "63b0bac63e6a8d4a13fe35e04eac455258e9500b_18_18",
        "type": "new"
      },
      "end": {
        "line_code": "63b0bac63e6a8d4a13fe35e04eac455258e9500b_18_20",
        "type": "new"
      }
    },
    "ignore_whitespace_change": false
  },
  "body": "test comment body"
}

This comment, unlike the comments i can make via the website, doesn't have the "Comment on lines +28 to +30" text above it.

The comment without the text:

Comment without text

The comment with the text:

Comment with text

Is it possible to get the text above it using the GitLab REST API? I've tried browsing the GitLab REST API documentation to find a solution, but no luck.

1

There are 1 answers

1
Raghu On

There is an attribute called "new_line" in the start and end sections of line_range

Assuming the comments are for line 28 to 30 as you mentioned in your screenshot

{
    "position": {
        "base_sha": "4af5230bd8894e9e56a5343537fd0f5dd1d3341b",
        "start_sha": "4af5230bd8894e9e56a5343537fd0f5dd1d3341b",
        "head_sha": "7e5df9ede4766b0c33509881055d00be94debdae",
        "new_path": "test.php",
        "old_path": "test.php",
        "position_type": "text",
        "new_line": 20,
        "line_range": {
            "start": {
                "line_code": "63b0bac63e6a8d4a13fe35e04eac455258e9500b_18_18",
                "type": "new",
                "new_line": 28
            },
            "end": {
                "line_code": "63b0bac63e6a8d4a13fe35e04eac455258e9500b_18_20",
                "type": "new",
                "new_line": 30
            }
        },
        "ignore_whitespace_change": false
    },
    "body": "test comment body"
}

Let me know if this works for you and if it does then please accept the answer, so that others can benefit.