Last active
December 8, 2021 05:30
-
-
Save onacit/95d9496d88cca9a32abeadab288df3c2 to your computer and use it in GitHub Desktop.
Lists old branches from BitBucket
This file contains hidden or 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
import datetime | |
from datetime import timedelta | |
import getpass | |
import json | |
try: | |
import requests | |
except ImportError: | |
sys.exit("run pip install requests") | |
from requests.auth import HTTPBasicAuth | |
import sys | |
import time | |
import urllib3 | |
if len(sys.argv) < 3: | |
print("required arguments: <baseurl> <weeks> (keys...)") | |
sys.exit() | |
baseurl = sys.argv[1] | |
weeks = int(sys.argv[2]) | |
if weeks < 0: | |
print('wrong value for weeks: ' + str(weeks)) | |
sys.exit() | |
threshold = (int(time.time()) - (weeks * 604800)) * 1000 | |
username = getpass.getpass("username: ") | |
password = getpass.getpass("password: ") | |
api_path = '/rest/api/1.0' | |
project_keys = list() | |
for i in range(3, len(sys.argv)): | |
project_keys.append(sys.argv[i]) | |
repository_slugs = list() | |
branch_ids = dict() | |
author_names = list() | |
def get_projects_url(): | |
return baseurl + api_path + '/projects' | |
def get_repositories_url(project_key): | |
return get_projects_url() + '/' + project_key + '/repos' | |
def get_repository_url(project_key, repository_slug): | |
return get_projects_url() + '/' + project_key + '/repos/' + repository_slug | |
def get_branches_url(project_key, repository_slug): | |
return get_projects_url() + '/' + project_key + '/repos/' + repository_slug + "/branches" | |
def get_branch_url(project_key, repository_slug, branch_id): | |
return get_branches_url(project_key, repository_slug) + '/' + branch_id | |
def get_commits_url(project_key, repository_slug): | |
return get_repository_url(project_key, repository_slug) + '/commits' | |
def get_commit_url(project_key, repository_slug, commit_id): | |
return get_commits_url(project_key, repository_slug) + '/' + commit_id | |
def list_project_keys(): | |
projects_url = get_projects_url() | |
start = 0 | |
limit = None | |
while True: | |
url = projects_url + '?start=' + str(start) + ('&limit=' + str(limit) if limit is not None else '') | |
r = requests.get(url, auth=HTTPBasicAuth(username, password)) | |
r.raise_for_status() | |
j = r.json() | |
for value in j['values']: | |
key = value['key'] | |
if key is not None: | |
project_keys.append(key) | |
if j['isLastPage']: | |
break | |
if limit is None: | |
limit = j['size'] | |
start = j['nextPageStart'] | |
def list_repository_slugs(project_key): | |
repository_slugs.clear() | |
repositories_url = get_repositories_url(project_key) | |
start = 0 | |
limit = None | |
while True: | |
url = repositories_url + '?start=' + str(start) + ('&limit=' + str(limit) if limit is not None else '') | |
r = requests.get(url, auth=HTTPBasicAuth(username, password)) | |
if r.status_code != 200: | |
print('unable to read repositories for ' + project_key + '/' + status_code) | |
break | |
j = r.json() | |
for value in j['values']: | |
slug = value['slug'] | |
repository_slugs.append(slug) | |
if j['isLastPage']: | |
break | |
if limit is None: | |
limit = j['size'] | |
start = j['nextPageStart'] | |
def list_branch_ids(project_key, repository_slug): | |
branch_ids.clear() | |
branches_url = get_branches_url(project_key, repository_slug) | |
start = 0 | |
limit = None | |
while True: | |
url = branches_url + '?start=' + str(start) + ('&limit=' + str(limit) if limit is not None else '') | |
r = requests.get(url, auth=HTTPBasicAuth(username, password)) | |
if r.status_code != 200: | |
print('unable to read branches for ' + project_key + '/' + repository_slug) | |
break | |
j = r.json() | |
for value in j['values']: | |
display_id = value['displayId'] | |
if (display_id == 'master') or (display_id == 'develop'): | |
continue | |
latest_commit = value['latestCommit'] | |
if latest_commit is None: | |
continue | |
author_timestamp = get_author_timestamp(project_key, repository_slug, latest_commit) | |
if (author_timestamp is None) or (author_timestamp > threshold): | |
continue | |
branch_ids[display_id] = (author_timestamp / 1000) | |
if j['isLastPage']: | |
break | |
if limit is None: | |
limit = j['size'] | |
start = j['nextPageStart'] | |
return True | |
def get_author_timestamp(project_key, repository_slug, commit_id): | |
commit_url = get_commit_url(project_key, repository_slug, commit_id) | |
url = commit_url | |
r = requests.get(url, auth=HTTPBasicAuth(username, password)) | |
if r.status_code != 200: | |
print('unable to read commit for ' | |
+ project_key | |
+ '/' + repository_slug | |
+ '/' + commit_id | |
+ '/' + r.status_code) | |
return True | |
j = r.json() | |
return j['authorTimestamp'] | |
def list_author_names(project_key, repository_slug): | |
author_names.clear() | |
commits_url = get_commits_url(project_key, repository_slug) | |
url = commits_url + "?limit=128" | |
r = requests.get(url, auth=HTTPBasicAuth(username, password)) | |
if r.status_code != 200: | |
print('unable to read commits for ' + project_key + '/' + repository_slug) | |
return | |
j = r.json() | |
for value in j['values']: | |
author_name = value['author']['name'] | |
if author_names.count(author_name) == 0: | |
author_names.append(author_name) | |
if len(author_names) > 5: | |
break | |
if len(project_keys) == 0: | |
list_project_keys() | |
for project_key in project_keys: | |
repository_slugs.clear() | |
list_repository_slugs(project_key) | |
for repository_slug in repository_slugs: | |
branch_ids.clear() | |
list_branch_ids(project_key, repository_slug) | |
for branch_id in branch_ids.keys(): | |
author_names.clear() | |
list_author_names(project_key, repository_slug) | |
print(project_key + ' / ' + repository_slug + ' / ' | |
+ branch_id + ' / ' + datetime.datetime.fromtimestamp(branch_ids[branch_id]).strftime('%Y-%m-%d') | |
+ ' / ' + str(author_names)) | |
Author
onacit
commented
Jun 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment