review changed files only

This commit is contained in:
Ville Saukkonen 2023-04-22 16:47:51 +03:00
parent 32f7a8317c
commit 0f627714f1
3 changed files with 106 additions and 9 deletions

48
dist/index.js vendored

@ -89,6 +89,8 @@ function analyzeCode(parsedDiff, prDetails) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const comments = []; const comments = [];
for (const file of parsedDiff) { for (const file of parsedDiff) {
if (file.to === "/dev/null")
continue; // Ignore deleted files
for (const chunk of file.chunks) { for (const chunk of file.chunks) {
const prompt = createPrompt(file, chunk, prDetails); const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = yield getAIResponse(prompt); const aiResponse = yield getAIResponse(prompt);
@ -103,6 +105,30 @@ function analyzeCode(parsedDiff, prDetails) {
return comments; return comments;
}); });
} }
function getChangedFiles(owner, repo, baseSha, headSha) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield octokit.repos.compareCommits({
owner,
repo,
base: baseSha,
head: headSha,
});
return response.data.diff_url;
});
}
function getBaseAndHeadShas(owner, repo, pull_number) {
return __awaiter(this, void 0, void 0, function* () {
const prResponse = yield octokit.pulls.get({
owner,
repo,
pull_number,
});
return {
baseSha: prResponse.data.base.sha,
headSha: prResponse.data.head.sha,
};
});
}
function createPrompt(file, chunk, prDetails) { function createPrompt(file, chunk, prDetails) {
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}] return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- Do not give positive comments or compliments. - Do not give positive comments or compliments.
@ -181,14 +207,27 @@ function createReviewComment(owner, repo, pull_number, comments) {
}); });
}); });
} }
(function main() { function main() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const prDetails = yield getPRDetails(); const prDetails = yield getPRDetails();
const diff = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number); const { baseSha, headSha } = yield getBaseAndHeadShas(prDetails.owner, prDetails.repo, prDetails.pull_number);
if (!diff) { let diffUrl;
if (process.env.GITHUB_EVENT_NAME === "pull_request") {
diffUrl = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number);
}
else if (process.env.GITHUB_EVENT_NAME === "push") {
diffUrl = yield getChangedFiles(prDetails.owner, prDetails.repo, baseSha, headSha);
}
else {
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
return;
}
if (!diffUrl) {
console.log("No diff found"); console.log("No diff found");
return; return;
} }
const diffResponse = yield octokit.request({ url: diffUrl });
const diff = diffResponse.data;
const parsedDiff = (0, parse_diff_1.default)(diff); const parsedDiff = (0, parse_diff_1.default)(diff);
const excludePatterns = core const excludePatterns = core
.getInput("exclude") .getInput("exclude")
@ -202,7 +241,8 @@ function createReviewComment(owner, repo, pull_number, comments) {
yield createReviewComment(prDetails.owner, prDetails.repo, prDetails.pull_number, comments); yield createReviewComment(prDetails.owner, prDetails.repo, prDetails.pull_number, comments);
} }
}); });
})().catch((error) => { }
main().catch((error) => {
console.error("Error:", error); console.error("Error:", error);
process.exit(1); process.exit(1);
}); });

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

@ -79,6 +79,37 @@ async function analyzeCode(
return comments; return comments;
} }
async function getChangedFiles(
owner: string,
repo: string,
baseSha: string,
headSha: string
): Promise<string | null> {
const response = await octokit.repos.compareCommits({
owner,
repo,
base: baseSha,
head: headSha,
});
return response.data.diff_url;
}
async function getBaseAndHeadShas(
owner: string,
repo: string,
pull_number: number
): Promise<{ baseSha: string; headSha: string }> {
const prResponse = await octokit.pulls.get({
owner,
repo,
pull_number,
});
return {
baseSha: prResponse.data.base.sha,
headSha: prResponse.data.head.sha,
};
}
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string { function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}] return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- Do not give positive comments or compliments. - Do not give positive comments or compliments.
@ -177,19 +208,43 @@ async function createReviewComment(
}); });
} }
(async function main() { async function main() {
const prDetails = await getPRDetails(); const prDetails = await getPRDetails();
const diff = await getDiff( const { baseSha, headSha } = await getBaseAndHeadShas(
prDetails.owner, prDetails.owner,
prDetails.repo, prDetails.repo,
prDetails.pull_number prDetails.pull_number
); );
if (!diff) {
let diffUrl: string | null;
if (process.env.GITHUB_EVENT_NAME === "pull_request") {
diffUrl = await getDiff(
prDetails.owner,
prDetails.repo,
prDetails.pull_number
);
} else if (process.env.GITHUB_EVENT_NAME === "push") {
diffUrl = await getChangedFiles(
prDetails.owner,
prDetails.repo,
baseSha,
headSha
);
} else {
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
return;
}
if (!diffUrl) {
console.log("No diff found"); console.log("No diff found");
return; return;
} }
const diffResponse = await octokit.request({ url: diffUrl });
const diff = diffResponse.data;
const parsedDiff = parseDiff(diff); const parsedDiff = parseDiff(diff);
const excludePatterns = core const excludePatterns = core
.getInput("exclude") .getInput("exclude")
.split(",") .split(",")
@ -210,7 +265,9 @@ async function createReviewComment(
comments comments
); );
} }
})().catch((error) => { }
main().catch((error) => {
console.error("Error:", error); console.error("Error:", error);
process.exit(1); process.exit(1);
}); });