forked from gamenic-madan-pd/github-labels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-github-labels.sh
More file actions
executable file
·296 lines (264 loc) · 7.51 KB
/
create-github-labels.sh
File metadata and controls
executable file
·296 lines (264 loc) · 7.51 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env bash
##
# Populate GitHub project labels.
#
# @usage:
# Interactive prompt:
# ./create-github-labels
#
# Silent, if $GITHUB_TOKEN is set in environment and repository provided as an argument:
# ./create-github-labels myorg/myrepo
# gh repo list lifecheq --json "name" | jq ".[].name" | cut -f 2 -d\" | awk '{print "lifecheq/" $0}'
#
# shellcheck disable=SC2155
GITHUB_TOKEN="${GITHUB_TOKEN:-}"
REPO="${REPO:-$1}"
# Delete existing labels to mirror the list below.
DELETE_EXISTING_LABELS="${DELETE_EXISTING_LABELS:-0}"
# Array of labels to create. If DELETE_EXISTING_LABELS=1, the labels list will
# be exactly as below, otherwise labels below will be added to existing ones.
LABELS=(
"Feature Flag? No" "B60205" "The change is not behind a feature flag, experiment, dark launch or similar"
"Feature Flag? Yes" "0E8A16" "The change is behind a feature flag, experiment, dark launch or similar"
"Risk: high" "E99695" "High Risk change"
"Risk: low" "C2E0C6" "Low Risk change"
"Risk: medium" "FEF2C0" "Medium Risk change"
"Tested in Dev" "1C6CC9" "The change was manually tested in dev and it is ready to go to staging"
)
## Labels that we will not delete
LABELS_TO_KEEP=(
"adr" "FEE8DB" "To be able to exclude ADR PR from Github pullreminders"
)
main() {
echo
if [ "${DELETE_EXISTING_LABELS}" == "1" ]; then
echo "This script will remove the default GitHub labels."
else
echo "This script will not remove the default GitHub labels."
fi
echo "This script will create new labels."
echo "A personal access token is required to access private repositories."
echo
if [ "${REPO}" == "" ]; then
echo ''
echo -n 'GitHub Org/Repo (e.g. foo/bar): '
read -r REPO
else
REPO="$1"
fi
if [ "${GITHUB_TOKEN}" == "" ]; then
echo ''
echo -n 'GitHub Personal Access Token: '
read -r -s GITHUB_TOKEN
fi
REPO_USER=$(echo "$REPO" | cut -f1 -d /)
REPO_NAME=$(echo "$REPO" | cut -f2 -d /)
if ! user_has_access; then
echo "User does not have access to specified repository. Please check your credentials" && exit 1
fi
echo
echo "Starting label processing"
echo
timeout 3
echo
if [ "${DELETE_EXISTING_LABELS}" == "1" ]; then
echo "Checking existing labels"
existing_labels_strings="$(label_all)"
# shellcheck disable=SC2207
IFS=$'\n' existing_labels=($(xargs -n1 <<<"${existing_labels_strings}"))
for existing_label_name in "${existing_labels[@]}"; do
if ! is_provided_label "${existing_label_name}"; then
echo "Removing label \"${existing_label_name}\" as it is not in thr provided list"
if label_delete "${existing_label_name}"; then
echo " DELETED label \"${existing_label_name}\""
else
echo " Unable to DELETE label \"${existing_label_name}\""
fi
fi
done
fi
count=0
for value in "${LABELS[@]}"; do
if ((count % 3 == 0)); then
name="${value}"
elif ((count % 3 == 1)); then
color="${value}"
else
description="${value}"
echo "Processing label \"${name}\""
if label_exists "${name}"; then
if label_update "${name}" "${color}" "${description}"; then
echo " UPDATED label \"${name}\""
else
echo " Unable to UPDATE label \"${name}\""
fi
else
if label_create "${name}" "${color}" "${description}"; then
echo " CREATED label \"${name}\""
else
echo " Unable to CREATE label \"${name}\""
fi
fi
fi
count=$((count + 1))
done
echo
echo "Label processing complete"
echo
}
is_provided_label() {
label="${1}"
count=0
for value in "${LABELS[@]}"; do
if ((count % 3 == 0)); then
name="${value}"
if [ "${label}" == "${name}" ]; then
return 0
fi
fi
count=$((count + 1))
done
count=0
for value in "${LABELS_TO_KEEP[@]}"; do
if ((count % 3 == 0)); then
name="${value}"
if [ "${label}" == "${name}" ]; then
return 0
fi
fi
count=$((count + 1))
done
return 1
}
user_has_access() {
status=$(
curl -s -I \
-u "${GITHUB_TOKEN}":x-oauth-basic \
--include -H "Accept: application/vnd.github.symmetra-preview+json" \
-o /dev/null \
-w "%{http_code}" \
--request GET \
"https://api.github.com/repos/${REPO_USER}/${REPO_NAME}/labels"
)
[ "${status}" == "200" ]
}
label_all() {
response=$(
curl -s \
-u "${GITHUB_TOKEN}":x-oauth-basic \
--include -H "Accept: application/vnd.github.symmetra-preview+json" \
--request GET \
"https://api.github.com/repos/${REPO_USER}/${REPO_NAME}/labels"
)
jsonval "${response}" "name"
}
label_exists() {
local name="${1}"
local name_encoded=$(uriencode "${name}")
status=$(
curl -s -I \
-u "${GITHUB_TOKEN}":x-oauth-basic \
--include -H "Accept: application/vnd.github.symmetra-preview+json" \
-o /dev/null \
-w "%{http_code}" \
--request GET \
"https://api.github.com/repos/${REPO_USER}/${REPO_NAME}/labels/${name_encoded}"
)
[ "${status}" == "200" ]
}
label_create() {
local name="${1}"
local color="${2}"
local description="${3}"
local status=$(
curl -s \
-u "${GITHUB_TOKEN}":x-oauth-basic \
-H "Accept: application/vnd.github.symmetra-preview+json" \
-o /dev/null \
-w "%{http_code}" \
--request POST \
--data "{\"name\":\"${name}\",\"color\":\"${color}\", \"description\":\"${description}\"}" \
"https://api.github.com/repos/${REPO_USER}/${REPO_NAME}/labels"
)
[ "${status}" == "201" ]
}
label_update() {
local name="${1}"
local color="${2}"
local description="${3}"
local name_encoded=$(uriencode "${name}")
local status=$(
curl -s \
-u "${GITHUB_TOKEN}":x-oauth-basic \
-H "Accept: application/vnd.github.symmetra-preview+json" \
-o /dev/null \
-w "%{http_code}" \
--request PATCH \
--data "{\"name\":\"${name}\",\"color\":\"${color}\", \"description\":\"${description}\"}" \
"https://api.github.com/repos/${REPO_USER}/${REPO_NAME}/labels/${name_encoded}"
)
[ "${status}" == "200" ]
}
label_delete() {
local name="${1}"
local color="${2}"
local description="${3}"
local name_encoded=$(uriencode "${name}")
local status=$(
curl -s \
-u "${GITHUB_TOKEN}":x-oauth-basic \
-H "Accept: application/vnd.github.symmetra-preview+json" \
-o /dev/null \
-w "%{http_code}" \
--request DELETE \
"https://api.github.com/repos/${REPO_USER}/${REPO_NAME}/labels/${name_encoded}"
)
[ "${status}" == "204" ]
}
jsonval() {
local json="${1}"
local prop="${2}"
temp=$(
echo "${json}" |
sed 's/\\\\\//\//g' |
sed 's/[{}]//g' |
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' |
sed 's/\"\:\"/\|/g' |
sed 's/[\,]/ /g' |
grep -w "${prop}" |
cut -d":" -f2 |
sed -e 's/^ *//g' -e 's/ *$//g'
)
temp="${temp//${prop}|/}"
temp="$(echo "${temp}" | tr '\r\n' ' ')"
echo "${temp}"
}
uriencode() {
s="${1//'%'/%25}"
s="${s//' '/%20}"
s="${s//'"'/%22}"
s="${s//'#'/%23}"
s="${s//'$'/%24}"
s="${s//'&'/%26}"
s="${s//'+'/%2B}"
s="${s//','/%2C}"
s="${s//'/'/%2F}"
s="${s//':'/%3A}"
s="${s//';'/%3B}"
s="${s//'='/%3D}"
s="${s//'?'/%3F}"
s="${s//'@'/%40}"
s="${s//'['/%5B}"
s="${s//']'/%5D}"
printf %s "$s"
}
timeout() {
local seconds=${1}
while [ "${seconds}" -gt 0 ]; do
echo -ne "Processing will start in $seconds seconds. Press Ctrl+C to abort\033[0K\r"
sleep 1
: $((seconds--))
done
echo
}
main "$@"