@@ -3,7 +3,6 @@ import shell from 'shelljs'
33import { dirname , basename } from 'path'
44import fetch from 'node-fetch'
55import { extension } from 'mime-types'
6- import credentials from './credentials.js'
76
87shell . config . fatal = true
98
@@ -12,7 +11,8 @@ const retryCount = 10
1211const retryDelayRateLimit = 6 * 60
1312const retryDelayOthers = 6
1413
15- const { username, token, folder } = credentials
14+ const { USERNAME , TOKEN } = process . env
15+ const FOLDER = '/usr/src/backup'
1616
1717function delay ( seconds ) {
1818 return new Promise ( resolve => {
@@ -32,7 +32,7 @@ function request(path, options = {}) {
3232 try {
3333 resp = await fetch ( `${ baseUrl } ${ path } ` , {
3434 headers : {
35- Authorization : `Token ${ token } `
35+ Authorization : `Token ${ TOKEN } `
3636 } ,
3737 ...options
3838 } )
@@ -125,15 +125,15 @@ function downloadFile(sourceFileUrl, targetFilePath) {
125125 } )
126126}
127127
128- function downloadAssets ( body , folder , filename ) {
128+ function downloadAssets ( body , FOLDER , filename ) {
129129 return new Promise ( async ( resolve , reject ) => {
130130 try {
131131 const assets = body ?. match ( / [ " ( ] h t t p s : \/ \/ g i t h u b \. c o m \/ ( .+ ) \/ a s s e t s \/ ( .+ ) [ ) " ] / g) || [ ]
132132 for ( const assetId in assets ) {
133133 const targetFilename = filename . replace ( '{id}' , assetId )
134- const targetPath = folder + '/' + targetFilename
134+ const targetPath = FOLDER + '/' + targetFilename
135135 const sourceUrl = assets [ assetId ] . replace ( / ^ [ " ( ] ( .+ ) [ ) " ] $ / , '$1' )
136- fs . ensureDirSync ( folder )
136+ fs . ensureDirSync ( FOLDER )
137137 const realTargetFilename = basename ( await downloadFile ( sourceUrl , targetPath ) )
138138 body = body . replace ( `"${ sourceUrl } "` , '"file://./assets/' + realTargetFilename + '"' )
139139 body = body . replace ( `(${ sourceUrl } )` , '(file://./assets/' + realTargetFilename + ')' )
@@ -153,28 +153,28 @@ function writeJSON(path, json) {
153153async function backup ( ) {
154154 try {
155155
156- // Reset the backup folder
157- fs . emptyDirSync ( folder )
156+ // Reset the backup FOLDER
157+ fs . emptyDirSync ( FOLDER )
158158
159159 // Get repositories
160160 const repositories = await requestAllWithRetry ( '/user/repos' )
161161
162162 // Save repositories
163- writeJSON ( `${ folder } /repositories.json` , repositories )
163+ writeJSON ( `${ FOLDER } /repositories.json` , repositories )
164164
165165 // Loop repositories
166166 for ( const repository of repositories ) {
167167
168168 // Get issues
169- const issues = await requestAllWithRetry ( `/repos/${ username } /${ repository . name } /issues?state=all` )
169+ const issues = await requestAllWithRetry ( `/repos/${ USERNAME } /${ repository . name } /issues?state=all` )
170170
171171 // Loop issues
172172 for ( const issueId in issues ) {
173173
174174 // Download issue assets
175175 issues [ issueId ] . body = await downloadAssets (
176176 issues [ issueId ] . body ,
177- `${ folder } /repositories/${ repository . name } /assets` ,
177+ `${ FOLDER } /repositories/${ repository . name } /assets` ,
178178 `issue_${ issueId } _{id}`
179179 )
180180
@@ -190,7 +190,7 @@ async function backup() {
190190 // Download issue assets
191191 issues [ issueId ] . comments [ commentId ] . body = await downloadAssets (
192192 issues [ issueId ] . comments [ commentId ] . body ,
193- `${ folder } /repositories/${ repository . name } /assets` ,
193+ `${ FOLDER } /repositories/${ repository . name } /assets` ,
194194 `issue_${ issueId } _comment_${ commentId } _{id}`
195195 )
196196
@@ -199,20 +199,20 @@ async function backup() {
199199 }
200200
201201 // Save issues
202- writeJSON ( `${ folder } /repositories/${ repository . name } /issues.json` , issues )
202+ writeJSON ( `${ FOLDER } /repositories/${ repository . name } /issues.json` , issues )
203203
204204 // Clone repository
205- shell . exec ( `git clone https://${ token } @github.com/${ username } /${ repository . name } .git ${ folder } /repositories/${ repository . name } /repository` )
205+ shell . exec ( `git clone https://${ TOKEN } @github.com/${ USERNAME } /${ repository . name } .git ${ FOLDER } /repositories/${ repository . name } /repository` )
206206
207207 }
208208
209209 // Get user details
210210 const user = await requestJson ( '/user' )
211- writeJSON ( `${ folder } /user/user.json` , user )
211+ writeJSON ( `${ FOLDER } /user/user.json` , user )
212212
213213 // Get starred repositories
214214 const starred = await requestAllWithRetry ( '/user/starred' )
215- writeJSON ( `${ folder } /user/starred.json` , starred )
215+ writeJSON ( `${ FOLDER } /user/starred.json` , starred )
216216
217217 // Complete script
218218 console . log ( 'Backup completed!' )
0 commit comments