-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (131 loc) · 4.42 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
const inquirer = require('inquirer');
const program = require('commander');
const colors = require('colors');
const https = require('https');
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
// Can be 10 to 100, https://developer.atlassian.com/bitbucket/api/2/reference/resource/teams
const pagelen = 100;
let team;
program
.option('-t, --team [team]', 'bitbucket team name')
.option('-a, --authentication [authentication]', 'bitbucket authentication in username:password format')
// .option('-k, --key [key]', 'bitbucket project key') // TODO specify a key to get a specific project
.parse(process.argv);
const request = function (options) {
return new Promise((resolve, reject) => {
console.log(`${colors.green(options.method)} to: https://${options.host}${options.path}`);
const req = https.request(options, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode)).catch(error => { console.log('caught an error: ' + error.message)});
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => {
let data = body.join('');
try {
data = JSON.parse(data);
} catch (e) {
console.log(colors.yellow('Unable to parse response to JSON'));
}
resolve(data);
});
});
req.on('error', (err) => reject(err));
req.end();
});
};
function getProjects(credentials, page) {
let url = `/2.0/teams/${team}/projects/`;
if (page) {
url += `?page=${page}`;
}
const options = {
host: 'api.bitbucket.org',
path: url,
method: 'GET',
auth: `${credentials.username}:${credentials.password}`
};
return request(options);
}
function getProject(credentials, key, pagelen) {
const options = {
host: 'api.bitbucket.org',
path: `/2.0/repositories/${team}?q=project.key="${key}"&pagelen=${pagelen}`,
method: 'GET',
auth: `${credentials.username}:${credentials.password}`
};
return request(options);
}
function mkDirInCwd(name) {
return new Promise((resolve) => {
fs.mkdir(path.join(process.cwd(), name), (err) => {
if (err && err.code !== 'EEXIST') { throw err; }
else resolve();
});
});
}
function compress(name, path) {
var pathToArchive = `./${name}.tar.gz`;
var pathToFolder = path;
var tar = spawn('tar', ['czf', pathToArchive, pathToFolder]);
tar.on('exit', function (code) {
if (code === 0) {
console.log(colors.green(`Team code compressed: ${colors.grey(pathToArchive)}`));
} else {
console.log(colors.red(`Failed to compress folder. Check that you have the 'tar' utility installed.`));
}
});
}
async function main() {
let credentials;
if (program.authentication) {
const auth = program.authentication.split(':');
credentials = {
username: auth[0],
password: auth[1]
};
} else {
credentials = await inquirer.prompt([{
type: 'input',
name: 'team',
message: 'Enter bitbucket team name',
when: () => { return !program.team; }
}, {
type: 'input',
name: 'username',
message: 'Enter bitbucket username'
}, {
type: 'password',
name: 'password',
message: 'Enter bitbucket password'
}]);
}
team = program.team || credentials.team;
await mkDirInCwd(team);
let projects = [];
const projectsRes = await getProjects(credentials);
projects = projectsRes.values;
let numberOfPages = Math.ceil(projectsRes.size / projectsRes.pagelen);
while (numberOfPages > 1) {
const pageRes = await getProjects(credentials, numberOfPages);
numberOfPages--;
projects = projects.concat(pageRes.values);
}
for (let i = 0; i < projects.length; i++) {
let project = projects[i];
await mkDirInCwd(`${team}/${project.name}`);
const projectRes = await getProject(credentials, project.key, pagelen);
for (let j = 0; j < projectRes.values.length; j++) {
let repo = projectRes.values[j];
await exec(`git clone https://${credentials.username}@bitbucket.org/${repo.full_name}.git`, { cwd: path.join(process.cwd(), team, project.name) }, (err, stdout, stderr) => {
if (err) console.error(colors.red(err));
console.log(colors.gray(stderr));
});
}
}
compress(team, path.join(process.cwd(), team));
}
main();