Skip to content

Commit 6f8a532

Browse files
authored
Merge pull request #26 from emanuel-braz/prompts
feat: testing differents prompts
2 parents fe4d5cf + 88bd9b9 commit 6f8a532

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

.github/workflows/code-review.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ jobs:
2525
exclude: "**/*.json, **/*.md, **/*.g.dart"
2626
append_prompt: |
2727
- Give a maximum of 4 suggestions
28-
- Do not suggest code formatting issues.
2928
- Do not suggest imports issues.

code-review/action.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,47 @@ async function getDiff(owner, repo, pull_number) {
5757
}
5858

5959
async function analyzeCode(parsedDiff, prDetails) {
60-
const comments = []; //Array<{ body: string; path: string; line: number }>
60+
const allReviews = []; //Array<{ body: string; path: string; line: number }>
6161

6262
for (const file of parsedDiff) {
6363
if (file.to === "/dev/null") continue; // Ignore deleted files
6464
for (const chunk of file.chunks) {
6565

66-
const messages = createMessages(file, chunk, prDetails);
66+
const messages = generateMessages(file, chunk, prDetails);
6767
const aiResponse = await getAIResponse(messages);
6868
if (aiResponse) {
69-
const newComments = createComment(file, chunk, aiResponse);
70-
if (newComments) {
71-
comments.push(...newComments);
69+
if (!isJSON(aiResponse)) {
70+
logger.log(`AI response is not in JSON format: ${aiResponse}`);
71+
createCommentOnPr(aiResponse, prDetails.owner, prDetails.repo, prDetails.pull_number, file.to);
72+
} else {
73+
logger.log(`AI response is in JSON format: ${aiResponse}`);
74+
const reviews = generateReviewsFromJsonArray(file, chunk, aiResponse);
75+
if (reviews) {
76+
allReviews.push(...reviews);
77+
}
7278
}
7379
}
7480
}
7581
}
76-
return comments;
82+
return allReviews;
7783
}
7884

79-
function createMessages(file, chunk, prDetails) {
80-
const instructionJsonFormat = `- Always provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]`;
85+
function isJSON(obj) {
86+
try {
87+
JSON.parse(obj);
88+
return true;
89+
} catch (e) {
90+
return false;
91+
}
92+
}
93+
94+
function generateMessages(file, chunk, prDetails) {
8195

82-
var contentSystemMessage = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions below:
83-
- You will provide suggestions only if there are issues or bugs in the code, otherwise return an empty array.
96+
const instructionJsonFormat = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions:
97+
- Always provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
98+
- You will provide suggestions only if there are issues or bugs in the code, otherwise return an empty array.`;
99+
100+
var contentSystemMessage = `
84101
- Do not give positive comments or compliments.
85102
- Don't suggest removing empty line
86103
- Never suggest adding newline at end of file.
@@ -94,10 +111,10 @@ function createMessages(file, chunk, prDetails) {
94111
contentSystemMessage = overridePrompt;
95112
}
96113

97-
contentSystemMessage = `${contentSystemMessage}\n${instructionJsonFormat}`;
114+
contentSystemMessage = `${instructionJsonFormat}\n${contentSystemMessage}`;
98115

99116
if (appendPrompt) {
100-
contentSystemMessage = `${contentSystemMessage}\n\n${appendPrompt}`;
117+
contentSystemMessage = `${contentSystemMessage}\n${appendPrompt}`;
101118
}
102119

103120
var systemPrompt =
@@ -141,7 +158,7 @@ async function getAIResponse(messages) {
141158
const chatCompletionParams = new ChatCompletionParams({
142159
messages: messages,
143160
model: OPENAI_API_MODEL,
144-
temperature: 0,
161+
temperature: 0.1,
145162
max_tokens: parseInt(maxTokens),
146163
top_p: 1,
147164
frequency_penalty: 0,
@@ -153,15 +170,18 @@ async function getAIResponse(messages) {
153170

154171
const result = response?.trim() || "[]";
155172
logger.log(`AI response: ${result}`);
156-
return JSON.parse(result);
173+
return result;
157174
} catch (error) {
158175
console.error("Error:", error);
159176
return null;
160177
}
161178
}
162179

163180
// Array<{ body: string; path: string; line: number }>
164-
function createComment(file, chunk, aiResponses) {
181+
function generateReviewsFromJsonArray(file, chunk, aiResponses) {
182+
183+
aiResponses = JSON.parse(aiResponses);
184+
165185
return aiResponses.flatMap((aiResponse) => {
166186
if (!file.to) {
167187
return [];
@@ -174,7 +194,7 @@ function createComment(file, chunk, aiResponses) {
174194
});
175195
}
176196

177-
async function createReviewComment(owner, repo, pull_number, comments) {
197+
async function createReviewOnPr(owner, repo, pull_number, comments) {
178198
await octokit.pulls.createReview({
179199
owner,
180200
repo,
@@ -184,6 +204,17 @@ async function createReviewComment(owner, repo, pull_number, comments) {
184204
});
185205
}
186206

207+
async function createCommentOnPr(body, owner, repo, pull_number, path) {
208+
octokit.pulls.createReview({
209+
owner,
210+
repo,
211+
pull_number,
212+
path,
213+
body,
214+
event: "COMMENT",
215+
});
216+
}
217+
187218
async function main() {
188219

189220
const prDetails = await getPRDetails();
@@ -233,7 +264,7 @@ async function main() {
233264

234265
const comments = await analyzeCode(filteredDiff, prDetails);
235266
if (comments.length > 0) {
236-
await createReviewComment(
267+
createReviewOnPr(
237268
prDetails.owner,
238269
prDetails.repo,
239270
prDetails.pull_number,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "braz-actions",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"description": "GitHub Actions",
66
"main": "create-update-release/action.js",

0 commit comments

Comments
 (0)