-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribute
executable file
·404 lines (398 loc) · 13.6 KB
/
distribute
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env bash
## functions
get_cache_folder() {
printf -- "%s/%s" "${CACHE_FOLDER}" "${user_project_ns}"
}
init_cache() {
# shellcheck disable=SC2155
rm -rf "${user_cache_folder}"
git_clone "$(get_remote_url "${user_project_ns}")" "${user_cache_folder}"
}
sync_cache() {
# shellcheck disable=SC2086
rsync -a --delete --exclude .git/ ${3:-} "${1:-${WORKING_DIR}}/" "${2:-${user_cache_folder}}" \
|| exception "Unable to rsync files"
}
get_cache() {
[[ ! -d "${user_cache_folder}" ]] \
&& init_cache "${user_cache_folder}" \
&& return
# verify local remote
declare actual_remote_ns
actual_remote_ns="$(get_remote_namespace "${user_cache_folder}")" \
|| exit 1
[[ "${actual_remote_ns}" != "${user_project_ns}" ]] \
&& exception 'Invalid user project remote origin url'
git_pull "${user_cache_folder}" 'origin' "${SOURCE_BRANCH}:${SOURCE_BRANCH}"
git_pull "${user_cache_folder}" 'origin' "${SUBMIT_BRANCH}:${SUBMIT_BRANCH}"
git_pull "${user_cache_folder}" 'origin' "${DRAFT_BRANCH}:${DRAFT_BRANCH}"
git_pull "${user_cache_folder}" 'origin' "${user_default_branch}:${user_default_branch}"
}
update_links() {
local user_cache_folder="${1:-${user_cache_folder}}"
local PROJECT_LINK="${2:-${PROJECT_LINK}}"
local user_project_ns="${3:-${user_project_ns}}"
local PROJECT_BRANCH="${4:-${PROJECT_BRANCH}}"
local user_default_branch="${5:-${user_default_branch}}"
local PROJECT_ID="${6:-${PROJECT_ID}}"
local project_id="${7:-${project_id}}"
[[ "${UPDATE_LINKS}" != 'true' ]] \
&& return
declare -r user_readme_path="${user_cache_folder}/${README_FILE}"
sed -i "s~${PROJECT_LINK}~${user_project_ns}~g" "${user_readme_path}"
sed -i "s~/${PROJECT_BRANCH}/\(pipeline\|raw\|file\)~/${user_default_branch}/\1~g" "${user_readme_path}"
sed -i "s~/\(pipeline\|raw\|file\)/${PROJECT_BRANCH}~/\1/${user_default_branch}~g" "${user_readme_path}"
sed -i "s~ref=${PROJECT_BRANCH}~ref=${user_default_branch}~g" "${user_readme_path}"
sed -i "s~\(https://[^/]*\)/${PROJECT_LINK#*/}~\1/${user_project_ns#*/}~g" "${user_readme_path}"
sed -i "s~/projects/${PROJECT_ID}/jobs/~/projects/${project_id}/jobs/~g" "${user_readme_path}"
}
init_user_repo() {
declare user_id=''
[[ "${ASSIGN}" == "${NEVER}" ]] \
|| user_id="$(get_user_id)" \
|| exit 1
[[ "${ASSIGN}" == "${ALWAYS}" && -z "${user_id}" ]] \
&& exception "User ${user_name} does not exist"
[[ -n "${GROUP_ID}" ]] \
|| GROUP_ID="$(get_group_id "${NAMESPACE}" 2>/dev/null)" \
|| GROUP_ID="$(create_ns "${NAMESPACE}")" \
|| exit 1
project_id="$(create_user_project)" \
|| exit 1
add_member
copy_issues
init_cache
git_commit "${user_cache_folder}" '--allow-empty' '-m "Initial commit"'
git_push "${user_cache_folder}" '--all'
declare user_default_branch
user_default_branch="$(get_default_branch)" \
|| exit 1
sync_cache
update_links
git_add_all "${user_cache_folder}"
git_commit "${user_cache_folder}" '--allow-empty' '-m "Create assignment"'
git_push "${user_cache_folder}" '--all'
git_checkout "${user_cache_folder}" "-B${SOURCE_BRANCH}"
git_checkout "${user_cache_folder}" "-B${DRAFT_BRANCH}"
git_checkout "${user_cache_folder}" "-B${SUBMIT_BRANCH}"
git_push "${user_cache_folder}" '--all'
remove_protected_branch
set_protected_branch "${user_default_branch}" "${DEV_ACCESS_LEVEL}"
set_protected_branch "${SOURCE_BRANCH}" "${MAINTAINER_ACCESS_LEVEL}"
set_protected_branch "${DRAFT_BRANCH}" "${DEV_ACCESS_LEVEL}"
set_protected_branch "${SUBMIT_BRANCH}" "${MAINTAINER_ACCESS_LEVEL}"
set_public_pages "${project_id}" >/dev/null
create_project_submit_token
}
status_empty () {
get_cache
validate \
|| return 1
git_checkout "${user_cache_folder}" "${SOURCE_BRANCH}"
sync_cache
update_links
git_status_empty "${user_cache_folder}"
}
update_user_repo() {
git_add_all "${user_cache_folder}"
git_commit "${user_cache_folder}" '-m "Update assignment"'
git_push "${user_cache_folder}" '--all'
if [[ $FORCE_MERGE == 1 ]]; then
git_checkout "${user_cache_folder}" "${user_default_branch}"
git_merge "${user_cache_folder}" '--no-ff' "${SOURCE_BRANCH}"
git_push "${user_cache_folder}" origin "${user_default_branch}"
git_checkout "${user_cache_folder}" "${SOURCE_BRANCH}"
else
request_exists \
|| create_request
fi
copy_issues
set_public_pages "${project_id}" >/dev/null
}
read_issues() {
local pid="${1:?project id missing}"
local label="${2}"
ISSUES="$(gitlab_api "api/v4/projects/${pid}/issues?labels=${label}")" \
&& ISSUES_COUNT="$(jq length <<< "${ISSUES}")" \
|| exit 1
}
copy_issues() {
declare i issue_src issues_dst issues_count_dst
declare -A description=()
read_issues "${project_id}" \
&& issues_dst="${ISSUES}" issues_count_dst="${ISSUES_COUNT}"
for (( i=0; i < issues_count_dst; i++ )); do
description+=( ["$(jq -r ".[${i}].title" <<< "${issues_dst}")"]="$(jq -r ".[${i}].description" <<< "${issues_dst}")" )
done
for (( i=0; i < issues_count_src; i++ )); do
issue_src="$(jq ".[${i}] | {title, description, due_date}" <<< "${issues_src}")"
[[ "${description["$(jq -r ".title" <<< "${issue_src}")"]}" == "$(jq -r ".description" <<< "${issue_src}")" ]] \
&& continue
#printf -- "[issues] skipping '%s'\n" "$(jq -r ".title" <<< "${issue_src}")'" >&2
# Update existing issue
# ToDo
# Add new issue
#printf -- "[issues] adding '%s'\n" "${issue_src//$'\n'/|}" >&2
[[ -n "${user_id}" ]] \
&& issue_src="$(jq --arg a "${user_id}" '. + {assignee_ids:[$a]}' <<< "${issue_src}")"
gitlab_api "api/v4/projects/${project_id}/issues" "${issue_src}" >/dev/null
done
}
read_project_info() {
msg_start 'Getting project information'
[[ -z "${ISSUES_LABEL}" && "${UPDATE_LINKS}" == 'false' && -n "${NAMESPACE}" ]] \
&& msg_end SKIPPED \
&& return
[[ -d "${WORKING_DIR}/.git" ]] \
|| exception 'GitLab project not found in WORKING_DIR'
PROJECT_LINK="$(get_remote_namespace "${WORKING_DIR}")" \
&& PROJECT_ID="$(get_project_id "${PROJECT_LINK}")" \
&& PROJECT_NAME="$(get_project_name "${PROJECT_LINK}")" \
&& PROJECT_BRANCH="$(git_current_branch "${WORKING_DIR}")" \
|| exit 1
[[ -z "${NAMESPACE}" ]] \
&& NAMESPACE="$(dirname "${PROJECT_LINK}")"
read_issues "${PROJECT_ID}" "${ISSUES_LABEL}" \
&& issues_src="${ISSUES}" issues_count_src="${ISSUES_COUNT}"
msg_end
}
validate_ci() {
local branch="${1}"
cmp -s "${WORKING_DIR}/.gitlab-ci.yml" <(git_show "origin/${branch}:.gitlab-ci.yml" "${user_cache_folder}") \
|| echo "[WARNING] CI validation failed on ${branch}." >&2
}
validate() {
# conform branches
! git_remote_branch_exists "${SOURCE_BRANCH}" "${user_cache_folder}" \
&& git_checkout "${user_cache_folder}" "-B${SOURCE_BRANCH}" \
&& git_push "${user_cache_folder}" origin "${SOURCE_BRANCH}"
! git_remote_branch_exists "${DRAFT_BRANCH}" "${user_cache_folder}" \
&& git_checkout "${user_cache_folder}" "-B${DRAFT_BRANCH}" \
&& git_push "${user_cache_folder}" origin "${DRAFT_BRANCH}"
! git_remote_branch_exists "${SUBMIT_BRANCH}" "${user_cache_folder}" \
&& git_checkout "${user_cache_folder}" "-B${SUBMIT_BRANCH}" \
&& git_push "${user_cache_folder}" origin "${SUBMIT_BRANCH}"
# check ci hack
validate_ci "${user_default_branch}"
validate_ci "${SOURCE_BRANCH}"
validate_ci "${DRAFT_BRANCH}"
validate_ci "${SUBMIT_BRANCH}"
}
update_default_branch() {
[[ -z "${DEFAULT_BRANCH_NAMESPACE}" ]] \
&& return
declare dest_default_branch
declare dest_project_ns
declare dest_project_id
dest_default_branch="$(get_default_branch)"
dest_project_ns="${user_project_ns}"
dest_project_id="${project_id}"
declare dest_cache_folder
declare user_cache_folder
declare user_project_name
declare user_project_ns
declare user_project_id
declare user_default_branch
dest_cache_folder="${1}"
user_project_name="${PREFIX}${user_name}"
user_project_ns="${DEFAULT_BRANCH_NAMESPACE}/${user_project_name}"
user_project_id="$(get_project_id "${user_project_ns}")"
user_cache_folder="$(get_cache_folder)"
user_default_branch="$(get_default_branch)" \
|| return 1
get_cache
git_checkout "${user_cache_folder}" "-B${user_default_branch}"
git_checkout "${dest_cache_folder}" "-B${dest_default_branch}"
sync_cache "${user_cache_folder}" "${dest_cache_folder}" --exclude=.gitlab-ci.yml
update_links "${dest_cache_folder}" "$(get_remote_namespace "${user_cache_folder}")" \
"${dest_project_ns}" "${user_default_branch}" "${dest_default_branch}" "${user_project_id}" "${dest_project_id}"
git_add_all "${dest_cache_folder}"
git_commit "${dest_cache_folder}" '--allow-empty' '-m "Update from user repo"'
git_push "${dest_cache_folder}" '--all'
}
process_users() {
declare user_name
declare user_project_name
declare user_project_ns
declare user_cache_folder
declare user_default_branch
declare -i valid=0
declare -i invalid=0
declare status_empty_code
# shellcheck disable=SC2013
for user_name in $(cat <&3); do
user_project_name="${PREFIX}${user_name}"
user_project_ns="${NAMESPACE}/${user_project_name}"
msg_start "${user_project_ns}"
[[ ! "${user_name}" =~ ^[a-zA-Z0-9][a-z0-9_.-]*$ ]] \
&& msg_end INVALID \
&& invalid+=1 \
&& continue
valid+=1
[[ "${DRY_RUN}" == 'true' ]] \
&& msg_end SKIPPED \
&& continue
user_cache_folder="$(get_cache_folder)"
if ! project_exists; then
init_user_repo \
&& project_id="$(get_project_id "${user_project_ns}")" \
&& update_default_branch "${user_cache_folder}" \
&& msg_end CREATED
continue
fi
user_default_branch="$(get_default_branch)" \
|| exit 1
status_empty
status_empty_code=$?
project_id="$(get_project_id "${user_project_ns}")" \
|| continue
create_project_submit_token
update_default_branch "${user_cache_folder}" \
|| continue
user_id="$(get_user_id)"
add_member
[[ "$status_empty_code" == 0 ]] \
&& msg_end PASSED \
&& continue
update_user_repo \
&& msg_end UPDATED
done
(( valid != 0 || invalid != 0 )) \
|| exception 'Empty or invalid stdin' 2
(( invalid == 0 )) \
|| exception "Invalid username occurred ${invalid} time(s)" 3
}
distribute() {
# shellcheck disable=SC2155
[[ ! -t 0 ]] \
|| exception 'Missing stdin' 2
exec 3<&0
acquire_token
read_project_info
[[ -d "${WORKING_DIR}/.git" ]] \
&& ! git_status_empty "${WORKING_DIR}" \
&& exception 'Git status is not empty'
[[ -z "${CACHE_FOLDER}" ]] \
&& CACHE_FOLDER="$(mktemp -d)"
decalre action='distribute'
[[ "${COLLECT}" == 1 ]] \
&& action='collect'
# shellcheck disable=SC1090
[[ "${COLLECT}" == 1 && -f "${DIR}/pre-${action}" ]] \
&& source "${DIR}/pre-${action}" "${CACHE_FOLDER}"
process_users
# shellcheck disable=SC1090
[[ "${COLLECT}" == 1 && -f "${DIR}/post-${action}" ]] \
&& source "${DIR}/post-${action}" "${CACHE_FOLDER}"
return 0
}
# global constants
declare -r README_FILE='README.md'
declare -r SOURCE_BRANCH='source'
declare -r DRAFT_BRANCH='draft'
declare -r SUBMIT_BRANCH='submit'
declare -r ALWAYS='always'
declare -r NEVER='never'
declare -r AUTO='auto'
declare -ri DEV_ACCESS_LEVEL=30
declare -ri MAINTAINER_ACCESS_LEVEL=40
# default variables
declare COLLECT=0
declare CACHE_FOLDER=''
declare DRY_RUN='false'
declare UPDATE_LINKS='false'
declare ASSIGN="${AUTO}"
declare ISSUES_LABEL=''
declare NAMESPACE=''
declare PREFIX=''
declare GROUP_ID=''
declare PROJECT_LINK=''
declare PROJECT_ID=''
declare -i MEMBER_ROLE=$DEV_ACCESS_LEVEL
declare DEFAULT_BRANCH_NAMESPACE=''
# shellcheck disable=SC2034
declare PROJECT_NAME=''
declare PROJECT_BRANCH=''
declare ISSUES=''
declare issues_src="$ISSUES"
declare -i ISSUES_COUNT=0
declare -i issues_count_src="${ISSUES_COUNT}"
# shellcheck disable=SC2034
declare ACADEMY_GITLAB_ACCESS_TOKEN
declare FORCE_MERGE=0
# get options
declare OPT
OPT="$(getopt --name "${0}" --options 'a:cd:hi:lno:p:s:r:m' \
--longoptions 'assign:,help,collect,process-issues:,update-links,default-branch-namespace:,dry-run,output-dir:,prefix:,member-role:,namespace:,force-merge' \
-- "$@")" \
&& eval set -- "${OPT}" \
|| exit 1
# process options
while (( $# > 0 )); do
case "${1}" in
-a|--assign)
ASSIGN="${2}"
[[ "${ASSIGN}" =~ ^(${ALWAYS}|${NEVER}|${AUTO})$ ]] \
|| exception 'Invalid option ASSIGN value'
shift 2
;;
-c|--collect)
COLLECT="1"
shift
;;
-d|--default-branch-namespace)
DEFAULT_BRANCH_NAMESPACE="${2}"
shift 2
;;
-h|--help)
print_usage \
&& exit 0
;;
-i|--process-issues)
ISSUES_LABEL="$2"
[[ "${ISSUES_LABEL}" =~ ^[a-z0-9][a-z0-9_-]+$ ]] \
|| exception 'Unsupported ISSUES_LABEL value' 2
shift 2
;;
-l|--update-links)
UPDATE_LINKS='true'
[[ -f "${WORKING_DIR}/${README_FILE}" ]] \
|| exception 'Readme file not found in WORKING_DIR'
shift
;;
-m|--force-merge)
FORCE_MERGE=1
shift
;;
-n|--dry-run)
DRY_RUN='true'
shift
;;
-p|--prefix)
PREFIX="${2}"
[[ "${PREFIX}" =~ ^[a-z0-9]+([a-z0-9._-]+)*$ ]] \
|| exception 'Unsupported PREFIX value' 2
shift 2
;;
-r|--member-role)
# shellcheck disable=SC2034
MEMBER_ROLE="${2}"
shift 2
;;
-s|--namespace)
NAMESPACE="${2}"
shift 2
;;
-o|--output-dir)
CACHE_FOLDER="${2}";
[[ -d "${CACHE_FOLDER}" ]] \
|| mkdir -p "${CACHE_FOLDER}" \
|| exception "Unable to create '${CACHE_FOLDER}' directory"
shift 2
;;
--) shift; break ;;
*) break ;;
esac
done
# run main function
distribute