Skip to content

Only comment on lines that are part of the changed code #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
};
});
}
Expand Down