Last active
July 1, 2020 18:56
-
-
Save erikvanzijst/2170ab3e3ac2b8160b5b7ffd9a322268 to your computer and use it in GitHub Desktop.
Clone all Bitbucket Mercurial repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import subprocess | |
# install deps: | |
# $ pip install requests mercurial | |
import requests | |
# Create an OAuth Consumer with Account and Repository Read permission: | |
# https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-k', '--key', type=str, required=True, help="the OAuth consumer key (e.g. pu5HwV2FKaU7JTkqRW)") | |
parser.add_argument('-s', '--secret', type=str, required=True, | |
help="the OAuth consumer secret (e.g. gAgtajY8RjBZc6rpxNbFuNhLj5x8JJnE)") | |
parser.add_argument("dest_dir", default=os.getcwd(), type=str, nargs='?', | |
help="directory to clone repositories under") | |
args = parser.parse_args() | |
os.path.exists(args.dest_dir) or os.makedirs(args.dest_dir) | |
with requests.Session() as session: | |
resp = session.post("https://bitbucket.org/site/oauth2/access_token", data={"grant_type": "client_credentials"}, | |
auth=(args.key, args.secret)) | |
resp.raise_for_status() | |
token = resp.json()['access_token'] | |
session.headers["Authorization"] = "Bearer " + token | |
url = ("https://api.bitbucket.org/2.0/repositories/" + | |
session.get("https://api.bitbucket.org/2.0/user").json()['username'] + | |
"?q=scm=%22hg%22&fields=values.links,values.slug,next,values.full_name") | |
while url: | |
resp = session.get(url) | |
resp.raise_for_status() | |
data = resp.json() | |
for repo in data['values']: | |
print("\nCloning " + repo['links']['html']['href']) | |
subprocess.call(['hg', 'clone', 'https://x-token-auth:%[email protected]/%s' % (token, repo['full_name']), | |
os.path.join(args.dest_dir, repo['slug'])]) | |
url = data.get("next") |
Not anymore! :-D
😢
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you're reeeaaally missing a shebang at the top of the file >_>