Fix the bot to add review comments

This commit is contained in:
Arun Murugan 2024-05-22 09:49:41 -04:00
parent fcd39ade07
commit f2f7079221
3 changed files with 38 additions and 11 deletions

View file

@ -2,7 +2,7 @@ import { readFileSync } from "fs";
import * as core from "@actions/core";
import OpenAI from "openai";
import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff";
import parseDiff, { Chunk, File, Change } from "parse-diff";
import minimatch from "minimatch";
const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
@ -87,9 +87,7 @@ function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
- Use the given description only for the overall context and only comment the code.
- IMPORTANT: NEVER suggest adding comments to the code.
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:
@ -103,8 +101,7 @@ Git diff to review:
\`\`\`diff
${chunk.content}
${chunk.changes
// @ts-expect-error - ln and ln2 exists where needed
.map((c) => `${c.ln ? c.ln : c.ln2} ${c.content}`)
.map((c) => `${'ln' in c ? c.ln : 'ln2' in c ? c.ln2 : ''} ${c.content}`)
.join("\n")}
\`\`\`
`;
@ -173,10 +170,27 @@ function createComment(
if (!file.to) {
return [];
}
const lineNumber = Number(aiResponse.lineNumber);
// Find the matching change in the chunk
const change = chunk.changes.find((c) => {
if ("ln" in c && c.ln === lineNumber) return true;
if ("ln2" in c && c.ln2 === lineNumber) return true;
return false;
});
if (!change) {
console.error(`Line number ${aiResponse.lineNumber} not found in the diff for file ${file.to}`);
return [];
}
const commentLine = "ln" in change ? change.ln : "ln2" in change ? change.ln2 : 0;
return {
body: aiResponse.reviewComment,
path: file.to,
line: Number(aiResponse.lineNumber),
line: commentLine,
};
});
}