Created
September 28, 2018 14:48
-
-
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
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
#!/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