Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
"""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=[], auth=None):
req = None
if auth:
req = requests.get(url, auth=auth)
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)
return repo_list
def main(user, password, org_name, outfile):
auth = (user, password)
repo_url = '{0}/repos'.format(org_name)
headers = {'Content-Type': 'application/json; charset=UTF-8'}
repo_list = get_repos(repo_url, auth=auth)
lines = []
with open(outfile) as f:
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:
url = '{0}/{1}/subscription'.format(
res = requests.put(
data='{"subscribed": "1"}',
if res.status_code == 200:
print('status {0} | repo {1}'.format(
print('ERRORE! status {0} | repo {1}'.format(
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Watch/unwatch GitHub Repositories', version='%(prog)s 1.0') # noqa
parser.add_argument('user', type=str, help='GitHub username')
parser.add_argument('password', type=str, help='GitHub password')
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()
Copy link

eubnara commented Jan 26, 2017

Hello, I'd like to thank you. Your code is the one I wanted. Thanks to your code, I can easily add 'watching' status to all repositories in specific organization. Anyway, I would like to suggest a little bit update this code. If you don't mind, could you see my fork?

Copy link

thet commented Jun 7, 2017

@eubnara your revision looks good. I'd add a default value for host_name, defaulting to, though.

Copy link

isc-rsingh commented Feb 20, 2018

Thanks for this @thet. Really saved me a ton of time!

One note for those who come later. If you have two-factor authentication (2FA) enabled on your account, you can't use this script with your username and password because it's doing "basic" authentication. However, you can get around this by using a personal access token that has the right permissions. You can then use basic authentication with the token as your id/username and x-oauth-basic as your password.

Copy link

msarahan commented Apr 10, 2018

My fork has a fallback that works for username/password or for auth token. Just pass the token as the password. The username is ultimately ingnored. Thanks @thet and @rajrsingh for this example and the auth token howto.

Copy link

landonjx commented May 14, 2019

Seems outfile needs to exist even on first run.

Copy link

AnthonyBloomer commented Nov 14, 2019

I wrote a simple solution using PyGithub if anyone else is having issues with this script.

from github import Github


for repo in g.get_user().get_repos():

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment