Skip to content

Instantly share code, notes, and snippets.

@stuartlangridge
Created July 27, 2021 18:02
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 stuartlangridge/590e74e26e67cb6fe4eacec50ebd9e05 to your computer and use it in GitHub Desktop.
Save stuartlangridge/590e74e26e67cb6fe4eacec50ebd9e05 to your computer and use it in GitHub Desktop.
Unsubscribe from all Github repos for a specific org
#!/usr/bin/env python3
"""
Github won't let you say "don't subscribe me to new repos" for
one specific org only. You can either turn off autosubscribe
for everything, or for nothing.
This is extremely annoying if you're added to a very busy
new organisation, most of which you don't care about. This is
because you don't want to turn off all autosubscriptions -- if your
friend adds you to her new repo as a committer, you still want
to be told about it -- but you don't want to be subscribed to
the 50 new Terraform repos that the IT team add to your global
work org every day which you don't care about.
People have seemingly been asking for this since 2014, fgs:
https://webapps.stackexchange.com/a/62832
and the answer's the same: no you can't do that. (Or, often, "yes
you can, untick 'Automatically watch'" from people who didn't
read the damned question.)
So, here's a little Python script to unsubscribe you from all
these things. This doesn't actually HELP, because you'll still
get subscribed, and still get the emails telling you you've been
subscribed, because this won't be fixed until Github fix it.
But it will at least keep your subscriptions list tidy.
"""
# Add your Github username.
USERNAME = "hapless-email-reader"
# Get a personal use token from https://github.com/settings/tokens
# Note that if your org uses SSO, you have to copy the token first
# and then Enable SSO on it afterwards.
TOKEN = "ghp_blahblahblah"
# Fill in the org which you want to unsubscribe from here
ORG = "we-make-loads-of-repos-ltd"
# If there are repos in the org that you DO want to be subscribed to,
# add them here, as org/reponame strings.
EXCLUSIONS = ["we-make-loads-of-repos-ltd/the-one-i-actually-work-on"]
# And you're done. Run the script.
######################################################################
import requests
print("Reading all your subscriptions...", end="", flush=True)
api_subs_base_url = "https://api.github.com/user/subscriptions?per_page=100"
url = api_subs_base_url
to_remove_subscriptions = []
while True:
resp = requests.get(url, auth=(USERNAME, TOKEN))
names = [x.get("full_name") for x in resp.json()]
bhnames = [
x for x in names
if x.startswith(f"{ORG}/")
and x not in EXCLUSIONS
]
if bhnames:
to_remove_subscriptions += bhnames
if "next" in resp.links and "url" in resp.links["next"]:
url = resp.links["next"]["url"]
print(".", end="", flush=True)
else:
break
print("done.")
print("Removing offending subscriptions...")
if not to_remove_subscriptions:
print("...except that there are none, so we're done.")
for repo in to_remove_subscriptions:
remove_url = f"https://api.github.com/repos/{repo}/subscription"
resp = requests.delete(remove_url, auth=(USERNAME, TOKEN))
if resp.ok:
print(f" {repo}")
else:
print(f" {repo} failed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment