Utoljára aktív 6 days ago

Получить исходники из конкретной группы

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