-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprReview.js
More file actions
184 lines (171 loc) ยท 4.84 KB
/
prReview.js
File metadata and controls
184 lines (171 loc) ยท 4.84 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* PR ์๋ ๋ฆฌ๋ทฐ ์ ํธ๋ฆฌํฐ
*/
import { getGitHubHeaders } from "./github.js";
import { generateCodeReview } from "./openai.js";
/**
* PR diff ๊ฐ์ ธ์ค๊ธฐ
*
* @param {string} repoOwner - ์ ์ฅ์ ์์ ์
* @param {string} repoName - ์ ์ฅ์ ์ด๋ฆ
* @param {number} prNumber - PR ๋ฒํธ
* @param {string} githubToken - GitHub ํ ํฐ
* @returns {Promise<string>} PR diff ๋ด์ฉ
*/
export async function getPRDiff(repoOwner, repoName, prNumber, githubToken) {
const response = await fetch(
`https://api.github.com/repos/${repoOwner}/${repoName}/pulls/${prNumber}`,
{
headers: {
...getGitHubHeaders(githubToken),
Accept: "application/vnd.github.v3.diff",
},
}
);
if (!response.ok) {
throw new Error(`Failed to get PR diff: ${response.statusText}`);
}
return await response.text();
}
/**
* AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋๊ธ ์์ฑ
*
* @param {string} repoOwner - ์ ์ฅ์ ์์ ์
* @param {string} repoName - ์ ์ฅ์ ์ด๋ฆ
* @param {number} prNumber - PR ๋ฒํธ
* @param {string} reviewContent - ๋ฆฌ๋ทฐ ๋ด์ฉ (๋งํฌ๋ค์ด)
* @param {string} githubToken - GitHub ํ ํฐ
*/
async function postReviewComment(
repoOwner,
repoName,
prNumber,
reviewContent,
githubToken
) {
await fetch(
`https://api.github.com/repos/${repoOwner}/${repoName}/issues/${prNumber}/comments`,
{
method: "POST",
headers: getGitHubHeaders(githubToken),
body: JSON.stringify({ body: reviewContent }),
}
);
}
/**
* ์ค๋ ๋ ๋ต๋ณ์ผ๋ก AI ์ฝ๋ ๋ฆฌ๋ทฐ ์์ฑ
*
* @param {string} repoOwner - ์ ์ฅ์ ์์ ์
* @param {string} repoName - ์ ์ฅ์ ์ด๋ฆ
* @param {number} prNumber - PR ๋ฒํธ
* @param {number} commentId - ์๋ณธ ๋๊ธ ID
* @param {string} reviewContent - ๋ฆฌ๋ทฐ ๋ด์ฉ (๋งํฌ๋ค์ด)
* @param {string} githubToken - GitHub ํ ํฐ
*/
async function postThreadReply(
repoOwner,
repoName,
prNumber,
commentId,
reviewContent,
githubToken
) {
await fetch(
`https://api.github.com/repos/${repoOwner}/${repoName}/pulls/${prNumber}/comments/${commentId}/replies`,
{
method: "POST",
headers: getGitHubHeaders(githubToken),
body: JSON.stringify({ body: reviewContent }),
}
);
}
/**
* ๋๊ธ์ reaction ์ถ๊ฐ
*
* @param {string} repoOwner - ์ ์ฅ์ ์์ ์
* @param {string} repoName - ์ ์ฅ์ ์ด๋ฆ
* @param {number} commentId - ๋๊ธ ID
* @param {string} commentType - ๋๊ธ ํ์
('issue' ๋๋ 'pull')
* @param {string} reaction - reaction ํ์
(์: 'eyes')
* @param {string} githubToken - GitHub ํ ํฐ
*/
export async function addReactionToComment(
repoOwner,
repoName,
commentId,
commentType,
reaction,
githubToken
) {
const endpoint = commentType === "issue"
? `https://api.github.com/repos/${repoOwner}/${repoName}/issues/comments/${commentId}/reactions`
: `https://api.github.com/repos/${repoOwner}/${repoName}/pulls/comments/${commentId}/reactions`;
await fetch(endpoint, {
method: "POST",
headers: getGitHubHeaders(githubToken),
body: JSON.stringify({ content: reaction }),
});
}
/**
* PR์ ๋ํด AI ๋ฆฌ๋ทฐ ์ํ
*
* @param {string} repoOwner - ์ ์ฅ์ ์์ ์
* @param {string} repoName - ์ ์ฅ์ ์ด๋ฆ
* @param {number} prNumber - PR ๋ฒํธ
* @param {string} prTitle - PR ์ ๋ชฉ
* @param {string} prBody - PR ๋ณธ๋ฌธ
* @param {string} githubToken - GitHub ํ ํฐ
* @param {string} openaiApiKey - OpenAI API ํค
* @param {string} userRequest - ์ฌ์ฉ์์ ๊ตฌ์ฒด์ ์ธ ์์ฒญ (์ ํ์ฌํญ)
* @param {number} replyToCommentId - ์ค๋ ๋ ๋ต๋ณ์ผ๋ก ์์ฑํ ๋๊ธ ID (์ ํ์ฌํญ)
*/
export async function performAIReview(
repoOwner,
repoName,
prNumber,
prTitle,
prBody,
githubToken,
openaiApiKey,
userRequest = null,
replyToCommentId = null
) {
console.log(`Starting AI review for PR #${prNumber}${userRequest ? ` - Request: ${userRequest}` : ""}`);
// PR diff ๊ฐ์ ธ์ค๊ธฐ
const prDiff = await getPRDiff(repoOwner, repoName, prNumber, githubToken);
// diff๊ฐ ๋๋ฌด ํฌ๋ฉด ์คํต (ํ ํฐ ๋น์ฉ ์ ์ฝ)
const diffLines = prDiff.split("\n").length;
if (diffLines > 1000) {
console.log(`Skipping AI review: diff too large (${diffLines} lines)`);
return;
}
// AI ๋ฆฌ๋ทฐ ์์ฑ (userRequest ์ ๋ฌ)
const reviewContent = await generateCodeReview(
prDiff,
prTitle,
prBody,
openaiApiKey,
userRequest
);
// ๋ฆฌ๋ทฐ ๋๊ธ ์์ฑ (์ค๋ ๋ ๋ต๋ณ ๋๋ ์ผ๋ฐ ๋๊ธ)
if (replyToCommentId) {
await postThreadReply(
repoOwner,
repoName,
prNumber,
replyToCommentId,
reviewContent,
githubToken
);
console.log(`AI review posted as thread reply for PR #${prNumber}`);
} else {
await postReviewComment(
repoOwner,
repoName,
prNumber,
reviewContent,
githubToken
);
console.log(`AI review posted for PR #${prNumber}`);
}
}