gitlab copy all projects
· 5.6 KiB · Text
Raw
#!/bin/bash
function checks {
if [ -z "$1" ]
then
echo "Missing token file"
exit 1
fi
if [ -z "$2" ]
then
echo "Missing gitlab url"
exit 1
fi
if [ ! -f "$1" ]
then
echo "Token file not found"
exit 1
fi
}
function get_subgroups {
curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${1}/subgroups?per_page=100 | jq -r '.[].id'
}
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
}
checks ${1} ${2}
# Vars
declare -r GITLAB_URL=${2}
declare -r TOKEN=$(cat ${1})
for PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]'))
do
for GROUP in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups?per_page=100&page=${PAGE}" | jq -r '.[].id')
do
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
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
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
done
done
echo "OK"
| 1 | #!/bin/bash |
| 2 | |
| 3 | function checks { |
| 4 | if [ -z "$1" ] |
| 5 | then |
| 6 | echo "Missing token file" |
| 7 | exit 1 |
| 8 | fi |
| 9 | |
| 10 | if [ -z "$2" ] |
| 11 | then |
| 12 | echo "Missing gitlab url" |
| 13 | exit 1 |
| 14 | fi |
| 15 | |
| 16 | if [ ! -f "$1" ] |
| 17 | then |
| 18 | echo "Token file not found" |
| 19 | exit 1 |
| 20 | fi |
| 21 | } |
| 22 | |
| 23 | function get_subgroups { |
| 24 | curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups/${1}/subgroups?per_page=100 | jq -r '.[].id' |
| 25 | } |
| 26 | |
| 27 | function clone_project { |
| 28 | PROJECT_INFO=$(curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/projects/${1} | jq .) |
| 29 | echo "INFO: Check project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" |
| 30 | if [ ! -d "$(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" ] |
| 31 | then |
| 32 | echo "INFO: Clone project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" |
| 33 | mkdir -p $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace') |
| 34 | git clone $(echo ${PROJECT_INFO} | jq -r '. | "\(.ssh_url_to_repo) \(.path_with_namespace)"') |
| 35 | fi |
| 36 | } |
| 37 | |
| 38 | checks ${1} ${2} |
| 39 | |
| 40 | # Vars |
| 41 | declare -r GITLAB_URL=${2} |
| 42 | declare -r TOKEN=$(cat ${1}) |
| 43 | |
| 44 | for PAGE in $(seq 1 $(curl -X HEAD -w '%{header_json}' --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/groups?per_page=100 2>/dev/null| jq -r '."x-total-pages"[0]')) |
| 45 | do |
| 46 | for GROUP in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups?per_page=100&page=${PAGE}" | jq -r '.[].id') |
| 47 | do |
| 48 | 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]')) |
| 49 | do |
| 50 | 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') |
| 51 | do |
| 52 | clone_project ${PROJECT} |
| 53 | done |
| 54 | |
| 55 | done |
| 56 | 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]')) |
| 57 | do |
| 58 | 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') |
| 59 | do |
| 60 | 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]')) |
| 61 | do |
| 62 | 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') |
| 63 | do |
| 64 | clone_project ${PROJECT} |
| 65 | done |
| 66 | |
| 67 | done |
| 68 | |
| 69 | done |
| 70 | done |
| 71 | done |
| 72 | done |
| 73 | echo "OK" |
| 74 |