Skip to content

Instantly share code, notes, and snippets.

@rtt
Created February 20, 2020 17:17
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 rtt/1bc015ceb310bd798836961f0c3668c7 to your computer and use it in GitHub Desktop.
Save rtt/1bc015ceb310bd798836961f0c3668c7 to your computer and use it in GitHub Desktop.
mass github repo subscription delete
import os
import sys
import requests
def get_subscribed_repos_in_organization(organization, personal_access_token):
page = 1
has_more = True
while has_more:
print("GET https://api.github.com/user/subscriptions?per_page=100&page={}".format(page))
response = requests.get('https://api.github.com/user/subscriptions',
params={'per_page': 100, 'page': str(page)},
headers={'Authorization': 'token {}'.format(personal_access_token)})
page += 1
if not response.status_code == 200:
print('Check access token. Github said {}'.format(response.status_code))
sys.exit(1)
repos_from_response = [repo['full_name'] for repo in response.json() if repo['owner']['login'] == organization]
for repo_name in repos_from_response:
yield repo_name
if len(repos_from_response) == 0:
has_more = False
def yes_or_no(question):
reply = str(raw_input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
else:
return yes_or_no("Please choose")
def usage():
print('Usage: unsub.py <github org name>')
sys.exit(1)
if __name__ == '__main__':
personal_access_token = os.environ.get('GITHUB_PERSONAL_ACCESS_TOKEN')
if not personal_access_token:
print('Missing personal access token')
sys.exit(1)
try:
organization = sys.argv[1]
except IndexError:
usage()
if not organization:
usage()
repos_in_organization = [repo for repo in get_subscribed_repos_in_organization(organization, personal_access_token)]
if not repos_in_organization:
print('No repos in {}'.format(organization))
sys.exit(1)
do_unsubscribe = yes_or_no("\nUnsubscribe from the above {} repositories?".format(len(repos_in_organization)))
if do_unsubscribe:
for repo_full_name in repos_in_organization:
url = "https://api.github.com/repos/{}/subscription".format(repo_full_name)
print("DELETE {}".format(url))
response = requests.delete(url,
# params={'subscribed': False},
headers={'Authorization': 'token {}'.format(personal_access_token)})
print(response.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment