Merge pull request #14 from arunsnt/CICO-111286

CICO-111286: Fix error and more logs
This commit is contained in:
Arun Murugan 2024-06-12 03:22:26 -04:00 committed by GitHub
commit 14d4fe7bb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 40 deletions

23
dist/index.js vendored
View file

@ -467,7 +467,7 @@ function createComment(file, chunk, aiResponses) {
}; };
}); });
} }
function createReviewComment(owner, repo, pull_number, comments) { function createReviewComment(owner, repo, pull_number, comments, commit_id) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const validComments = comments.filter(comment => comment.path && comment.line > 0 && comment.body.trim() !== ""); const validComments = comments.filter(comment => comment.path && comment.line > 0 && comment.body.trim() !== "");
if (validComments.length === 0) { if (validComments.length === 0) {
@ -475,13 +475,17 @@ function createReviewComment(owner, repo, pull_number, comments) {
return; return;
} }
console.log("Attempting to create review comments:", JSON.stringify(validComments, null, 2)); console.log("Attempting to create review comments:", JSON.stringify(validComments, null, 2));
for (const comment of validComments) {
try { try {
yield octokit.pulls.createReview({ yield octokit.pulls.createReviewComment({
owner, owner,
repo, repo,
pull_number, pull_number,
comments: validComments, body: comment.body,
event: "COMMENT", path: comment.path,
line: comment.line,
side: 'RIGHT',
commit_id, // Include commit_id in the request
}); });
} }
catch (error) { catch (error) {
@ -490,10 +494,11 @@ function createReviewComment(owner, repo, pull_number, comments) {
owner, owner,
repo, repo,
pull_number, pull_number,
comments: validComments, comment,
event: "COMMENT", commit_id,
}); });
} }
}
}); });
} }
function main() { function main() {
@ -502,8 +507,10 @@ function main() {
const prDetails = yield getPRDetails(); const prDetails = yield getPRDetails();
let diff; let diff;
const eventData = JSON.parse((0, fs_1.readFileSync)((_a = process.env.GITHUB_EVENT_PATH) !== null && _a !== void 0 ? _a : "", "utf8")); const eventData = JSON.parse((0, fs_1.readFileSync)((_a = process.env.GITHUB_EVENT_PATH) !== null && _a !== void 0 ? _a : "", "utf8"));
let commit_id;
if (eventData.action === "opened") { if (eventData.action === "opened") {
diff = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number); diff = yield getDiff(prDetails.owner, prDetails.repo, prDetails.pull_number);
commit_id = eventData.pull_request.head.sha;
} }
else if (eventData.action === "synchronize") { else if (eventData.action === "synchronize") {
const newBaseSha = eventData.before; const newBaseSha = eventData.before;
@ -518,6 +525,7 @@ function main() {
head: newHeadSha, head: newHeadSha,
}); });
diff = String(response.data); diff = String(response.data);
commit_id = newHeadSha;
} }
else { else {
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME); console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
@ -539,7 +547,8 @@ function main() {
const existingComments = yield getExistingComments(prDetails.owner, prDetails.repo, prDetails.pull_number); const existingComments = yield getExistingComments(prDetails.owner, prDetails.repo, prDetails.pull_number);
const comments = yield analyzeCode(filteredDiff, prDetails, existingComments); const comments = yield analyzeCode(filteredDiff, prDetails, existingComments);
if (comments.length > 0) { if (comments.length > 0) {
yield createReviewComment(prDetails.owner, prDetails.repo, prDetails.pull_number, comments); yield createReviewComment(prDetails.owner, prDetails.repo, prDetails.pull_number, comments, commit_id // Pass commit_id to the function
);
} }
}); });
} }

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -474,7 +474,8 @@ async function createReviewComment(
owner: string, owner: string,
repo: string, repo: string,
pull_number: number, pull_number: number,
comments: Array<{ body: string; path: string; line: number }> comments: Array<{ body: string; path: string; line: number }>,
commit_id: string
): Promise<void> { ): Promise<void> {
const validComments = comments.filter(comment => comment.path && comment.line > 0 && comment.body.trim() !== ""); const validComments = comments.filter(comment => comment.path && comment.line > 0 && comment.body.trim() !== "");
@ -485,13 +486,17 @@ async function createReviewComment(
console.log("Attempting to create review comments:", JSON.stringify(validComments, null, 2)); console.log("Attempting to create review comments:", JSON.stringify(validComments, null, 2));
for (const comment of validComments) {
try { try {
await octokit.pulls.createReview({ await octokit.pulls.createReviewComment({
owner, owner,
repo, repo,
pull_number, pull_number,
comments: validComments, body: comment.body,
event: "COMMENT", path: comment.path,
line: comment.line,
side: 'RIGHT', // Ensure the comment is on the right side of the diff
commit_id, // Include commit_id in the request
}); });
} catch (error) { } catch (error) {
console.error("Error creating review comment:", error); console.error("Error creating review comment:", error);
@ -499,11 +504,12 @@ async function createReviewComment(
owner, owner,
repo, repo,
pull_number, pull_number,
comments: validComments, comment,
event: "COMMENT", commit_id,
}); });
} }
} }
}
async function main() { async function main() {
const prDetails = await getPRDetails(); const prDetails = await getPRDetails();
@ -512,12 +518,15 @@ async function main() {
readFileSync(process.env.GITHUB_EVENT_PATH ?? "", "utf8") readFileSync(process.env.GITHUB_EVENT_PATH ?? "", "utf8")
); );
let commit_id: string;
if (eventData.action === "opened") { if (eventData.action === "opened") {
diff = await getDiff( diff = await getDiff(
prDetails.owner, prDetails.owner,
prDetails.repo, prDetails.repo,
prDetails.pull_number prDetails.pull_number
); );
commit_id = eventData.pull_request.head.sha;
} else if (eventData.action === "synchronize") { } else if (eventData.action === "synchronize") {
const newBaseSha = eventData.before; const newBaseSha = eventData.before;
const newHeadSha = eventData.after; const newHeadSha = eventData.after;
@ -533,6 +542,7 @@ async function main() {
}); });
diff = String(response.data); diff = String(response.data);
commit_id = newHeadSha;
} else { } else {
console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME); console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME);
return; return;
@ -570,7 +580,8 @@ async function main() {
prDetails.owner, prDetails.owner,
prDetails.repo, prDetails.repo,
prDetails.pull_number, prDetails.pull_number,
comments comments,
commit_id // Pass commit_id to the function
); );
} }
} }