Skip to content

Instantly share code, notes, and snippets.

@rkennesson
Created May 29, 2017 04:30
Show Gist options
  • Save rkennesson/51f286c1b6b3ee0556229854d75c49c7 to your computer and use it in GitHub Desktop.
Save rkennesson/51f286c1b6b3ee0556229854d75c49c7 to your computer and use it in GitHub Desktop.
Migrate all your private github repos to bitbucket because you're CHEAP.
__author__ = 'schwa'
import os
import subprocess
import glob
from github import Github # pip install PyGithub
from bitbucket.bitbucket import Bitbucket # pip install --user bitbucket-api
GH_USERNAME = '[email protected]'
GH_PASSWORD = '1234'
BB_USERNAME = '[email protected]'
BB_PASSWORD = '5678'
## Set up
d = os.path.expanduser('~/Desktop/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)
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.clone_url))
### CLOWN ALL THE THIGNS
for theName, theCloneURL in theRepos:
print theName
subprocess.check_call(['git', 'clone', theCloneURL, theName])
### 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 = Bitbucket(BB_USERNAME, BB_PASSWORD, 'private_slug')
for name in glob.iglob('*'):
print name
result, r = bb.repository.create(name, scm='git', private=True)
if not result:
print 'Could not create repo, skipping'
continue
push_url = '[email protected]:{owner}/{name}.git'.format(owner = r['owner'], name = r['name'])
os.chdir(name)
subprocess.check_call(['git', 'remote', 'set-url', 'origin', push_url])
subprocess.check_call(['git', 'push'])
os.chdir(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment