Created
June 17, 2016 22:44
-
-
Save catherinedevlin/4ae5583e59d03fd5da05b3e9a7413e46 to your computer and use it in GitHub Desktop.
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
""" | |
Clones all of a GitHub organization's public repositories. | |
Includes a ``.repo-metadata.json`` in each repository with GitHub's | |
data on the repo. | |
""" | |
import itertools | |
import json | |
import os | |
import os.path | |
import subprocess | |
import click | |
import requests | |
def _all_repos(org_name): | |
base_url = "https://api.github.com/orgs/{org_name}/repos?page={page_no}" | |
for page_no in itertools.count(1): | |
click.echo('Page {page_no}'.format(page_no=page_no)) | |
url = base_url.format(org_name=org_name, page_no=page_no) | |
resp = requests.get(url) | |
if not resp.json(): | |
raise StopIteration | |
for repo in resp.json(): | |
if not repo['private']: | |
yield repo | |
@click.command() | |
@click.argument('org_name') | |
@click.option('--dest', help='Destination directory') | |
def clone(org_name, dest): | |
if not dest: | |
dest = os.path.join('..', org_name) | |
try: | |
os.mkdir(dest) | |
except FileExistsError: | |
pass | |
for repo in _all_repos(org_name): | |
repo_dest = os.path.join(dest, repo['name']) | |
click.echo(repo['name']) | |
subprocess.run(['git', 'clone', repo['clone_url'], repo_dest]) | |
metadata_dest = os.path.join(repo_dest, '.repo-metadata.json') | |
with open(metadata_dest, 'w') as outfile: | |
json.dump(repo, outfile, indent=2) | |
if __name__ == '__main__': | |
clone() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment