Skip to content

Commit dd84cb8

Browse files
committed
feat: add lgtm
1 parent ca9b672 commit dd84cb8

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/bot.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ export const robot = (app: Probot) => {
156156
}
157157
try {
158158
const res = await chat?.codeReview(patch);
159-
if (!!res) {
159+
if (!res.lgtm && !!res.review_comment) {
160160
ress.push({
161161
path: file.filename,
162-
body: res,
162+
body: res.review_comment,
163163
position: patch.split('\n').length - 1,
164164
})
165165
}
@@ -172,7 +172,7 @@ export const robot = (app: Probot) => {
172172
repo: repo.repo,
173173
owner: repo.owner,
174174
pull_number: context.pullRequest().pull_number,
175-
body: "Code review by ChatGPT",
175+
body: ress.length ? "Code review by ChatGPT" : "LGTM 👍",
176176
event: 'COMMENT',
177177
commit_id: commits[commits.length - 1].sha,
178178
comments: ress,

src/chat.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,26 @@ export class Chat {
3131
? `Answer me in ${process.env.LANGUAGE},`
3232
: '';
3333

34-
const prompt =
35-
process.env.PROMPT ||
36-
'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:';
34+
const userPrompt = process.env.PROMPT || 'Please review the following code patch. Focus on potential bugs, risks, and improvement suggestions.';
35+
36+
const jsonFormatRequirement = '\nProvide your feedback in a strict JSON format with the following structure:\n' +
37+
'{\n' +
38+
' "lgtm": boolean, // true if the code looks good to merge, false if there are concerns\n' +
39+
' "review_comment": string // Your detailed review comments. You can use markdown syntax in this string, but the overall response must be a valid JSON\n' +
40+
'}\n' +
41+
'Ensure your response is a valid JSON object.\n';
3742

38-
return `${prompt}, ${answerLanguage}:
39-
${patch}
43+
return `${userPrompt}${jsonFormatRequirement} ${answerLanguage}:
44+
${patch}
4045
`;
4146
};
4247

43-
public codeReview = async (patch: string) => {
48+
public codeReview = async (patch: string): Promise<{ lgtm: boolean, review_comment: string }> => {
4449
if (!patch) {
45-
return '';
50+
return {
51+
lgtm: true,
52+
review_comment: ""
53+
};
4654
}
4755

4856
console.time('code-review cost');
@@ -59,14 +67,28 @@ export class Chat {
5967
temperature: +(process.env.temperature || 0) || 1,
6068
top_p: +(process.env.top_p || 0) || 1,
6169
max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined,
70+
response_format: {
71+
type: "json_object"
72+
},
6273
});
6374

6475
console.timeEnd('code-review cost');
6576

6677
if (res.choices.length) {
67-
return res.choices[0].message.content;
78+
try {
79+
const json = JSON.parse(res.choices[0].message.content || "");
80+
return json
81+
} catch (e) {
82+
return {
83+
lgtm: false,
84+
review_comment: res.choices[0].message.content || ""
85+
}
86+
}
6887
}
6988

70-
return '';
89+
return {
90+
lgtm: true,
91+
review_comment: ""
92+
}
7193
};
7294
}

0 commit comments

Comments
 (0)