Skip to content

Commit b20aa38

Browse files
authored
Merge pull request #62 from AlyHG/master
Updated action.js to Handle Locked LeetCode Premium Problems and Improve Sync Reliability
2 parents 77d3da7 + 1e721e0 commit b20aa38

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit
5555
leetcode-session: ${{ secrets.LEETCODE_SESSION }}
5656
destination-folder: my-folder
5757
verbose: true
58-
commit-header: '[LeetCode Sync]'
58+
commit-header: "[LeetCode Sync]"
5959
```
6060
6161
6. After you've submitted a LeetCode solution, run the workflow by going to the `Actions` tab, clicking the action name, e.g. `Sync Leetcode`, and then clicking `Run workflow`. The workflow will also automatically run once a week by default (can be configured via the `cron` parameter).
@@ -101,6 +101,7 @@ Since this file is in the `.gitignore` file to avoid users accidentally committi
101101
This likely means that you hit a rate limit when committing to GitHub (this may happen if you have over ~300 submissions initially). Since the syncer writes in reverse chronological order, it should pick up syncing submissions from where it left off on the next run of the workflow, so just retry the workflow manually after some time.
102102

103103
#### Job fails with "HttpError: Resource not accessible by integration"
104+
104105
This means the github token you're using does not have permission to write to your repo. If you're using the default `github.token` method follow the instructions [here] (https://docs.github.com/en/actions/security-guides/automatic-token-authentication)
105106

106107
## Acknowledgements

src/action.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ async function getInfo(submission, session, csrfToken) {
113113
};
114114
} catch (exception) {
115115
if (retryCount >= maxRetries) {
116+
// If problem is locked due to user not having LeetCode Premium
117+
if (exception.response && exception.response.status === 403) {
118+
log(`Skipping locked problem: ${submission.title}`);
119+
return null;
120+
}
116121
throw exception;
117122
}
118123
log(
119124
"Error fetching submission info, retrying in " +
120125
3 ** retryCount +
121-
" seconds...",
126+
" seconds..."
122127
);
123128
await delay(3 ** retryCount * 1000);
124129
return getInfo(maxRetries, retryCount + 1);
@@ -238,11 +243,16 @@ async function getQuestionData(titleSlug, leetcodeSession, csrfToken) {
238243
const response = await axios.post(
239244
"https://leetcode.com/graphql/",
240245
graphql,
241-
{ headers },
246+
{ headers }
242247
);
243248
const result = await response.data;
244249
return result.data.question.content;
245250
} catch (error) {
251+
// If problem is locked due to user not having LeetCode Premium
252+
if (error.response && error.response.status === 403) {
253+
log(`Skipping locked problem: ${titleSlug}`);
254+
return null;
255+
}
246256
console.log("error", error);
247257
}
248258
}
@@ -315,7 +325,7 @@ async function sync(inputs) {
315325
for (const commit of commits.data) {
316326
if (
317327
!commit.commit.message.startsWith(
318-
!!commitHeader ? commitHeader : COMMIT_MESSAGE,
328+
!!commitHeader ? commitHeader : COMMIT_MESSAGE
319329
)
320330
) {
321331
continue;
@@ -363,7 +373,7 @@ async function sync(inputs) {
363373
const response = await axios.post(
364374
"https://leetcode.com/graphql/",
365375
graphql,
366-
{ headers },
376+
{ headers }
367377
);
368378
log(`Successfully fetched submission from LeetCode, offset ${offset}`);
369379
return response;
@@ -374,7 +384,7 @@ async function sync(inputs) {
374384
log(
375385
"Error fetching submissions, retrying in " +
376386
3 ** retryCount +
377-
" seconds...",
387+
" seconds..."
378388
);
379389
// There's a rate limit on LeetCode API, so wait with backoff before retrying.
380390
await delay(3 ** retryCount * 1000);
@@ -421,15 +431,24 @@ async function sync(inputs) {
421431
submission = await getInfo(
422432
submissions[i],
423433
leetcodeSession,
424-
leetcodeCSRFToken,
434+
leetcodeCSRFToken
425435
);
426436

437+
if (submission === null) {
438+
// Skip this submission if it is null (locked problem)
439+
continue;
440+
}
441+
427442
// Get the question data for the submission.
428443
const questionData = await getQuestionData(
429444
submission.titleSlug,
430445
leetcodeSession,
431-
leetcodeCSRFToken,
446+
leetcodeCSRFToken
432447
);
448+
if (questionData === null) {
449+
// Skip this submission if question data is null (locked problem)
450+
continue;
451+
}
433452
[treeSHA, latestCommitSHA] = await commit({
434453
octokit,
435454
owner,

0 commit comments

Comments
 (0)