@@ -7,9 +7,27 @@ import credentials from './credentials.js'
77
88const { username, token, folder } = credentials
99
10+ const startTime = ( new Date ( ) ) . getTime ( )
11+ const safetyFactor = 5
12+ const requestPerHour = 5000 / safetyFactor
13+ let requestNumber = 0
14+
15+ function wait ( seconds ) {
16+ return new Promise ( resolve => {
17+ console . log ( `... wait ${ seconds } s` )
18+ setTimeout ( ( ) => {
19+ resolve ( )
20+ } , seconds * 1000 )
21+ } )
22+ }
23+
1024function request ( path , options = { } ) {
11- return new Promise ( ( resolve , reject ) => {
25+ return new Promise ( async ( resolve , reject ) => {
1226 const baseUrl = path . substr ( 0 , 4 ) !== 'http' ? 'https://api.github.com' : ''
27+ requestNumber ++
28+ const allowedRequests = ( ( ( new Date ( ) ) . getTime ( ) ) - startTime ) * ( requestPerHour / 3600 / 1000 )
29+ if ( requestNumber > allowedRequests ) await wait ( 1 ) s + ' requests' )
30+ console . log ( `Request #${ requestNumber } : ${ baseUrl } ${ path } ` )
1331 fetch ( `${ baseUrl } ${ path } ` , {
1432 headers : {
1533 Authorization : `Token ${ token } `
@@ -44,11 +62,11 @@ function requestAll(path, options) {
4462 let page = 1
4563 while ( page !== null ) {
4664 const separator = path . indexOf ( '?' ) === - 1 ? '?' : '&'
47- const moreItemsResponse = await request ( `${ path } ${ separator } page=${ page } ` , options )
65+ const moreItemsResponse = await request ( `${ path } ${ separator } per_page=100& page=${ page } ` , options )
4866 const moreItems = await moreItemsResponse . json ( )
4967 if ( moreItems . length ) {
5068 items = [ ...items , ...moreItems ]
51- page ++
69+ page = moreItems . length === 100 ? page + 1 : null
5270 } else {
5371 page = null
5472 }
@@ -129,7 +147,7 @@ async function backup() {
129147 )
130148
131149 // Get issue comments
132- const comments = await requestAll ( issues [ issueId ] . comments_url )
150+ const comments = issues [ issueId ] . comments !== 0 ? await requestAll ( issues [ issueId ] . comments_url ) : [ ]
133151
134152 // Add issue comments to issues JSON
135153 issues [ issueId ] . comments = comments
@@ -169,7 +187,7 @@ async function backup() {
169187 shell . exit ( 1 )
170188
171189 } catch ( err ) {
172- throw err
190+ throw new Error ( err )
173191 }
174192}
175193
0 commit comments