Skip to content

Instantly share code, notes, and snippets.

@cassc
Created October 5, 2021 01:39
Show Gist options
  • Save cassc/7306aed42c94b28855d037cc35b65b6d to your computer and use it in GitHub Desktop.
Save cassc/7306aed42c94b28855d037cc35b65b6d to your computer and use it in GitHub Desktop.
Clone all repositories from Bitbucket
# Shallow clone all repos from bitbucket.
# Usage:
# python3 bitbucket.py -u [USERNAME] -p [BITbucket_APP_TOKEN] -o [OUTPUT_DIR]
# Requirements:
# 1. Setup app password in Bitbucket
# 2. Setup local ssh keys
# 3. Only git repositories are supported
from requests.auth import HTTPBasicAuth
import requests
import os
roles = 'owner member contributor admin'.split()
pagelen = 100
repos = set()
# recursively load all repos for this role
def load_repos(url, user, pwd):
rs = requests.get(url, auth=HTTPBasicAuth(user, pwd)).json()
for r in rs['values']:
name = r['name']
links = r['links']['clone']
href = next(link['href'] for link in links if link['name'] == 'ssh')
print(f'Found repo {name} {href}')
repos.add((name, href))
if 'next' in rs:
load_repos(rs['next'], user, pwd)
# load all repos and save in `repos`
def load_all_repos(user, pwd):
for role in roles:
url = f'https://api.bitbucket.org/2.0/repositories?pagelen=100&role={role}'
load_repos(url, user, pwd)
def download_repo(output, name, href):
print(f'Cloning {name} {href}')
os.system(f'git clone --depth=1 {href} "{output}/{name}"')
if __name__ == "__main__":
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--user", help="Bitbucket username", type=str, required=True)
ap.add_argument("-p", "--password", help="Bitbucket app password", type=str, required=True)
ap.add_argument("-o", "--output", help="Output directory", type=str, required=True)
args = vars(ap.parse_args())
user = args['user']
pwd = args['password']
output = args['output']
load_all_repos(user, pwd)
for repo in repos:
download_repo(output, *repo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment