forked from joshcai/leetcode-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
450 lines (409 loc) · 12 KB
/
action.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
const axios = require("axios");
const { Octokit } = require("@octokit/rest");
const path = require("path");
const COMMIT_MESSAGE = "Sync LeetCode submission";
const LANG_TO_EXTENSION = {
bash: "sh",
c: "c",
cpp: "cpp",
csharp: "cs",
dart: "dart",
elixir: "ex",
erlang: "erl",
golang: "go",
java: "java",
javascript: "js",
kotlin: "kt",
mssql: "sql",
mysql: "sql",
oraclesql: "sql",
php: "php",
python: "py",
python3: "py",
pythondata: "py",
postgresql: "sql",
racket: "rkt",
ruby: "rb",
rust: "rs",
scala: "scala",
swift: "swift",
typescript: "ts",
};
const BASE_URL = "https://leetcode.com";
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
function log(message) {
console.log(`[${new Date().toUTCString()}] ${message}`);
}
function pad(n) {
if (n.length > 4) {
return n;
}
var s = "000" + n;
return s.substring(s.length - 4);
}
function normalizeName(problemName) {
return problemName
.toLowerCase()
.replace(/\s/g, "-")
.replace(/[^a-zA-Z0-9_-]/gi, "");
}
function graphqlHeaders(session, csrfToken) {
return {
"content-type": "application/json",
origin: BASE_URL,
referer: BASE_URL,
cookie: `csrftoken=${csrfToken}; LEETCODE_SESSION=${session};`,
"x-csrftoken": csrfToken,
};
}
async function getInfo(submission, session, csrfToken) {
let data = JSON.stringify({
query: `query submissionDetails($submissionId: Int!) {
submissionDetails(submissionId: $submissionId) {
runtimePercentile
memoryPercentile
code
question {
questionId
}
}
}`,
variables: { submissionId: submission.id },
});
const headers = graphqlHeaders(session, csrfToken);
// No need to break on first request error since that would be done when getting submissions
const getInfo = async (maxRetries = 5, retryCount = 0) => {
try {
const response = await axios.post("https://leetcode.com/graphql/", data, {
headers,
});
const submissionDetails = response.data?.data?.submissionDetails;
const runtimePercentile =
submissionDetails.runtimePercentile !== null &&
submissionDetails.runtimePercentile !== undefined
? `${submissionDetails.runtimePercentile.toFixed(2)}%`
: "N/A";
const memoryPercentile =
submissionDetails.memoryPercentile !== null &&
submissionDetails.memoryPercentile !== undefined
? `${submissionDetails.memoryPercentile.toFixed(2)}%`
: "N/A";
const questionId = submissionDetails?.question?.questionId
? pad(submissionDetails.question.questionId.toString())
: "N/A";
log(`Got info for submission #${submission.id}`);
return {
runtimePerc: runtimePercentile,
memoryPerc: memoryPercentile,
qid: questionId,
code: response.data.data.submissionDetails.code,
};
} catch (exception) {
if (retryCount >= maxRetries) {
throw exception;
}
log(
"Error fetching submission info, retrying in " +
3 ** retryCount +
" seconds...",
);
await delay(3 ** retryCount * 1000);
return getInfo(maxRetries, retryCount + 1);
}
};
info = await getInfo();
return { ...submission, ...info };
}
async function commit(params) {
const {
octokit,
owner,
repo,
defaultBranch,
commitInfo,
treeSHA,
latestCommitSHA,
submission,
destinationFolder,
commitHeader,
questionData,
} = params;
const name = normalizeName(submission.title);
log(`Committing solution for ${name}...`);
if (!LANG_TO_EXTENSION[submission.lang]) {
throw `Language ${submission.lang} does not have a registered extension.`;
}
const prefix = !!destinationFolder ? destinationFolder : "";
const commitName = !!commitHeader ? commitHeader : COMMIT_MESSAGE;
if ("runtimePerc" in submission) {
message = `${commitName} Runtime - ${submission.runtime} (${submission.runtimePerc}), Memory - ${submission.memory} (${submission.memoryPerc})`;
qid = `${submission.qid}-`;
} else {
message = `${commitName} Runtime - ${submission.runtime}, Memory - ${submission.memory}`;
qid = "";
}
const folderName = `${qid}${name}`;
// Markdown file for the problem with question data
const questionPath = path.join(prefix, folderName, "README.md");
// Separate file for the solution
const solutionFileName = `solution.${LANG_TO_EXTENSION[submission.lang]}`;
const solutionPath = path.join(prefix, folderName, solutionFileName);
const treeData = [
{
path: path.normalize(questionPath),
mode: "100644",
content: questionData ?? "Unable to fetch the Problem statement.",
},
{
path: path.normalize(solutionPath),
mode: "100644",
content: `${submission.code}\n`, // Adds newline at EOF to conform to git recommendations
},
];
const treeResponse = await octokit.git.createTree({
owner: owner,
repo: repo,
base_tree: treeSHA,
tree: treeData,
});
const date = new Date(Number(submission.timestamp) * 1000).toISOString();
const commitResponse = await octokit.git.createCommit({
owner: owner,
repo: repo,
message: message,
tree: treeResponse.data.sha,
parents: [latestCommitSHA],
author: {
email: commitInfo.email,
name: commitInfo.name,
date: date,
},
committer: {
email: commitInfo.email,
name: commitInfo.name,
date: date,
},
});
await octokit.git.updateRef({
owner: owner,
repo: repo,
sha: commitResponse.data.sha,
ref: "heads/" + defaultBranch,
force: true,
});
log(`Committed solution for ${name}`);
return [treeResponse.data.sha, commitResponse.data.sha];
}
async function getQuestionData(titleSlug, leetcodeSession, csrfToken) {
log(`Getting question data for ${titleSlug}...`);
const headers = graphqlHeaders(leetcodeSession, csrfToken);
const graphql = JSON.stringify({
query: `query getQuestionDetail($titleSlug: String!) {
question(titleSlug: $titleSlug) {
content
}
}`,
variables: { titleSlug: titleSlug },
});
try {
const response = await axios.post(
"https://leetcode.com/graphql/",
graphql,
{ headers },
);
const result = await response.data;
return result.data.question.content;
} catch (error) {
console.log("error", error);
}
}
// Returns false if no more submissions should be added.
function addToSubmissions(params) {
const {
response,
lastTimestamp,
filterDuplicateSecs,
submissions_dict,
submissions,
} = params;
for (const submission of response.data.data.submissionList.submissions) {
submissionTimestamp = Number(submission.timestamp);
if (submissionTimestamp <= lastTimestamp) {
return false;
}
if (submission.statusDisplay !== "Accepted") {
continue;
}
const name = normalizeName(submission.title);
const lang = submission.lang;
if (!submissions_dict[name]) {
submissions_dict[name] = {};
}
// Filter out other accepted solutions less than one day from the most recent one.
if (
submissions_dict[name][lang] &&
submissions_dict[name][lang] - submissionTimestamp < filterDuplicateSecs
) {
continue;
}
submissions_dict[name][lang] = submissionTimestamp;
submissions.push(submission);
}
return true;
}
async function sync(inputs) {
const {
githubToken,
owner,
repo,
leetcodeCSRFToken,
leetcodeSession,
filterDuplicateSecs,
destinationFolder,
verbose,
commitHeader,
} = inputs;
const octokit = new Octokit({
auth: githubToken,
userAgent: "LeetCode sync to GitHub - GitHub Action",
});
// First, get the time the timestamp for when the syncer last ran.
const commits = await octokit.repos.listCommits({
owner: owner,
repo: repo,
per_page: 100,
});
let lastTimestamp = 0;
// commitInfo is used to get the original name / email to use for the author / committer.
// Since we need to modify the commit time, we can't use the default settings for the
// authenticated user.
let commitInfo = commits.data[commits.data.length - 1].commit.author;
for (const commit of commits.data) {
if (
!commit.commit.message.startsWith(
!!commitHeader ? commitHeader : COMMIT_MESSAGE,
)
) {
continue;
}
commitInfo = commit.commit.author;
lastTimestamp = Date.parse(commit.commit.committer.date) / 1000;
break;
}
// Get all Accepted submissions from LeetCode greater than the timestamp.
let response = null;
let offset = 0;
const submissions = [];
const submissions_dict = {};
do {
log(`Getting submission from LeetCode, offset ${offset}`);
const getSubmissions = async (maxRetries, retryCount = 0) => {
try {
const slug = undefined;
const graphql = JSON.stringify({
query: `query ($offset: Int!, $limit: Int!, $slug: String) {
submissionList(offset: $offset, limit: $limit, questionSlug: $slug) {
hasNext
submissions {
id
lang
timestamp
statusDisplay
runtime
title
memory
titleSlug
}
}
}`,
variables: {
offset: offset,
limit: 20,
slug,
},
});
const headers = graphqlHeaders(leetcodeSession, leetcodeCSRFToken);
const response = await axios.post(
"https://leetcode.com/graphql/",
graphql,
{ headers },
);
log(`Successfully fetched submission from LeetCode, offset ${offset}`);
return response;
} catch (exception) {
if (retryCount >= maxRetries) {
throw exception;
}
log(
"Error fetching submissions, retrying in " +
3 ** retryCount +
" seconds...",
);
// There's a rate limit on LeetCode API, so wait with backoff before retrying.
await delay(3 ** retryCount * 1000);
return getSubmissions(maxRetries, retryCount + 1);
}
};
// On the first attempt, there should be no rate limiting issues, so we fail immediately in case
// the tokens are configured incorrectly.
const maxRetries = response === null ? 0 : 5;
if (response !== null) {
// Add a 1 second delay before all requests after the initial request.
await delay(1000);
}
response = await getSubmissions(maxRetries);
if (
!addToSubmissions({
response,
lastTimestamp,
filterDuplicateSecs,
submissions_dict,
submissions,
})
) {
break;
}
offset += 20;
} while (response.data.data.submissionList.hasNext);
// We have all submissions we want to write to GitHub now.
// First, get the default branch to write to.
const repoInfo = await octokit.repos.get({
owner: owner,
repo: repo,
});
const defaultBranch = repoInfo.data.default_branch;
log(`Default branch for ${owner}/${repo}: ${defaultBranch}`);
// Write in reverse order (oldest first), so that if there's errors, the last sync time
// is still valid.
log(`Syncing ${submissions.length} submissions...`);
let latestCommitSHA = commits.data[0].sha;
let treeSHA = commits.data[0].commit.tree.sha;
for (i = submissions.length - 1; i >= 0; i--) {
submission = await getInfo(
submissions[i],
leetcodeSession,
leetcodeCSRFToken,
);
// Get the question data for the submission.
const questionData = await getQuestionData(
submission.titleSlug,
leetcodeSession,
leetcodeCSRFToken,
);
[treeSHA, latestCommitSHA] = await commit({
octokit,
owner,
repo,
defaultBranch,
commitInfo,
treeSHA,
latestCommitSHA,
submission,
destinationFolder,
commitHeader,
questionData,
});
}
log("Done syncing all submissions.");
}
module.exports = { log, sync };