-
-
Save ramseycc/b8d70e947201530dda6333208525f565 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"""Set all repositories of a given GitHub organization name for a given user | |
to watching. | |
""" | |
import argparse | |
import json | |
import requests | |
def get_repos(url, repo_list=[]): | |
req = requests.get(url) | |
if (req.ok): | |
repos = json.loads(req.text or req.content) | |
repo_list += repos | |
links = getattr(req, 'links', None) | |
if links and 'next' in links and 'url' in links['next']: | |
get_repos(links['next']['url'], repo_list=repo_list) | |
return repo_list | |
def main(org_name, access_token): | |
repo_url = 'https://api.github.com/orgs/{0}/repos?access_token={1}'.format(org_name, access_token) | |
headers = {'Content-Type': 'application/json; charset=UTF-8'} | |
repo_list = get_repos(repo_url) | |
for repo in repo_list: | |
repo_name = repo['name'] | |
url = 'https://api.github.com/repos/{0}/{1}/subscription?access_token={2}'.format( | |
org_name, | |
repo_name, | |
access_token | |
) | |
res = requests.put( | |
url=url, | |
data='{"subscribed": "1"}', | |
headers=headers | |
) | |
print('status {0} | repo {1}'.format( | |
res.status_code, | |
repo_name | |
)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Watch/unwatch GitHub Repositories') # noqa | |
parser.add_argument('org_name', type=str, help='GitHub organization name') | |
parser.add_argument('access_token', type=str, help='GitHub access token') | |
args = parser.parse_args() | |
main(**vars(args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added access token to be able to watch private repos.
Removed file writing stuff.