Skip to content

Instantly share code, notes, and snippets.

@sparky-raccoon
Last active March 22, 2024 12:04
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 sparky-raccoon/80031d03157758a38d2df871f4deb265 to your computer and use it in GitHub Desktop.
Save sparky-raccoon/80031d03157758a38d2df871f4deb265 to your computer and use it in GitHub Desktop.
Unsubscribe and ignore automatically every repository of your organisation.
#!/usr/bin/env bash
# GH_TOKEN=
# GITHUB_TOKEN=$GH_TOKEN
# ORGANIZATION=
# LOG_FILE=
exec 3>&1 1>>${LOG_FILE} 2>&1
now=$(date)
echo ">>>> Script started at: $now"
echo ""
gh auth login --with-token <<< $GH_TOKEN
subscribed=($(gh api -H "Accept: application/vnd.github.json" -H "X-GitHub-Api-Version: 2022-11-28" /user/subscriptions --jq '.[].full_name') )
echo ">> Unsubscribe automatically subscribed repositories from $ORGANIZATION"
echo "$subscribed"
for it in "${subscribed[@]}"
do
if [[ $it == *"$ORGANIZATION"* ]]; then
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${it}/subscription" \
-F subscribed=false \
-F ignored=true | jq .
fi
done
echo ""
echo ">> Cleaning notifications"
gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /notifications --jq '.[].repository.full_name'
notified=($(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /notifications --jq '.[].repository.full_name'))
echo "$notified"
for it in "${notified[@]}"
do
# Mark them as read. Missing steps :
# unsubscribe (/notifications/beta/unsubscribe) + done (/notifications/beta/archive)
if [[ $it == *"$ORGANIZATION"* ]]; then
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${it}/notifications"
fi
done
echo ""
now=$(date)
echo ">>>> Script ended at: $now"
@sparky-raccoon
Copy link
Author

FIXME : Unfortunately, there's no way to unsubscribe notifications or mark them as done. For now the beta apis exist but are not accessible via gh. There might a chance that you will be spammed with "read" notifications if the script does not unsubscribe repositories between an automatic subscribe and operated changes on those repositories.

@sparky-raccoon
Copy link
Author

Another option that might help to avoid automatic subscription at all is the "Automatically watch repositories > When you're given push access to a repository, automatically receive notifications for it." notifications setting from your profile. However, as a global setting it would prevent you to automatically watch repositories that do not belong to a specific organisation.

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