CICO-111286: Include framework as params

This commit is contained in:
Arun Murugan 2024-06-12 04:09:52 -04:00
parent 3b64b4f5fc
commit 2c508903b4
4 changed files with 97 additions and 86 deletions

View file

@ -8,7 +8,7 @@ import minimatch from "minimatch";
const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
const OPENAI_API_MODEL: string = core.getInput("OPENAI_API_MODEL");
const FRAMEWORK: string = core.getInput("framework"); // New input for framework
const FRAMEWORK: string = core.getInput("framework");
const octokit = new Octokit({ auth: GITHUB_TOKEN });
@ -57,29 +57,9 @@ async function getDiff(
return response.data;
}
async function getExistingComments(
owner: string,
repo: string,
pull_number: number
): Promise<Array<{ path: string; line: number; body: string }>> {
const commentsResponse = await octokit.pulls.listReviewComments({
owner,
repo,
pull_number,
});
return commentsResponse.data
.filter(comment => comment.line !== undefined)
.map(comment => ({
path: comment.path,
line: comment.line!,
body: comment.body,
}));
}
async function analyzeCode(
parsedDiff: File[],
prDetails: PRDetails,
existingComments: Array<{ path: string; line: number; body: string }>
prDetails: PRDetails
): Promise<Array<{ body: string; path: string; line: number }>> {
const comments: Array<{ body: string; path: string; line: number }> = [];
@ -90,16 +70,8 @@ async function analyzeCode(
const aiResponse = await getAIResponse(prompt);
if (aiResponse) {
const newComments = createComment(file, chunk, aiResponse);
for (const comment of newComments) {
const duplicate = existingComments.some(
existingComment =>
existingComment.path === comment.path &&
existingComment.line === comment.line &&
existingComment.body.trim() === comment.body.trim()
);
if (!duplicate) {
comments.push(comment);
}
if (newComments) {
comments.push(...newComments);
}
}
}
@ -396,8 +368,16 @@ async function getAIResponse(prompt: string): Promise<Array<{
const res = response.choices[0].message?.content?.trim() || "";
// Extract JSON content from Markdown code block
const jsonContent = res.match(/```json([\s\S]*)```/)?.[1];
let jsonContent: string | null = null;
// Check if the response is in a code block
const codeBlockMatch = res.match(/```json([\s\S]*)```/);
if (codeBlockMatch) {
jsonContent = codeBlockMatch[1];
} else {
// If not, assume the response is direct JSON
jsonContent = res;
}
if (!jsonContent) {
console.error("Failed to extract JSON content from response.");
@ -461,13 +441,36 @@ async function createReviewComment(
pull_number: number,
comments: Array<{ body: string; path: string; line: number }>
): Promise<void> {
await octokit.pulls.createReview({
owner,
repo,
pull_number,
comments,
event: "COMMENT",
});
const validComments = comments.filter(comment => comment.path && comment.line > 0 && comment.body.trim() !== "");
if (validComments.length === 0) {
console.log("No valid comments to add");
return;
}
console.log("Attempting to create review comments:", JSON.stringify(validComments, null, 2));
for (const comment of validComments) {
try {
await octokit.pulls.createReview({
owner,
repo,
pull_number,
body: comment.body,
path: comment.path,
line: comment.line,
event: 'COMMENT',
});
} catch (error) {
console.error("Error creating review comment:", error);
console.log("Request data:", {
owner,
repo,
pull_number,
comment,
});
}
}
}
async function main() {
@ -510,6 +513,8 @@ async function main() {
const parsedDiff = parseDiff(diff);
console.log("Parsed Diff:", JSON.stringify(parsedDiff, null, 2)); // Log parsed diff for debugging
const excludePatterns = core
.getInput("exclude")
.split(",")
@ -521,13 +526,7 @@ async function main() {
);
});
const existingComments = await getExistingComments(
prDetails.owner,
prDetails.repo,
prDetails.pull_number
);
const comments = await analyzeCode(filteredDiff, prDetails, existingComments);
const comments = await analyzeCode(filteredDiff, prDetails);
if (comments.length > 0) {
await createReviewComment(
prDetails.owner,