comment on line numbers and avoid giving positive comments

This commit is contained in:
Ville Saukkonen 2023-03-23 19:16:20 +02:00
parent 8308ada88f
commit dfff4cc30c
6 changed files with 1426 additions and 36 deletions

1374
dist/index.js vendored

File diff suppressed because it is too large Load Diff

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

25
dist/licenses.txt vendored

@ -580,6 +580,31 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
csv-parse
MIT
The MIT License (MIT)
Copyright (c) 2010 Adaltas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
delayed-stream
MIT
Copyright (c) 2011 Debuggable Limited <felix@debuggable.com>

@ -14,6 +14,7 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@octokit/rest": "^19.0.7",
"csv-parse": "^5.3.6",
"minimatch": "^7.4.2",
"openai": "^3.2.1",
"parse-diff": "^0.11.1",

@ -4,6 +4,7 @@ import { Configuration, OpenAIApi } from "openai";
import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff";
import minimatch from "minimatch";
import { parse } from "csv-parse/sync";
const GITHUB_TOKEN: string = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY: string = core.getInput("OPENAI_API_KEY");
@ -68,9 +69,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.concat(newComments);
}
}
}
@ -80,7 +81,7 @@ async function analyzeCode(
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
return `
Review the following code changes in the file "${
Review the following code diff in the file "${
file.to
}" and take the pull request title and description into account when writing the response.
@ -92,20 +93,30 @@ 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)
Please provide comments and suggestions ONLY if there is something to improve, write the answer in Github markdown. Don't give positive comments. Use the description only for the overall context and only comment the code.
Diff to review:
---
${chunk.content}
${chunk.changes
.map((c) => (c.type === "add" ? "+" : "-") + " " + c.content)
.join("\n")}
---
Give the answer in following TSV format:
line_number\treview_comment
`;
}
async function getAIResponse(prompt: string): Promise<string | null> {
async function getAIResponse(prompt: string): Promise<Array<{
line_number: string;
review_comment: 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 +133,12 @@ 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 parse(res, {
delimiter: "\t",
columns: ["line_number", "review_comment"],
skip_empty_lines: true,
});
} catch (error) {
console.error("Error:", error);
return null;
@ -132,20 +148,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) {
aiResponses: Array<{
line_number: string;
review_comment: string;
}>
): Array<{ body: string; path: string; line: number }> {
return aiResponses.flatMap((aiResponse) => {
if (!file.to) {
return [];
}
return {
body: aiResponse,
body: aiResponse.review_comment,
path: file.to,
// @ts-expect-error below properties exists on AddChange
line: lastAddChange.ln || lastAddChange.ln1,
line: Number(aiResponse.line_number),
};
}
return null;
});
}
async function createReviewComment(

@ -229,6 +229,11 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
csv-parse@^5.3.6:
version "5.3.6"
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.3.6.tgz#181d7c12300a60684bb51261ea9a5c3135ba8688"
integrity sha512-WI330GjCuEioK/ii8HM2YE/eV+ynpeLvU+RXw4R8bRU8R0laK5zO3fDsc4gH8s472e3Ga38rbIjCAiQh+tEHkw==
delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"