Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2016e49

Browse files
authored
Merge pull request #10 from CherryKatatonic/destination-folder
feat(Input)[]: Add `destination-folder` input param to allow for shared repos
2 parents c4855c7 + 6a29279 commit 2016e49

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit
5050
github-token: ${{ github.token }}
5151
leetcode-csrf-token: ${{ secrets.LEETCODE_CSRF_TOKEN }}
5252
leetcode-session: ${{ secrets.LEETCODE_SESSION }}
53+
destination-folder: my-folder
5354
```
5455
5556
5. 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).
@@ -60,6 +61,11 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit
6061
- `leetcode-csrf-token` *(required)*: The LeetCode CSRF token for retrieving submissions from LeetCode
6162
- `leetcode-session` *(required)*: The LeetCode session value for retrieving submissions from LeetCode
6263
- `filter-duplicate-secs`: Number of seconds after an accepted solution to ignore other accepted solutions for the same problem, default: 86400 (1 day)
64+
- `destination-folder` *(optional)*: The folder in your repo to save the submissions to (necessary for shared repos)
65+
66+
## Shared Repos
67+
68+
A single repo can be shared by multiple users by using the `destination-folder` input field to sync each user's files to a separate folder. This is useful for users who want to add a more social, collaborative, or competitive aspect to their LeetCode sync repo.
6369

6470
## FAQ
6571

‎action.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
description: 'Number of seconds after an accepted solution to ignore other accepted solutions for the same problem'
1818
required: false
1919
default: 86400
20+
destination-folder:
21+
description: 'The folder to save the synced files in (relative to the top level of your repo)'
22+
required: false
2023
runs:
2124
using: 'node12'
2225
main: 'index.js'

‎index.js‎

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,37 @@ function normalizeName(problemName) {
3636
return problemName.toLowerCase().replace(/\s/g, '_');
3737
}
3838

39-
async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) {
39+
async function commit(params) {
40+
const {
41+
octokit,
42+
owner,
43+
repo,
44+
defaultBranch,
45+
commitInfo,
46+
treeSHA,
47+
latestCommitSHA,
48+
submission,
49+
destinationFolder
50+
} = params;
51+
4052
const name = normalizeName(submission.title);
4153
log(`Committing solution for ${name}...`);
4254

4355
if (!LANG_TO_EXTENSION[submission.lang]) {
4456
throw `Language ${submission.lang} does not have a registered extension.`;
4557
}
4658

59+
const prefix = !!destinationFolder ? `${destinationFolder}/` : '';
60+
const path = `${prefix}problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`
61+
4762
const treeData = [
4863
{
49-
path: `problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`,
64+
path,
5065
mode: '100644',
5166
content: submission.code,
5267
}
5368
];
54-
69+
5570
const treeResponse = await octokit.git.createTree({
5671
owner: owner,
5772
repo: repo,
@@ -92,30 +107,48 @@ async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA,
92107
}
93108

94109
// Returns false if no more submissions should be added.
95-
function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions) {
96-
for (const submission of response.data.submissions_dump) {
97-
if (submission.timestamp <= lastTimestamp) {
98-
return false;
99-
}
100-
if (submission.status_display !== 'Accepted') {
101-
continue;
102-
}
103-
const name = normalizeName(submission.title);
104-
const lang = submission.lang;
105-
if (!submissions_dict[name]) {
106-
submissions_dict[name] = {};
107-
}
108-
// Filter out other accepted solutions less than one day from the most recent one.
109-
if (submissions_dict[name][lang] && submissions_dict[name][lang] - submission.timestamp < filterDuplicateSecs) {
110-
continue;
111-
}
112-
submissions_dict[name][lang] = submission.timestamp;
113-
submissions.push(submission);
110+
function addToSubmissions(params) {
111+
const {
112+
response,
113+
lastTimestamp,
114+
filterDuplicateSecs,
115+
submissions_dict,
116+
submissions
117+
} = params;
118+
119+
for (const submission of response.data.submissions_dump) {
120+
if (submission.timestamp <= lastTimestamp) {
121+
return false;
122+
}
123+
if (submission.status_display !== 'Accepted') {
124+
continue;
125+
}
126+
const name = normalizeName(submission.title);
127+
const lang = submission.lang;
128+
if (!submissions_dict[name]) {
129+
submissions_dict[name] = {};
114130
}
131+
// Filter out other accepted solutions less than one day from the most recent one.
132+
if (submissions_dict[name][lang] && submissions_dict[name][lang] - submission.timestamp < filterDuplicateSecs) {
133+
continue;
134+
}
135+
submissions_dict[name][lang] = submission.timestamp;
136+
submissions.push(submission);
137+
}
115138
return true;
116139
}
117140

118-
async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession) {
141+
async function sync(inputs) {
142+
const {
143+
githubToken,
144+
owner,
145+
repo,
146+
filterDuplicateSecs,
147+
leetcodeCSRFToken,
148+
leetcodeSession,
149+
destinationFolder
150+
} = inputs;
151+
119152
const octokit = new Octokit({
120153
auth: githubToken,
121154
userAgent: 'LeetCode sync to GitHub - GitHub Action',
@@ -131,7 +164,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
131164
// commitInfo is used to get the original name / email to use for the author / committer.
132165
// Since we need to modify the commit time, we can't use the default settings for the
133166
// authenticated user.
134-
let commitInfo = commits.data[commits.data.length-1].commit.author;
167+
let commitInfo = commits.data[commits.data.length-1].commit.author;
135168
for (const commit of commits.data) {
136169
if (!commit.commit.message.startsWith(COMMIT_MESSAGE)) {
137170
continue
@@ -156,7 +189,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
156189
headers: {
157190
'X-Requested-With': 'XMLHttpRequest',
158191
'X-CSRFToken': leetcodeCSRFToken,
159-
'Cookie': `csrftoken=${leetcodeCSRFToken};LEETCODE_SESSION=${leetcodeSession};`,
192+
'Cookie': `csrftoken=${leetcodeCSRFToken};LEETCODE_SESSION=${leetcodeSession};`,
160193
},
161194
};
162195
log(`Getting submission from LeetCode, offset ${offset}`);
@@ -183,7 +216,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
183216
await delay(1000);
184217
}
185218
response = await getSubmissions(maxRetries);
186-
if (!addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions)) {
219+
if (!addToSubmissions({response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions})) {
187220
break;
188221
}
189222

@@ -205,7 +238,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
205238
let treeSHA = commits.data[0].commit.tree.sha;
206239
for (i = submissions.length - 1; i >= 0; i--) {
207240
submission = submissions[i];
208-
[treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission);
241+
[treeSHA, latestCommitSHA] = await commit({octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder });
209242
}
210243
log('Done syncing all submissions.');
211244
}
@@ -217,8 +250,9 @@ async function main() {
217250
const leetcodeCSRFToken = core.getInput('leetcode-csrf-token');
218251
const leetcodeSession = core.getInput('leetcode-session');
219252
const filterDuplicateSecs = core.getInput('filter-duplicate-secs');
253+
const destinationFolder = core.getInput('destination-folder');
220254

221-
await sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession);
255+
await sync({githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession, destinationFolder });
222256
}
223257

224258
main().catch((error) => {

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
"@octokit/rest": "^18.0.0",
2424
"axios": "^0.19.2"
2525
}
26-
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /