def show_migration_status(args): """ List authorised repos for teams """ with cli.catch_api_errors(): src = Organisation(args.src) dest = Organisation(args.dest) repos_dest = list(dest.list_repos()) lod = [] # "list of dicts" for repo_src in src.list_repos(): (migrated, repo_dest) = exists_in(repo_src, repos_dest) dest_private = str(repo_dest['private']) if migrated else 'N/A' lod.append({ 'NAME': repo_src['name'], 'MIGRATED': str(migrated), 'SRC_PRIVATE': str(repo_src['private']), 'DEST_PRIVATE': dest_private }) # TODO: # Technically you should be able to do something like: # query_lod(lod, sort_keys=('MIGRATED', 'NAME'))) but I can't seem # to get it to work query_results = query_lod(lod, lambda r: r['MIGRATED'] == 'True') pattern = '{0:45s} {1:10s} {2:11s} {3:11s}' inputs = ['NAME', 'MIGRATED', 'SRC_PRIVATE', 'DEST_PRIVATE'] print(pattern.format(*inputs)) for row in query_results: print(pattern.format(*[row[r] for r in inputs]))
def migrate_project(args): """ Migrate a Github project from one Github instance to another. Migration includes: - Project metadata - Git repository - Issues & pull requests - Comments - Hooks WARNING: This will copy the git repository verbatim. Any commits on the target repository that are not also on the source will be lost. Note: All issues and comments will be migrated as the target user with links back to the source Github instance. """ with cli.catch_api_errors(): src_org = Organisation(args.src) dst_org = Organisation(args.dst) src = Repo(args.src) dst = Repo(args.dst) # Create the repo object log.info("Migrating %s to %s -> creating repo", src, dst) project = src_org.get_repo(src.repo) project['name'] = dst.repo dst_org.create_repo(project) # Migrate repo data migrators.repo.migrate(src, dst) migrators.issues.migrate(src, dst) migrators.comments.migrate(src, dst) migrators.hooks.migrate(src, dst)
def show_migration_status(args): """ List authorised repos for teams """ with cli.catch_api_errors(): src = Organisation(args.src) dest = Organisation(args.dest) repos_dest = list(dest.list_repos()) lod = [ ] # "list of dicts" for repo_src in src.list_repos(): (migrated,repo_dest) = exists_in(repo_src, repos_dest) dest_private = str(repo_dest['private']) if migrated else 'N/A' lod.append({ 'NAME':repo_src['name'], 'MIGRATED':str(migrated), 'SRC_PRIVATE':str(repo_src['private']), 'DEST_PRIVATE':dest_private}) # TODO: # Technically you should be able to do something like: # query_lod(lod, sort_keys=('MIGRATED', 'NAME'))) but I can't seem # to get it to work query_results = query_lod(lod, lambda r:r['MIGRATED']=='True' ) pattern = '{0:45s} {1:10s} {2:11s} {3:11s}' inputs = ['NAME', 'MIGRATED', 'SRC_PRIVATE', 'DEST_PRIVATE'] print(pattern.format(*inputs)) for row in query_results: print(pattern.format(*[row[r] for r in inputs]))
def permissions(args): """ Print a list of teams. For each team, list the members, repos and the permission (pull/push/admin) """ with cli.catch_api_errors(): org = Organisation(args.org) result = [] for team in org.list_teams(): members = org.list_team_members(team) repos = org.list_team_repos(team) result.append({ "name": team['name'], "permission": team['permission'], "repos": [{ 'full_name': repo['full_name'], 'url': repo['url'] } for repo in repos], "members": [{ 'login': member['login'], 'url': member['url'] } for member in members] }) print(json.dumps(result, indent=2))
def get(args): """ Print the JSON representation of the specified repository to STDOUT """ repo = Repo(args.repo) with cli.catch_api_errors(): return json.dumps(repo.get(), indent=2)
def delete(args): """ Delete the specified repository """ repo = Repo(args.repo) with cli.catch_api_errors(): repo.delete()
def create(args): """ Create a repo with specified name """ repo = Repo(args.repo) with cli.catch_api_errors(): return repo.create()
def login(args): """ Log into a GitHub instance, and print the resulting OAuth token. """ with cli.catch_api_errors(): client = GithubAPIClient(nickname=args.github) login_if_needed(client, args.scope, comment=args.comment) oauth_token_key = envkey(client.nickname, "oauth_token") print("export {0}='{1}'".format(oauth_token_key, client.token))
def login(args): """ Log into a GitHub instance, and print the resulting OAuth token. """ with cli.catch_api_errors(): client = GithubAPIClient(nickname=args.github) login_if_needed(client, args.scope, comment=args.comment) oauth_token_key = envkey(client.nickname, 'oauth_token') print("export {0}='{1}'".format(oauth_token_key, client.token))
def members(args): """ Print a list of organisation members """ with cli.catch_api_errors(): org = Organisation(args.org) for member in org.list_members(): if args.json: print(json.dumps(member, indent=2)) else: print(member["login"])
def repos(args): """ Print a list of organisation repositories """ with cli.catch_api_errors(): org = Organisation(args.org) for repo in org.list_repos(): if args.json: print(json.dumps(repo, indent=2)) else: print(repo['name'])
def members(args): """ Print a list of organisation members """ with cli.catch_api_errors(): org = Organisation(args.org) for member in org.list_members(): if args.json: print(json.dumps(member, indent=2)) else: print(member['login'])
def repos(args): """ Print a list of organisation repositories """ with cli.catch_api_errors(): org = Organisation(args.org) for repo in org.list_repos(): if args.json: print(json.dumps(repo, indent=2)) else: print(repo["name"])
def migrate_wiki(args): """ Migrate a Github wiki from one Github instance to another. WARNING: This will copy the git repository verbatim. Any commits on the target repository that are not also on the source will be lost. """ with cli.catch_api_errors(): src = Repo(args.src) dst = Repo(args.dst) migrators.wiki.migrate(src, dst)
def list_members(args): """ List all members of an organisation along with the teams they're in. """ with cli.catch_api_errors(): org = Organisation(args.org) members = defaultdict(list) for team in org.list_teams(): for member in org.list_team_members(team): members[member['login']].append(team['name']) writer = csv.writer(sys.stdout, delimiter=',', quotechar='"') for member in org.list_members(): if member['type'] == 'User': writer.writerow([member['login']] + members[member['login']])
def list_members(args): """ List all members of an organisation along with the teams they're in. """ with cli.catch_api_errors(): org = Organisation(args.org) members = defaultdict(list) for team in org.list_teams(): for member in org.list_team_members(team): members[member["login"]].append(team["name"]) writer = csv.writer(sys.stdout, delimiter=",", quotechar='"') for member in org.list_members(): if member["type"] == "User": writer.writerow([member["login"]] + members[member["login"]])
def browse(args): """ Print the GitHub API response at the given URL """ with cli.catch_api_errors(): client = GithubAPIClient(nickname=args.github) res = client.request(args.method, args.url, _raise=False) print('HTTP/1.1 {0} {1}'.format(res.status_code, res.reason)) for k, v in res.headers.items(): print("{0}: {1}".format(k, v)) print() if res.json() is not None: print(json.dumps(res.json(), indent=2)) else: print(res.content)
def permissions(args): """ Print a list of teams. For each team, list the members, repos and the permission (pull/push/admin) """ with cli.catch_api_errors(): org = Organisation(args.org) result = [] for team in org.list_teams(): members = org.list_team_members(team) repos = org.list_team_repos(team) result.append( { "name": team["name"], "permission": team["permission"], "repos": [{"full_name": repo["full_name"], "url": repo["url"]} for repo in repos], "members": [{"login": member["login"], "url": member["url"]} for member in members], } ) print(json.dumps(result, indent=2))
def status(args): """ Set build status for a commit on GitHub """ repo = Repo(args.repo) payload = {'state': args.state} if args.description is not None: payload['description'] = args.description if args.url is not None: payload['target_url'] = args.url if args.context is not None: payload['context'] = args.context with cli.catch_api_errors(): res = repo.set_build_status(args.sha, payload) print(json.dumps(res.json(), indent=2))
def migrate_teams(args): with cli.catch_api_errors(): src = Organisation(args.src) dst = Organisation(args.dst) teams.migrate(src, dst, args.mapping)