-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.topics.sh
99 lines (81 loc) · 3.24 KB
/
repo.topics.sh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env -S bash -e
#
# Updating repository topics on GitHub.
#
# @package Bash
# @author Kai Kimera <mail@kai.kim>
# @copyright 2023 iHub TO
# @license MIT
# @version 0.0.1
# @link https://lib.onl
# -------------------------------------------------------------------------------------------------------------------- #
(( EUID == 0 )) && { echo >&2 'This script should not be run as root!'; exit 1; }
# -------------------------------------------------------------------------------------------------------------------- #
# CONFIGURATION.
# -------------------------------------------------------------------------------------------------------------------- #
curl="$( command -v curl )"
sleep='2'
# Help.
read -r -d '' help <<- EOF
Options:
-x 'TOKEN' GitHub user token.
-o 'OWNER' Repository owner (organization).
-r 'REPO_1;REPO_2;REPO_3' Repository name (array).
-t 'TOPIC_1;TOPIC_2;TOPIC_3' Topic name (array).
EOF
# -------------------------------------------------------------------------------------------------------------------- #
# OPTIONS.
# -------------------------------------------------------------------------------------------------------------------- #
OPTIND=1
while getopts 'x:o:r:t:h' opt; do
case ${opt} in
x)
token="${OPTARG}"
;;
o)
owner="${OPTARG}"
;;
r)
repos="${OPTARG}"; IFS=';' read -ra repos <<< "${repos}"
;;
t)
topics="${OPTARG}"; IFS=';' read -ra topics <<< "${topics}"
;;
h|*)
echo "${help}"; exit 2
;;
esac
done
shift $(( OPTIND - 1 ))
(( ! ${#repos[@]} )) && { echo >&2 '[ERROR] Repository name not specified!'; exit 1; }
(( ! ${#topics[@]} )) && { echo >&2 '[ERROR] Topic name not specified!'; exit 1; }
[[ -z "${token}" ]] && { echo >&2 '[ERROR] Token not specified!'; exit 1; }
# -------------------------------------------------------------------------------------------------------------------- #
# INITIALIZATION.
# -------------------------------------------------------------------------------------------------------------------- #
init() {
repo_topics
}
# -------------------------------------------------------------------------------------------------------------------- #
# GITHUB: REPOSITORY TOPICS.
# -------------------------------------------------------------------------------------------------------------------- #
repo_topics() {
topic=$( printf ',"%s"' "${topics[@]}" )
for repo in "${repos[@]}"; do
echo '' && echo "--- OPEN: '${repo}'"
${curl} -X PUT \
-H "Authorization: token ${token}" \
-H 'Accept: application/vnd.github.mercy-preview+json' \
"https://api.github.com/repos/${owner}/${repo}/topics" \
-d @- << EOF
{
"names": [${topic:1}]
}
EOF
echo '' && echo "--- DONE: '${repo}'" && echo ''; sleep ${sleep}
done
}
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------< INIT FUNCTIONS >------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
init "$@"; exit 0