#!/bin/bash function usage { echo "basename ${0} Get src of group in Gitlab" echo "--help | -h Print this help" echo "--token-file= | -t Set file with Gitlab token" echo "--gitlab-url=* | -g Set Gtilab URL" echo "--group-id=* | -i Set group id" exit 1 } function checks { if [ -z "$TOKEN_FILE" ] then echo "Missing token file" usage fi if [ -z "$GITLAB_URL" ] then echo "Missing gitlab url" usage fi if [ -z "$GROUP" ] then echo "Missing group id" usage fi if [ ! -f "$TOKEN_FILE" ] then echo "Token file not found" usage fi } function get_subgroups { for PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${1}/subgroups?per_page=100 2>/dev/null | jq -r '."x-total-pages"[0]')) do curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${1}/subgroups?per_page=100&page=${PAGE:1}" | jq -r '.[].id' done } function clone_project { PROJECT_INFO=$(curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/projects/${1} | jq .) echo "INFO: Check project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" if [ ! -d "$(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" ] then echo "INFO: Clone project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" mkdir -p $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace') git clone $(echo ${PROJECT_INFO} | jq -r '. | "\(.ssh_url_to_repo) \(.path_with_namespace)"') fi } # Map long options to short ones and rebuild argv translated=() while (( $# )); do case "$1" in --help) translated+=("-h"); shift ;; --token-file=*) translated+=("-t" "${1#*=}"); shift ;; --gitlab-url=*) translated+=("-g" "${1#*=}"); shift ;; --group-id=*) translated+=("-i" "${1#*=}"); shift ;; --) translated+=("--"); shift; translated+=("$@"); set -- ;; *) translated+=("$1"); shift ;; esac done set -- "${translated[@]}" declare GITLAB_URL="https://git.mws-team.ru" declare TOKEN_FILE="" declare GROUP="" while getopts ":ht:g:i:" opt; do case "$opt" in h) usage ;; t) TOKEN_FILE=$OPTARG ;; g) GITLAB_URL=$OPTARG ;; i) GROUP=$OPTARG ;; :) echo "Missing arg for -$OPTARG"; usage ;; \?) echo "Invalid option: -$OPTARG"; usage ;; esac done checks declare -r TOKEN=$(cat ${TOKEN_FILE}) for P_PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${GROUP}/projects?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]')) do echo "INFO: Check Projects" for PROJECT in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${GROUP}/projects?per_page=100&page=${P_PAGE}" | jq -r '.[].id') do clone_project ${PROJECT} done done for SG_PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${GROUP}/subgroups?per_page=100 2>/dev/null | jq -r '."x-total-pages"[0]')) do for SUBGROUP in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${GROUP}/subgroups?per_page=100&page=${SG_PAGE}" | jq -r '.[].id') do echo "INFO: Check Subgroups" for P_SG_PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${SUBGROUP}/projects?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]')) do for PROJECT in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${SUBGROUP}/projects?per_page=100&page=${P_SG_PAGE}" | jq -r '.[].id') do clone_project ${PROJECT} done done done done echo "OK"