Created
January 29, 2021 00:35
-
-
Save rtyler/0595ba18541dda8fcfa77d6904ff0a4e to your computer and use it in GitHub Desktop.
A simple Python script for copying an organization's repositories
This file contains 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 python3 | |
# | |
# Requires https://pygithub.readthedocs.io/en/latest | |
# | |
# Set the GITHUB_TOKEN to a personal access token with all repo scopes | |
import os | |
import subprocess | |
import sys | |
from github import Github | |
from optparse import OptionParser | |
token = os.getenv('GITHUB_TOKEN') | |
if token is None: | |
print("Must set GITHUB_TOKEN") | |
sys.exit(1) | |
parser = OptionParser() | |
parser.add_option("-u", "--user", dest="user", help="User to use for authentication") | |
parser.add_option("-o", "--org", dest="org", help="Organization to back up") | |
(options, args) = parser.parse_args() | |
g = Github(token) | |
os.putenv('GIT_USER', options.user) | |
if options.org: name = options.org | |
org = g.get_organization(name) | |
if not os.path.exists(name): os.mkdir(name) | |
for repo in org.get_repos(type='all'): | |
git_path = os.path.join(name, '{}.git'.format(repo.name)) | |
print(git_path) | |
helper_sh = '!f() { echo \'username=${GIT_USER}\'; echo \'password=${GITHUB_TOKEN}\'; }; f' | |
if os.path.exists(git_path): | |
subprocess.run('cd {} && git -c credential.helper="{}" fetch --all'.format(git_path, helper_sh), shell=True, check=True) | |
else: | |
subprocess.run('cd {} && git -c credential.helper="{}" clone --mirror --bare {}'.format(name, helper_sh, repo.clone_url), shell=True, check=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I put my all build stage i.e., SCM Checkout, Build, test, and uploading artifacts etc.. in a shared library? any example?