-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (114 loc) · 3.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const core = require('@actions/core');
const github = require('@actions/github');
const { Octokit } = require('@octokit/rest');
const axios = require('axios');
const githubToken = core.getInput('gh-token');
const openaiApiKey = core.getInput('openai-api-key');
const model = core.getInput('model');
const language = core.getInput('language');
const octokit = new Octokit({ auth: githubToken });
const prompt = (code, lang) => `
# Role: Code Reviewer
## Profile
- Author: User
- Version: 5.0
- Language: ${lang}
- Description: A code reviewer is an individual who critically evaluates a piece of code and provides constructive feedback. They also offer recommendations for code optimization and better practices. If feasible, they provide sample code to illustrate their suggestions.
## Prompt
Please examine the following code snippet and provide your feedback. Also, suggest enhancements and provide illustrative sample code if possible.
## Instruction
- When providing feedback, break it down into Feedback and Suggestions for Improvement sections.
- In the Feedback section, highlight any issues, mistakes, or areas of confusion you find in the code.
- In the Suggestions for Improvement section, offer actionable steps to improve the code.
- If possible, provide sample code to demonstrate your suggestions. Ensure to enclose the sample code within a separate code block.
- Generate the response in the ${lang} language.
## Code for review:
${code}
`;
const reviewCodeWithOpenAI = async (code) => {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: model,
messages: [
{
role: 'system',
content: 'You are a helpful assistant that reviews code.',
},
{
role: 'user',
content: prompt(code, language),
},
],
max_tokens: 1000,
n: 1,
stop: null,
temperature: 0.7,
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${openaiApiKey}`,
},
}
);
return response.data.choices[0].message.content.trim();
};
const run = async () => {
try {
const context = github.context;
const event = context.eventName;
const payload = context.payload;
if (
event === 'issue_comment' &&
payload.action === 'created' &&
payload.comment &&
payload.issue
) {
const comment = payload.comment.body;
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
const issueNumber = payload.issue.number;
if (comment.trim() === '/review' && payload.issue.pull_request) {
const pullRequestUrl = payload.issue.pull_request.url;
const pullRequestNumber = pullRequestUrl.split('/').pop();
try {
const { data: files } = await octokit.pulls.listFiles({
owner,
repo,
pull_number: pullRequestNumber,
});
let reviewComments = '';
for (const file of files) {
if (file.patch) {
const review = await reviewCodeWithOpenAI(file.patch);
reviewComments += `#### Review for ${file.filename}:\n\n${review}\n\n`;
}
}
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: reviewComments,
});
} catch (error) {
console.error(`Error fetching PR files: ${error.message}`);
if (error.response) {
console.error(
`Error details: ${JSON.stringify(error.response.data, null, 2)}`
);
}
core.setFailed(`Error fetching PR files: ${error.message}`);
}
}
}
} catch (error) {
core.setFailed(error.message);
if (error.response) {
core.setFailed(
`Error details: ${JSON.stringify(error.response.data, null, 2)}`
);
}
}
};
run();