@@ -10,10 +10,8 @@ init_cache() {
1010 git_clone " $( get_remote_url " ${user_project_ns} " ) " " ${user_cache_folder} "
1111}
1212sync_cache () {
13- rsync -a --delete --exclude .git/ " ${WORKING_DIR} /" " ${user_cache_folder} " \
13+ rsync -a --delete --exclude .git/ ${3 :- } " ${1 :- ${ WORKING_DIR}} /" " ${2 :- ${ user_cache_folder} } " \
1414 || exception " Unable to rsync files"
15- [[ " ${UPDATE_LINKS} " == ' true' ]] \
16- && update_links
1715}
1816get_cache () {
1917 [[ ! -d " ${user_cache_folder} " ]] \
@@ -26,12 +24,22 @@ get_cache() {
2624 [[ " ${actual_remote_ns} " != " ${user_project_ns} " ]] \
2725 && exception ' Invalid user project remote origin url'
2826 git_pull " ${user_cache_folder} " ' origin' " ${SOURCE_BRANCH} :${SOURCE_BRANCH} "
27+ git_pull " ${user_cache_folder} " ' origin' " ${SUBMIT_BRANCH} :${SUBMIT_BRANCH} "
2928 git_pull " ${user_cache_folder} " ' origin' " ${user_default_branch} :${user_default_branch} "
3029}
30+ # TODO
3131update_links () {
32+ local user_cache_folder=" ${1:- ${user_cache_folder} } "
33+ local PROJECT_LINK=" ${2:- ${PROJECT_LINK} } "
34+ local user_project_ns=" ${3:- ${user_project_ns} } "
35+ local PROJECT_BRANCH=" ${4:- ${PROJECT_BRANCH} } "
36+ local user_default_branch=" ${5:- ${user_default_branch} } "
37+ [[ " ${UPDATE_LINKS} " != ' true' ]] \
38+ && return
3239 declare -r user_readme_path=" ${user_cache_folder} /${README_FILE} "
3340 sed -i " s~${PROJECT_LINK} ~${user_project_ns} ~g" " ${user_readme_path} "
3441 sed -i " s~/${PROJECT_BRANCH} /\(pipeline\|raw\|file\)~/${user_default_branch} /\1~g" " ${user_readme_path} "
42+ sed -i " s~/\(pipeline\|raw\|file\)/${PROJECT_BRANCH} ~/\1/${user_default_branch} ~g" " ${user_readme_path} "
3543 sed -i " s~ref=${PROJECT_BRANCH} ~ref=${user_default_branch} ~g" " ${user_readme_path} "
3644 sed -i " s~\(https://[^/]*\)/${PROJECT_LINK#*/ } ~\1/${user_project_ns#*/ } ~g" " ${user_readme_path} "
3745}
@@ -48,7 +56,7 @@ init_user_repo() {
4856 || exit 1
4957 project_id=" $( create_user_project) " \
5058 || exit 1
51- add_developer
59+ add_member
5260 copy_issues
5361 init_cache
5462 git_commit " ${user_cache_folder} " ' --allow-empty' ' -m "Initial commit"'
@@ -57,20 +65,27 @@ init_user_repo() {
5765 user_default_branch=" $( get_default_branch) " \
5866 || exit 1
5967 sync_cache
68+ update_links
6069 git_add_all " ${user_cache_folder} "
6170 git_commit " ${user_cache_folder} " ' --allow-empty' ' -m "Create assignment"'
6271 git_push " ${user_cache_folder} " ' --all'
6372 git_checkout " ${user_cache_folder} " " -B${SOURCE_BRANCH} "
73+ git_checkout " ${user_cache_folder} " " -B${SUBMIT_BRANCH} "
6474 git_push " ${user_cache_folder} " ' --all'
6575 remove_protected_branch
6676 set_protected_branch " ${user_default_branch} " " ${DEV_ACCESS_LEVEL} "
6777 set_protected_branch " ${SOURCE_BRANCH} " " ${MAINTAINER_ACCESS_LEVEL} "
78+ set_protected_branch " ${SUBMIT_BRANCH} " " ${MAINTAINER_ACCESS_LEVEL} "
6879 set_public_pages " ${project_id} " > /dev/null
80+ create_project_submit_token
6981}
7082status_empty () {
7183 get_cache
84+ validate \
85+ || return 1
7286 git_checkout " ${user_cache_folder} " " ${SOURCE_BRANCH} "
7387 sync_cache
88+ update_links
7489 git_status_empty " ${user_cache_folder} "
7590}
7691update_user_repo () {
@@ -140,6 +155,57 @@ read_project_info() {
140155 && issues_src=" ${ISSUES} " issues_count_src=" ${ISSUES_COUNT} "
141156 msg_end
142157}
158+ validate_ci () {
159+ local branch=" ${1} "
160+ cmp -s " ${WORKING_DIR} /.gitlab-ci.yml" <( git_show " origin/${branch} :.gitlab-ci.yml" " ${user_cache_folder} " ) \
161+ || echo " [WARNING] CI validation failed on ${branch} ." >&2
162+ }
163+ validate () {
164+ # conform branches
165+ ! git_remote_branch_exists " ${SOURCE_BRANCH} " " ${user_cache_folder} " \
166+ && git_checkout " ${user_cache_folder} " " -B${SOURCE_BRANCH} " \
167+ && git_push " ${user_cache_folder} " origin " ${SOURCE_BRANCH} "
168+ ! git_remote_branch_exists " ${SUBMIT_BRANCH} " " ${user_cache_folder} " \
169+ && git_checkout " ${user_cache_folder} " " -B${SUBMIT_BRANCH} " \
170+ && git_push " ${user_cache_folder} " origin " ${SUBMIT_BRANCH} "
171+ # check ci hack
172+ validate_ci " ${user_default_branch} "
173+ validate_ci " ${SOURCE_BRANCH} "
174+ validate_ci " ${SUBMIT_BRANCH} "
175+ }
176+ update_default_branch () {
177+ [[ -z " ${DEFAULT_BRANCH_NAMESPACE} " ]] \
178+ && return
179+ declare dest_default_branch
180+ declare dest_project_ns
181+ dest_default_branch=" $( get_default_branch) "
182+ dest_project_ns=" ${user_project_ns} "
183+ declare dest_cache_folder
184+ declare user_cache_folder
185+ declare user_project_name
186+ declare user_project_ns
187+ declare user_default_branch
188+ dest_cache_folder=" ${1} "
189+ user_project_name=" ${PREFIX}${user_name} "
190+ user_project_ns=" ${DEFAULT_BRANCH_NAMESPACE} /${user_project_name} "
191+ user_cache_folder=" $( get_cache_folder) "
192+ user_default_branch=" $( get_default_branch) " \
193+ || return 1
194+ get_cache
195+ git_checkout " ${user_cache_folder} " " -B${user_default_branch} "
196+ git_checkout " ${dest_cache_folder} " " -B${dest_default_branch} "
197+ sync_cache " ${user_cache_folder} " " ${dest_cache_folder} " --exclude=.gitlab-ci.yml
198+ # user_cache_folder ~ dest
199+ # PROJECT_LINK ~ src
200+ # user_project_ns ~ dest
201+ # PROJECT_BRANCH ~ src
202+ # user_default_branch ~ dest
203+ update_links " ${dest_cache_folder} " " $( get_remote_namespace " ${user_cache_folder} " ) " \
204+ " ${dest_project_ns} " " ${user_default_branch} " " ${dest_default_branch} "
205+ git_add_all " ${dest_cache_folder} "
206+ git_commit " ${dest_cache_folder} " ' --allow-empty' ' -m "Update from user repo"'
207+ git_push " ${dest_cache_folder} " ' --all'
208+ }
143209process_users () {
144210 declare user_name
145211 declare user_project_name
@@ -148,6 +214,7 @@ process_users() {
148214 declare user_default_branch
149215 declare -i valid=0
150216 declare -i invalid=0
217+ declare status_empty_code
151218 [[ -z " ${CACHE_FOLDER} " ]] \
152219 && CACHE_FOLDER=" $( mktemp -d) "
153220 # shellcheck disable=SC2013
@@ -164,13 +231,24 @@ process_users() {
164231 && msg_end SKIPPED \
165232 && continue
166233 user_cache_folder=" $( get_cache_folder) "
167- ! project_exists \
168- && init_user_repo \
169- && msg_end CREATED \
170- && continue
234+ if ! project_exists; then
235+ init_user_repo \
236+ && update_default_branch " ${user_cache_folder} " \
237+ && msg_end CREATED
238+ continue
239+ fi
171240 user_default_branch=" $( get_default_branch) " \
172241 || exit 1
173- status_empty \
242+ status_empty
243+ status_empty_code=$?
244+ update_default_branch " ${user_cache_folder} " \
245+ || continue
246+ project_id=" $( get_project_id " ${user_project_ns} " ) " \
247+ || continue
248+ create_project_submit_token
249+ user_id=" $( get_user_id) "
250+ add_member
251+ [[ " $status_empty_code " == 0 ]] \
174252 && msg_end PASSED \
175253 && continue
176254 update_user_repo \
@@ -197,6 +275,7 @@ distribute() {
197275# global constants
198276declare -r README_FILE=' README.md'
199277declare -r SOURCE_BRANCH=' source'
278+ declare -r SUBMIT_BRANCH=' submit'
200279declare -r ALWAYS=' always'
201280declare -r NEVER=' never'
202281declare -r AUTO=' auto'
@@ -214,6 +293,8 @@ declare PREFIX=''
214293declare GROUP_ID=' '
215294declare PROJECT_LINK=' '
216295declare PROJECT_ID=' '
296+ declare -i MEMBER_ROLE=$DEV_ACCESS_LEVEL
297+ declare DEFAULT_BRANCH_NAMESPACE=' '
217298# shellcheck disable=SC2034
218299declare PROJECT_NAME=' '
219300declare PROJECT_BRANCH=' '
@@ -227,8 +308,8 @@ declare FORCE_MERGE=0
227308
228309# get options
229310declare OPT
230- OPT=" $( getopt --name " ${0} " --options ' a:hi:lno:p:s:m' \
231- --longoptions ' assign:,help,process-issues:,update-links,dry-run,output-dir:,prefix:,namespace:,force-merge' \
311+ OPT=" $( getopt --name " ${0} " --options ' a:d: hi:lno:p:s:r :m' \
312+ --longoptions ' assign:,help,process-issues:,update-links,default-branch-namespace:, dry-run,output-dir:,prefix:,member-role :,namespace:,force-merge' \
232313 -- " $@ " ) " \
233314 && eval set -- " ${OPT} " \
234315 || exit 1
@@ -242,6 +323,10 @@ while (( $# > 0 )); do
242323 || exception ' Invalid option ASSIGN value'
243324 shift 2
244325 ;;
326+ -d|--default-branch-namespace)
327+ DEFAULT_BRANCH_NAMESPACE=" ${2} "
328+ shift 2
329+ ;;
245330 -h|--help)
246331 print_usage \
247332 && exit 0
@@ -272,10 +357,12 @@ while (( $# > 0 )); do
272357 || exception ' Unsupported PREFIX value' 2
273358 shift 2
274359 ;;
360+ -r|--member-role)
361+ MEMBER_ROLE=" ${2} "
362+ shift 2
363+ ;;
275364 -s|--namespace)
276365 NAMESPACE=" ${2} "
277- [[ " ${NAMESPACE} " =~ ^[a-z0-9-]{2,}(/[a-z0-9-]{2,})* $ ]] \
278- || exception " Unsupported REMOTE_NAMESPACE '$NAMESPACE ' dirname" 2
279366 shift 2
280367 ;;
281368 -o|--output-dir)
0 commit comments