Verify if an AI comment is part of the actual PR changes prior to submit it

This commit is contained in:
Jimmy Royer 2024-09-16 18:21:57 -04:00
parent 912010c7b3
commit 1abe8317f1
3 changed files with 44 additions and 3 deletions

View file

@ -109,10 +109,26 @@ async function analyzeCode(
for (const file of parsedDiff) {
if (file.to === "/dev/null") continue; // Ignore deleted files
for (const chunk of file.chunks) {
const validLineNumbers = new Set<number>();
chunk.changes.forEach((change: parseDiff.Change) => {
if ("ln" in change && change.ln) validLineNumbers.add(change.ln);
if ("ln2" in change && change.ln2) validLineNumbers.add(change.ln2);
// Generate a range of line numbers for additive changes.
if ("ln1" in change && "ln2" in change && change.ln1 && change.ln2) {
for (let i = change.ln1; i <= change.ln2; i++) {
validLineNumbers.add(i);
}
}
});
const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = await getAIResponse(prompt);
if (aiResponse) {
const newComments = createComment(file, chunk, aiResponse);
const validAIResponses = aiResponse.filter((response) =>
validLineNumbers.has(Number(response.lineNumber))
);
const newComments = createComment(file, chunk, validAIResponses);
if (newComments) {
comments.push(...newComments);
}