mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2025-04-21 10:06:47 +00:00
chore: fix formatting issues in main.ts file
This commit is contained in:
parent
271056abf4
commit
977935fd5e
1 changed files with 53 additions and 54 deletions
107
src/main.ts
107
src/main.ts
|
@ -1,8 +1,8 @@
|
||||||
import { readFileSync } from "fs";
|
import {readFileSync} from "fs";
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import OpenAI from "openai";
|
import OpenAI from "openai";
|
||||||
import { Octokit } from "@octokit/rest";
|
import {Octokit} from "@octokit/rest";
|
||||||
import parseDiff, { Chunk, File } from "parse-diff";
|
import parseDiff, {Chunk, File} from "parse-diff";
|
||||||
import minimatch from "minimatch";
|
import minimatch from "minimatch";
|
||||||
import {parseJsonForChatGPTResponse} from "./parseJsonForChatGPTResponse";
|
import {parseJsonForChatGPTResponse} from "./parseJsonForChatGPTResponse";
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
|
||||||
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
|
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
|
||||||
const OPENAI_API_MODEL: string = core.getInput("OPENAI_API_MODEL");
|
const OPENAI_API_MODEL: string = core.getInput("OPENAI_API_MODEL");
|
||||||
|
|
||||||
const octokit = new Octokit({ auth: GITHUB_TOKEN });
|
const octokit = new Octokit({auth: GITHUB_TOKEN});
|
||||||
|
|
||||||
const openai = new OpenAI({
|
const openai = new OpenAI({
|
||||||
apiKey: OPENAI_API_KEY,
|
apiKey: OPENAI_API_KEY,
|
||||||
});
|
});
|
||||||
|
|
||||||
interface PRDetails {
|
interface PRDetails {
|
||||||
owner: string;
|
owner: string;
|
||||||
|
@ -25,14 +25,14 @@ interface PRDetails {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPRDetails(): Promise<PRDetails> {
|
async function getPRDetails(): Promise<PRDetails> {
|
||||||
const { repository, number } = JSON.parse(
|
const {repository, number} = JSON.parse(
|
||||||
readFileSync(process.env.GITHUB_EVENT_PATH || "", "utf8")
|
readFileSync(process.env.GITHUB_EVENT_PATH || "", "utf8")
|
||||||
);
|
);
|
||||||
const prResponse = await octokit.pulls.get({
|
const prResponse = await octokit.pulls.get({
|
||||||
owner: repository.owner.login,
|
owner: repository.owner.login,
|
||||||
repo: repository.name,
|
repo: repository.name,
|
||||||
pull_number: number,
|
pull_number: number,
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
owner: repository.owner.login,
|
owner: repository.owner.login,
|
||||||
repo: repository.name,
|
repo: repository.name,
|
||||||
|
@ -48,11 +48,11 @@ async function getDiff(
|
||||||
pull_number: number
|
pull_number: number
|
||||||
): Promise<string | null> {
|
): Promise<string | null> {
|
||||||
const response = await octokit.pulls.get({
|
const response = await octokit.pulls.get({
|
||||||
owner,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
pull_number,
|
pull_number,
|
||||||
mediaType: { format: "diff" },
|
mediaType: {format: "diff"},
|
||||||
});
|
});
|
||||||
// @ts-expect-error - response.data is a string
|
// @ts-expect-error - response.data is a string
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
|
@ -108,9 +108,9 @@ Git diff to review:
|
||||||
\`\`\`diff
|
\`\`\`diff
|
||||||
${chunk.content}
|
${chunk.content}
|
||||||
${chunk.changes
|
${chunk.changes
|
||||||
// @ts-expect-error - ln and ln2 exists where needed
|
// @ts-expect-error - ln and ln2 exists where needed
|
||||||
.map((c) => `${c.ln ? c.ln : c.ln2} ${c.content}`)
|
.map((c) => `${c.ln ? c.ln : c.ln2} ${c.content}`)
|
||||||
.join("\n")}
|
.join("\n")}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -128,27 +128,26 @@ async function getAIResponse(prompt: string): Promise<Array<{
|
||||||
presence_penalty: 0,
|
presence_penalty: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
const response =
|
||||||
const response = await openai.chat.completions.create({
|
await openai.chat.completions.create(
|
||||||
...queryConfig,
|
{
|
||||||
// return JSON if the model supports it:
|
...queryConfig,
|
||||||
...(OPENAI_API_MODEL === "gpt-4-1106-preview"
|
// return JSON if the model supports it:
|
||||||
? { response_format: { type: "json_object" } }
|
...(OPENAI_API_MODEL === "gpt-4-1106-preview"
|
||||||
: {}),
|
? {response_format: {type: "json_object"}}
|
||||||
messages: [
|
: {}),
|
||||||
{
|
messages: [
|
||||||
role: "system",
|
{
|
||||||
content: prompt,
|
role: "system",
|
||||||
},
|
content: prompt,
|
||||||
],
|
},
|
||||||
});
|
],
|
||||||
|
});
|
||||||
|
|
||||||
const res = response.choices[0].message?.content?.trim() || "{}";
|
const res = response.choices[0].message?.content?.trim() || "{}";
|
||||||
return parseJsonForChatGPTResponse(res).reviews;
|
console.log("GPT response:")
|
||||||
} catch (error) {
|
console.log(res);
|
||||||
console.error("Error:", error);
|
return parseJsonForChatGPTResponse(res).reviews;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createComment(
|
function createComment(
|
||||||
|
@ -178,12 +177,12 @@ async function createReviewComment(
|
||||||
comments: Array<{ body: string; path: string; line: number }>
|
comments: Array<{ body: string; path: string; line: number }>
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await octokit.pulls.createReview({
|
await octokit.pulls.createReview({
|
||||||
owner,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
pull_number,
|
pull_number,
|
||||||
comments,
|
comments,
|
||||||
event: "COMMENT",
|
event: "COMMENT",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
|
@ -204,14 +203,14 @@ async function main() {
|
||||||
const newHeadSha = eventData.after;
|
const newHeadSha = eventData.after;
|
||||||
|
|
||||||
const response = await octokit.repos.compareCommits({
|
const response = await octokit.repos.compareCommits({
|
||||||
headers: {
|
headers: {
|
||||||
accept: "application/vnd.github.v3.diff",
|
accept: "application/vnd.github.v3.diff",
|
||||||
},
|
},
|
||||||
owner: prDetails.owner,
|
owner: prDetails.owner,
|
||||||
repo: prDetails.repo,
|
repo: prDetails.repo,
|
||||||
base: newBaseSha,
|
base: newBaseSha,
|
||||||
head: newHeadSha,
|
head: newHeadSha,
|
||||||
});
|
});
|
||||||
|
|
||||||
diff = String(response.data);
|
diff = String(response.data);
|
||||||
} else {
|
} else {
|
||||||
|
@ -233,7 +232,7 @@ async function main() {
|
||||||
|
|
||||||
const filteredDiff = parsedDiff.filter((file) => {
|
const filteredDiff = parsedDiff.filter((file) => {
|
||||||
return !excludePatterns.some((pattern) =>
|
return !excludePatterns.some((pattern) =>
|
||||||
minimatch(file.to ?? "", pattern)
|
minimatch(file.to ?? "", pattern)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue