@@ -204,16 +204,22 @@ export const robot = (app: Probot) => {
204204 ) ;
205205 continue ;
206206 }
207+
207208 try {
208- const res = await chat ?. codeReview ( patch ) ;
209- if ( ! res . lgtm && ! ! res . review_comment ) {
210- const { line, side } = computeLineAndSideFromPatch ( patch ) ;
211- ress . push ( {
212- path : file . filename ,
213- body : res . review_comment ,
214- line,
215- side,
216- } )
209+ // Split patch into individual hunks (by @@ markers)
210+ const hunks = splitPatchIntoHunks ( patch ) ;
211+
212+ for ( const hunk of hunks ) {
213+ const res = await chat ?. codeReview ( hunk . content ) ;
214+ if ( ! res . lgtm && ! ! res . review_comment ) {
215+ const { line, side } = computeLineAndSideFromPatch ( hunk . content ) ;
216+ ress . push ( {
217+ path : file . filename ,
218+ body : res . review_comment ,
219+ line : hunk . newStart + line - 1 ,
220+ side,
221+ } )
222+ }
217223 }
218224 } catch ( e ) {
219225 log . info ( `review ${ file . filename } failed` , e ) ;
@@ -323,4 +329,48 @@ const computeLineAndSideFromPatch = (patch: string | undefined) => {
323329 if ( firstDeleted !== null ) return { line : firstDeleted , side : 'LEFT' as 'RIGHT' | 'LEFT' } ;
324330
325331 return { line : 1 , side : 'RIGHT' as 'RIGHT' | 'LEFT' } ;
332+ }
333+
334+ const splitPatchIntoHunks = ( patch : string ) : Array < { header : string ; newStart : number ; content : string } > => {
335+ // Split patch into individual hunks by @@ markers
336+ const hunks : Array < { header : string ; newStart : number ; content : string } > = [ ] ;
337+ const lines = patch . split ( '\n' ) ;
338+ let currentHunk : string [ ] = [ ] ;
339+ let currentHeader = '' ;
340+ let currentNewStart = 0 ;
341+
342+ const parseHunkHeader = ( hdr : string ) => {
343+ const m = hdr . match ( / @ @ \s + - \d + (?: , \d + ) ? \s + \+ ( \d + ) (?: , \d + ) ? \s + @ @ / ) ;
344+ if ( ! m ) return 0 ;
345+ return parseInt ( m [ 1 ] , 10 ) ;
346+ } ;
347+
348+ for ( const line of lines ) {
349+ if ( line . startsWith ( '@@' ) ) {
350+ // Save previous hunk if it exists
351+ if ( currentHunk . length > 0 ) {
352+ hunks . push ( {
353+ header : currentHeader ,
354+ newStart : currentNewStart ,
355+ content : currentHunk . join ( '\n' ) ,
356+ } ) ;
357+ }
358+ currentHeader = line ;
359+ currentNewStart = parseHunkHeader ( line ) ;
360+ currentHunk = [ line ] ;
361+ } else if ( currentHunk . length > 0 ) {
362+ currentHunk . push ( line ) ;
363+ }
364+ }
365+
366+ // Save the last hunk
367+ if ( currentHunk . length > 0 ) {
368+ hunks . push ( {
369+ header : currentHeader ,
370+ newStart : currentNewStart ,
371+ content : currentHunk . join ( '\n' ) ,
372+ } ) ;
373+ }
374+
375+ return hunks ;
326376}
0 commit comments