From 549a44edfb0b1ef69f97dcee70d75011e9a6f976 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:13:46 -0500 Subject: [PATCH 01/16] Create a folder in repo for each user --- index.js | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index a3a612f5..20c1f62c 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ function normalizeName(problemName) { return problemName.toLowerCase().replace(/\s/g, '_'); } -async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) { +async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, user) { const name = normalizeName(submission.title); log(`Committing solution for ${name}...`); @@ -46,12 +46,12 @@ async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, const treeData = [ { - path: `problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`, + path: `${user}/problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`, mode: '100644', content: submission.code, } ]; - + const treeResponse = await octokit.git.createTree({ owner: owner, repo: repo, @@ -93,25 +93,25 @@ async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, // Returns false if no more submissions should be added. function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions) { - for (const submission of response.data.submissions_dump) { - if (submission.timestamp <= lastTimestamp) { - return false; - } - if (submission.status_display !== '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] - submission.timestamp < filterDuplicateSecs) { - continue; - } - submissions_dict[name][lang] = submission.timestamp; - submissions.push(submission); + for (const submission of response.data.submissions_dump) { + if (submission.timestamp <= lastTimestamp) { + return false; } + if (submission.status_display !== '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] - submission.timestamp < filterDuplicateSecs) { + continue; + } + submissions_dict[name][lang] = submission.timestamp; + submissions.push(submission); + } return true; } @@ -131,7 +131,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT // 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; + let commitInfo = commits.data[commits.data.length - 1].commit.author; for (const commit of commits.data) { if (!commit.commit.message.startsWith(COMMIT_MESSAGE)) { continue @@ -156,7 +156,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT headers: { 'X-Requested-With': 'XMLHttpRequest', 'X-CSRFToken': leetcodeCSRFToken, - 'Cookie': `csrftoken=${leetcodeCSRFToken};LEETCODE_SESSION=${leetcodeSession};`, + 'Cookie': `csrftoken=${leetcodeCSRFToken};LEETCODE_SESSION=${leetcodeSession};`, }, }; log(`Getting submission from LeetCode, offset ${offset}`); @@ -211,6 +211,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT } async function main() { + const user = core.getInput('user'); const githubToken = core.getInput('github-token'); const owner = context.repo.owner; const repo = context.repo.repo; From 5b318b9e3e82dc89715f876fa8a599be081a4afb Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:21:52 -0500 Subject: [PATCH 02/16] Add "user" input --- action.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/action.yml b/action.yml index a7de30e3..3fb4b1d0 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,9 @@ branding: icon: git-commit color: yellow inputs: + user: + description: 'The user to sync' + required: false github-token: description: 'The GitHub token' required: true From 0b0548a33bae6fbc4bfce92a3651b2933bf50c4b Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:25:54 -0500 Subject: [PATCH 03/16] Add user variable to sync function params --- README.md | 1 + index.js | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f565caab..19110703 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit ## Inputs +- `user` *(optional)*: The user to sync (for shared repos) - `github-token` *(required)*: The GitHub access token for pushing solutions to the repository - `leetcode-csrf-token` *(required)*: The LeetCode CSRF token for retrieving submissions from LeetCode - `leetcode-session` *(required)*: The LeetCode session value for retrieving submissions from LeetCode diff --git a/index.js b/index.js index 20c1f62c..76d26e6c 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ function normalizeName(problemName) { return problemName.toLowerCase().replace(/\s/g, '_'); } -async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, user) { +async function commit(octokit, user, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) { const name = normalizeName(submission.title); log(`Committing solution for ${name}...`); @@ -115,7 +115,7 @@ function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissi return true; } -async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession) { +async function sync(user, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession) { const octokit = new Octokit({ auth: githubToken, userAgent: 'LeetCode sync to GitHub - GitHub Action', @@ -205,7 +205,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT let treeSHA = commits.data[0].commit.tree.sha; for (i = submissions.length - 1; i >= 0; i--) { submission = submissions[i]; - [treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission); + [treeSHA, latestCommitSHA] = await commit(octokit, user, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission); } log('Done syncing all submissions.'); } @@ -219,7 +219,7 @@ async function main() { const leetcodeSession = core.getInput('leetcode-session'); const filterDuplicateSecs = core.getInput('filter-duplicate-secs'); - await sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession); + await sync(user, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession); } main().catch((error) => { From 3cac1c66b9ee7bdddddfe93526d7fff5d4652540 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:34:02 -0500 Subject: [PATCH 04/16] Update README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 19110703..6954c262 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,9 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit steps: - name: Sync - uses: joshcai/leetcode-sync@v1.3 + uses: CherryKatatonic/leetcode-sync@v1.3 with: + user: name github-token: ${{ github.token }} leetcode-csrf-token: ${{ secrets.LEETCODE_CSRF_TOKEN }} leetcode-session: ${{ secrets.LEETCODE_SESSION }} From cf9b865375f7c7671a1c3b88208f61150519e623 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:47:57 -0500 Subject: [PATCH 05/16] Add .npmrc & update package.json & action.yml --- .npmrc | 1 + action.yml | 2 +- package.json | 12 ++++++------ 3 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..62fa300a --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +@CherryKatatonic:registry=https://npm.pkg.github.com \ No newline at end of file diff --git a/action.yml b/action.yml index 3fb4b1d0..bbd5d6e2 100644 --- a/action.yml +++ b/action.yml @@ -1,4 +1,4 @@ -name: 'LeetCode Sync' +name: 'CherryKatatonic LeetCode Sync' description: 'Sync LeetCode submissions to GitHub' branding: icon: git-commit diff --git a/package.json b/package.json index 5864813b..96ee0a80 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,26 @@ { - "name": "leetcode-sync", + "name": "cherrykatatonic-leetcode-sync", "version": "1.0.0", - "description": "GitHub Action for syncing LeetCode submissions to a git repository", + "description": "Forked GitHub Action for syncing LeetCode submissions to a git repository", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "git+https://github.com/joshcai/leetcode-sync.git" + "url": "git+https://github.com/cherrykatatonic/leetcode-sync.git" }, "keywords": [], "author": "", "license": "ISC", "bugs": { - "url": "https://github.com/joshcai/leetcode-sync/issues" + "url": "https://github.com/cherrykatatonic/leetcode-sync/issues" }, - "homepage": "https://github.com/joshcai/leetcode-sync#readme", + "homepage": "https://github.com/cherrykatatonic/leetcode-sync#readme", "dependencies": { "@actions/core": "^1.2.6", "@actions/github": "^4.0.0", "@octokit/rest": "^18.0.0", "axios": "^0.19.2" } -} +} \ No newline at end of file From 7c0e7d000ef25aa042f1ef13d523e56edd779d3f Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:50:14 -0500 Subject: [PATCH 06/16] Remove .npmrc (not needed) --- .npmrc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .npmrc diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 62fa300a..00000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -@CherryKatatonic:registry=https://npm.pkg.github.com \ No newline at end of file From 25d6ffdf89e8485e6feac3870d4763fcbf1d9f20 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 14:51:40 -0500 Subject: [PATCH 07/16] Update version --- README.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6954c262..4c16cfdf 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit steps: - name: Sync - uses: CherryKatatonic/leetcode-sync@v1.3 + uses: CherryKatatonic/leetcode-sync@v1.4 with: user: name github-token: ${{ github.token }} diff --git a/package.json b/package.json index 96ee0a80..490f02b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cherrykatatonic-leetcode-sync", - "version": "1.0.0", + "version": "1.4.0", "description": "Forked GitHub Action for syncing LeetCode submissions to a git repository", "main": "index.js", "scripts": { From 7a99b11a82a626f1d709c6f6c67e787179850f42 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:10:08 -0500 Subject: [PATCH 08/16] Change "user" input field to "destination-folder" --- README.md | 4 ++-- action.yml | 4 ++-- index.js | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4c16cfdf..cfedca78 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit - name: Sync uses: CherryKatatonic/leetcode-sync@v1.4 with: - user: name + destination-folder: my-folder github-token: ${{ github.token }} leetcode-csrf-token: ${{ secrets.LEETCODE_CSRF_TOKEN }} leetcode-session: ${{ secrets.LEETCODE_SESSION }} @@ -57,7 +57,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit ## Inputs -- `user` *(optional)*: The user to sync (for shared repos) +- `destination-folder` *(optional)*: The folder in your repo to save the submissions to (necessary for shared repos) - `github-token` *(required)*: The GitHub access token for pushing solutions to the repository - `leetcode-csrf-token` *(required)*: The LeetCode CSRF token for retrieving submissions from LeetCode - `leetcode-session` *(required)*: The LeetCode session value for retrieving submissions from LeetCode diff --git a/action.yml b/action.yml index bbd5d6e2..9c3373a8 100644 --- a/action.yml +++ b/action.yml @@ -4,8 +4,8 @@ branding: icon: git-commit color: yellow inputs: - user: - description: 'The user to sync' + destination-folder: + description: 'The folder to save the synced files in (relative to the top level of your repo)' required: false github-token: description: 'The GitHub token' diff --git a/index.js b/index.js index 76d26e6c..d27f482b 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ function normalizeName(problemName) { return problemName.toLowerCase().replace(/\s/g, '_'); } -async function commit(octokit, user, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) { +async function commit(octokit, destinationFolder, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) { const name = normalizeName(submission.title); log(`Committing solution for ${name}...`); @@ -46,7 +46,7 @@ async function commit(octokit, user, owner, repo, defaultBranch, commitInfo, tre const treeData = [ { - path: `${user}/problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`, + path: `${destinationFolder}/problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`, mode: '100644', content: submission.code, } @@ -115,7 +115,7 @@ function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissi return true; } -async function sync(user, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession) { +async function sync(destinationFolder, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession) { const octokit = new Octokit({ auth: githubToken, userAgent: 'LeetCode sync to GitHub - GitHub Action', @@ -205,13 +205,13 @@ async function sync(user, githubToken, owner, repo, filterDuplicateSecs, leetcod let treeSHA = commits.data[0].commit.tree.sha; for (i = submissions.length - 1; i >= 0; i--) { submission = submissions[i]; - [treeSHA, latestCommitSHA] = await commit(octokit, user, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission); + [treeSHA, latestCommitSHA] = await commit(octokit, destinationFolder, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission); } log('Done syncing all submissions.'); } async function main() { - const user = core.getInput('user'); + const destinationFolder = core.getInput('destination-folder'); const githubToken = core.getInput('github-token'); const owner = context.repo.owner; const repo = context.repo.repo; @@ -219,7 +219,7 @@ async function main() { const leetcodeSession = core.getInput('leetcode-session'); const filterDuplicateSecs = core.getInput('filter-duplicate-secs'); - await sync(user, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession); + await sync(destinationFolder, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession); } main().catch((error) => { From c8cee32bc604501af3fbff49f9b7077a68f9283d Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:24:38 -0500 Subject: [PATCH 09/16] Revert owner to original for contribution PR --- README.md | 2 +- action.yml | 2 +- package.json | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cfedca78..213a0743 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit steps: - name: Sync - uses: CherryKatatonic/leetcode-sync@v1.4 + uses: joshcai/leetcode-sync@v1.4 with: destination-folder: my-folder github-token: ${{ github.token }} diff --git a/action.yml b/action.yml index 9c3373a8..cc423828 100644 --- a/action.yml +++ b/action.yml @@ -1,4 +1,4 @@ -name: 'CherryKatatonic LeetCode Sync' +name: 'LeetCode Sync' description: 'Sync LeetCode submissions to GitHub' branding: icon: git-commit diff --git a/package.json b/package.json index 490f02b8..15a46e53 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "cherrykatatonic-leetcode-sync", + "name": "leetcode-sync", "version": "1.4.0", "description": "Forked GitHub Action for syncing LeetCode submissions to a git repository", "main": "index.js", @@ -8,15 +8,15 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/cherrykatatonic/leetcode-sync.git" + "url": "git+https://github.com/joshcai/leetcode-sync.git" }, "keywords": [], "author": "", "license": "ISC", "bugs": { - "url": "https://github.com/cherrykatatonic/leetcode-sync/issues" + "url": "https://github.com/joshcai/leetcode-sync/issues" }, - "homepage": "https://github.com/cherrykatatonic/leetcode-sync#readme", + "homepage": "https://github.com/joshcai/leetcode-sync#readme", "dependencies": { "@actions/core": "^1.2.6", "@actions/github": "^4.0.0", From 380119c5e290de6ae0ddda304b37893035e0f0e9 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:28:44 -0500 Subject: [PATCH 10/16] Update README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 213a0743..188e67b9 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit - `leetcode-session` *(required)*: The LeetCode session value for retrieving submissions from LeetCode - `filter-duplicate-secs`: Number of seconds after an accepted solution to ignore other accepted solutions for the same problem, default: 86400 (1 day) +## Shared Repos + +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. + ## FAQ #### Job fails with "HttpError: API rate limit exceeded for installation ID \" From 8ee1b8832f18df3b685240c4eb46677af43d23a5 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:32:30 -0500 Subject: [PATCH 11/16] Move destination-folder to end of params because it is optional --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d27f482b..10fbcdec 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ function normalizeName(problemName) { return problemName.toLowerCase().replace(/\s/g, '_'); } -async function commit(octokit, destinationFolder, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) { +async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder) { const name = normalizeName(submission.title); log(`Committing solution for ${name}...`); @@ -115,7 +115,7 @@ function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissi return true; } -async function sync(destinationFolder, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession) { +async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession, destinationFolder) { const octokit = new Octokit({ auth: githubToken, userAgent: 'LeetCode sync to GitHub - GitHub Action', @@ -205,7 +205,7 @@ async function sync(destinationFolder, githubToken, owner, repo, filterDuplicate let treeSHA = commits.data[0].commit.tree.sha; for (i = submissions.length - 1; i >= 0; i--) { submission = submissions[i]; - [treeSHA, latestCommitSHA] = await commit(octokit, destinationFolder, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission); + [treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder); } log('Done syncing all submissions.'); } @@ -219,7 +219,7 @@ async function main() { const leetcodeSession = core.getInput('leetcode-session'); const filterDuplicateSecs = core.getInput('filter-duplicate-secs'); - await sync(destinationFolder, githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession); + await sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession, destinationFolder); } main().catch((error) => { From 58524633d809e74243a42428229b4960ebcdc44e Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:41:18 -0500 Subject: [PATCH 12/16] Use destructuring so param order is irrelevant --- index.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 10fbcdec..d360f19d 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,19 @@ function normalizeName(problemName) { return problemName.toLowerCase().replace(/\s/g, '_'); } -async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder) { +async function commit(params) { + const { + octokit, + owner, + repo, + defaultBranch, + commitInfo, + treeSHA, + latestCommitSHA, + submission, + destinationFolder + } = params; + const name = normalizeName(submission.title); log(`Committing solution for ${name}...`); @@ -92,7 +104,15 @@ async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, } // Returns false if no more submissions should be added. -function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions) { +function addToSubmissions(params) { + const { + response, + lastTimestamp, + filterDuplicateSecs, + submissions_dict, + submissions + } = params; + for (const submission of response.data.submissions_dump) { if (submission.timestamp <= lastTimestamp) { return false; @@ -115,7 +135,17 @@ function addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissi return true; } -async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession, destinationFolder) { +async function sync(inputs) { + const { + githubToken, + owner, + repo, + filterDuplicateSecs, + leetcodeCSRFToken, + leetcodeSession, + destinationFolder + } = inputs; + const octokit = new Octokit({ auth: githubToken, userAgent: 'LeetCode sync to GitHub - GitHub Action', @@ -183,7 +213,7 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT await delay(1000); } response = await getSubmissions(maxRetries); - if (!addToSubmissions(response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions)) { + if (!addToSubmissions({ response, lastTimestamp, filterDuplicateSecs, submissions_dict, submissions })) { break; } @@ -205,21 +235,21 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT let treeSHA = commits.data[0].commit.tree.sha; for (i = submissions.length - 1; i >= 0; i--) { submission = submissions[i]; - [treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder); + [treeSHA, latestCommitSHA] = await commit({ octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder }); } log('Done syncing all submissions.'); } async function main() { - const destinationFolder = core.getInput('destination-folder'); const githubToken = core.getInput('github-token'); const owner = context.repo.owner; const repo = context.repo.repo; const leetcodeCSRFToken = core.getInput('leetcode-csrf-token'); const leetcodeSession = core.getInput('leetcode-session'); const filterDuplicateSecs = core.getInput('filter-duplicate-secs'); + const destinationFolder = core.getInput('destination-folder'); - await sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession, destinationFolder); + await sync({ githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFToken, leetcodeSession, destinationFolder }); } main().catch((error) => { From feeca771036ee182aa6861d6a101eeb7425f479b Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:43:47 -0500 Subject: [PATCH 13/16] Add conditional for `destination-path` input field --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d360f19d..e2ff9767 100644 --- a/index.js +++ b/index.js @@ -56,9 +56,13 @@ async function commit(params) { throw `Language ${submission.lang} does not have a registered extension.`; } + const path = !!destinationFolder + ? `${destinationFolder}/problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}` + : `problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}` + const treeData = [ { - path: `${destinationFolder}/problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}`, + path, mode: '100644', content: submission.code, } From a1a21379c59414a642758aad69a64392c45311f0 Mon Sep 17 00:00:00 2001 From: Kat Grennan Date: Wed, 19 Jan 2022 15:51:01 -0500 Subject: [PATCH 14/16] Remove "Forked" from package description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 15a46e53..52624d46 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "leetcode-sync", "version": "1.4.0", - "description": "Forked GitHub Action for syncing LeetCode submissions to a git repository", + "description": "GitHub Action for syncing LeetCode submissions to a git repository", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" From d6c31ccbb1e2f22ac68ab35c011e3a875823737c Mon Sep 17 00:00:00 2001 From: Kat Date: Sat, 22 Jan 2022 04:04:49 -0500 Subject: [PATCH 15/16] Make requested changes from review comments --- README.md | 6 +++--- index.js | 5 ++--- package.json | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 188e67b9..726bf8b3 100644 --- a/README.md +++ b/README.md @@ -45,23 +45,23 @@ GitHub Action for automatically syncing LeetCode submissions to a GitHub reposit steps: - name: Sync - uses: joshcai/leetcode-sync@v1.4 + uses: joshcai/leetcode-sync@v1.3 with: - destination-folder: my-folder github-token: ${{ github.token }} leetcode-csrf-token: ${{ secrets.LEETCODE_CSRF_TOKEN }} leetcode-session: ${{ secrets.LEETCODE_SESSION }} + destination-folder: my-folder ``` 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). ## Inputs -- `destination-folder` *(optional)*: The folder in your repo to save the submissions to (necessary for shared repos) - `github-token` *(required)*: The GitHub access token for pushing solutions to the repository - `leetcode-csrf-token` *(required)*: The LeetCode CSRF token for retrieving submissions from LeetCode - `leetcode-session` *(required)*: The LeetCode session value for retrieving submissions from LeetCode - `filter-duplicate-secs`: Number of seconds after an accepted solution to ignore other accepted solutions for the same problem, default: 86400 (1 day) +- `destination-folder` *(optional)*: The folder in your repo to save the submissions to (necessary for shared repos) ## Shared Repos diff --git a/index.js b/index.js index e2ff9767..d010a5e5 100644 --- a/index.js +++ b/index.js @@ -56,9 +56,8 @@ async function commit(params) { throw `Language ${submission.lang} does not have a registered extension.`; } - const path = !!destinationFolder - ? `${destinationFolder}/problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}` - : `problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}` + const prefix = !!destinationFolder ? `${destinationFolder}/` : ''; + const path = `${prefix}problems/${name}/solution.${LANG_TO_EXTENSION[submission.lang]}` const treeData = [ { diff --git a/package.json b/package.json index 52624d46..dc1839a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "leetcode-sync", - "version": "1.4.0", + "version": "1.0.0", "description": "GitHub Action for syncing LeetCode submissions to a git repository", "main": "index.js", "scripts": { From 6a292798ba8179a5559f242c33a88819eb2a391a Mon Sep 17 00:00:00 2001 From: Kat Date: Sat, 22 Jan 2022 04:06:43 -0500 Subject: [PATCH 16/16] Fix order of inputs in yaml (nit) --- action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index cc423828..a691dbbb 100644 --- a/action.yml +++ b/action.yml @@ -4,9 +4,6 @@ branding: icon: git-commit color: yellow inputs: - destination-folder: - description: 'The folder to save the synced files in (relative to the top level of your repo)' - required: false github-token: description: 'The GitHub token' required: true @@ -20,6 +17,9 @@ inputs: description: 'Number of seconds after an accepted solution to ignore other accepted solutions for the same problem' required: false default: 86400 + destination-folder: + description: 'The folder to save the synced files in (relative to the top level of your repo)' + required: false runs: using: 'node12' main: 'index.js' \ No newline at end of file