From 897fedd47ccee3c02a1e1233ff3b26b8c055fc0f Mon Sep 17 00:00:00 2001 From: Shain Lafazan Date: Mon, 13 Jan 2025 14:48:34 -0800 Subject: [PATCH] Only comment on lines that are part of the changed code --- src/main.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 13677ae4..bb8138cf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -86,10 +86,9 @@ function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string { - Write the comment in GitHub Markdown format. - Use the given description only for the overall context and only comment the code. - IMPORTANT: NEVER suggest adding comments to the code. +- IMPORTANT: Only comment on lines that are part of the changed code (lines starting with + or -). -Review the following code diff in the file "${ - file.to - }" and take the pull request title and description into account when writing the response. +Review the following code diff in the file "${file.to}" and take the pull request title and description into account when writing the response. Pull request title: ${prDetails.title} Pull request description: @@ -158,10 +157,27 @@ function createComment( if (!file.to) { return []; } + + // Convert lineNumber to number + const lineNum = Number(aiResponse.lineNumber); + + // Verify the line number is within the current chunk + const isLineInChunk = chunk.changes.some( + (change) => + // Check both new and old line numbers + (change.ln && change.ln === lineNum) || + (change.ln2 && change.ln2 === lineNum) + ); + + if (!isLineInChunk) { + console.log(`Warning: Line ${lineNum} is not part of the current diff chunk in ${file.to}`); + return []; + } + return { body: aiResponse.reviewComment, path: file.to, - line: Number(aiResponse.lineNumber), + line: lineNum, }; }); }