Comment position is wrong


I’ve observed that adding a review comment to a new file via a GraphQL adds a comment with a different position.

For example is added at line 21 and displayed properly but fetching it with GraphQL api returns a comment with position 7.

Is it a bug? How to get a position 21? Count lines in diff hunk?


It’s been a long time but IIRC in the v3 API that’s how it works, you annotate/comment on the diff, not the files. In fact if you try through the web UI you can only add comments on lines of the original diff, even if you expand the folded lines.


Thanks for the response. I know it, but should not position already reflect the correct line in diff? In my case it points to bogus line in the diff, like a comment inserted at 21, but end up at 7 and 7 does not make any sense. I’ve noticed that originalPosition actually points to the proper position though.

    "diff_hunk": "@@ -0,0 +1,91 @@\n+foo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo\n+foo\n+foofoo",
    "path": "bar.js",
    "position": 7,
    "original_position": 21,
    "commit_id": "704dc95bf7c2e573fa91894487e6fc3a9a2dc8c1",
    "original_commit_id": "704dc95bf7c2e573fa91894487e6fc3a9a2dc8c1",

So you can see that original commit and commit are the same, but positions are for some reasons are different. No idea from where 7 come from, also 7 does not match amount of lines in the diff hunk.

As a workaround i am going to use the original position if commit and original commit are the same and if not then the position.


See v3 doc, I’m guessing the fields correspond and thus:

The position value is the number of lines down from the first “@@” hunk header in the file… The line just below the “@@” line is position 1, the next line is position 2, and so on. The position in the file’s diff continues to increase through lines of whitespace and additional hunks until a new file is reached.

original_position is the number of the line in the original file I think.