Currently, the position
JSON property is used as a line number when clicking on review comments and going to a file.
However, this does not go to the correct line number!
The GitHub API documentation says this:
Note: To comment on a specific line in a file, you need to first determine the position of that line in the diff. The GitHub REST API v3 offers the application/vnd.github.v3.diff media type. To see a pull request diff, add this media type to the Accept header of a call to the single pull request endpoint.
The position value equals the number of lines down from the first "@@" hunk header in the file you want to add a comment. The line just below the "@@" line is position 1, the next line is position 2, and so on. The position in the diff continues to increase through lines of whitespace and additional hunks until the beginning of a new file.
So, what this means for getting the correct line number of a comment is:
- You'd have to get the diff by doing a call to
/repos/$owner/$repo/pulls/$id
with the header Accept: application/vnd.github.v3.diff
- You'd have to look in the diff for the start of the file of the comment, it looks like this:
diff --git a/[OLD_PATH] b/[NEW_PATH]
index 1234578..123468576 123456
--- a/[OLD_PATH]
+++ b/[NEW_PATH]
@@ -123,12 +123,12 @@
[CODE]
- Finally, you have to go down
position
lines, parsing and incorporating the @@
lines on the way, to stay at the correct line number.
So, for this diff:
@@ -122,12 +123,12 @@
some.code1();
+some.code2();
some.code3();
@@ -143,12 +145,12 @@
some.code4();
-some.code5();
position
1 would be some.code1();
, or line 123.
position
3 would be some.code3();
, or line 125.
position
5 would be some.code4();
, or line 145.