Last active 6 days ago

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

botanikanet revised this gist 6 days ago. Go to revision

No changes

botanikanet revised this gist 6 days ago. Go to revision

1 file changed, 114 insertions

get_group_src.sh(file created)

@@ -0,0 +1,114 @@
1 + #!/bin/bash
2 +
3 + function 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 +
12 + function 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 +
38 + function 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 +
45 + function 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
57 + translated=()
58 + while (( $# )); 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
67 + done
68 + set -- "${translated[@]}"
69 +
70 + declare GITLAB_URL="https://git.mws-team.ru"
71 + declare TOKEN_FILE=""
72 + declare GROUP=""
73 +
74 + while 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
83 + done
84 +
85 + checks
86 +
87 + declare -r TOKEN=$(cat ${TOKEN_FILE})
88 +
89 + 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]'))
90 + do
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 +
97 + done
98 + 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]'))
99 + do
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
113 + done
114 + echo "OK"
Newer Older