@@ -3,7 +3,6 @@ import shell from 'shelljs'
3
3
import { dirname , basename } from 'path'
4
4
import fetch from 'node-fetch'
5
5
import { extension } from 'mime-types'
6
- import credentials from './credentials.js'
7
6
8
7
shell . config . fatal = true
9
8
@@ -12,7 +11,8 @@ const retryCount = 10
12
11
const retryDelayRateLimit = 6 * 60
13
12
const retryDelayOthers = 6
14
13
15
- const { username, token, folder } = credentials
14
+ const { USERNAME , TOKEN } = process . env
15
+ const FOLDER = '/usr/src/backup'
16
16
17
17
function delay ( seconds ) {
18
18
return new Promise ( resolve => {
@@ -32,7 +32,7 @@ function request(path, options = {}) {
32
32
try {
33
33
resp = await fetch ( `${ baseUrl } ${ path } ` , {
34
34
headers : {
35
- Authorization : `Token ${ token } `
35
+ Authorization : `Token ${ TOKEN } `
36
36
} ,
37
37
...options
38
38
} )
@@ -125,15 +125,15 @@ function downloadFile(sourceFileUrl, targetFilePath) {
125
125
} )
126
126
}
127
127
128
- function downloadAssets ( body , folder , filename ) {
128
+ function downloadAssets ( body , FOLDER , filename ) {
129
129
return new Promise ( async ( resolve , reject ) => {
130
130
try {
131
131
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) || [ ]
132
132
for ( const assetId in assets ) {
133
133
const targetFilename = filename . replace ( '{id}' , assetId )
134
- const targetPath = folder + '/' + targetFilename
134
+ const targetPath = FOLDER + '/' + targetFilename
135
135
const sourceUrl = assets [ assetId ] . replace ( / ^ [ " ( ] ( .+ ) [ ) " ] $ / , '$1' )
136
- fs . ensureDirSync ( folder )
136
+ fs . ensureDirSync ( FOLDER )
137
137
const realTargetFilename = basename ( await downloadFile ( sourceUrl , targetPath ) )
138
138
body = body . replace ( `"${ sourceUrl } "` , '"file://./assets/' + realTargetFilename + '"' )
139
139
body = body . replace ( `(${ sourceUrl } )` , '(file://./assets/' + realTargetFilename + ')' )
@@ -153,28 +153,28 @@ function writeJSON(path, json) {
153
153
async function backup ( ) {
154
154
try {
155
155
156
- // Reset the backup folder
157
- fs . emptyDirSync ( folder )
156
+ // Reset the backup FOLDER
157
+ fs . emptyDirSync ( FOLDER )
158
158
159
159
// Get repositories
160
160
const repositories = await requestAllWithRetry ( '/user/repos' )
161
161
162
162
// Save repositories
163
- writeJSON ( `${ folder } /repositories.json` , repositories )
163
+ writeJSON ( `${ FOLDER } /repositories.json` , repositories )
164
164
165
165
// Loop repositories
166
166
for ( const repository of repositories ) {
167
167
168
168
// 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` )
170
170
171
171
// Loop issues
172
172
for ( const issueId in issues ) {
173
173
174
174
// Download issue assets
175
175
issues [ issueId ] . body = await downloadAssets (
176
176
issues [ issueId ] . body ,
177
- `${ folder } /repositories/${ repository . name } /assets` ,
177
+ `${ FOLDER } /repositories/${ repository . name } /assets` ,
178
178
`issue_${ issueId } _{id}`
179
179
)
180
180
@@ -190,7 +190,7 @@ async function backup() {
190
190
// Download issue assets
191
191
issues [ issueId ] . comments [ commentId ] . body = await downloadAssets (
192
192
issues [ issueId ] . comments [ commentId ] . body ,
193
- `${ folder } /repositories/${ repository . name } /assets` ,
193
+ `${ FOLDER } /repositories/${ repository . name } /assets` ,
194
194
`issue_${ issueId } _comment_${ commentId } _{id}`
195
195
)
196
196
@@ -199,20 +199,20 @@ async function backup() {
199
199
}
200
200
201
201
// Save issues
202
- writeJSON ( `${ folder } /repositories/${ repository . name } /issues.json` , issues )
202
+ writeJSON ( `${ FOLDER } /repositories/${ repository . name } /issues.json` , issues )
203
203
204
204
// 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` )
206
206
207
207
}
208
208
209
209
// Get user details
210
210
const user = await requestJson ( '/user' )
211
- writeJSON ( `${ folder } /user/user.json` , user )
211
+ writeJSON ( `${ FOLDER } /user/user.json` , user )
212
212
213
213
// Get starred repositories
214
214
const starred = await requestAllWithRetry ( '/user/starred' )
215
- writeJSON ( `${ folder } /user/starred.json` , starred )
215
+ writeJSON ( `${ FOLDER } /user/starred.json` , starred )
216
216
217
217
// Complete script
218
218
console . log ( 'Backup completed!' )
0 commit comments