Last active 6 days ago

Получить все исходники, доступные в Gitlab

Revision d5361568776eef3472a8674be36be1aa51a48277

get_all_src.sh Raw
1#!/bin/bash
2set -ex
3
4usage() {
5 echo "basename ${0} get all src from Gitlab"
6 echo "--help | -h Print this help"
7 echo "--token-file= | -t <file> Set file with Gitlab token"
8 echo "--gitlab-url=* | -g <url> Set Gtilab URL"
9 exit 1
10}
11
12# Map long options to short ones and rebuild argv
13translated=()
14while (( $# )); do
15 case "$1" in
16 --help) translated+=("-h"); shift ;;
17 --token-file=*) translated+=("-t" "${1#*=}"); shift ;;
18 --gitlab-url=*) translated+=("-g" "${1#*=}"); shift ;;
19 --) translated+=("--"); shift; translated+=("$@"); set -- ;;
20 *) translated+=("$1"); shift ;;
21 esac
22done
23set -- "${translated[@]}"
24
25declare GITLAB_URL="https://git.mws-team.ru"
26declare TOKEN_FILE=""
27
28while getopts ":ht:g:" opt; do
29 case "$opt" in
30 h) usage ;;
31 t) TOKEN_FILE=$OPTARG ;;
32 g) GITLAB_URL=$OPTARG ;;
33 :) echo "Missing arg for -$OPTARG"; usage ;;
34 \?) echo "Invalid option: -$OPTARG"; usage ;;
35 esac
36done
37
38function checks {
39 if [ -z "${TOKEN_FILE}" ]
40 then
41 echo "Missing token file"
42 usage
43 fi
44
45 if [ -z "${GITLAB_URL}" ]
46 then
47 echo "Missing gitlab url"
48 usage
49 fi
50
51 if [ ! -f "${TOKEN_FILE}" ]
52 then
53 echo "Token file not found"
54 usage
55 fi
56}
57
58function get_subgroups {
59 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]'))
60 do
61 curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups/${1}/subgroups?per_page=100&page=${PAGE}" | jq -r '.[].id'
62 done
63}
64
65function clone_project {
66 PROJECT_INFO=$(curl --silent --header "Authorization: Bearer ${TOKEN}" ${GITLAB_URL}/api/v4/projects/${1} | jq .)
67 echo "INFO: Check project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')"
68 if [ ! -d "$(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')" ]
69 then
70 echo "INFO: Clone project $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')"
71 mkdir -p $(echo ${PROJECT_INFO} | jq -r '.path_with_namespace')
72 git clone $(echo ${PROJECT_INFO} | jq -r '. | "\(.ssh_url_to_repo) \(.path_with_namespace)"')
73 fi
74}
75
76checks
77
78TOKEN=$(cat ${TOKEN_FILE})
79
80for 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]'))
81do
82 for GROUP in $(curl --silent --header "Authorization: Bearer ${TOKEN}" "${GITLAB_URL}/api/v4/groups?per_page=100&page=${PAGE}" | jq -r '.[].id')
83 do
84 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]'))
85 do
86 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')
87 do
88 clone_project ${PROJECT}
89 done
90 done
91 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]'))
92 do
93 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')
94 do
95 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]'))
96 do
97 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')
98 do
99 clone_project ${PROJECT}
100 done
101 done
102 done
103 done
104 done
105done
106echo "OK"
107