Gitlab API create discussion on file line

Viewed 222

I'm trying to create discussion on specific line of changed file.

Request i make:


await axios.post('https://gitlab.<project>/api/v4/projects/<project id>/merge_requests/103/discussions', {
   body: 'test123',
  'position[base_sha]': '9f4284ba82fc0667464ca8abaa76b334415278fc',
  'position[start_sha]': '9f4284ba82fc0667464ca8abaa76b334415278fc',
  'position[head_sha]': 'c6272616033ddcceb3ced2d37c0fd75a8cf5472f',
  'position[position_type]': 'text',
  'position[new_path]': 'LICENSE',
  'position[new_line]': 80,
  'position[old_path]': 'LICENSE',
  'position[old_line]': null,
    
  'position[line_range][start_line_code]': '0398ccd0f49298b10a3d76a47800d2ebecd49859_0_355',
  'position[line_range][end_line_code]': '0398ccd0f49298b10a3d76a47800d2ebecd49859_0_355',
  'position[line_range][start_line_type]': 'old',
  'position[line_range][end_line_type]': 'old',
});

Most of the body values are taken from inspecting the browser and seeing what request gitlab makes when i create the thread manually.

The request results in simple thread that does not appear on top of a file, the documentation does not explain well the body fields and their functionality so I'm currently stuck with this.

I'm unsure if those fields are correct, i took the start_line_code and end_line_code from the gitlab request i saw in the browser:

'position[line_range][start_line_code]': '0398ccd0f49298b10a3d76a47800d2ebecd49859_0_355',
'position[line_range][end_line_code]': '0398ccd0f49298b10a3d76a47800d2ebecd49859_0_355',
'position[line_range][start_line_type]': 'old',
'position[line_range][end_line_type]': 'old',

How the thread looks like:

enter image description here

What i expect it to look like:

enter image description here

What is the correct way of creating thread that appears on top of a file?

Link to gitlab api resource

2 Answers

Posting it here since idk if comments support markdown.

I ended up doing:

const mergeRequestDiffs = <request merge request changes>
const position: {
      ...mergeRequestDiffs.diff_refs,
      old_path: pathFromRootOfTheProject,
      new_path: pathFromRootOfTheProject,
      position_type: 'text',
      new_line: line,
      head_sha: line SHA,
}

<make discussion request with payload below i was using axios.post>

{
  body: <some message>,
  position,
}

I also encountered issues when files were actually not changed in the merge request. But never looked into it more deep. Since my team dropped the feature.

Related