Skip to content

Instantly share code, notes, and snippets.

@e0ne
Created March 3, 2019 21:01
Show Gist options
  • Save e0ne/1ab17bcc62cd4f8f698efdf8b3840d19 to your computer and use it in GitHub Desktop.
Save e0ne/1ab17bcc62cd4f8f698efdf8b3840d19 to your computer and use it in GitHub Desktop.
import os
import click
import git
from github import Github
GITHUB_API_KEY = os.environ['GITHUB_API_KEY']
BASE_DIR = os.environ['SRC_BASE_DIR']
def get_all_repos():
repos_to_clone = []
g = Github(GITHUB_API_KEY)
for repo in g.get_user().get_repos():
git_url = BASE_DIR
# TODO(e0ne): cache organization details to make this function faster
if not repo.organization and repo.fork:
git_url += 'forks'
elif repo.organization:
git_url += repo.organization.login
git_url += '/' + repo.name
repos_to_clone.append((repo.ssh_url, git_url))
for repo, path in repos_to_clone:
clone_repo(repo, path)
return repos_to_clone
def clone():
repos_to_clone = get_all_repos()
def clone_repo(git_url, path):
git.Repo.clone_from(git_url, path)
def update_repo(path):
repo = git.Repo(path)
repo.remotes.origin.pull()
def update():
repos = get_all_repos()
for _, path in repos:
update_repo(path)
@click.command()
@click.option('--command', prompt='command name: clone or updaet',
help='Clone or update repos')
def main(command):
if command == 'clone':
clone()
elif command == 'update':
update()
if __name__ == '__main__':
main()
click
GitPython
PyGithub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment