Skip to content

Instantly share code, notes, and snippets.

@xcsrz
Last active March 4, 2020 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcsrz/6aa1fb3f42fa82c9f41b22efe847f273 to your computer and use it in GitHub Desktop.
Save xcsrz/6aa1fb3f42fa82c9f41b22efe847f273 to your computer and use it in GitHub Desktop.
With atlassian falling apart and abandoning mercurial (the only reason to use bitbucket unless you're one of those anti-microsoft holdouts) you may find yourself looking to collect all your bitbucket repos to take elsewhere ... like ... github. This script is the first step. This is the first draft, only tested on mac running python 3.7 but shou…
from requests import get
from os import environ, getcwd, chdir, mkdir, system
from json import dumps
basedir = getcwd() + '/repos'
mkdir(basedir)
username=environ['BITBUCKET_USER']
password=environ['BITBUCKET_PASS']
team=environ['BITBUCKET_TEAM']
def grab_repos(repos):
for repo in repos:
print(">> grabbing >>", repo['slug'])
chdir(basedir)
if repo['scm'] == 'hg':
print("cloning %s via hg" % repo['slug'])
system('hg clone ssh://hg@bitbucket.org/%s/%s' % (team, repo['slug']))
elif repo['scm'] == 'git':
print("cloning %s via git" % repo['slug'])
system('git clone --mirror git@bitbucket.org:%s/%s' % (team, repo['slug']))
else:
raise Exception("Unable to handle: " . dumps(repo))
repolist = []
next_page = 'https://api.bitbucket.org/2.0/teams/%s/repositories' % team
while next_page != None:
print(">> loading repo list from", next_page)
results = get(next_page, auth=(username, password)).json()
next_page = results.get('next', None)
grab_repos(results['values'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment