Anchoring file and line

GitHub inline review comments require two pieces of information: the file path and a position inside the diff. Position is not the same as line number in the file. It is the zero-indexed offset inside the diff hunk, counting from the first @@ line. Getting this wrong is the number one reason review comments show up in the wrong place or disappear entirely.

Line numbers versus diff positions

The left side is the file line number. The right side is the diff position, which is what GitHub wants.

github_client.py
python
payload = []
for c in comments:
    payload.append(
        {
            "path": c.file,
            "position": c.line,
            "body": f"{prefix.get(c.severity, '')} {c.suggestion}".strip(),
        }
    )

review = pr.create_review(
    commit=commit,
    body=summary or "Automated review from mcp-pr-review-server.",
    event="COMMENT",
    comments=payload,
)

Every comment carries a path and a position. The review is created as a single COMMENT event so reviewers see one coherent review, not ten separate notifications. Pinning to the latest commit means the line anchors resolve against the current PR head.

Walk the diff for the target file, count lines starting from the first @@ hunk header, and map the file-line the model returned to the offset inside the hunk. If the line is outside any hunk (meaning it was not changed), skip the comment. GitHub rejects positions outside of hunks with a 422 error, so this check saves you from a noisy failure.

Quiz: Quiz

Loading practice…