Skip to content

Instantly share code, notes, and snippets.

@madhead
Created January 8, 2025 11:16
Show Gist options
  • Save madhead/dbdb6640d400d77a1e87c0a76062525f to your computer and use it in GitHub Desktop.
Save madhead/dbdb6640d400d77a1e87c0a76062525f to your computer and use it in GitHub Desktop.
Bulk Unwatch GitHub Repositories
#!/bin/bash
TOKEN=""
GRAPHQL_API="https://api.github.com/graphql"
unwatch_repository() {
local repo_id=$1
local mutation=$(cat <<EOF
{
"query": "mutation (\$repoId: ID!) { \
updateSubscription(input: {subscribableId: \$repoId, state: UNSUBSCRIBED}) { \
subscribable { \
id \
} \
} \
}", \
"variables": { \
"repoId": "$repo_id" \
} \
}
EOF
)
curl -s -H "Authorization: Bearer $TOKEN" -d "$mutation" $GRAPHQL_API
}
jq -c '.' repositories.json-stream | while read -r object; do
repo_id=$(echo "$object" | jq -r '.id')
repo=$(echo "$object" | jq -r '.nameWithOwner')
unwatch_repository "$repo_id"
echo "Unwatched $repo ($repo_id)"
done
  1. ./watching.sh > repositories.json-stream.

  2. Review the repositories.json-stream and remove all the repositories you want to be subscribed on.

  3. ./unwatch.sh.

#!/bin/bash
TOKEN=""
GRAPHQL_API="https://api.github.com/graphql"
fetch_repositories() {
local cursor=$1
local query=$(cat <<EOF
{
"query": "query(\$cursor: String) { \
viewer { \
watching(first: 100, after: \$cursor) { \
nodes { \
id \
nameWithOwner \
} \
pageInfo { \
hasNextPage \
endCursor \
} \
} \
} \
}", \
"variables": { \
"cursor": $cursor \
}
}
EOF
)
curl -s -H "Authorization: Bearer $TOKEN" -d "$query" $GRAPHQL_API
}
cursor="null"
while true; do
response=$(fetch_repositories $cursor)
has_next_page=$(echo $response | jq -r '.data.viewer.watching.pageInfo.hasNextPage')
cursor=$(echo $response | jq -r '.data.viewer.watching.pageInfo.endCursor')
[ "$cursor" != "null" ] && cursor="\"$cursor\""
echo $response | jq '.data.viewer.watching.nodes[]'
if [ "$has_next_page" != "true" ]; then
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment