Skip to content

Instantly share code, notes, and snippets.

@jimbo8098
Last active November 17, 2022 21:09
Show Gist options
  • Save jimbo8098/edec84e2babfddd4ddb86c405a313502 to your computer and use it in GitHub Desktop.
Save jimbo8098/edec84e2babfddd4ddb86c405a313502 to your computer and use it in GitHub Desktop.
Get All BitBucket Repositories (OAuth 2)
# pip install requests requests-oauthlib
# Quick run with:
# docker run -it --rm -v "${PWD}:/src" -v "C:\Projects\:/repos" -w /src python:3.11 bash -c "pip install requests requests-oauthlib && python ./update-repos.py"
#
# 1. Create a new OAuth consumer in BitBucket
# 2. Run the script. Either:
# a. A browser window will open
# b. An authorization URL will be shown
# In both cases the script will await your input
# 3. Log into BitBucket if necessary
# 4. Copy/Paste the 127.0.0.1 address from your browser's address bar directly into the console
# then press enter
# 5. All of the private repositories will be downloaded in parallel
from requests_oauthlib import OAuth2Session
import webbrowser
import json
import subprocess
from urllib.parse import urlparse
projectRoot = "/repos"
organisation = "organisation"
client_id = r'gZhtmB....'
client_secret = r'Wrxf2BRH....'
redirect_uri = 'https://127.0.0.1'
oauth = OAuth2Session(client_id)
authorization_url, state = oauth.authorization_url('https://bitbucket.org/site/oauth2/authorize')
webbrowser.open(authorization_url)
print("If a browser window didn't open, manually navigate to the following URL:")
print(authorization_url)
print()
redirect_response = input('Paste the full redirect URL here: ')
token = oauth.fetch_token(
'https://bitbucket.org/site/oauth2/access_token',
client_secret=client_secret,
authorization_response=redirect_response
)
reposToGet = []
def handleRepositoriesResponsePage(jResponse):
for repo in jResponse['values']:
for linkName, linkValue in repo['links'].items():
if linkName == "clone":
for cloneMethod in linkValue:
if cloneMethod['name'] == "ssh":
reposToGet.append(cloneMethod['href'])
if 'next' in jResponse:
return jResponse['next']
else:
return False
response = oauth.get(f'https://api.bitbucket.org/2.0/repositories/{organisation}')
jResponse = json.loads(response.content)
next = handleRepositoriesResponsePage(jResponse)
while next:
url = urlparse(next)
response = oauth.get(next,params={"page":url.query.split('=')[1]})
jResponse = json.loads(response.content)
next = handleRepositoriesResponsePage(jResponse)
for repo in reposToGet:
subprocess.Popen(["git","clone",repo],cwd=projectRoot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment