This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
forked from bahricanyesil/nodejs-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (91 loc) · 3.18 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
const { Octokit } = require("@octokit/rest");
const express = require('express');
const { Configuration, OpenAIApi } = require("openai");
const octokit = new Octokit({auth: process.env.MY_GITHUB_TOKEN})
const openAIAPIKey = process.env.API_KEY
const app = express()
const port = 4000
async function createReviewComment(pull_number, body, commit_id, path) {
await octokit.request('POST /repos/{owner}/{repo}/pulls/{pull_number}/comments', {
owner: 'RedVentures',
repo: 'rd-hackathon-2023-team-2',
pull_number: pull_number,
body: body,
commit_id: commit_id,
path: path,
start_line: 1,
start_side: 'RIGHT',
line: 2,
side: 'RIGHT',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
}
async function getLatestPR() {
const res = await octokit.request('GET /repos/{owner}/{repo}/pulls', {
owner: 'RedVentures',
repo: 'rd-hackathon-2023-team-2',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
// console.log('--- Get latest PR', res)
return [res.data[0].number, res.data[0].path]
}
async function getPRFiles(pull_number) {
const res = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/files', {
owner: 'RedVentures',
repo: 'rd-hackathon-2023-team-2',
pull_number: pull_number,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
// console.log('Files Diff Response', res)
return res
}
async function getLatestCommitID(pull_number) {
const res = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/commits', {
owner: 'RedVentures',
repo: 'rd-hackathon-2023-team-2',
pull_number: pull_number,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
// console.log('Latest Commit Response Response', res)
return res.data[res.data.length-1].sha
}
async function commentChatGPTReview(queryText, pull_number, commit_id, path) {
const configuration = new Configuration({
organization: "org-FNLEwrTIjd8amAb84DOcvNNL",
apiKey: openAIAPIKey,
});
const openai = new OpenAIApi(configuration);
const res = await openai.createChatCompletion({
model: "gpt-4",
messages: [
{role: "user", content: `Act as a Programming expert and review this code and give helpful feedback. It should only include improvement suggestions along with examples. Provide the feedback along with the line number. This is the code for which the feedback is needed ${queryText}`},
],
});
for(let comment of res.data.choices) {
// console.log(`\n\n${comment}\n\n`)
await createReviewComment(pull_number, comment.message.content, commit_id, path)
}
}
app.get('/', async (req, res) => {
const [pull_number, path] = await getLatestPR()
const filesDiff = await getPRFiles(pull_number)
const commitId = await getLatestCommitID(pull_number)
for(const file of filesDiff.data) {
// console.log(file,' ---file')
if(file.filename.includes('.js') || file.filename.includes('.css') || file.filename.includes('.py')) {
await commentChatGPTReview(file.patch, pull_number,commitId,file.filename)
}
}
res.send('Review Completed!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})