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:
Ville Saukkonen 2023-03-28 01:55:30 +03:00 committed by GitHub
commit ea61d39797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 51 deletions

52
dist/index.js vendored

@ -93,9 +93,9 @@ function analyzeCode(parsedDiff, prDetails) {
const prompt = createPrompt(file, chunk, prDetails); const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = yield getAIResponse(prompt); const aiResponse = yield getAIResponse(prompt);
if (aiResponse) { if (aiResponse) {
const comment = createComment(file, chunk, aiResponse); const newComments = createComment(file, chunk, aiResponse);
if (comment) { if (newComments) {
comments.push(comment); comments.push(...newComments);
} }
} }
} }
@ -104,23 +104,28 @@ function analyzeCode(parsedDiff, prDetails) {
}); });
} }
function createPrompt(file, chunk, prDetails) { function createPrompt(file, chunk, prDetails) {
return ` return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
Review the following code changes in the file "${file.to}" and take the pull request title and description into account when writing the response. - 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} ${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.content}
${chunk.changes ${chunk.changes.map((c) => c.content).join("\n")}
.map((c) => (c.type === "add" ? "+" : "-") + " " + c.content) \`\`\`
.join("\n")}
`; `;
} }
function getAIResponse(prompt) { function getAIResponse(prompt) {
@ -129,7 +134,7 @@ function getAIResponse(prompt) {
const queryConfig = { const queryConfig = {
model: "gpt-4", model: "gpt-4",
temperature: 0.2, temperature: 0.2,
max_tokens: 400, max_tokens: 700,
top_p: 1, top_p: 1,
frequency_penalty: 0, frequency_penalty: 0,
presence_penalty: 0, presence_penalty: 0,
@ -141,7 +146,8 @@ function getAIResponse(prompt) {
content: 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) { catch (error) {
console.error("Error:", error); console.error("Error:", error);
@ -149,19 +155,17 @@ function getAIResponse(prompt) {
} }
}); });
} }
function createComment(file, chunk, aiResponse) { function createComment(file, chunk, aiResponses) {
const lastAddChange = [...chunk.changes] return aiResponses.flatMap((aiResponse) => {
.reverse() if (!file.to) {
.find((c) => c.type === "add"); return [];
if (lastAddChange && file.to) { }
return { return {
body: aiResponse, body: aiResponse.reviewComment,
path: file.to, path: file.to,
// @ts-expect-error below properties exists on AddChange line: Number(aiResponse.lineNumber),
line: lastAddChange.ln || lastAddChange.ln1,
}; };
} });
return null;
} }
function createReviewComment(owner, repo, pull_number, comments) { function createReviewComment(owner, repo, pull_number, comments) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

@ -68,9 +68,9 @@ async function analyzeCode(
const prompt = createPrompt(file, chunk, prDetails); const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = await getAIResponse(prompt); const aiResponse = await getAIResponse(prompt);
if (aiResponse) { if (aiResponse) {
const comment = createComment(file, chunk, aiResponse); const newComments = createComment(file, chunk, aiResponse);
if (comment) { if (newComments) {
comments.push(comment); comments.push(...newComments);
} }
} }
} }
@ -79,33 +79,41 @@ async function analyzeCode(
} }
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string { function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
return ` return `- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
Review the following code changes in the file "${ - 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 file.to
}" and take the pull request title and description into account when writing the response. }" and take the pull request title and description into account when writing the response.
Title: ${prDetails.title} Pull request title: ${prDetails.title}
Pull request description:
Description:
--- ---
${prDetails.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.content}
${chunk.changes ${chunk.changes.map((c) => c.content).join("\n")}
.map((c) => (c.type === "add" ? "+" : "-") + " " + 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 = { const queryConfig = {
model: "gpt-4", model: "gpt-4",
temperature: 0.2, temperature: 0.2,
max_tokens: 400, max_tokens: 700,
top_p: 1, top_p: 1,
frequency_penalty: 0, frequency_penalty: 0,
presence_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) { } catch (error) {
console.error("Error:", error); console.error("Error:", error);
return null; return null;
@ -132,20 +141,21 @@ async function getAIResponse(prompt: string): Promise<string | null> {
function createComment( function createComment(
file: File, file: File,
chunk: Chunk, chunk: Chunk,
aiResponse: string aiResponses: Array<{
): { body: string; path: string; line: number } | null { lineNumber: string;
const lastAddChange = [...chunk.changes] reviewComment: string;
.reverse() }>
.find((c) => c.type === "add"); ): Array<{ body: string; path: string; line: number }> {
if (lastAddChange && file.to) { return aiResponses.flatMap((aiResponse) => {
if (!file.to) {
return [];
}
return { return {
body: aiResponse, body: aiResponse.reviewComment,
path: file.to, path: file.to,
// @ts-expect-error below properties exists on AddChange line: Number(aiResponse.lineNumber),
line: lastAddChange.ln || lastAddChange.ln1,
}; };
} });
return null;
} }
async function createReviewComment( async function createReviewComment(