Skip to content

Instantly share code, notes, and snippets.

@archatas
Last active March 29, 2025 23:34
Show Gist options
  • Save archatas/7ddfc5f04836e39d555fd134ebfa519d to your computer and use it in GitHub Desktop.
Save archatas/7ddfc5f04836e39d555fd134ebfa519d to your computer and use it in GitHub Desktop.
import requests
import base64
# Authentication - prompt for sensitive information
username = input("Enter your Bitbucket username: ")
app_password = input("Enter your Bitbucket app password (Generate this in Bitbucket settings: https://bitbucket.org/account/settings/app-passwords/): ")
auth_string = f"{username}:{app_password}"
encoded_auth = base64.b64encode(auth_string.encode()).decode()
headers = {
"Authorization": f"Basic {encoded_auth}",
"Content-Type": "application/json"
}
# Get list of repositories - prompt for workspace
workspace = input("Enter your Bitbucket workspace (might be the same as username): ")
url = f"https://api.bitbucket.org/2.0/repositories/{workspace}"
repos = []
while url:
response = requests.get(url, headers=headers)
data = response.json()
repos.extend(data["values"])
url = data.get("next")
# Calculate total size
total_size = 0
for repo in repos:
repo_slug = repo["slug"]
size_url = f"https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}"
size_response = requests.get(size_url, headers=headers)
size_data = size_response.json()
# Size is in bytes
if "size" in size_data:
total_size += size_data["size"]
print(f"Repository: {repo_slug}, Size: {size_data.get('size', 'Unknown')} bytes")
# Convert to more readable format
def format_size(bytes):
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes < 1024 or unit == 'TB':
return f"{bytes:.2f} {unit}"
bytes /= 1024
print(f"\nTotal size of all repositories: {format_size(total_size)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment