Skip to content

Instantly share code, notes, and snippets.

@MartinHarding
Last active September 28, 2017 13:00
Show Gist options
  • Save MartinHarding/e52a122d387bfcf691a6251663428f1a to your computer and use it in GitHub Desktop.
Save MartinHarding/e52a122d387bfcf691a6251663428f1a to your computer and use it in GitHub Desktop.
Clone repositories from a given organization into working directory
""" Clone repositories from a given organization into working directory """
import os
import re
import subprocess
import requests
token = os.environ.get('GITHUB_TOKEN', False)
if not token:
print('GITHUB_TOKEN envar not set, enter one to clone private repositories')
print('Tokens can be created at https://github.com/settings/tokens')
token = input('Github Personal Access Token (optional): ')
if token:
headers = {"Authorization": "token {}".format(token)}
else:
headers = {}
host = 'https://api.github.com'
org = input('Organization name: ')
uri = '{host}/orgs/{org}/repos?per_page=200'.format(host=host, org=org)
response = requests.get(uri, headers=headers)
exclude_patterns = ['deprecated.*']
clone_urls = []
if response.ok and response.status_code == 200:
repos = response.json()
repos = sorted(repos, key=lambda k: k['name'].lower())
for repo in repos:
clone_url = repo.get('clone_url')
name = repo.get('name')
repo_path = '{}/{}'.format(os.getcwd(), name)
if os.path.isdir(repo_path):
continue
matched = False
for exclude_pattern in exclude_patterns:
regex = re.compile(exclude_pattern, re.IGNORECASE)
if re.match(regex, name):
matched = True
if matched:
continue
clone_urls.append(clone_url)
else:
raise Exception(response.text)
print('\n'.join(clone_urls))
confirm = input('Clone these repositories into current directory? [y/n]: ')
if confirm in ['y', 'Y']:
for clone_url in clone_urls:
if token:
clone_url = clone_url.replace('https://', 'https://{}@'.format(token))
command = ['git', 'clone', clone_url, '-j8']
subprocess.call(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment