-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.js
88 lines (74 loc) · 2.65 KB
/
migration.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
const { Octokit, App } = require("@octokit/core");
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const assert = require('assert');
const {existsSync, readFile, writeFile, promises: fsPromises} = require('fs');
const argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 --ght [string] --bbt [string] --repo [string] --owner [string]')
.demandOption(['ght', 'bbt', 'repo', 'owner'])
.argv;
const COMPLETE_STATUS = 'complete';
async function execAndPrint(command) {
try {
const { stdout, stderr } = await exec(command);
console.log(stdout);
console.log(stderr);
return 0;
} catch (error) {
console.log(`ERROR: ${error.message}`);
process.exit(-1);
}
}
(async () => {
const octokit = new Octokit({
auth: argv.ght
})
const createRepoResponse = await octokit.request('POST /orgs/AI21Labs/repos', {
org: 'AI21Labs',
name: argv.repo,
homepage: 'https://github.com',
'private': true,
});
assert([200, 201].includes(createRepoResponse.status));
const importResponse = await octokit.request(`PUT /repos/AI21Labs/${argv.repo}/import`, {
owner: argv.owner,
vcs_url: `https://bitbucket.org/ai21labs/${argv.repo}.git`,
vcs_username: argv.owner,
vcs_password: argv.bbt
})
assert([200, 201].includes(importResponse.status));
var status = 'importing'
while (status !== COMPLETE_STATUS) {
const response = await octokit.request(`GET /repos/AI21Labs/${argv.repo}/import`);
status = response.data.status;
if (status !== COMPLETE_STATUS) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log(`import status: ${status}`);
}
await execAndPrint(`git clone [email protected]:AI21Labs/${argv.repo}.git`);
await execAndPrint(`mkdir -p ${argv.repo}/.github/workflows`);
await execAndPrint(`cp .github/CODEOWNERS ${argv.repo}/.github`);
await execAndPrint(`cp .github/workflows/quality-checks.yml ${argv.repo}/.github/workflows`);
readFile('.github/settings.yml', 'utf-8', function (err, contents) {
if (err) {
console.log(err);
return;
}
const replaced = contents.replace(/name: github-migration/g, `name: ${argv.repo}`);
writeFile(`${argv.repo}/.github/settings.yml`, replaced, 'utf-8', function (err) {
console.log(err);
});
});
if (!existsSync(`${argv.repo}/.pre-commit-config.yaml`)) {
await execAndPrint(`cp .pre-commit-config.yaml ${argv.repo}`);
}
await execAndPrint(`\
cd ${argv.repo} && \
git add -A
git commit -m \"ci: migrating from bitbucket\" && \
git push -u origin master
`
);
await execAndPrint(`cd .. && rm -rf ${argv.repo}`);
})();