-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
169 lines (149 loc) · 5.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
const detect = require('language-detect');
const httpsProxyAgent = require('https-proxy-agent');
function configWithProxy(config) {
var c = config || {};
if (process.env.OPENAI_PROXY) {
core.debug(`use proxy: ${process.env.OPENAI_PROXY}`);
c.proxy = false;
c.httpsAgent = new httpsProxyAgent(process.env.OPENAI_PROXY);
return c;
}
return c;
}
async function run() {
try {
// Get input values
const programmingLanguage = core.getInput('PROGRAMMING_LANGUAGE');
const openaiToken = core.getInput('OPENAI_TOKEN');
const fullReviewComment = core.getInput('FULL_REVIEW_COMMENT');
const reviewCommentPrefix = core.getInput('REVIEW_COMMENT_PREFIX');
const githubToken = core.getInput('GITHUB_TOKEN');
const githubBaseURL = core.getInput('GITHUB_BASE_URL') || process.env.GITHUB_API_URL;
const promptTemplate = core.getInput('PROMPT_TEMPLATE');
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
core.debug(`programmingLanguage: ${programmingLanguage}`);
core.debug(`openaiToken length: ${openaiToken.length}`);
core.debug(`fullReviewComment: ${fullReviewComment}`);
core.debug(`reviewCommentPrefix: ${reviewCommentPrefix}`);
core.debug(`githubToken length: ${githubToken.length}`);
core.debug(`githubBaseURL: ${githubBaseURL}`);
core.debug(`promptTemplate: ${promptTemplate}`);
core.debug(`maxCodeLength: ${maxCodeLength}`);
core.debug(`answerTemplate: ${answerTemplate}`);
// Get information about the pull request review
const comment = github.context.payload.comment;
const repoName = github.context.payload.repository.name;
const repoOwner = github.context.payload.repository.owner.login;
const prNumber = github.context.payload.number || github.context.payload.issue.number; // get number from a pull request event or comment event
// Get the code to analyze from the review comment
var content = comment && comment.body || "";
const url = `${githubBaseURL}/repos/${repoOwner}/${repoName}/pulls/${prNumber}`;
core.debug(`diff url: ${url}`);
var response = await axios.get(url, {
headers: {
Authorization: `Bearer ${githubToken}`,
Accept: 'application/vnd.github.diff'
}
});
const code = response.data;
core.debug(`diff code: ${code}`);
const files = parsePullRequestDiff(code);
core.debug(`diff files: ${files}`);
if (!content || content == fullReviewComment) {
// Extract the code from the pull request content
content = promptTemplate.replace('${code}', code);
} else {
content = content.substring(reviewCommentPrefix.length);
content = content.replace('${code}', code);
const fileNames = findFileNames(content);
core.debug(`found files name in commment: ${fileNames}`);
for (const fileName of fileNames) {
for (const key of Object.keys(files)) {
if (key.includes(fileName)) {
core.debug(`replace \${file:${fileName}} with ${key}'s diff`);
content = content.replace(`\${file:${fileName}}`, files[key]);
break;
}
}
}
}
content = content.substring(0, maxCodeLength);
// Determine the programming language if it was not provided
if (programmingLanguage == 'auto') {
const detectedLanguage = detect(code);
core.debug(`Detected programming language: ${detectedLanguage}`);
programmingLanguage = detectedLanguage;
}
var messages = [{
role: "system",
content: `You are a master of programming language ${programmingLanguage}`
}, {
role: "user",
content: content
}];
core.debug(`content: ${content}`);
// Call the OpenAI ChatGPT API to analyze the code
response = await axios.post('https://api.openai.com/v1/chat/completions', {
"model": "gpt-3.5-turbo",
"messages": messages
}, configWithProxy({
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${openaiToken}`
}
}));
const answer = response.data.choices[0].message.content;
core.debug(`openai response: ${answer}`);
// Reply to the review comment with the OpenAI response
const octokit = github.getOctokit(githubToken, {
baseUrl: githubBaseURL
});
await octokit.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
body: answerTemplate.replace('${answer}', answer)
// in_reply_to: comment.id
});
} catch (error) {
core.setFailed(error.message);
}
}
function parsePullRequestDiff(diffContent) {
const files = {};
const diffLines = diffContent.split('\n');
let currentFile = null;
let currentLines = [];
for (const line of diffLines) {
if (line.startsWith('diff --git')) {
// Start of a new file
if (currentFile) {
files[currentFile] = currentLines.join('\n');
}
currentFile = line.substring('diff --git'.length + 1);
currentLines = [line];
} else {
// Add the line to the current file's diff
currentLines.push(line);
}
}
// Add the last file's diff
if (currentFile) {
files[currentFile] = currentLines.join('\n');
}
return files;
}
function findFileNames(str) {
const pattern = /\${file:([^{}]+)}/g;
const matches = str.matchAll(pattern);
const names = [];
for (const match of matches) {
names.push(match[1]);
}
return names;
}
run();