Skip to content

Instantly share code, notes, and snippets.

@FreddieLindsey
Last active May 13, 2016 09:39
Show Gist options
  • Save FreddieLindsey/8ca16203410c2d314b1ec682b02b4032 to your computer and use it in GitHub Desktop.
Save FreddieLindsey/8ca16203410c2d314b1ec682b02b4032 to your computer and use it in GitHub Desktop.
Import from Bitbucket
#!/usr/bin/env python
import os, sys
import json, pprint
import subprocess
# Prerequisites:
# - GitHub Personal Access Token to be set in env to GITHUB_PA_TOKEN
# - BitBucket username in BITBUCKET_USERNAME
# - BitBucket password in BITBUCKET_PASSWORD
config = {
'repos': None,
'github': None,
'bitbucket': {
'user': None,
'pass': None
}
}
def get_token(env_variable):
if env_variable not in os.environ:
print(env_variable + ' has not been set. Exiting!')
exit(-1)
else:
return os.environ[env_variable]
def get_repos():
cmd = 'curl -H "Authorization: token ' + config['github'] + \
'" https://api.github.com/user/repos'
print('Getting repos with:\t' + cmd)
proc = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE)
result = proc.stdout.read()
return json.loads(result)
def github_request(url, method = 'GET'):
cmd = 'curl -X ' + method + ' -H "Authorization: token ' + \
config['github'] + '" https://api.github.com' + url
proc = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE)
result = proc.stdout.read()
return json.loads(result)
def does_repo_exist(repo):
for i in config['repos']:
if i[u'name'] == unicode(repo):
return True
return False
def move_to_github(repo):
def main():
global config
if len(sys.argv) < 2:
print('You need to give a json file for the program to work with')
exit(-1)
with open(sys.argv[1], 'r') as f:
repositories = json.loads(f.read())
config['github'] = get_token('GITHUB_PA_TOKEN')
config['bitbucket']['user'] = get_token('BITBUCKET_USERNAME')
config['bitbucket']['pass'] = get_token('BITBUCKET_PASSWORD')
config['repos'] = github_request('/user/repos')
contains_bundles = True
while contains_bundles:
contains_bundles = False
for i in repositories:
if u'bundle' in i.keys():
repositories += i[u'bundle']
repositories.remove(i)
contains_bundles = True
for i in repositories:
move_to_github(i)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment