Skip to content

Instantly share code, notes, and snippets.

@tanhaa
Created October 5, 2019 19:01
Show Gist options
  • Save tanhaa/844ae9dc80262d6e750eb10a391c0aee to your computer and use it in GitHub Desktop.
Save tanhaa/844ae9dc80262d6e750eb10a391c0aee to your computer and use it in GitHub Desktop.
Python Script to get a list of repos from bitbucket/stash
import stashy
from datetime import datetime
# Fill in below ----
stash_url = ""
stash_user = ""
stash_password = ""
# ------------------
stash = stashy.connect(stash_url, stash_user, stash_password)
projects = stash.projects.list()
repo_list = []
for repo in stash.repos.all():
project_key = repo['project']['key']
project_name = repo['project']['name']
project_description = ""
if 'description' in repo['project']:
project_description = repo['project']['description']
repo_name = repo['name']
repo_slug = repo['slug']
repo_uri = repo['links']['self'][0]['href']
latest_commit_ts = 0
try:
for commit in stash.projects[project_key].repos[repo_slug].commits('master'):
ts = int(commit['authorTimestamp'])/1000
if ts > latest_commit_ts:
latest_commit_ts = int(commit['authorTimestamp'])/1000
break
except Exception as e:
pass
latest_commit_date = datetime.utcfromtimestamp(latest_commit_ts).strftime('%Y-%m-%d %H:%M:%S')
repository = project_key + "/" + repo_slug
repo_list.append({'project_name': project_name, 'repository': repository, 'last_push': latest_commit_date, 'description': project_description, 'uri': repo_uri})
print(repo_list)
import csv
keys = repo_list[0].keys()
with open('./bitbucket_repos.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(repo_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment