-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (130 loc) · 5.83 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
const { default: axios } = require("axios");
const { execSync } = require("child_process");
const { mkdirSync, writeFileSync } = require("fs");
const { Octokit } = require("octokit");
let config = {
githubToken: process.env.GITHUB_TOKEN,
personalAccessToken: process.env.INPUT_PERSONAL_ACCESS_TOKEN,
purgptApiKey: process.env.INPUT_PURGPT_API_KEY,
issue: process.env.INPUT_ISSUE ? JSON.parse(process.env.INPUT_ISSUE) : undefined,
repository: process.env.INPUT_REPOSITORY,
model: process.env.INPUT_MODEL ?? 'gpt-3.5-turbo-16k'
};
console.log('Initial config loaded', config);
const gitHub = new Octokit({
auth: config.pat,
request: {
fetch: async (url, options) => {
let response;
try {
response = await axios({
url,
method: options.method,
data: options.body,
headers: {
...options.headers,
Authorization: `Bearer ${config.personalAccessToken}`
}
});
} catch (error) {
console.error('Error occured while fetching from GitHub.', error, error?.response?.data);
response = error;
};
return {
status: response.status,
json: () => Promise.resolve(response?.data ?? error.response ?? {}),
headers: response.headers
};
}
}
});
function code(content) {
return /```(\w+)?\n([\s\S]*?)\n```/.exec(content)?.[2];
};
async function purgpt(message) {
let response;
let tries = 0;
console.log('Asking', message);
while (!response && tries < 3) {
try {
response = (await axios.post('https://beta.purgpt.xyz/openai/chat/completions', {
model: config.model,
messages: [
{
role: 'system',
content: `You are PurRequest, a GitHub bot that automatically manages pull requests. Your current task is create a pull request for issue #${config.issue.number}. Here is the issue description:\n\n**Number:** ${config.issue.number}\n**Title:** ${config.issue.title}\n**Author:** ${config.issue.user.login}\n**Labels:** ${config.issue.labels.map(label => label.name).join(', ')}\n**Message:** ${config.issue.body}\n\nCurrent file map of the repository:\n${config.files.map(file => file).join('\n')}\nYou can only respond with code blocks with JSON format. You have to provide the correct information to users.`
},
...[message]
]
}, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.purgptApiKey}`
}
})).data;
} catch (error) {
console.error('Error occured while fetching from PurGPT.', error, error?.response?.data);
};
tries++;
await new Promise(resolve => setTimeout(resolve, 3000));
};
if (!response) throw new Error('Failed to fetch from PurGPT.');
console.log('Response', response);
return response;
};
async function validJSON(message) {
let response;
let json;
while (!json) {
try {
response = await purgpt(message);
response = response.choices[0].message.content;
console.log(response, code(response));
if (code(response)) json = JSON.parse(code(response));
} catch (error) {
console.error('Error occured while checking JSON.', error, error?.response?.data);
};
};
return json;
};
(async () => {
config.user = (await gitHub.rest.users.getAuthenticated()).data;
config.repository = (await gitHub.rest.repos.get({
owner: config.repository.split('/')[0],
repo: config.repository.split('/')[1]
})).data;
execSync(`git clone https://${config.personalAccessToken}@github.com/${config.repository.owner.login}/${config.repository.name} && cd ./${config.repository.name} && git config user.email "${config.user.email}" && git config user.name "${config.user.login}" && git checkout -b issue-${config.issue.number}`);
config.files = execSync(`cd ${config.repository.name} && git ls-files`).toString().split('\n').filter(file => file);
config.issue = (await gitHub.rest.issues.get({
owner: config.repository.owner.login,
repo: config.repository.name,
issue_number: config.issue.url.split('/').pop()
})).data;
let createFiles = await validJSON({
role: 'user',
content: "Fill the following JSON with the files you want to create/edit. If you won't create any file, leave it an empty array.\n\n```json\n[\n{\n\"path\": \"example: path/to/file.js\",\n\"content\": \"file content\"\n}\n]\n```"
});
for (let createFile of createFiles) {
let folders = createFile.path.split('/');
folders.pop();
let path = '';
for (let folder of folders) {
path += `/${folder}`;
try {
mkdirSync(`./${config.repository.name}${path}`);
} catch (error) {
console.error('Error occured while creating folder.', error);
};
};
writeFileSync(`./${config.repository.name}/${createFile.path}`, createFile.content);
};
execSync(`cd ./${config.repository.name} && git add . && git commit -m "Issue ${config.issue.number}" && git push --set-upstream origin issue-${config.issue.number}`);
await gitHub.rest.pulls.create({
owner: config.repository.owner,
repo: config.repository.name,
title: `Issue ${config.issue.number}`,
head: `issue-${config.issue.number}`,
base: config.repository.default_branch,
body: `Fixes Issue #${config.issue.number}`
});
})();