Skip to content

Instantly share code, notes, and snippets.

@cursey
Last active December 11, 2018 00:32
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 cursey/47e6a977295446ac257f4b3fe2bb85d1 to your computer and use it in GitHub Desktop.
Save cursey/47e6a977295446ac257f4b3fe2bb85d1 to your computer and use it in GitHub Desktop.
Clone all repositories from a BitBucket account
from subprocess import run
from json import loads
import click
@click.command()
@click.option("--username", required=True, prompt="Username", help="The username you login with.")
@click.option("--app_password", required=True, prompt="App password", hide_input=True, confirmation_prompt=True,
help="The app password you've created to login with.")
@click.option("--team", required=True, prompt="Team", help="The team or username you want to clone all the repos of.")
def clone_all(username, app_password, team):
next_page_link = f"https://api.bitbucket.org/2.0/repositories/{team}?pagelen=10&role=member"
page = 1
while next_page_link:
print(f"Fetching page {page} ({next_page_link})")
response = run(
f'curl -q --silent --request GET --user "{username}:{app_password}" "{next_page_link}"', capture_output=True, encoding="utf-8").stdout
json = loads(response)
next_page_link = json.get("next", None)
page += 1
values = json["values"]
for repo in values:
name = repo["name"]
print(f"Cloning {name}...")
links = repo["links"]
clones = links["clone"]
url = clones[0]["href"].replace(
username, f"{username}:{app_password}", 1)
if repo["scm"] == "git":
run(f"git clone {url}")
elif repo["scm"] == "hg":
run(f"hg clone {url}")
if __name__ == "__main__":
clone_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment