Last active
May 27, 2016 18:55
-
-
Save alasarr/ea130fd1caf2288a843f5011cff9c26b to your computer and use it in GitHub Desktop.
NodeJS script to migrate all repos from bitbucket to GitHub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var USER_BITBUCKET='XXX', | |
ORG_BITBUCKET='XXX', | |
PASS_BITBUCKET='XXX', | |
USER_GITHUB='XXX', | |
ORG_GITHUB='XXX', | |
PASS_GITHUB='XXX'; | |
var request = require('request'); | |
var listrepos = []; | |
var repoerrors = []; | |
var blacklist = ['mexicodf-dashboard-ios'] | |
function getRepoList(page){ | |
//var url = 'https://' + USER + ':' + PASS + '@api.bitbucket.org/2.0/teams/' + ORG + '/repositories?page=' + page; | |
var url = 'https://' + USER_BITBUCKET + ':' + PASS_BITBUCKET + '@api.bitbucket.org/2.0/repositories/' + ORG_BITBUCKET +'?page=' + page; | |
request({url: url,json:true}, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
if(body.values.length){ | |
//console.log(body.values); | |
for (var i in body.values){ | |
if (blacklist.indexOf(body.values[i].name)==-1){ | |
listrepos.push(body.values[i]); | |
} | |
} | |
getRepoList(page+1); | |
} | |
else{ | |
listReposCompleted(); | |
} | |
} | |
}); | |
} | |
function listReposCompleted(){ | |
var GitHubApi = require("github"); | |
var github = new GitHubApi({ | |
// required | |
version: "3.0.0", | |
}); | |
github.authenticate({ | |
type: "basic", | |
username: USER_GITHUB, | |
password: PASS_GITHUB | |
}); | |
var fs = require('fs'); | |
var dir = './tmp'; | |
if (!fs.existsSync(dir)){ | |
fs.mkdirSync(dir); | |
} | |
createRepo(github,0); | |
} | |
function createRepo(github,index){ | |
function getNextReport(){ | |
//console.log(listrepos); | |
if (index< listrepos.length-1){ | |
createRepo(github,index+1); | |
} | |
else{ | |
console.log('Migration completed'); | |
console.log('Errors '+ repoerrors.length); | |
if (repoerrors.length){ | |
for (var i in repoerrors){ | |
console.log('Error at: '+ repoerrors[i]); | |
} | |
} | |
} | |
} | |
var r = listrepos[index]; | |
console.log("Creating repo at Github: " + r.name); | |
github.repos.createFromOrg({ | |
org: ORG_GITHUB, | |
name: r.name, | |
description: r.description, | |
private: 'true', | |
// DEVELOPERS TEAM ! | |
team_id: 766728 | |
},function(err,data){ | |
if (err){ | |
console.error(err); | |
console.error('Cannot create repo at GitHub ' + r.name); | |
repoerrors.push(r.name); | |
getNextReport(); | |
} | |
else{ | |
migrateRepo(r,function(error){ | |
if (error){ | |
repoerrors.push(r.name); | |
} | |
//console.log(listrepos); | |
getNextReport(); | |
}); | |
} | |
}); | |
} | |
function migrateRepo(r,cb){ | |
var sys = require('sys') | |
var exec = require('child_process').exec; | |
var child; | |
console.log("Fetching repo from Bitbucket: "+ r.name); | |
child = exec("cd tmp && git clone --mirror https://bitbucket.org/"+ ORG_BITBUCKET + "/" + r.name +".git", function (error, stdout, stderr) { | |
if (error !== null) { | |
console.log('exec error: ' + error); | |
cb(error); | |
} | |
else{ | |
console.log("Pushing repo to GitHub: " + r.name) | |
child = exec("cd tmp/" + r.name + ".git "+ | |
"&& git remote set-url --push origin https://github.com/"+ ORG_GITHUB + "/" + r.name + | |
"&& git push --mirror", function (error, stdout, stderr) { | |
if (error !== null) { | |
console.log('exec error: ' + error); | |
cb(error); | |
} | |
else{ | |
cb(null); | |
} | |
}); | |
} | |
}); | |
} | |
getRepoList(1); | |
//listReposCompleted(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script has been written really really fast, so take care if you're going to use it on your own. The implementation of a minimal test suite is recommended.