This repository was archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.js
497 lines (412 loc) · 13.5 KB
/
backup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
const { Command, flags } = require('@oclif/command')
const Promise = require('aigle')
const chalk = require('chalk')
const fs = require('fs')
const inquirer = require('inquirer')
const path = require('path')
const Spinnies = require('spinnies')
const config = require('../config')
const GithubAPI = require('../lib/github-api')
const Git = require('../lib/git')
const spinnies = new Spinnies()
const backupPath = path.join(process.cwd(), `gbulk-backup-${Date.now()}`)
const defaultParallelCount = 8
class BackupCommand extends Command {
static description = chalk`backup repositories
With {bold gbulk}, you can backup from different sources:
- to backup repositories you own, run {yellow gbulk backup} without arguments (if you want to specify a backup path, use {yellow gbulk backup $YOUR_USERNAME $BACKUP_PATH})
- to backup repositories of another user, run {yellow gbulk backup $USERNAME}
- to backup repositories of an organization, run {yellow gbulk backup $ORGNAME}
Git LFS objects will be backup if {bold git-lfs} is available in path.`
static flags = {
help: flags.help({ char: 'h' }),
public: flags.boolean({
description: 'include/exclude public repositories'
}),
private: flags.boolean({
description: 'include/exclude private repositories'
}),
owner: flags.boolean({
description: 'include/exclude owned repositories',
allowNo: true
}),
collaborator: flags.boolean({
description: 'include/exclude repositories where user is collaborator',
allowNo: true
}),
member: flags.boolean({
description: 'include/exclude repositories where user is member',
allowNo: true
}),
exclude: flags.string({
char: 'x',
description:
'exclude repositories whose name is matching specified string or regex pattern (omitting start and end delimiters)',
multiple: true
}),
match: flags.string({
char: 'm',
description:
'include only repositories whose name is matching specified string or regex pattern (omitting start and end delimiters)',
multiple: true
}),
'clean-refs': flags.boolean({
char: 'c',
description: 'clean GitHub specific pull refs (refs/pull) from backup repositories',
default: false
}),
lfs: flags.boolean({
description: 'include LFS objects in backup',
default: true,
allowNo: true
}),
quiet: flags.boolean({
char: 'q',
description: 'disable logging',
default: false
}),
interactive: flags.boolean({
char: 'i',
description: 'interactive mode',
default: false,
exclusive: [
'public',
'private',
'owner',
'collaborator',
'member',
'exclude',
'match',
'clean-refs',
'lfs',
'quiet'
]
}),
parallel: flags.string({
char: 'p',
description: 'backup multiple repositories in parallel',
default: defaultParallelCount
})
}
static args = [
{
name: 'from',
description: chalk`{underline user} name or {underline organization} name to backup from`,
default: config.get('auth.user')
},
{
name: 'destination',
description: 'backup destination path',
default: backupPath
}
]
async run() {
const { args, flags } = this.parse(BackupCommand)
let exitCode = 0
if (flags.quiet) {
this.debug('quiet mode enabled')
}
flags.parallel = +flags.parallel
if (flags.parallel === NaN) {
this.debug(`parallel flag value [${flags.parallel}] is invalid, using default value (${defaultParallelCount})`)
flags.parallel = defaultParallelCount
}
this.debug('checking auth')
const auth = config.get('auth')
if (!auth || !auth.token) {
this.error(chalk`You are not authenticated, please run {yellow gbulk login} first.`)
} else {
this.debug('authenticated user is', auth.user)
}
this.debug('checking git command availability')
await Git.check()
this.debug('git command available')
let gitLFS = false
if (flags.lfs) {
this.debug('checking git lfs availability')
gitLFS = await Git.LFS.check()
if (!gitLFS) {
this.warn('Git LFS is not installed, objects stored through LFS will not be backup.')
}
this.debug('git lfs available')
} else {
this.debug('backup will skip lfs objects')
}
this.debug('checking destination path', args.destination)
try {
await fs.promises.access(args.destination)
this.debug('destination path exists')
} catch (err) {
try {
this.debug('destination path does not exists, create it...')
await fs.promises.mkdir(args.destination)
this.debug('destination path created')
} catch (err) {
this.debug(err)
this.error(`Cannot create ${args.destination}`)
}
}
// Enable flags interactively
if (flags.interactive) {
const { filters } = await inquirer.prompt([
{
type: 'checkbox',
name: 'filters',
message: 'Select search filters',
choices: [
new inquirer.Separator('### Repository type ###'),
{
name: 'public',
checked: true
},
{
name: 'private',
checked: true
},
new inquirer.Separator('### Affiliation with repository ###'),
{
name: 'owner',
checked: true
},
{
name: 'collaborator',
checked: true
},
{
name: 'member',
checked: true
}
]
}
])
for (const filter of filters) {
flags[filter] = true
}
}
let repositories = []
// If `from` equals authenticated user, use it as backup source (not as another user)
if (args.from === auth.user) {
if (!flags.quiet) {
spinnies.add('fetch', { text: `Fetching repositories of ${auth.user}` })
}
this.debug('backup authenticated user repositories')
const affiliationFlags = ['owner', 'collaborator', 'member']
const affiliation = []
// Handle affiliation flags enabling
affiliationFlags.forEach((flag) => {
if (flags[flag]) affiliation.push(flag)
})
// If no affiliation flag explicitly enabled
if (!affiliation.length) {
// Enable all affiliation flags not explicitly disabled
affiliationFlags.forEach((flag) => {
if (flags[flag] !== false) affiliation.push(flag)
})
}
const options = {
affiliation,
type: flags.public && !flags.private ? 'public' : flags.private && !flags.public ? 'private' : 'all'
}
this.debug('backup', options.type, 'repositories where user is', affiliation.join(','))
repositories = await GithubAPI.get.repositories({
token: auth.token,
options
})
}
// @TODO: handle other cases than auth user backup source
// - user
// - organization
if (!flags.quiet) {
spinnies.succeed('fetch')
}
if (!repositories.length) {
this.warn('No repositories to backup.')
exitCode = 1
} else {
if (!flags.quiet) {
spinnies.add('prepare', { text: `Preparing backup of ${repositories.length} repositories...` })
}
if (flags.exclude) {
this.debug('filter repos following exclude flag')
const excluded = []
repositories = repositories.filter((repo) => {
return flags.exclude.reduce((acc, exclude) => {
// If not yet excluded, test exclude rule
if (acc) {
const keep = !new RegExp(exclude).test(repo.name)
if (!keep) {
excluded.push(repo.name)
}
return keep
}
return acc
}, true)
})
this.debug(`excluded ${excluded.length} repositories ${JSON.stringify(excluded)}`)
}
if (flags.match) {
this.debug('filter repos following match flag')
const excluded = []
repositories = repositories.filter((repo) => {
return flags.match.reduce((acc, match) => {
// If not yet excluded, test exclude rule
if (acc) {
const keep = new RegExp(match).test(repo.name)
if (!keep) {
excluded.push(repo.name)
}
return keep
}
return acc
}, true)
})
this.debug(`excluded ${excluded.length} repositories ${JSON.stringify(excluded)}`)
}
if (flags.interactive) {
// This replaces exclude flag in interactive mode
const { repos: selectedRepos } = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select repositories to backup',
name: 'repos',
choices: repositories
.sort((a, b) => {
if (a.private && b.private) {
return 0
} else if (!a.private && b.private) {
return -1
}
return 1
})
.filter((repo) => !repo.private)
.reduce((acc, repo) => {
if (!acc.length) {
acc.push(new inquirer.Separator('### Public repositories ###'))
}
acc.push({
checked: true,
name: repo.fullName
})
return acc
}, [])
.concat(
repositories
.filter((repo) => repo.private)
.reduce((acc, repo) => {
if (!acc.length) {
acc.push(new inquirer.Separator('### Private repositories ###'))
}
acc.push({
checked: true,
name: repo.fullName
})
return acc
}, [])
),
validate: function(selectedRepos) {
if (selectedRepos.length < 1) {
return 'You must choose at least one repository.'
}
return true
}
}
])
repositories = repositories.filter((repository) => {
return selectedRepos.find((selectedRepo) => repository.fullName === selectedRepo)
})
const { cleanRefs } = await inquirer.prompt([
{
type: 'list',
message: 'Do you want to clean pull request references from backup repositories?',
name: 'cleanRefs',
choices: ['Yes', 'No']
}
])
if (cleanRefs === 'Yes') {
flags['clean-refs'] = true
}
if (gitLFS) {
const { lfs } = await inquirer.prompt([
{
type: 'list',
message: 'Do you want to include LFS objects (if relevant) in backup?',
name: 'lfs',
choices: ['Yes', 'No']
}
])
if (lfs === 'No') {
flags.lfs = false
}
}
}
await Promise.all(repositories).mapLimit(flags.parallel, async (repository) => {
const data = {
path: path.resolve(args.destination, repository.fullName + '.git'),
url: repository.urls.https
}
if (!flags.quiet) {
spinnies.add(repository.fullName, { text: `${repository.fullName} ==> Cloning...` })
}
try {
await Git.clone(data)
if (flags.lfs && gitLFS) {
if (!flags.quiet) {
spinnies.update(repository.fullName, { text: `${repository.fullName} ==> Fetching LFS objects...` })
}
try {
await Git.LFS.fetch(data)
} catch (err) {
this.warn(`Failed to fetch LFS objects from ${repository.fullName}`)
this.debug(err)
exitCode = 1
}
}
if (flags['clean-refs']) {
if (!flags.quiet) {
spinnies.update(repository.fullName, { text: `${repository.fullName} ==> Cleaning /pull refs...` })
}
try {
await Git.cleanRefs(data)
} catch (err) {
this.warn(`Failed to clean /pull refs from ${repository.fullName}`)
this.debug(err)
exitCode = 1
}
}
if (!flags.quiet) {
spinnies.succeed(repository.fullName, { text: `${repository.fullName} ===> ${data.path}` })
spinnies.update('backup', { text: globalBackupText(++backupCount) })
if (repositories.length > 25) {
setTimeout(() => {
spinnies.remove(repository.fullName)
}, 5000)
}
}
} catch (err) {
if (err.exitCode === 128) {
const error = `${repository.fullName}: destination path exists and is not empty`
if (!flags.quiet) {
spinnies.fail(repository.fullName, {
text: error
})
} else {
this.warn(error)
}
} else {
if (!flags.quiet) {
spinnies.fail(repository.fullName, { text: err.shortMessage })
} else {
this.warn(err.shortMessage)
}
}
this.debug(err)
}
})
if (!flags.quiet) {
spinnies.succeed('backup')
}
}
this.exit(exitCode)
}
}
module.exports = BackupCommand