-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.js
executable file
·152 lines (134 loc) · 3.64 KB
/
index.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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const rp = require('request-promise')
const _ = require('lodash')
const Promise = require('bluebird')
const cmd = require('node-cmd')
const cmdAsync = Promise.promisify(cmd.get, {
multiArgs: true,
context: cmd
})
const cliProgress = require('cli-progress');
(async () => {
let argv = require('yargs')
.usage('Utility to backup all gitlab repos to a local directory')
.option('token', {
alias: 't',
type: 'string',
description: 'Gitlab Token'
})
.option('output', {
alias: 'o',
type: 'string',
description: 'Backup to output directory, defaults to ./gitlab-backup'
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Enable verbose output'
})
.option('url', {
alias: 'u',
type: 'string',
description: 'Specify Gitlab instance URL'
})
.option('method', {
alias: 'm',
type: 'string',
description: 'Specify clone method (default is http)'
})
.help(true)
.argv
const baseUrl = argv.url || 'https://gitlab.com'
if(argv.verbose){
console.log(`Set gitlab base url to ${baseUrl}`)
}
console.log()
if (!argv.token) {
console.log(
`Please pass your gitlab token using the --token flag,\nGet your token at ${baseUrl}/profile/personal_access_tokens\n\npass --help for full help\n\n`
)
process.exit(1)
}
const method = argv.method == 'ssh' ? 'ssh_url_to_repo' : 'http_url_to_repo'
const requestOptions = {
json: true,
qs: {
simple: true
},
headers: {
'PRIVATE-TOKEN': argv.token
}
}
const user = await rp.get(`${baseUrl}/api/v4/user`, requestOptions)
if (argv.verbose) {
console.log(`Got user: ${user.name} (${user.username}) ID: ${user.id}`)
}
const personalProjects = await rp.get(
`${baseUrl}/api/v4/users/${user.id}/projects?per_page=100`,
requestOptions
)
if (argv.verbose) {
console.log(
'Got personal projects:\n',
// personalProjects
personalProjects.map(p => p.name)
)
}
let pgits = _.map(personalProjects, method)
const groups = await rp.get(
`${baseUrl}/api/v4/groups?per_page=100`,
requestOptions
)
if (argv.verbose) {
console.log(
'Got groups:\n',
groups.map(g => g.name)
)
}
const gids = _.map(groups, 'id')
for (let gid of gids) {
let projects = await rp.get(
`${baseUrl}/api/v4/groups/${gid}/projects?per_page=100`,
requestOptions
)
let ps = _.map(projects, method)
for (let p of ps) {
console.log(`Got project ${p} of ${gid}`)
pgits.push(p)
}
}
if (argv.verbose) {
console.log('Backing up following repos')
console.log(pgits)
}
const cloneProgressBar = new cliProgress.SingleBar(
{},
cliProgress.Presets.legacy
)
cloneProgressBar.start(pgits.length, 0)
let index = 0
for (let repo of pgits) {
const repoName = repo.substring(argv.method == 'ssh' ? 15 : 19, repo.length - 4)
const repoPath = `${argv.output || 'gitlab-backup'}/${repoName}`
if (fs.existsSync(repoPath)) {
const stats = fs.statSync(repoPath)
if (!stats.isDirectory) {
console.error(`Path ${repoPath} exist and not a directory. Skipped.`)
} else {
console.log(`Pulling ${repoName}`)
const stdout = await cmdAsync(`git -C "${repoPath}" pull`).catch(
console.log
)
}
} else {
console.log(`Cloning ${repoName}`)
const stdout = await cmdAsync(`git clone ${repo} "${repoPath}"`).catch(
console.log
)
}
cloneProgressBar.update(++index)
}
cloneProgressBar.stop()
})()