-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearningData.js
More file actions
360 lines (312 loc) ยท 8.61 KB
/
learningData.js
File metadata and controls
360 lines (312 loc) ยท 8.61 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* Learning data fetching utilities for DaleStudy GitHub App
*/
import { getGitHubHeaders } from "./github.js";
const GITHUB_GRAPHQL_URL = "https://api.github.com/graphql";
const COHORT_PROJECT_PATTERN = /๋ฆฌํธ์ฝ๋ ์คํฐ๋\s*\d+๊ธฐ/;
/**
* GitHub GraphQL API ํธ์ถ ํฌํผ
*
* @param {string} query
* @param {string} appToken
* @returns {Promise<object>}
*/
async function graphql(query, appToken) {
const response = await fetch(GITHUB_GRAPHQL_URL, {
method: "POST",
headers: {
...getGitHubHeaders(appToken),
"Content-Type": "application/json",
},
body: JSON.stringify({ query }),
});
if (!response.ok) {
throw new Error(`GraphQL request failed: ${response.status} ${response.statusText}`);
}
const result = await response.json();
if (result.errors) {
throw new Error(`GraphQL error: ${JSON.stringify(result.errors)}`);
}
return result.data;
}
/**
* ํ์ฌ ์งํ ์ค์ธ ๊ธฐ์ ํ๋ก์ ํธ ID๋ฅผ ์กฐํํ๋ค.
* "๋ฆฌํธ์ฝ๋ ์คํฐ๋X๊ธฐ" ํจํด์ ์ด๋ฆฐ ํ๋ก์ ํธ๋ฅผ ์ฐพ๋๋ค.
*
* @param {string} repoOwner
* @param {string} repoName
* @param {string} appToken
* @returns {Promise<string|null>} ํ๋ก์ ํธ node ID, ์์ผ๋ฉด null
*/
async function fetchActiveCohortProjectId(repoOwner, repoName, appToken) {
const data = await graphql(
`{
repository(owner: "${repoOwner}", name: "${repoName}") {
projectsV2(first: 20) {
nodes {
id
title
closed
}
}
}
}`,
appToken
);
const projects = data.repository.projectsV2.nodes;
const active = projects.find(
(p) => !p.closed && COHORT_PROJECT_PATTERN.test(p.title)
);
if (!active) {
console.warn(
`[fetchActiveCohortProjectId] No open cohort project found for ${repoOwner}/${repoName}`
);
return null;
}
console.log(
`[fetchActiveCohortProjectId] Active cohort project: "${active.title}" (${active.id})`
);
return active.id;
}
/**
* ๊ธฐ์ ํ๋ก์ ํธ์์ ํด๋น ์ ์ ๊ฐ ๋จธ์งํ PR ๋ฒํธ ๋ชฉ๋ก์ ๋ฐํํ๋ค.
* ํ๋ก์ ํธ ์์ดํ
์ ํ์ด์ง๋ค์ด์
ํ๋ฉฐ author.login์ผ๋ก ํํฐ๋งํ๋ค.
*
* @param {string} projectId
* @param {string} username
* @param {string} appToken
* @returns {Promise<number[]>}
*/
async function fetchUserMergedPRsInProject(projectId, username, appToken) {
const prNumbers = [];
let cursor = null;
while (true) {
const afterClause = cursor ? `, after: "${cursor}"` : "";
const data = await graphql(
`{
node(id: "${projectId}") {
... on ProjectV2 {
items(first: 100${afterClause}) {
pageInfo { hasNextPage endCursor }
nodes {
content {
... on PullRequest {
number
state
author { login }
}
}
}
}
}
}
}`,
appToken
);
const { nodes, pageInfo } = data.node.items;
for (const item of nodes) {
const pr = item.content;
if (
pr?.state === "MERGED" &&
pr?.author?.login?.toLowerCase() === username.toLowerCase()
) {
prNumbers.push(pr.number);
}
}
if (!pageInfo.hasNextPage) break;
cursor = pageInfo.endCursor;
}
return prNumbers;
}
/**
* ํ์ฌ ๊ธฐ์ ํ๋ก์ ํธ์์ ํด๋น ์ ์ ๊ฐ ์ ์ถํ ๋ฌธ์ ๋ชฉ๋ก์ ๋ฐํํ๋ค.
*
* ๊ธฐ์ ํ๋ก์ ํธ๋ฅผ ์ฐพ์ง ๋ชปํ๋ฉด ์ ์ฒด ๋ ํฌ ํธ๋ฆฌ ์ค์บ(fetchUserSolutions)์ผ๋ก ํด๋ฐฑํ๋ค.
*
* @param {string} repoOwner
* @param {string} repoName
* @param {string} username
* @param {string} appToken
* @returns {Promise<string[]>}
*/
export async function fetchCohortUserSolutions(
repoOwner,
repoName,
username,
appToken
) {
const projectId = await fetchActiveCohortProjectId(
repoOwner,
repoName,
appToken
);
if (!projectId) {
console.warn(
`[fetchCohortUserSolutions] Falling back to full tree scan for ${username}`
);
return fetchUserSolutions(repoOwner, repoName, username, appToken);
}
const prNumbers = await fetchUserMergedPRsInProject(
projectId,
username,
appToken
);
console.log(
`[fetchCohortUserSolutions] ${username} has ${prNumbers.length} merged PRs in current cohort`
);
const problemNames = new Set();
for (const prNumber of prNumbers) {
const submissions = await fetchPRSubmissions(
repoOwner,
repoName,
prNumber,
username,
appToken
);
for (const { problemName } of submissions) {
problemNames.add(problemName);
}
}
return Array.from(problemNames);
}
/**
* Fetches problem-categories.json from the repo root via GitHub API.
* Returns parsed JSON object, or null if the file is not found (404).
* Throws on other errors.
*
* @param {string} repoOwner
* @param {string} repoName
* @param {string} appToken
* @returns {Promise<object|null>}
*/
export async function fetchProblemCategories(repoOwner, repoName, appToken) {
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/problem-categories.json`;
const response = await fetch(url, {
headers: {
...getGitHubHeaders(appToken),
Accept: "application/vnd.github.raw+json",
},
});
if (response.status === 404) {
return null;
}
if (!response.ok) {
throw new Error(
`Failed to fetch problem-categories.json: ${response.status} ${response.statusText}`
);
}
return await response.json();
}
/**
* Fetches the full repo file tree and returns a deduplicated array of problem
* names that have a solution file submitted by the given username.
*
* Matches files of the form: {problem-name}/{username}.{ext}
*
* @param {string} repoOwner
* @param {string} repoName
* @param {string} username
* @param {string} appToken
* @returns {Promise<string[]>}
*/
export async function fetchUserSolutions(
repoOwner,
repoName,
username,
appToken
) {
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/git/trees/main?recursive=1`;
const response = await fetch(url, {
headers: getGitHubHeaders(appToken),
});
if (!response.ok) {
throw new Error(
`Failed to fetch repo tree: ${response.status} ${response.statusText}`
);
}
const data = await response.json();
if (data.truncated) {
console.warn(
`[fetchUserSolutions] Tree response truncated for ${repoOwner}/${repoName}. Results may be incomplete.`
);
}
// Pattern: {problem-name}/{username}.{ext}
// The path must have exactly two segments and the filename must be username.ext
const usernamePattern = new RegExp(
`^([^/]+)/${escapeRegExp(username)}\\.[^/]+$`
);
const problemNames = new Set();
for (const item of data.tree) {
if (item.type !== "blob") continue;
const match = item.path.match(usernamePattern);
if (match) {
problemNames.add(match[1]);
}
}
return Array.from(problemNames);
}
/**
* Fetches the files changed in a PR and returns those that match
* {problem-name}/{username}.{ext} and are added, modified, or renamed.
*
* @param {string} repoOwner
* @param {string} repoName
* @param {number} prNumber
* @param {string} username
* @param {string} appToken
* @returns {Promise<Array<{ problemName: string, filename: string, rawUrl: string }>>}
*/
export async function fetchPRSubmissions(
repoOwner,
repoName,
prNumber,
username,
appToken
) {
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/pulls/${prNumber}/files?per_page=100`;
const response = await fetch(url, {
headers: getGitHubHeaders(appToken),
});
if (!response.ok) {
throw new Error(
`Failed to fetch PR files: ${response.status} ${response.statusText}`
);
}
const files = await response.json();
if (files.length === 100) {
console.warn(
`[fetchPRSubmissions] PR #${prNumber} has 100+ files. Some submissions may be missed.`
);
}
// Pattern: {problem-name}/{username}.{ext}
const usernamePattern = new RegExp(
`^([^/]+)/${escapeRegExp(username)}\\.[^/]+$`
);
const results = [];
for (const file of files) {
if (
file.status !== "added" &&
file.status !== "modified" &&
file.status !== "renamed"
)
continue;
const match = file.filename.match(usernamePattern);
if (match) {
results.push({
problemName: match[1],
filename: file.filename,
rawUrl: file.raw_url,
});
}
}
return results;
}
/**
* Escapes special regex characters in a string.
*
* @param {string} str
* @returns {string}
*/
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}