Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Last active March 28, 2020 18:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hrules6872/af8655427e6ec7741a82516713d99ebf to your computer and use it in GitHub Desktop.
Save hrules6872/af8655427e6ec7741a82516713d99ebf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#################
## CREDENTIALS ##
#################
GH_PERSONAL_ACCESS_TOKEN = ''
BB_USERNAME = ''
BB_APP_PASSWORD = ''
BB_OAUTH_KEY = ''
BB_SECRET_KEY = ''
#################
###################
## CONFIGURATION ##
###################
GH_REPO_PUBLIC_ALLOWED = False
GH_REPO_PRIVATE_ALLOWED = True
GH_REPO_ARCHIVED_ALLOWED = False
GH_REPO_FORK_ALLOWED = False
###################
import os
import subprocess
if not 'SUDO_UID' in os.environ.keys():
print 'Super user privileges required. Use:'
print '$ sudo %s' % (os.path.basename(__file__))
quit()
try:
from github import Github
except ImportError:
print 'PyGithub: not found.'
print 'Run the following command in your terminal:'
print '$ pip install PyGithub'
quit()
try:
from bitbucket.bitbucket import Bitbucket
except ImportError:
print 'bitbucket-api: not found.'
print 'Run the following command in your terminal:'
print '$ pip install --user bitbucket-api'
quit()
if not GH_PERSONAL_ACCESS_TOKEN.strip():
print 'GH_PERSONAL_ACCESS_TOKEN must not be empty. More information here: https://github.com/settings/tokens'
if not BB_USERNAME.strip():
print 'BB_USERNAME must not be empty'
if not BB_APP_PASSWORD.strip():
print 'BB_APP_PASSWORD must not be empty. More information here: https://bitbucket.org/account -> Bitbucket settings -> App passwords'
if not BB_OAUTH_KEY.strip() or not BB_SECRET_KEY.strip():
print 'BB_OAUTH_KEY and/or BB_SECRET_KEY must not be empty. More information here: https://bitbucket.org/account -> Bitbucket settings -> OAuth'
print 'Initializing...'
temporal_folder = os.path.expanduser('./TemporalMigration')
if not os.path.exists(temporal_folder):
os.makedirs(temporal_folder)
os.chdir(temporal_folder)
print 'Initializing GitHub...'
gh = Github(GH_PERSONAL_ACCESS_TOKEN)
print 'Initializing BitBucket...'
bb = Bitbucket(BB_USERNAME)
bb.authorize(BB_OAUTH_KEY, BB_SECRET_KEY, 'http://localhost/')
print "- Open this in your browser: " + bb.url('AUTHENTICATE', token=bb.access_token)
oauth_verifier = raw_input('- Enter verifier from url [oauth_verifier]')
result, r = bb.verify(oauth_verifier)
if not result:
print r
quit()
BB_OAUTH_ACCESS_TOKEN = bb.access_token
BB_OAUTH_ACCESS_TOKEN_SECRET = bb.access_token_secret
bb.authorize(BB_OAUTH_KEY, BB_SECRET_KEY, 'http://localhost/', BB_OAUTH_ACCESS_TOKEN, BB_OAUTH_ACCESS_TOKEN_SECRET)
gh_user = gh.get_user()
print 'Getting %s GitHub repositories... (This process may take some time)' % (gh_user.login)
gh_repos = []
for gh_repo in gh.get_user().get_repos():
if gh_repo.owner.name != gh.get_user().name:
continue
if gh_repo.private and GH_REPO_PRIVATE_ALLOWED == False:
continue
if gh_repo.archived and GH_REPO_ARCHIVED_ALLOWED == False:
continue
if gh_repo.fork and GH_REPO_FORK_ALLOWED == False:
continue
if not gh_repo.private and GH_REPO_PUBLIC_ALLOWED == False:
continue
gh_repos.append((gh_repo.name, gh_repo.private))
print 'Cloning %s GitHub repositories...' % (len(gh_repos))
for gh_repo_name, gh_repo_private in gh_repos:
if os.path.exists(gh_repo_name):
print "- %s already cloned, skipping" % (gh_repo_name)
continue
gh_is_private = '(private)' if gh_repo_private else ''
print '- %s %s' % (gh_repo_name, gh_is_private)
gh_clone_url = "https://%s@github.com/%s/%s.git" % (GH_PERSONAL_ACCESS_TOKEN, gh_user.login, gh_repo_name)
try:
command = ['git', 'clone', '--mirror', gh_clone_url, gh_repo_name]
output = subprocess.check_output(command, stderr = subprocess.STDOUT).decode()
except subprocess.CalledProcessError as e:
print 'Aborting...'
print e.output.decode()
quit()
print 'Pushing %s repositories to BitBucket...' % (len(gh_repos))
for gh_repo_name, gh_repo_private in gh_repos:
result, r = bb.repository.create(gh_repo_name, scm='git', private=gh_repo_private)
if not result:
print '- Could not create repo %s, skipping' % (gh_repo_name)
continue
bb_push_url = "https://%s:%s@bitbucket.org/%s/%s.git" % (BB_USERNAME, BB_APP_PASSWORD, BB_USERNAME, gh_repo_name)
bb_push_url_clean_credentials = 'git@bitbucket.org:%s/%s.git' % (BB_USERNAME, gh_repo_name)
gh_is_private = '(private)' if gh_repo_private else ''
print '- %s %s to: %s' % (gh_repo_name, gh_is_private, bb_push_url_clean_credentials)
try:
os.chdir(gh_repo_name)
output = subprocess.check_output(['git', 'remote', 'set-url', 'origin', bb_push_url], stderr = subprocess.STDOUT).decode()
output = subprocess.check_output(['git', 'push', '--mirror'], stderr = subprocess.STDOUT).decode()
## clean .git/config file
output = subprocess.check_output(['git', 'remote', 'set-url', 'origin', bb_push_url_clean_credentials], stderr = subprocess.STDOUT).decode()
except subprocess.CalledProcessError as e:
print e.output.decode()
quit()
os.chdir("..")
print 'All your repositories were cloned in %s folder' % (temporal_folder)
print 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment