Created
June 10, 2020 19:12
-
-
Save gerkey/6cbdae981d92489774558439fdd07cc0 to your computer and use it in GitHub Desktop.
Script used to add all org repos to a given team, with admin permission
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 | |
import requests | |
import sys | |
USAGE = 'add-all-org-repos-to-team.py <github-oauth-token>' | |
org = 'osrf' | |
team = 'ex-owners' | |
permission = "admin" | |
base_url = 'https://api.github.com/' | |
api_list_repos = 'orgs/%s/repos' | |
# org, team, repo full_name | |
api_add_repo_to_team = 'orgs/%s/teams/%s/repos/%s' | |
def go(token): | |
headers = {'Authorization': 'token %s'%(token)} | |
# Get all the repo names, as org/repo | |
done = False | |
repos = [] | |
url = base_url + api_list_repos%(org) | |
sys.stdout.write('Getting list of repos') | |
while not done: | |
sys.stdout.write('.') | |
sys.stdout.flush() | |
r = requests.get(url, headers=headers) | |
for i in r.json(): | |
repos.append(i['full_name']) | |
if r.links.get('next'): | |
url = r.links['next']['url'] | |
else: | |
done = True | |
print('') | |
# Add each one to the team, with the given permission | |
# I couldn't get the permission setting to work via API (it works via curl). | |
# So instead I used another API to do a one-time change of the team's permission to admin. | |
#data = '{permission: %s}'%(permission) | |
for r in repos: | |
print('Adding repo %s to team %s with permission %s'%(r, team, permission)) | |
url = base_url + api_add_repo_to_team%(org,team,r) | |
r = requests.put(url, headers=headers) #, data=data) | |
if r.status_code != 204: | |
print('Unexpected status code: %d'%(r.status_code)) | |
print(r.headers) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print(USAGE) | |
sys.exit(1) | |
token = sys.argv[1] | |
go(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment