fix: address review findings (#2381)
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { getPRReviewCommentLineNumbersFromPatch } from './pr-review-comment-lines'
|
||||
|
||||
describe('getPRReviewCommentLineNumbersFromPatch', () => {
|
||||
it('returns modified-side context and added lines from GitHub patch hunks', () => {
|
||||
const patch = [
|
||||
'@@ -10,4 +20,5 @@ function example() {',
|
||||
' const kept = true',
|
||||
'-const oldValue = 1',
|
||||
'+const newValue = 1',
|
||||
'+const added = true',
|
||||
' return kept',
|
||||
'@@ -40,2 +51,2 @@ function other() {',
|
||||
'-removeMe()',
|
||||
'+addMe()',
|
||||
' done()'
|
||||
].join('\n')
|
||||
|
||||
expect(getPRReviewCommentLineNumbersFromPatch(patch)).toEqual([20, 21, 22, 23, 51, 52])
|
||||
})
|
||||
|
||||
it('returns an empty list when GitHub omits the patch', () => {
|
||||
expect(getPRReviewCommentLineNumbersFromPatch(undefined)).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,42 @@
|
||||
const HUNK_HEADER_RE = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/
|
||||
|
||||
export function getPRReviewCommentLineNumbersFromPatch(patch: string | undefined): number[] {
|
||||
if (!patch) {
|
||||
return []
|
||||
}
|
||||
|
||||
const lineNumbers: number[] = []
|
||||
let nextModifiedLine: number | null = null
|
||||
|
||||
for (const line of patch.split('\n')) {
|
||||
const hunk = HUNK_HEADER_RE.exec(line)
|
||||
if (hunk) {
|
||||
const start = Number(hunk[1])
|
||||
const count = hunk[2] === undefined ? 1 : Number(hunk[2])
|
||||
nextModifiedLine = Number.isInteger(start) && count > 0 ? start : null
|
||||
continue
|
||||
}
|
||||
|
||||
if (nextModifiedLine === null || line.startsWith('\\')) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (line.startsWith('+') && !line.startsWith('+++')) {
|
||||
lineNumbers.push(nextModifiedLine)
|
||||
nextModifiedLine++
|
||||
continue
|
||||
}
|
||||
|
||||
if (line.startsWith(' ')) {
|
||||
lineNumbers.push(nextModifiedLine)
|
||||
nextModifiedLine++
|
||||
continue
|
||||
}
|
||||
|
||||
if (!line.startsWith('-')) {
|
||||
nextModifiedLine++
|
||||
}
|
||||
}
|
||||
|
||||
return lineNumbers
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
} from './gh-utils'
|
||||
import { getWorkItem, getPRChecks, getPRComments } from './client'
|
||||
import { noteRateLimitSpend, rateLimitGuard } from './rate-limit'
|
||||
import { getPRReviewCommentLineNumbersFromPatch } from './pr-review-comment-lines'
|
||||
|
||||
// Why: a PR "changed file" listing returned by the REST endpoint is paginated
|
||||
// at 100 per page; we cap at a reasonable total so a massive PR cannot starve
|
||||
@@ -328,7 +329,8 @@ async function getPRFiles(
|
||||
status: mapFileStatus(file.status),
|
||||
additions: file.additions,
|
||||
deletions: file.deletions,
|
||||
isBinary: isBinaryHint(file)
|
||||
isBinary: isBinaryHint(file),
|
||||
reviewCommentLineNumbers: getPRReviewCommentLineNumbersFromPatch(file.patch)
|
||||
}))
|
||||
} catch {
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user