How to comment on a specific line number in PR on github

I am trying to write a small script that can comment on github PRs using eslint output.

The problem is that eslint gives me absolute line numbers for every error. But github's API requires a line number relative to diff.

From github API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment

To comment on a specific line in a file, you need to first determine the position in the difference. GitHub offers application / vnd.github.v3.diff, which you can use in the previous request to view the pull request request. The difference must be interpreted to translate from a line in a file to a position in a difference. The position value is the number of lines down from the first "@@" hunk header in the file that you would like to comment on.

The line just below the line "@@" is position 1, the next line is position 2, etc. The position in the diff file continues to increase the number of spaces and additional blocks until the file is reached.

enter image description here

So, if I want to add a comment to line 5 in the above image, then I will need to pass 12 to the API

My question is how can I easily match between the new line numbers that eslint will give in it error messages to the relative line numbers required by the github API

What I tried so far

I use parse-diff to convert the diff provided by the github API to a json object

[{ "chunks": [{ "content": "@@ -,OLD_TOTAL_LINES +NEW_STARTING_LINE_NUMBER,NEW_TOTAL_LINES @@", "changes": [ { "type": STRING("normal"|"add"|"del"), "normal": BOOLEAN, "add": BOOLEAN, "del": BOOLEAN, "ln1": OLD_LINE_NUMBER, "ln2": NEW_LINE_NUMBER, "content": STRING, "oldStart": NUMBER, "oldLines": NUMBER, "newStart": NUMBER, "newLines": NUMBER } }] }] 

I am thinking about the following algorithm

  • create an array of new line numbers, starting from NEW_STARTING_LINE_NUMBER to NEW_STARTING_LINE_NUMBER+NEW_TOTAL_LINES for each file
  • subtract newStart from each number and make it another array of relativeLineNumbers
  • moving through the array and for each deleted row ( type==='del' ) increases the corresponding remaining relativeLineNumbers
  • for another hunk (line with @@ ) reduces the remaining relativeLineNumbers
+5
source share
1 answer

I have found a solution. I did not add it here because it includes a simple loop and nothing special. But in any case, answering now to help others.

I opened a transfer request in order to create a similar situation, as shown on the question https://github.com/harryi3t/5134/pull/7/files

Diff image

Using the Github API, you can get diff data. I wrote a small JS wrapper for it https://github.com/harryi3t/github-adapter/blob/master/src/Adapter.js#L223

 diff --git a/test.js b/test.js index 2aa9a08..066fc99 100644 --- a/test.js +++ b/test.js @@ -2,14 +2,7 @@ var hello = require('./hello.js'); -var names = [ - 'harry', - 'barry', - 'garry', - 'harry', - 'barry', - 'marry', -]; +var names = ['harry', 'barry', 'garry', 'harry', 'barry', 'marry']; var names2 = [ 'harry', @@ -23,9 +16,7 @@ var names2 = [ // after this line new chunk will be created var names3 = [ 'harry', - 'barry', - 'garry', 'harry', 'barry', - 'marry', + 'marry', 'garry', ]; 

Now just pass this data to the diff-parse module and do the calculations.

 var parsedFiles = parseDiff(data); // diff output parsedFiles.forEach( function (file) { var relativeLine = 0; file.chunks.forEach( function (chunk, index) { if (index !== 0) // relative line number should increment for each chunk relativeLine++; // except the first one (see rel-line 16 in the image) chunk.changes.forEach( function (change) { relativeLine++; console.log( change.type, change.ln1 ? change.ln1 : '-', change.ln2 ? change.ln2 : '-', change.ln ? change.ln : '-', relativeLine ); } ); } ); } ); 

It will print

 type (ln1) old line (ln2) new line (ln) added/deleted line relative line normal 2 2 - 1 normal 3 3 - 2 normal 4 4 - 3 del - - 5 4 del - - 6 5 del - - 7 6 del - - 8 7 del - - 9 8 del - - 10 9 del - - 11 10 del - - 12 11 add - - 5 12 normal 13 6 - 13 normal 14 7 - 14 normal 15 8 - 15 normal 23 16 - 17 normal 24 17 - 18 normal 25 18 - 19 del - - 26 20 del - - 27 21 normal 28 19 - 22 normal 29 20 - 23 del - - 30 24 add - - 21 25 normal 31 22 - 26 

Now you can use the relative line number to post a comment using the github api.

For my purpose, I only need relative line numbers for newly added lines, but using the table above, you can also get it for deleted lines.

Here is a link to the linting project in which I used this. https://github.com/harryi3t/lint-github-pr

+2
source

Source: https://habr.com/ru/post/1262840/


All Articles