Created
August 30, 2018 16:23
-
-
Save atucom/1942ef2fb088657e99f372be509b45da to your computer and use it in GitHub Desktop.
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 | |
# @atucom 2018 | |
# This script, given credentials and a host, will clone all the git repos | |
# from a bitbucket server into appropriately named project folders locally. | |
# Just update the username, password, and host, and let it go. | |
import stashy | |
import os | |
import subprocess | |
username = 'jimbodev' | |
password = 'totallysecurepassword' | |
# Here we are using the bitbucket server API typically on TCP7990 | |
host = 'http://git.example.com:7990/' | |
stash = stashy.connect(host,username,password) | |
def getRepoCloneLinks(): | |
""" Returns all HTTP clone links along with associated project name | |
""" | |
cloneLinks = {} | |
projects = stash.projects.list() | |
# Returns nested dicts you have to drill through | |
projectKeys = [project['key'] for project in projects] | |
for projectKey in projectKeys: | |
repos = stash.projects[projectKey].repos.list() | |
for repo in repos: | |
for clones in repo['links']['clone']: | |
if clones['href'].startswith('http'): | |
# Returns: | |
# https://git.example.com/project/MyRepo.git, project | |
cloneLinks[clones['href']] = projectKey | |
return cloneLinks | |
for link, project in getRepoCloneLinks().items(): | |
try: | |
os.mkdir(project) | |
except FileExistsError as e: | |
pass | |
print('Moving into {} to clone {}'.format(project, link)) | |
os.chdir(project) | |
subprocess.run('git -c http.sslVerify=false clone ' + link ,shell=True) | |
os.chdir('..') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment