Skip to content

Instantly share code, notes, and snippets.

@eubnara
Forked from thet/github_watch.py
Last active January 26, 2017 07:19
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 eubnara/57a2d5c68e2bdef1f29e832afaed1587 to your computer and use it in GitHub Desktop.
Save eubnara/57a2d5c68e2bdef1f29e832afaed1587 to your computer and use it in GitHub Desktop.
"""Set all repositories of a given GitHub organization name for a given user
to watching.
"""
import argparse
import json
import requests
import getpass
def get_repos(url, repo_list=[], auth=None):
req = None
if auth:
req = requests.get(url, auth=auth)
else:
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, auth=auth)
else:
data = req.json()
print('status {0} | {1}'.format(
req.status_code,
data['message'],
))
exit(-1)
return repo_list
def main(host_name, org_name, outfile):
user = raw_input("id: ")
password = getpass.getpass("password: ")
auth = (user, password)
repo_url = 'https://{0}/api/v3/orgs/{1}/repos'.format(host_name, org_name)
headers = {'Content-Type': 'application/json; charset=UTF-8'}
repo_list = get_repos(repo_url, auth=auth)
lines = []
with open(outfile, "a+") as f:
f.seek(0)
lines = [it.strip('\n').strip('\r') for it in f]
with open(outfile, 'a') as f:
for repo in repo_list:
repo_name = repo['name']
if repo_name in lines:
continue
url = 'https://{0}/api/v3/user/subscriptions/{1}/{2}'.format(
host_name,
org_name,
repo_name
)
res = requests.put(
url=url,
headers=headers,
auth=auth
)
if 204 == res.status_code:
f.write('{0}\n'.format(repo_name))
print('status {0} [watching] | repo {1}'.format(
res.status_code,
repo_name
))
else:
print('ERROR! status {0} | repo {1}'.format(
res.status_code,
repo_name
))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Watch/unwatch GitHub Repositories', version='%(prog)s 1.0') # noqa
parser.add_argument('host_name', type=str, help='GitHub host name')
parser.add_argument('org_name', type=str, help='GitHub organization name')
parser.add_argument('--outfile', type=str, default='repos_set.txt', help='Name of the file to write each successful changed repository to. Used for avoiding unnecessart API calls.') # noqa
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