-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitlab-check.sh
executable file
·288 lines (239 loc) · 6.9 KB
/
gitlab-check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
set -o errexit
SUBJECT="GitLab Checker"
START_TIME=$SECONDS
white=$(tput bold)
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 6)
bg_red=$(tput setab 1)
bg_green=$(tput setab 2)
bg_yellow=$(tput setab 3)
normal=$(tput sgr0)
OK="${green}OK${normal}"
FAIL="${red}FAIL${normal}"
function _echo
{
if [[ -z "$2" ]]
then
printf '%s' "$1"
else
printf '%s\n' "$1"
fi
}
function _echoTest
{
_echo "${yellow}$1 test${normal} ... "
}
function _ok
{
_echo $OK;
}
function _fail
{
_echo $FAIL; exit 1;
}
function _startTime
{
start=$(date +'%s')
}
function _endTime
{
_echo " ($(($(date +'%s') - $start))s)" 1
}
function _logo
{
echo "${blue} _ _ ${yellow}_${normal}"
echo "${blue} | | | |_ _ _ __ ___ _ _${yellow}(_)${normal}${blue} __ _ ${normal}"
echo "${blue} | |_| | | | | '_ \ / _ \ '_${yellow}| |${normal}${blue}/ _' |${normal}"
echo "${blue} | _ | |_| | |_) | __/ | ${yellow}| |${normal}${blue} (_| |${normal}"
echo "${blue} |_| |_|\__, | .__/ \___|_| ${yellow}|_|${normal}${blue}\__,_|${normal}"
echo "${blue} |___/|_| ${normal}${white}$SUBJECT${normal}"
echo ""
}
function usage
{
cat <<EOF
usage:
$(basename $0) [ -n | -i | -t | -h ]
-n <ip|hostname>
Name of the host or IP address.
-i <number>
Project ID. Gitlab path:
"Project" -> "General Settings" -> "Expand: General project settings"
-t <string>
Personal access tokens. Gitlab path:
"User Settings" -> "Access Tokens"
-h
Prints this help.
EOF
exit $1
}
function _curl
{
_curl_with_error_code "$@" | sed '$d'
}
function _curl_with_error_code
{
local curl_error_code http_code
exec 17>&1
http_code=$(curl --write-out '\n%{http_code}\n' "$@" | tee /dev/fd/17 | tail -n 1)
curl_error_code=$?
exec 17>&-
if [ $curl_error_code -ne 0 ]; then
return $curl_error_code
fi
if [ $http_code -ge 400 ] && [ $http_code -lt 600 ]; then
echo "$FAIL (HTTP $http_code)" # >&2
return 127
fi
}
function _checkUpdate
{
if [[ $1 != "null" ]]; then
VERSION=$1
VERSIONB64=`echo "{\"version\":\"$VERSION\"}" | base64`
URL="https://version.gitlab.com/check.svg?gitlab_info=${VERSIONB64}"
RESULT=`wget -q -O- --header="Referer: https://google.com" ${URL} | grep -oPm1 "(?<=\">)(.*)<\/text>" | grep -oP ".+?(?=<\/text>)"`
if [[ $RESULT == "update asap" ]]; then
echo "${bg_red}${white} ${RESULT} ${normal}"
elif [[ $RESULT == "update available" ]]; then
echo "${bg_yellow}${white} ${RESULT} ${normal}"
else
echo "${bg_green}${white} ${RESULT} ${normal}"
fi
else
echo ""
fi
}
OPTERR=0
GITLAB_API=""
TOKEN=""
PROJECT_ID=""
while getopts ":n:i:t:h" options
do
case $options in
n)
GITLAB_API=$OPTARG"/api/v4"
;;
i)
PROJECT_ID=$OPTARG
;;
t)
TOKEN=$OPTARG
;;
h|*)
_logo
usage 0
;;
esac
done
shift $(($OPTIND - 1))
function _errorMessage
{
_echo "${red}Error${normal}" 1
_echo "${red}$1${normal}" 1
_echo "" 1
_echo "usage:" 1
_echo "$(basename $0) -h" 1
exit 1
}
if [[ -z $GITLAB_API ]]; then
_errorMessage "Hostname not set."
fi
if [[ -z $PROJECT_ID ]]; then
_errorMessage "Project ID not set."
fi
if [[ -z $TOKEN ]]; then
_errorMessage "Token not set."
fi
function main
{
_logo
# init / clean
tmp_dir="tmp_dir/"
rm -rf $tmp_dir
_echoTest "API"
_startTime
gitlab_version=$(mktemp)
_curl --silent "$GITLAB_API/version/?private_token=$TOKEN" > $gitlab_version; td=`cat $gitlab_version`; if [[ -z $td ]]; then _fail; else _ok; fi;
_endTime
version=`cat $gitlab_version | jq '.version' | tr -d '"'`
_echo " ├ version: $version "
_checkUpdate $version
_echo " └ revision: "`cat $gitlab_version | jq '.revision' | tr -d '"'` 1
_echoTest "Project detail"
_startTime
project_data=$(mktemp)
_curl --silent "$GITLAB_API/projects/$PROJECT_ID/?private_token=$TOKEN" > $project_data; td=`cat $project_data`; if [[ -z $td ]]; then _fail; else _ok; fi;
_endTime
_echo " ├ id: $PROJECT_ID" 1
_echo " ├ name: "`cat $project_data | jq '.name' | tr -d '"'` 1
_echo " └ url: "`cat $project_data | jq '.web_url' | tr -d '"'` 1
_echoTest "Star project"
_startTime
_curl -XPOST --silent "$GITLAB_API/projects/$PROJECT_ID/star/" -d "private_token=$TOKEN" >&/dev/null && _ok || _fail
_endTime
_echoTest "Unstar project"
_startTime
_curl -XPOST --silent "$GITLAB_API/projects/$PROJECT_ID/unstar/" -d "private_token=$TOKEN" >&/dev/null && _ok || _fail
_endTime
_echoTest "Clone"
_startTime
repo_url=$(echo $(cat $project_data | jq '.ssh_url_to_repo') | tr -d '"' | sed 's/\.vpn//')
git clone --quiet $repo_url $tmp_dir && _ok || _fail
_endTime
_echo " └ git version: $(git --version | awk '{ print $3 }')" 1
_echoTest "Change directory"
_startTime
cd $tmp_dir && _ok || _fail
_endTime
_echoTest "Commit"
_startTime
echo "Test `date`
" >> test.md || _echo $FAIL
git add . && git commit --quiet -am "test `date`" && _ok || _fail
_endTime
_echoTest "Push to master"
_startTime
git push --quiet origin master && _ok || _fail
_endTime
_echoTest "Checkout to develop branch"
_startTime
git checkout --quiet -b develop && _ok || _fail
_endTime
_echoTest "Push to develop branch"
_startTime
git push --quiet origin develop >&/dev/null && git checkout --quiet master && _ok || _fail
_endTime
_echoTest "Create merge request"
_startTime
mr_data=$(mktemp)
title="test `date`"
_curl -XPOST --silent "$GITLAB_API/projects/$PROJECT_ID/merge_requests/" -d "private_token=$TOKEN&source_branch=master&target_branch=develop&title=$title" > $mr_data && _ok || _fail
mr_id=$(cat $mr_data | jq '.iid')
_endTime
_echo " ├ id: $mr_id" 1
_echo " ├ name: "`cat $mr_data | jq '.title' | tr -d '"'` 1
_echo " └ url: "`cat $mr_data | jq '.web_url' | tr -d '"'` 1
_echoTest "Add comment to merge request"
_startTime
mr_note=$(mktemp)
_curl -XPOST --silent "$GITLAB_API/projects/$PROJECT_ID/merge_requests/$mr_id/notes" -d "private_token=$TOKEN&body=message%20test%20hi" > $mr_note && _ok || _fail
_endTime
_echoTest "Close merge request"
_startTime
mr_close=$(mktemp)
_curl -XPUT --silent "$GITLAB_API/projects/$PROJECT_ID/merge_requests/$mr_id" -d "private_token=$TOKEN&state_event=close" > $mr_close && _ok || _fail
_endTime
cd ../
rm -rf $tmp_dir
_echo "" 1
}
main
ELAPSED_TIME=$(($SECONDS - $START_TIME))
_echo "Time: $(($ELAPSED_TIME/60)) minutes $(($ELAPSED_TIME%60)) seconds" 1
_echo "${green}done.${normal}" 1
exit 0