Created
June 10, 2020 04:42
-
-
Save gerkey/775b5c5d60b82097532377fdc2a3a182 to your computer and use it in GitHub Desktop.
Un-watch a list of repos based on the URLs given in the "Subscribed to..." email from GitHub
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 | |
# This script can be used to un-watch a bunch of repos in the situation in | |
# which you were automatically made to watch them. It relies on the email | |
# notification(s) that you received from GH about the newly watched repos. | |
# | |
# Usage: | |
# | |
# 1. Copy and paste all the URLs of the form | |
# https://github.com/ORG/REPO/subscription from the GH notification | |
# email(s) into a file, newline-separated. Don't worry about whitespace on | |
# each line. Note that you may need to concatenate the contents of multiple | |
# emails, as they're batched 100 per email. | |
# | |
# 2. If necessary, generate (and copy to somewhere safe) a personal access | |
# token at GH. In the GH web UI: Settings->Developer Settings->Personal | |
# Access Tokens->Generate new token. That token should have at least the | |
# 'notifications' scope. | |
# | |
# 3. Run this script: | |
# python3 unwatch-repos.py <token> <file> | |
# where <token> is the token you created in step 2, and <file> is the file | |
# you created in step 1. | |
import requests | |
import sys | |
USAGE = 'unwatch-repos.py <github-oauth-token> <text-file-of-repo-urls>' | |
org = 'osrf' | |
base_url = 'https://api.github.com/' | |
# org, repo; HTTP DELETE to unwatch | |
api_repo_subscription = 'repos/%s/%s/subscription' | |
def go(token, fname): | |
with open(fname) as f: | |
repos = [x.strip() for x in f.readlines()] | |
headers = {'Authorization': 'token %s'%(token)} | |
for r in repos: | |
components = r.split('/') | |
if len(components) != 6 or components[3] != org or components[5] != 'subscription': | |
print('URL %s does not match the expected pattern; skipping'%(r)) | |
continue | |
reponame = components[4] | |
url = base_url + api_repo_subscription%(org, reponame) | |
print('Unwatching repo %s/%s via DELETE %s'%(org, reponame, url)) | |
resp = requests.delete(url, headers=headers) | |
if resp.status_code != 204: | |
print('Unexpected status code: %d'%(resp.status_code)) | |
print(resp.headers) | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print(USAGE) | |
sys.exit(1) | |
go(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment