-
Notifications
You must be signed in to change notification settings - Fork 282
/
shared_functions
executable file
·328 lines (269 loc) · 7.9 KB
/
shared_functions
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env bash
#
# Shared functions
# shellcheck disable=SC1091
source "$HOME/.trainingmanualrc"
# shellcheck source=script/ask
source ./ask
# script variables
org_repos_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG"
red=$(tput setaf 1)
green=$(tput setaf 2)
blue=$(tput setaf 4)
reset=$(tput sgr0)
print_error() {
echo "${red}Error! $1${reset}"
}
print_success() {
echo "${green}Success! $1${reset}"
}
print_done() {
echo "${blue}Done! $1${reset}"
}
# https://superuser.com/a/1415376
# Returns exit code 0 (success) if $1 is a reachable git remote url
repo_is_reachable() {
local repo_url=$1
git ls-remote "$repo_url" CHECK_GIT_REMOTE_URL_REACHABILITY &>/dev/null
}
git_push() {
local repo_name=$1
echo -n "Pushing to $CLASS_ORG/$repo_name... "
if git push --mirror "https://$TOKEN_OWNER:$TEACHER_PAT@$ROOT_URL/$CLASS_ORG/$repo_name" \
>>log.out 2>&1; then
echo "Done."
else
# If the git push command failed
print_error "Failed to push commits to $CLASS_ORG/$repo_name."
exit 1
fi
}
create_repo() {
while (($#)); do
case $1 in
--name) local name=$2 ;;
--description) local description=$2 ;;
--homepage) local homepage=$2 ;;
--private) local private=$2 ;;
--has-wiki) local has_wiki=$2 ;;
esac
shift
done
local repos_endpoint="https://$INSTANCE_URL/orgs/$CLASS_ORG/repos"
echo -n "Creating repository: $name... "
if http --check-status --ignore-stdin --quiet --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$repos_endpoint" \
name="$name" \
description="$description" \
homepage="$homepage" \
private:="$private" \
has_wiki="$has_wiki"; then
echo "Done."
else
print_error "Failed to create repository."
fi
}
create_repo_project() {
while (($#)); do
case $1 in
--repo) local repo=$2 ;;
--name) local name=$2 ;;
--body) local body=$2 ;;
esac
shift
done
local projects_endpoint="$org_repos_endpoint/$repo/projects"
echo -n "Creating project: $name... "
# Unset $REPO_PROJECT_ID in case it was set previously
unset REPO_PROJECT_ID
# Create the project and get the project ID
REPO_PROJECT_ID=$(http --check-status --ignore-stdin --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$projects_endpoint" \
"Accept:application/vnd.github.inertia-preview+json" \
name="$name" \
body="$body" |
jq .id)
# Export $REPO_PROJECT_ID so it can be used in other scripts
export REPO_PROJECT_ID
# Check if $REPO_PROJECT_ID has a value
if [[ -n "$REPO_PROJECT_ID" ]] || [[ "$REPO_PROJECT_ID" = null ]]; then
echo "Done."
else
print_error "Failed to create project."
fi
}
create_project_column() {
while (($#)); do
case $1 in
--project_id) local project_id=$2 ;;
--name) local name=$2 ;;
esac
shift
done
local project_column_endpoint="https://$INSTANCE_URL/projects/$project_id/columns"
echo -n "Creating project column: $name... "
# Unset $COLUMN_ID in case it was set previously
unset COLUMN_ID
# Create the project column and get the column ID
COLUMN_ID=$(http --check-status --ignore-stdin --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$project_column_endpoint" \
"Accept:application/vnd.github.inertia-preview+json" \
project_id="$project_id" \
name="$name" |
jq .id)
# Export $COLUMN_ID so it can be used in other scripts
export COLUMN_ID
# Check if $COLUMN_ID has a value
if [[ -n "$COLUMN_ID" ]] || [[ "$COLUMN_ID" = null ]]; then
echo "Done."
else
print_error "Failed to create project column."
fi
}
create_project_card() {
while (($#)); do
case $1 in
--project_id) local project_id=$2 ;;
--column_id) local column_id=$2 ;;
--note) local note=$2 ;;
esac
shift
done
local project_card_endpoint="https://$INSTANCE_URL/projects/columns/$column_id/cards"
echo -n "Creating project card: $name... "
if http --check-status --ignore-stdin --quiet --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$project_card_endpoint" \
"Accept:application/vnd.github.inertia-preview+json" \
project_id="$project_id" \
column_id="$column_id" \
note="$note"; then
echo "Done."
fi
}
get_repo_content() {
while (($#)); do
case $1 in
--repo) local repo=$2 ;;
--path) local file_path=$2 ;;
esac
shift
done
local contents_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG/$repo/contents/$file_path"
if ! http --check-status --ignore-stdin --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$contents_endpoint"; then
print_error "Failed to get content of $file_path."
fi
}
get_repo_file_contents() {
while (($#)); do
case $1 in
--repo) local repo=$2 ;;
--path) local file_path=$2 ;;
esac
shift
done
local contents_endpoint="$org_repos_endpoint/$repo/contents/$file_path"
if ! http --check-status --ignore-stdin --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$contents_endpoint" "Accept:application/vnd.github.v3.raw"; then
print_error "Failed to get contents of $file_path."
fi
}
update_file_contents() {
while (($#)); do
case $1 in
--repo) local repo=$2 ;;
--path) local file_path=$2 ;;
--content) local content=$2 ;;
--message) local message=$2 ;;
esac
shift
done
local contents_endpoint="$org_repos_endpoint/$repo/contents/$file_path"
echo -n "Updating contents of $file_path... "
if http --check-status --ignore-stdin --quiet --auth \
"$TOKEN_OWNER:$TEACHER_PAT" PUT "$contents_endpoint" \
content="$(echo "$content" | base64)" \
message="$message" \
sha="$(get_repo_content --repo "$repo" --path "$file_path" | jq -r '.sha')"; then
echo "Done."
else
print_error "Failed to update contents of $file_path."
fi
}
add_repo_collaborator() {
while (($#)); do
case $1 in
--repo) local repo=$2 ;;
--user) local user=$2 ;;
--permission) local permission=$2 ;;
esac
shift
done
local collaborators_endpoint="$org_repos_endpoint/$repo/collaborators/$user"
echo -n "Inviting $user as a collaborator to $repo... "
if http --check-status --ignore-stdin --quiet --auth "$TOKEN_OWNER:$TEACHER_PAT" \
PUT "$collaborators_endpoint" \
permission="$permission"; then
echo "Done."
else
print_error "Failed to add collaborator $user to $repo."
fi
}
create_issue() {
while (($#)); do
case $1 in
--repo) local repo=$2 ;;
--title) local title=$2 ;;
--body) local body=$2 ;;
esac
shift
done
echo -n "Creating issue: $title... "
# Wait for 2 second to avoid secondary rate limiting, 1 second was nog enough
sleep 2
if http --check-status --ignore-stdin --quiet --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$org_repos_endpoint/$repo/issues" \
title="$title" \
body="$body"; then
echo "Done."
fi
}
create_pull_request() {
while (($#)); do
case $1 in
--title) local title=$2 ;;
--body) local body=$2 ;;
--head) local head=$2 ;;
--base) local base=$2 ;;
--repo) local repo=$2 ;;
esac
shift
done
echo -n "Creating pull request: $title... "
# Wait for 2 seconds to avoid secondary rate limiting, 1 second was nog enough
sleep 2
if http --check-status --ignore-stdin --quiet --auth \
"$TOKEN_OWNER:$TEACHER_PAT" "$org_repos_endpoint/$repo/pulls" \
title="$title" \
head="$head" \
base="$base" \
body="$body"; then
echo "Done."
fi
}
get_collaborators() {
local repo_name=$1
local repo_endpoint="https://$INSTANCE_URL/repos/$CLASS_ORG/$repo_name"
# Get array of repository collaborators
IFS=" " read -ra collaborators <<<"$(
http -a "$TOKEN_OWNER:$TEACHER_PAT" GET "$repo_endpoint/collaborators" \
affiliation==direct per_page==100 | jq -r 'map(.login) | unique | @sh' | tr -d \'
)" >>log.out 2>&1
# Get array of repository invitees
IFS=" " read -ra invitees <<<"$(
http -a "$TOKEN_OWNER:$TEACHER_PAT" GET "$repo_endpoint/invitations" |
jq -r 'map(.invitee.login) | unique | @sh' | tr -d \'
)" >>log.out 2>&1
# Invitees are collaborators too
collaborators+=("${invitees[@]}")
}