communication api_usage ai_generated true

GitHub PR review comment appears on wrong line or API returns 'position is not a valid hunk position'

ID: communication/github-pr-review-comment-position

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

GitHub PR review comment 'position' is NOT the file line number. It's the 1-based index within the diff hunk. Line 42 in the file might be position 5 in the diff. Using 'line' parameter (newer API) is simpler but only works for lines visible in the diff.

generic

Workarounds

  1. 90% success Use the newer 'line' and 'side' parameters instead of 'position'
    body = {"body": "comment", "path": "file.py", "line": 42, "side": "RIGHT"}  # RIGHT for new code, LEFT for old
  2. 85% success Use 'subject_type': 'file' for comments not tied to specific lines
    body = {"body": "general comment about this file", "path": "file.py", "subject_type": "file"}
  3. 78% success Parse the diff to calculate correct position if you must use the legacy parameter
    Count lines in the diff hunk from @@ marker to the target line. That count is your position value.

Dead Ends

Common approaches that don't work:

  1. Use file line number as 'position' in PR review comment API 92% fail

    The 'position' parameter is the line index within the diff, not the file. Line 100 in the file might be position 3 in the diff if only 3 lines of context are shown.

  2. Use 'line' parameter on any line in the file 82% fail

    The 'line' parameter only works for lines visible in the diff. Comments on unchanged lines outside the diff context fail with 422.