mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2024-11-23 04:29:03 +00:00
Merge pull request #4 from freeedcom/comment-individual-lines-and-do-not-give-positive-comments
Comment individual lines and do not give positive comments
This commit is contained in:
commit
ea61d39797
54
dist/index.js
vendored
54
dist/index.js
vendored
@ -93,9 +93,9 @@ function analyzeCode(parsedDiff, prDetails) {
|
||||
const prompt = createPrompt(file, chunk, prDetails);
|
||||
const aiResponse = yield getAIResponse(prompt);
|
||||
if (aiResponse) {
|
||||
const comment = createComment(file, chunk, aiResponse);
|
||||
if (comment) {
|
||||
comments.push(comment);
|
||||
const newComments = createComment(file, chunk, aiResponse);
|
||||
if (newComments) {
|
||||
comments.push(...newComments);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -104,23 +104,28 @@ function analyzeCode(parsedDiff, prDetails) {
|
||||
});
|
||||
}
|
||||
function createPrompt(file, chunk, prDetails) {
|
||||
return `
|
||||
Review the following code changes in the file "${file.to}" and take the pull request title and description into account when writing the response.
|
||||
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
|
||||
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
|
||||
- Write the comment in GitHub markdown.
|
||||
- Don't give positive comments.
|
||||
- Use the given description only for the overall context and only comment the code.
|
||||
- Calculate the line number from \`@@ -WW,XX +YY,ZZ @@\` using following formula: \`YY + L = line_number\`, where \`YY\` is the starting line number from the diff hunk, and \`L\` is the number of lines (including unchanged lines) from the starting line until the line you want to comment on. Pay special attention to this instruction and ensure that you count lines accurately.
|
||||
|
||||
Title: ${prDetails.title}
|
||||
Review the following code diff in the file "${file.to}" and take the pull request title and description into account when writing the response.
|
||||
|
||||
Description:
|
||||
Pull request title: ${prDetails.title}
|
||||
Pull request description:
|
||||
|
||||
---
|
||||
${prDetails.description}
|
||||
---
|
||||
|
||||
Please provide comments and suggestions ONLY if there is something to improve, write the answer in Github markdown. If the code looks good, DO NOT return any text (leave the response completely empty)
|
||||
Git diff to review:
|
||||
|
||||
\`\`\`diff
|
||||
${chunk.content}
|
||||
${chunk.changes
|
||||
.map((c) => (c.type === "add" ? "+" : "-") + " " + c.content)
|
||||
.join("\n")}
|
||||
${chunk.changes.map((c) => c.content).join("\n")}
|
||||
\`\`\`
|
||||
`;
|
||||
}
|
||||
function getAIResponse(prompt) {
|
||||
@ -129,7 +134,7 @@ function getAIResponse(prompt) {
|
||||
const queryConfig = {
|
||||
model: "gpt-4",
|
||||
temperature: 0.2,
|
||||
max_tokens: 400,
|
||||
max_tokens: 700,
|
||||
top_p: 1,
|
||||
frequency_penalty: 0,
|
||||
presence_penalty: 0,
|
||||
@ -141,7 +146,8 @@ function getAIResponse(prompt) {
|
||||
content: prompt,
|
||||
},
|
||||
] }));
|
||||
return ((_b = (_a = response.data.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || null;
|
||||
const res = ((_b = (_a = response.data.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || "[]";
|
||||
return JSON.parse(res);
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error:", error);
|
||||
@ -149,19 +155,17 @@ function getAIResponse(prompt) {
|
||||
}
|
||||
});
|
||||
}
|
||||
function createComment(file, chunk, aiResponse) {
|
||||
const lastAddChange = [...chunk.changes]
|
||||
.reverse()
|
||||
.find((c) => c.type === "add");
|
||||
if (lastAddChange && file.to) {
|
||||
return {
|
||||
body: aiResponse,
|
||||
path: file.to,
|
||||
// @ts-expect-error below properties exists on AddChange
|
||||
line: lastAddChange.ln || lastAddChange.ln1,
|
||||
};
|
||||
function createComment(file, chunk, aiResponses) {
|
||||
return aiResponses.flatMap((aiResponse) => {
|
||||
if (!file.to) {
|
||||
return [];
|
||||
}
|
||||
return null;
|
||||
return {
|
||||
body: aiResponse.reviewComment,
|
||||
path: file.to,
|
||||
line: Number(aiResponse.lineNumber),
|
||||
};
|
||||
});
|
||||
}
|
||||
function createReviewComment(owner, repo, pull_number, comments) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
|
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
66
src/main.ts
66
src/main.ts
@ -68,9 +68,9 @@ async function analyzeCode(
|
||||
const prompt = createPrompt(file, chunk, prDetails);
|
||||
const aiResponse = await getAIResponse(prompt);
|
||||
if (aiResponse) {
|
||||
const comment = createComment(file, chunk, aiResponse);
|
||||
if (comment) {
|
||||
comments.push(comment);
|
||||
const newComments = createComment(file, chunk, aiResponse);
|
||||
if (newComments) {
|
||||
comments.push(...newComments);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -79,33 +79,41 @@ async function analyzeCode(
|
||||
}
|
||||
|
||||
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
|
||||
return `
|
||||
Review the following code changes in the file "${
|
||||
return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
|
||||
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
|
||||
- Write the comment in GitHub markdown.
|
||||
- Don't give positive comments.
|
||||
- Use the given description only for the overall context and only comment the code.
|
||||
- Calculate the line number from \`@@ -WW,XX +YY,ZZ @@\` using following formula: \`YY + L = line_number\`, where \`YY\` is the starting line number from the diff hunk, and \`L\` is the number of lines (including unchanged lines) from the starting line until the line you want to comment on. Pay special attention to this instruction and ensure that you count lines accurately.
|
||||
|
||||
Review the following code diff in the file "${
|
||||
file.to
|
||||
}" and take the pull request title and description into account when writing the response.
|
||||
|
||||
Title: ${prDetails.title}
|
||||
|
||||
Description:
|
||||
Pull request title: ${prDetails.title}
|
||||
Pull request description:
|
||||
|
||||
---
|
||||
${prDetails.description}
|
||||
---
|
||||
|
||||
Please provide comments and suggestions ONLY if there is something to improve, write the answer in Github markdown. If the code looks good, DO NOT return any text (leave the response completely empty)
|
||||
Git diff to review:
|
||||
|
||||
\`\`\`diff
|
||||
${chunk.content}
|
||||
${chunk.changes
|
||||
.map((c) => (c.type === "add" ? "+" : "-") + " " + c.content)
|
||||
.join("\n")}
|
||||
${chunk.changes.map((c) => c.content).join("\n")}
|
||||
\`\`\`
|
||||
`;
|
||||
}
|
||||
|
||||
async function getAIResponse(prompt: string): Promise<string | null> {
|
||||
async function getAIResponse(prompt: string): Promise<Array<{
|
||||
lineNumber: string;
|
||||
reviewComment: string;
|
||||
}> | null> {
|
||||
const queryConfig = {
|
||||
model: "gpt-4",
|
||||
temperature: 0.2,
|
||||
max_tokens: 400,
|
||||
max_tokens: 700,
|
||||
top_p: 1,
|
||||
frequency_penalty: 0,
|
||||
presence_penalty: 0,
|
||||
@ -122,7 +130,8 @@ async function getAIResponse(prompt: string): Promise<string | null> {
|
||||
],
|
||||
});
|
||||
|
||||
return response.data.choices[0].message?.content?.trim() || null;
|
||||
const res = response.data.choices[0].message?.content?.trim() || "[]";
|
||||
return JSON.parse(res);
|
||||
} catch (error) {
|
||||
console.error("Error:", error);
|
||||
return null;
|
||||
@ -132,20 +141,21 @@ async function getAIResponse(prompt: string): Promise<string | null> {
|
||||
function createComment(
|
||||
file: File,
|
||||
chunk: Chunk,
|
||||
aiResponse: string
|
||||
): { body: string; path: string; line: number } | null {
|
||||
const lastAddChange = [...chunk.changes]
|
||||
.reverse()
|
||||
.find((c) => c.type === "add");
|
||||
if (lastAddChange && file.to) {
|
||||
return {
|
||||
body: aiResponse,
|
||||
path: file.to,
|
||||
// @ts-expect-error below properties exists on AddChange
|
||||
line: lastAddChange.ln || lastAddChange.ln1,
|
||||
};
|
||||
aiResponses: Array<{
|
||||
lineNumber: string;
|
||||
reviewComment: string;
|
||||
}>
|
||||
): Array<{ body: string; path: string; line: number }> {
|
||||
return aiResponses.flatMap((aiResponse) => {
|
||||
if (!file.to) {
|
||||
return [];
|
||||
}
|
||||
return null;
|
||||
return {
|
||||
body: aiResponse.reviewComment,
|
||||
path: file.to,
|
||||
line: Number(aiResponse.lineNumber),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function createReviewComment(
|
||||
|
Loading…
Reference in New Issue
Block a user