Skip to content

Commit 07b4e53

Browse files
authored
Merge pull request #10 from freeedcom/reveiw-changed-files-only
Reveiw changed files only
2 parents 32f7a83 + cc41538 commit 07b4e53

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

.github/workflows/code_review.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Checkout repository
1313
uses: actions/checkout@v3
1414
- name: Code Review
15-
uses: freeedcom/ai-codereviewer@main
15+
uses: freeedcom/ai-codereviewer@reveiw-changed-files-only
1616
with:
1717
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1818
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

dist/index.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ function analyzeCode(parsedDiff, prDetails) {
8989
return __awaiter(this, void 0, void 0, function* () {
9090
const comments = [];
9191
for (const file of parsedDiff) {
92+
if (file.to === "/dev/null")
93+
continue; // Ignore deleted files
9294
for (const chunk of file.chunks) {
9395
const prompt = createPrompt(file, chunk, prDetails);
9496
const aiResponse = yield getAIResponse(prompt);
@@ -103,12 +105,25 @@ function analyzeCode(parsedDiff, prDetails) {
103105
return comments;
104106
});
105107
}
108+
function getBaseAndHeadShas(owner, repo, pull_number) {
109+
return __awaiter(this, void 0, void 0, function* () {
110+
const prResponse = yield octokit.pulls.get({
111+
owner,
112+
repo,
113+
pull_number,
114+
});
115+
return {
116+
baseSha: prResponse.data.base.sha,
117+
headSha: prResponse.data.head.sha,
118+
};
119+
});
120+
}
106121
function createPrompt(file, chunk, prDetails) {
107122
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
108123
- Do not give positive comments or compliments.
109-
- Do not recommend adding comments to the code.
124+
- NEVER suggest adding a comment explaining the code.
110125
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
111-
- Write the comment in GitHub markdown.
126+
- Write the comment in GitHub Markdown format.
112127
- Use the given description only for the overall context and only comment the code.
113128

114129
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) {
181196
});
182197
});
183198
}
184-
(function main() {
199+
function main() {
200+
var _a;
185201
return __awaiter(this, void 0, void 0, function* () {
186202
const prDetails = yield getPRDetails();
187-
const diff = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number);
203+
let diff;
204+
const eventData = JSON.parse((0, fs_1.readFileSync)((_a = process.env.GITHUB_EVENT_PATH) !== null && _a !== void 0 ? _a : "", "utf8"));
205+
if (eventData.action === "opened") {
206+
diff = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number);
207+
}
208+
else if (eventData.action === "synchronize") {
209+
const newBaseSha = eventData.before;
210+
const newHeadSha = eventData.after;
211+
const response = yield octokit.repos.compareCommits({
212+
owner: prDetails.owner,
213+
repo: prDetails.repo,
214+
base: newBaseSha,
215+
head: newHeadSha,
216+
});
217+
diff = response.data.diff_url
218+
? yield octokit
219+
.request({ url: response.data.diff_url })
220+
.then((res) => res.data)
221+
: null;
222+
}
223+
else {
224+
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
225+
return;
226+
}
188227
if (!diff) {
189228
console.log("No diff found");
190229
return;
@@ -202,7 +241,8 @@ function createReviewComment(owner, repo, pull_number, comments) {
202241
yield createReviewComment(prDetails.owner, prDetails.repo, prDetails.pull_number, comments);
203242
}
204243
});
205-
})().catch((error) => {
244+
}
245+
main().catch((error) => {
206246
console.error("Error:", error);
207247
process.exit(1);
208248
});

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,28 @@ async function analyzeCode(
7979
return comments;
8080
}
8181

82+
async function getBaseAndHeadShas(
83+
owner: string,
84+
repo: string,
85+
pull_number: number
86+
): Promise<{ baseSha: string; headSha: string }> {
87+
const prResponse = await octokit.pulls.get({
88+
owner,
89+
repo,
90+
pull_number,
91+
});
92+
return {
93+
baseSha: prResponse.data.base.sha,
94+
headSha: prResponse.data.head.sha,
95+
};
96+
}
97+
8298
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
8399
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
84100
- Do not give positive comments or compliments.
85-
- Do not recommend adding comments to the code.
101+
- NEVER suggest adding a comment explaining the code.
86102
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
87-
- Write the comment in GitHub markdown.
103+
- Write the comment in GitHub Markdown format.
88104
- Use the given description only for the overall context and only comment the code.
89105
90106
Review the following code diff in the file "${
@@ -177,19 +193,47 @@ async function createReviewComment(
177193
});
178194
}
179195

180-
(async function main() {
196+
async function main() {
181197
const prDetails = await getPRDetails();
182-
const diff = await getDiff(
183-
prDetails.owner,
184-
prDetails.repo,
185-
prDetails.pull_number
198+
let diff: string | null;
199+
const eventData = JSON.parse(
200+
readFileSync(process.env.GITHUB_EVENT_PATH ?? "", "utf8")
186201
);
202+
203+
if (eventData.action === "opened") {
204+
diff = await getDiff(
205+
prDetails.owner,
206+
prDetails.repo,
207+
prDetails.pull_number
208+
);
209+
} else if (eventData.action === "synchronize") {
210+
const newBaseSha = eventData.before;
211+
const newHeadSha = eventData.after;
212+
213+
const response = await octokit.repos.compareCommits({
214+
owner: prDetails.owner,
215+
repo: prDetails.repo,
216+
base: newBaseSha,
217+
head: newHeadSha,
218+
});
219+
220+
diff = response.data.diff_url
221+
? await octokit
222+
.request({ url: response.data.diff_url })
223+
.then((res) => res.data)
224+
: null;
225+
} else {
226+
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
227+
return;
228+
}
229+
187230
if (!diff) {
188231
console.log("No diff found");
189232
return;
190233
}
191234

192235
const parsedDiff = parseDiff(diff);
236+
193237
const excludePatterns = core
194238
.getInput("exclude")
195239
.split(",")
@@ -210,7 +254,9 @@ async function createReviewComment(
210254
comments
211255
);
212256
}
213-
})().catch((error) => {
257+
}
258+
259+
main().catch((error) => {
214260
console.error("Error:", error);
215261
process.exit(1);
216262
});

0 commit comments

Comments
 (0)