-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
131 lines (102 loc) · 3.69 KB
/
server.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
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const path = require('path');
const { Configuration, OpenAIApi } = require('openai');
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname)));
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/review-file', async (req, res) => {
const fileUrl = req.query.repo_url;
const accessToken = req.query.access_token; // For private repos
console.log(fileUrl);
if (!fileUrl) {
return res.status(400).send('File URL is required');
}
try {
const fileResponse = await axios.get(fileUrl, {
headers: { Authorization: `token ${accessToken}` },
});
let fileContent;
if (fileResponse.data.encoding === 'base64') {
const encodedContent = fileResponse.data.content;
fileContent = Buffer.from(encodedContent, 'base64').toString('utf-8');
} else {
fileContent = fileResponse.data.content;
}
console.log(fileContent);
const openaiResponse = await openai.createCompletion({
model: "code-davinci-002", // Choose the appropriate model
prompt: "Review this code:\n" + fileContent, // Construct your prompt
max_tokens: 150,
});
res.json({ review: openaiResponse.data.choices[0].text });
} catch (error) {
console.error('Error:', error.response?.data || error.message);
res.status(500).send('Internal Server Error');
}
});
app.get('/check-repo', async (req, res) => {
const fileUrl = req.query.repo_url; // Assuming the file URL is passed as a query parameter
console.log(fileUrl);
try {
await axios.get(fileUrl);
res.redirect(`/review-file?repo_url=${encodeURIComponent(fileUrl)}`);
} catch (error) {
console.log(error);
res.redirect('/auth/github?repo_url=' + encodeURIComponent(fileUrl));
}
});
app.get('/auth/github', (req, res) => {
const url = `https://github.com/login/oauth/authorize?client_id=${process.env.GITHUB_CLIENT_ID}`;
res.redirect(url);
});
app.get('/auth/github/callback', async (req, res) => {
const code = req.query.code;
if (!code) {
return res.status(400).send('Code not provided');
}
try {
const tokenResponse = await axios.post(
'https://github.com/login/oauth/access_token',
{
client_id: process.env.GITHUB_CLIENT_ID,
client_secret: process.env.GITHUB_CLIENT_SECRET,
code: code,
},
{
headers: {
accept: 'application/json',
},
}
);
const accessToken = tokenResponse.data.access_token;
res.redirect(`/repo?access_token=${accessToken}`);
} catch (error) {
console.error('Error exchanging code for token:', error.response?.data || error.message);
res.status(500).send('Internal Server Error');
}
});
app.get('/repo', async (req, res) => {
const repoUrl = req.query.repo_url;
try {
const repoData = await axios.get(repoUrl, {
headers: { Authorization: `token ${accessToken}` },
});
res.json(repoData.data);
} catch (error) {
res.status(500).json({ message: 'Error accessing repository' });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});