@@ -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
224258main ( ) . catch ( ( error ) => {
0 commit comments