Created
March 13, 2018 15:59
-
-
Save jairamc/a8e4377b47998f49d5da7db51656f9a7 to your computer and use it in GitHub Desktop.
Migrate from Github to Bitbucket
This file contains 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
# Modified version of https://gist.github.com/schwa/5907109 | |
import os | |
import subprocess | |
import glob | |
from github import Github # pip install PyGithub | |
from pybitbucket.bitbucket import Client # pip install pybitbucket | |
from pybitbucket.auth import BasicAuthenticator | |
from pybitbucket.repository import Repository, RepositoryPayload, RepositoryType, RepositoryForkPolicy | |
GH_USERNAME = 'xyz@abc.com' | |
GH_PASSWORD = '1234' | |
BB_USERNAME = 'xyz' | |
BB_EMAIL = 'xyz@abc.com' | |
BB_PASSWORD = '5678' | |
## Set up | |
d = os.path.expanduser('~/private-repos') | |
if not os.path.exists(d): | |
os.makedirs(d) | |
os.chdir(d) | |
# Get list of all your github private repos. | |
# By default we filter out public repos and repos where you are not the owner. You can change this. | |
g = Github(GH_USERNAME, GH_PASSWORD) | |
g = Github(GH_PASSWORD) | |
theRepos = [] | |
for repo in g.get_user().get_repos(): | |
if not repo.private: | |
continue | |
if repo.owner.name != g.get_user().name: | |
continue | |
theRepos.append((repo.name, repo.ssh_url)) | |
### Clone ALL THE THIGNS | |
for theName, theCloneURL in theRepos: | |
print theName | |
subprocess.check_call(['git', 'clone', theCloneURL, theName]) | |
os.chdir(theName) | |
branches = g.get_user().get_repo(theName).get_branches() | |
for branch in branches: | |
subprocess.check_call(['git', 'checkout', branch.name]) | |
subprocess.check_call(['git', 'pull']) | |
subprocess.check_call(['git', 'checkout', 'master']) | |
subprocess.check_call(['git', 'fetch', '--all']) | |
os.chdir("../") | |
### Go through all the cloned directories, create a bitbucket repo and then push them | |
### If the repo already exists on github this will skip it. | |
bb = Client(BasicAuthenticator(BB_USERNAME, BB_PASSWORD, BB_EMAIL)) | |
for name in glob.iglob('*'): | |
print name | |
os.chdir(name) | |
payload = RepositoryPayload().add_scm(RepositoryType.GIT) \ | |
.add_is_private(True) \ | |
.add_fork_policy(RepositoryForkPolicy.NO_FORKS) \ | |
.add_name(name) | |
result = Repository.create(payload=payload, client=bb) | |
if not result: | |
print 'Could not create repo, skipping' | |
continue | |
push_url = 'git@bitbucket.org:{owner}/{name}.git'.format(owner=result.data.get('owner').get('username'), name=result.data.get('name')) | |
subprocess.check_call(['git', 'remote', 'set-url', 'origin', push_url]) | |
subprocess.check_call(['git', 'push', '--all']) | |
subprocess.check_call(['git', 'push', '--tags']) | |
os.chdir("../") | |
os.chdir(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment