Skip to content

Instantly share code, notes, and snippets.

@mhl
Created September 28, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhl/09ebc41e2e03092e8bbbd6d933c0afff to your computer and use it in GitHub Desktop.
Save mhl/09ebc41e2e03092e8bbbd6d933c0afff to your computer and use it in GitHub Desktop.
A script for updating or cloning all repositories in a GitHub organization in the current directory
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
import argparse
import json
from os.path import exists, expanduser, join
from os import getcwd
import requests
from subprocess import check_call, call
parser = argparse.ArgumentParser(description='Clone or update all repos in a GitHub organization')
parser.add_argument('org', metavar='ORGANIZATION')
args = parser.parse_args()
with open(expanduser('~/.github-oauth-token.json')) as f:
github_token = json.load(f)['token']
current_url = 'https://api.github.com/orgs/{.org}/repos?per_page=200'.format(args)
root_dir = getcwd()
while current_url:
r = requests.get(
current_url,
headers = {'Authorization': 'token {0}'.format(github_token)}
)
r.raise_for_status()
for repo_data in r.json():
name = repo_data['name']
repo_directory = join(root_dir, name)
if exists(repo_directory):
print("Trying to update", name)
check_call(['git', 'remote', 'update'], cwd=repo_directory)
if 0 == call(['git', 'rev-parse', '--verify', '--quiet', '@{u}'], cwd=repo_directory):
call(['git', 'merge', '--ff-only', '@{u}'], cwd=repo_directory)
else:
print("... so skipping update")
else:
print("Cloning", name)
check_call(['git', 'clone', repo_data['ssh_url']], cwd=root_dir)
if 'next' not in r.links:
break
current_url = r.links['next']['url']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment