refactor(github): move inline PR comment line placement to src/shared (#36904)

This commit is contained in:
Kelvin Amoaba
2026-11-01 05:43:51 +00:00
committed by GitHub
parent c0190ff9fa
commit fddfd7b024
4 changed files with 54 additions and 35 deletions
@@ -0,0 +1,29 @@
import { describe, expect, it } from 'vitest'
import type { PRComment } from './comment-types'
import { getPRInlineCommentPlacement } from './pr-inline-comment-placement'
const comment = (overrides: Partial<PRComment>): PRComment => ({
id: 1,
author: 'octocat',
authorAvatarUrl: '',
body: 'body',
createdAt: '2026-11-01T00:00:00Z',
url: 'https://github.com/o/r/pull/1#discussion_r1',
...overrides
})
describe('getPRInlineCommentPlacement', () => {
it('places a current review comment on its range', () => {
expect(
getPRInlineCommentPlacement(comment({ path: 'src/a.ts', line: 12, startLine: 10 }))
).toEqual({ path: 'src/a.ts', line: 12, startLine: 10 })
})
it('keeps outdated threads and conversation comments out of the diff', () => {
expect(
getPRInlineCommentPlacement(comment({ path: 'src/a.ts', line: 12, isOutdated: true }))
).toBeNull()
expect(getPRInlineCommentPlacement(comment({ path: 'src/a.ts' }))).toBeNull()
expect(getPRInlineCommentPlacement(comment({}))).toBeNull()
})
})
@@ -0,0 +1,16 @@
import type { PRComment } from './comment-types'
export type PRInlineCommentPlacement = {
path: string
line: number
startLine: number | undefined
}
/** Where a PR review comment sits in the current diff, or null when it should not render inline. */
export function getPRInlineCommentPlacement(comment: PRComment): PRInlineCommentPlacement | null {
// Why: outdated threads keep originalLine for the sidebar, but rendering it inline can attach the comment to unrelated current code.
if (comment.isOutdated || !comment.path || typeof comment.line !== 'number') {
return null
}
return { path: comment.path, line: comment.line, startLine: comment.startLine }
}