Last active 5 months ago

botanikanet revised this gist 5 months ago. Go to revision

1 file changed, 73 insertions, 1 deletion

gitlab copy all projects

@@ -1 +1,73 @@
1 - TOKEN_FILE="${HOME}/work/tst.token; GITLAB_ENDPOINT="https://gitlab.example"; for GROUP in $(curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_ENDPOINT}/api/v4/groups" | jq .[].id); do for ID in $(curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_ENDPOINT}/api/v4/groups/${GROUP}/subgroups" | jq -r '.[].id?'); do for LINE in $(curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_ENDPOINT}/api/v4/groups/${ID}" | jq -r '.projects[] | "\(.path_with_namespace);\(.ssh_url_to_repo)"'); do mkdir -p ${LINE%;*}; git clone ${LINE#*;} ${LINE%;*}; done ; done; done
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"

botanikanet revised this gist 1 year ago. Go to revision

1 file changed, 1 insertion

gitlab copy all projects (file created)

@@ -0,0 +1 @@
1 + TOKEN_FILE="${HOME}/work/tst.token; GITLAB_ENDPOINT="https://gitlab.example"; for GROUP in $(curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_ENDPOINT}/api/v4/groups" | jq .[].id); do for ID in $(curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_ENDPOINT}/api/v4/groups/${GROUP}/subgroups" | jq -r '.[].id?'); do for LINE in $(curl --silent --header "PRIVATE-TOKEN: $(cat ${TOKEN_FILE})" "${GITLAB_ENDPOINT}/api/v4/groups/${ID}" | jq -r '.projects[] | "\(.path_with_namespace);\(.ssh_url_to_repo)"'); do mkdir -p ${LINE%;*}; git clone ${LINE#*;} ${LINE%;*}; done ; done; done
Newer Older