-
Notifications
You must be signed in to change notification settings - Fork 73
/
add_team_to_github_org_repos.py
executable file
·68 lines (58 loc) · 1.94 KB
/
add_team_to_github_org_repos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
#
# Script using PyGithub to add a specified GitHub Team to all of an Organization's repositories.
#
# Copyright 2015 Jason Antman <[email protected]> <http://www.jasonantman.com>
# Free for any use provided that patches are submitted back to me.
#
# The latest version of this script can be found at:
# <https://github.com/jantman/misc-scripts/blob/master/add_team_to_github_org_repos.py>
#
# Requires PyGithub - `pip install PyGithub` (tested against 1.23.0)
# tested with py27 and py32
#
# Assumes you have a GitHub API Token, either in ~/.ssh/apikeys.py or
# in a GITHUB_TOKEN environment variable.
#
# CHANGELOG:
# - initial script
#
from github import Github
import os
import sys
if len(sys.argv) < 3:
sys.stderr.write("USAGE: github_org_repos.py <orgname> <teamname>\n")
raise SystemExit(1)
orgname = sys.argv[1]
teamname = sys.argv[2]
TOKEN = None
try:
# look for GITHUB_TOKEN defined in ~/.ssh/apikeys.py
sys.path.append(os.path.abspath(os.path.join(os.path.expanduser('~'), '.ssh')))
from apikeys import GITHUB_TOKEN
TOKEN = GITHUB_TOKEN
except ImportError:
pass
if TOKEN is None:
try:
TOKEN = os.environ['GITHUB_TOKEN']
except KeyError:
sys.stderr.write("ERROR: you must either set GITHUB_TOKEN in ~/.ssh/apikeys.py or export it as an env variable.\n")
raise SystemExit(1)
g = Github(login_or_token=TOKEN)
org = g.get_organization(orgname)
team = None
for t in org.get_teams():
if t.name == teamname:
team = t
if team is None:
sys.stderr.write("ERROR: could not find team '%s'\n" % teamname)
raise SystemExit(1)
team_repos = [r.id for r in team.get_repos()]
print("Team %s has %d repositories" % (teamname, len(team_repos)))
for repo in org.get_repos():
if repo.id in team_repos:
print("%s is already in team's repositories" % repo.name)
continue
print("Adding repo %s to team's repositories" % repo.name)
team.add_to_repos(repo)