Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/9217435 to your computer and use it in GitHub Desktop.
Save RichardBronosky/9217435 to your computer and use it in GitHub Desktop.
The GitHub web UI for adding access to repos for organization teams is pretty terrible. Luckily the API is pretty robust.
import re
import sys
from pygithub3 import Github
# Notice: there is a requirements.txt file to help you get pygithub3 via...
# pip install -r requirements.txt
if __name__ == '__main__':
larg = len(sys.argv)
if larg < 5 or larg == 6 or (larg > 7 and sys.argv[5] != '--include'):
print " {command} username password oranization team [--include 'RegularExpression']". format(command=sys.argv[0])
sys.exit()
regex = re.compile('.')
if larg == 7:
if sys.argv[5] == '--include':
regex = re.compile(sys.argv[6])
login = sys.argv[1]
password = sys.argv[2]
org = sys.argv[3]
team = sys.argv[4]
gh = Github(login=login, password=password)
teams = gh.orgs.teams.list(org).all()
team_id = [t.id for t in teams if t.name == team][0]
all_repos = gh.repos.list_by_org(org).all()
repo_names = filter(regex.search, [r.name for r in all_repos])
print "login: {login}".format(login=login)
print "org: {org}".format(org=org)
print "team: {team}".format(team=team)
print "team_id: {team_id}".format(team_id=team_id)
print "repos: {repos}".format(repos=', '.join(repo_names))
response = raw_input('proceed with these values? [Ny] ').lower()
if response != 'y':
sys.exit()
for repo in repo_names:
gh.orgs.teams.add_repo(id=team_id, user=org, repo=repo)
pygithub3>=0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment