Skip to content

Commit

Permalink
Merge pull request #10 from freeedcom/reveiw-changed-files-only
Browse files Browse the repository at this point in the history
Reveiw changed files only
  • Loading branch information
villesau authored Apr 22, 2023
2 parents 32f7a83 + cc41538 commit 07b4e53
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code_review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- name: Code Review
uses: freeedcom/ai-codereviewer@main
uses: freeedcom/ai-codereviewer@reveiw-changed-files-only
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
Expand Down
50 changes: 45 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ function analyzeCode(parsedDiff, prDetails) {
return __awaiter(this, void 0, void 0, function* () {
const comments = [];
for (const file of parsedDiff) {
if (file.to === "/dev/null")
continue; // Ignore deleted files
for (const chunk of file.chunks) {
const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = yield getAIResponse(prompt);
Expand All @@ -103,12 +105,25 @@ function analyzeCode(parsedDiff, prDetails) {
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) {
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- 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.
- 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.

Review the following code diff in the file "${file.to}" and take the pull request title and description into account when writing the response.
Expand Down Expand Up @@ -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* () {
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) {
console.log("No diff found");
return;
Expand All @@ -202,7 +241,8 @@ function createReviewComment(owner, repo, pull_number, comments) {
yield createReviewComment(prDetails.owner, prDetails.repo, prDetails.pull_number, comments);
}
});
})().catch((error) => {
}
main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

62 changes: 54 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,28 @@ async function analyzeCode(
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 {
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
- 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.
- 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.
Review the following code diff in the file "${
Expand Down Expand Up @@ -177,19 +193,47 @@ async function createReviewComment(
});
}

(async function main() {
async function main() {
const prDetails = await getPRDetails();
const diff = await getDiff(
prDetails.owner,
prDetails.repo,
prDetails.pull_number
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.repo,
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) {
console.log("No diff found");
return;
}

const parsedDiff = parseDiff(diff);

const excludePatterns = core
.getInput("exclude")
.split(",")
Expand All @@ -210,7 +254,9 @@ async function createReviewComment(
comments
);
}
})().catch((error) => {
}

main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});

0 comments on commit 07b4e53

Please sign in to comment.