@@ -16,7 +16,7 @@ function delay(seconds) {
1616 return new Promise ( resolve => {
1717 console . log ( `... delay ${ seconds } s` )
1818 setTimeout ( ( ) => {
19- resolve ( )
19+ return resolve ( )
2020 } , seconds * 1000 )
2121 } )
2222}
@@ -26,14 +26,25 @@ function request(path, options = {}) {
2626 const baseUrl = path . substr ( 0 , 4 ) !== 'http' ? 'https://api.github.com' : ''
2727 console . log ( `Request ${ baseUrl } ${ path } ` )
2828 for ( let n = 1 ; n <= retryCount ; n ++ ) {
29- const resp = await fetch ( `${ baseUrl } ${ path } ` , {
30- headers : {
31- Authorization : `Token ${ token } `
32- } ,
33- ...options
34- } )
29+ let resp
30+ try {
31+ resp = await fetch ( `${ baseUrl } ${ path } ` , {
32+ headers : {
33+ Authorization : `Token ${ token } `
34+ } ,
35+ ...options
36+ } )
37+ } catch {
38+ console . log ( `... failed at #${ n } attempt` )
39+ if ( n < retryCount ) {
40+ await delay ( retryDelayOthers )
41+ continue
42+ } else {
43+ return reject ( )
44+ }
45+ }
3546 if ( resp . ok ) {
36- resolve ( resp )
47+ return resolve ( resp )
3748 } else {
3849 const rateLimitRemaining = parseInt ( [ ...resp . headers ] . filter ( obj => obj [ 0 ] === 'x-ratelimit-remaining' ) [ 0 ] [ 1 ] )
3950 const rateLimitLimit = parseInt ( [ ...resp . headers ] . filter ( obj => obj [ 0 ] === 'x-ratelimit-limit' ) [ 0 ] [ 1 ] )
@@ -46,7 +57,7 @@ function request(path, options = {}) {
4657 }
4758 }
4859 }
49- reject ( )
60+ return reject ( )
5061 } )
5162}
5263
@@ -55,9 +66,9 @@ function requestJson(path, options) {
5566 try {
5667 const response = await request ( path , options )
5768 const json = await response . json ( )
58- resolve ( json )
69+ return resolve ( json )
5970 } catch ( err ) {
60- reject ( err )
71+ return reject ( err )
6172 }
6273 } )
6374}
@@ -78,23 +89,36 @@ function requestAll(path, options) {
7889 page = null
7990 }
8091 }
81- resolve ( items )
92+ return resolve ( items )
8293 } catch ( err ) {
83- reject ( err )
94+ return reject ( err )
8495 }
8596 } )
8697}
8798
99+ async function requestAllWithRetry ( path , options ) {
100+ for ( let n = 1 ; n <= retryCount ; n ++ ) {
101+ try {
102+ const items = requestAll ( path , options )
103+ return items
104+ } catch ( err ) {
105+ if ( n === 10 ) return err
106+ console . log ( '... failed at attempt #' + n )
107+ await delay ( retryDelayOthers )
108+ }
109+ }
110+ }
111+
88112function downloadFile ( sourceFileUrl , targetFilePath ) {
89113 return new Promise ( async ( resolve , reject ) => {
90114 const response = await request ( sourceFileUrl )
91115 const ext = extension ( [ ...response . headers ] . filter ( obj => obj [ 0 ] === 'content-type' ) [ 0 ] [ 1 ] )
92116 targetFilePath = targetFilePath + ( ext ? '.' + ext : '' )
93117 const fileStream = fs . createWriteStream ( targetFilePath )
94118 response . body . pipe ( fileStream )
95- response . body . on ( 'error' , reject )
119+ response . body . on ( 'error' , ( ) => { return reject ( ) } )
96120 fileStream . on ( 'finish' , ( ) => {
97- resolve ( targetFilePath )
121+ return resolve ( targetFilePath )
98122 } )
99123 } )
100124}
@@ -112,9 +136,9 @@ function downloadAssets(body, folder, filename) {
112136 body = body . replace ( `"${ sourceUrl } "` , '"file://./assets/' + realTargetFilename + '"' )
113137 body = body . replace ( `(${ sourceUrl } )` , '(file://./assets/' + realTargetFilename + ')' )
114138 }
115- resolve ( body )
139+ return resolve ( body )
116140 } catch ( err ) {
117- reject ( err )
141+ return reject ( err )
118142 }
119143 } )
120144}
@@ -132,7 +156,7 @@ async function backup() {
132156 fs . ensureDirSync ( folder )
133157
134158 // Get repositories
135- const repositories = await requestAll ( '/user/repos' )
159+ const repositories = await requestAllWithRetry ( '/user/repos' )
136160
137161 // Save repositories
138162 writeJSON ( `${ folder } /repositories.json` , repositories )
@@ -141,7 +165,7 @@ async function backup() {
141165 for ( const repository of repositories ) {
142166
143167 // Get issues
144- const issues = await requestAll ( `/repos/${ username } /${ repository . name } /issues?state=all` )
168+ const issues = await requestAllWithRetry ( `/repos/${ username } /${ repository . name } /issues?state=all` )
145169
146170 // Loop issues
147171 for ( const issueId in issues ) {
@@ -154,7 +178,7 @@ async function backup() {
154178 )
155179
156180 // Get issue comments
157- const comments = issues [ issueId ] . comments !== 0 ? await requestAll ( issues [ issueId ] . comments_url ) : [ ]
181+ const comments = issues [ issueId ] . comments !== 0 ? await requestAllWithRetry ( issues [ issueId ] . comments_url ) : [ ]
158182
159183 // Add issue comments to issues JSON
160184 issues [ issueId ] . comments = comments
@@ -186,7 +210,7 @@ async function backup() {
186210 writeJSON ( `${ folder } /user/user.json` , user )
187211
188212 // Get starred repositories
189- const starred = await requestAll ( '/user/starred' )
213+ const starred = await requestAllWithRetry ( '/user/starred' )
190214 writeJSON ( `${ folder } /user/starred.json` , starred )
191215
192216 // Complete script
0 commit comments