-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-subscribe
executable file
·51 lines (46 loc) · 1.57 KB
/
gh-subscribe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
set -eu -o pipefail
# NAME
# gh-subscribe - Subscribe to notifications for GitHub repositories.
#
# SYNOPSIS
# ./gh-subscribe
#
# DESCRIPTION
# This script retrieves a list of remote GitHub repositories associated with the current Git project
# and automatically subscribes to notifications for these repositories using the GitHub API.
#
# REQUIREMENTS
# - The script requires a valid GitHub token to be set in the environment variable GITHUB_TOKEN.
# - curl utility is required for API requests.
#
# USAGE
# To run this script, navigate to a Git repository and execute:
# ./gh-subscribe
#
# Ensure that the `GITHUB_TOKEN` environment variable is exported with a valid personal access token
# before running this script.
#
# EXAMPLES
# $ export GITHUB_TOKEN=your_personal_access_token
# $ ./gh-subscribe
#
# NOTES
# - The script subscribes you to all repositories listed in your Git remotes.
# - Existing subscription settings will be updated to ensure notifications are enabled
# and ignored topics are turned off.
#```
if [ -z "$GITHUB_TOKEN" ]; then
echo "GITHUB_TOKEN is not set" >&2
exit 1
fi
owner_repos="$(git remote -v | sed -E 's#.*(github\.com[:/])([^/]+)/([^.]+).*#\2/\3#g' | sort -u)"
for owner_repo in ${owner_repos}; do
curl -L \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${owner_repo}/subscription" \
-d '{"subscribed":true,"ignored":false}'
done