2

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

Marcin
  • 510
  • 6
  • 22
  • Did you figure this out? I'm currently fighting with those strange hashes as well. – oskarth Nov 20 '20 at 13:21
  • @oskarth i posted answer that might help you :) – Marcin Nov 20 '20 at 16:32
  • 1
    thanks! i ended up reading the source code and the code is SHA1(path)_(old_line)_(new_line) actually.. didn't help me enough tho. I feel like I tried your solution but might try again. Talked to their support and they told me to use the commit API instead, because it seems to have the same effect. – oskarth Nov 22 '20 at 19:39
  • cool, will keep this in mind if i ever get back to it :) – Marcin Nov 22 '20 at 20:34

2 Answers2

1

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.

Marcin
  • 510
  • 6
  • 22
0

start_line_code is the hash of the file path + position of the line - code to generate hash - https://play.golang.org/p/M8cY3WeEqe2