Merge pull request #10 from freeedcom/reveiw-changed-files-only

Reveiw changed files only
This commit is contained in:
Ville Saukkonen 2023-04-22 18:08:07 +03:00 committed by GitHub
commit 07b4e53660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 15 deletions

@ -12,7 +12,7 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Code Review - name: Code Review
uses: freeedcom/ai-codereviewer@main uses: freeedcom/ai-codereviewer@reveiw-changed-files-only
with: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

50
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,12 +105,25 @@ function analyzeCode(parsedDiff, prDetails) {
return comments; return comments;
}); });
} }
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.
- Do not recommend adding comments to the code. - NEVER suggest adding a comment explaining the code.
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array. - Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
- Write the comment in GitHub markdown. - Write the comment in GitHub Markdown format.
- Use the given description only for the overall context and only comment the code. - Use the given description only for the overall context and only comment 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.
@ -181,10 +196,34 @@ function createReviewComment(owner, repo, pull_number, comments) {
}); });
}); });
} }
(function main() { function main() {
var _a;
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); let diff;
const eventData = JSON.parse((0, fs_1.readFileSync)((_a = process.env.GITHUB_EVENT_PATH) !== null && _a !== void 0 ? _a : "", "utf8"));
if (eventData.action === "opened") {
diff = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number);
}
else if (eventData.action === "synchronize") {
const newBaseSha = eventData.before;
const newHeadSha = eventData.after;
const response = yield octokit.repos.compareCommits({
owner: prDetails.owner,
repo: prDetails.repo,
base: newBaseSha,
head: newHeadSha,
});
diff = response.data.diff_url
? yield octokit
.request({ url: response.data.diff_url })
.then((res) => res.data)
: null;
}
else {
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
return;
}
if (!diff) { if (!diff) {
console.log("No diff found"); console.log("No diff found");
return; return;
@ -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,12 +79,28 @@ async function analyzeCode(
return comments; return comments;
} }
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.
- Do not recommend adding comments to the code. - NEVER suggest adding a comment explaining the code.
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array. - Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
- Write the comment in GitHub markdown. - Write the comment in GitHub Markdown format.
- Use the given description only for the overall context and only comment the code. - Use the given description only for the overall context and only comment the code.
Review the following code diff in the file "${ Review the following code diff in the file "${
@ -177,19 +193,47 @@ async function createReviewComment(
}); });
} }
(async function main() { async function main() {
const prDetails = await getPRDetails(); const prDetails = await getPRDetails();
const diff = await getDiff( let diff: string | null;
const eventData = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH ?? "", "utf8")
);
if (eventData.action === "opened") {
diff = await getDiff(
prDetails.owner, prDetails.owner,
prDetails.repo, prDetails.repo,
prDetails.pull_number prDetails.pull_number
); );
} else if (eventData.action === "synchronize") {
const newBaseSha = eventData.before;
const newHeadSha = eventData.after;
const response = await octokit.repos.compareCommits({
owner: prDetails.owner,
repo: prDetails.repo,
base: newBaseSha,
head: newHeadSha,
});
diff = response.data.diff_url
? await octokit
.request({ url: response.data.diff_url })
.then((res) => res.data)
: null;
} else {
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
return;
}
if (!diff) { if (!diff) {
console.log("No diff found"); console.log("No diff found");
return; return;
} }
const parsedDiff = parseDiff(diff); const parsedDiff = parseDiff(diff);
const excludePatterns = core const excludePatterns = core
.getInput("exclude") .getInput("exclude")
.split(",") .split(",")
@ -210,7 +254,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);
}); });