-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-cat.bash
290 lines (253 loc) · 9.94 KB
/
configure-cat.bash
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
289
290
#!/bin/bash
# CONSTANTS
SONARQUBE_URL="http://localhost:9000"
##
# Parameters :
# - 1 : status
# - 2 : errors
#
log_error(){
status=$1
errors=$2
msg="[ERROR] docker-cat, ${status} due to : ${errors}."
echo ${msg}
exit 1
}
##
# Parameters :
# - 1 : status
# - 2 : errors
#
log_warning(){
status=$1
errors=$2
msg="[WARNING] docker-cat, ${status} due to : ${errors}."
echo ${msg}
}
##
# Parameters
# - 1 : status
log_info(){
status=$1
msg="[INFO] docker-cat, $status."
echo ${msg}
}
#
# Parameters (not optional):
# - 1 : metric_name
# - 2 : metric_key
# - 3 : metric_operator [EQ,NE, LT or GT]
# - 4 : gate_id
# - 5 : overleak period 1 or 0
# - 6 : metric's warning threshold ("none" if not to set)
# - 7 : metric's error threshold ("none" if not to set)
add_condition(){
metric_name=$1
metric_key=$2
metric_operator=$3
gate_id=$4
overleak=$5
metric_warnings=$6
metric_errors=$7
log_info "adding CNES quality gate condition : ${metric_name} ${metric_operator} thresholds : [ warnings: ${metric_warnings} ] [ errors: ${metric_errors}] and overleak set on ${overleak}"
if [ "${metric_warnings}" != "none" ] && [ "${metric_errors}" != "none" ]
then
threshold="&warning=${metric_warnings}&error=${metric_errors}"
elif [ "${metric_errors}" != "none" ]
then
threshold="&error=${metric_errors}"
elif [ "${metric_warnings}" != "none" ]
then
threshold="&warning=${metric_warnings}"
fi
echo "threshold=${threshold}"
if [ "${overleak}" != "1" ]
then
unset overleak
else
overleak="&period=1"
fi
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/qualitygates/create_condition?gateId=${gate_id}&metric=${metric_key}&op=${metric_operator}${overleak}${threshold}")
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "metric $metric_name condition succesfully added."
else
log_warning "impossible to add $metric_name condition" "$( echo ${RES} | jq '.errors[].msg' )"
fi
}
create_quality_gates(){
log_info "creating CNES quality gate"
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/qualitygates/create?name=CNES")
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "successfully created CNES quality gate... now configuring it."
else
log_warning "impossible to create quality gate" "$( echo ${RES} | jq '.errors[].msg' )"
fi
# Retrieve CNES quality gates ID
log_info "retrieving CNES quality gate ID"
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/qualitygates/show?name=CNES")
if [ "$( echo ${RES} | jq '(.errors | length)')" == "0" ]
then
GATEID=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/qualitygates/show?name=CNES" | jq '.id')
log_info "successfully retrived CNES quality gate ID (ID=$GATEID)"
else
log_error "impossible to reach CNES quality gate ID" "$( echo ${RES} | jq '.errors[].msg' )"
fi
# Configure ratio comment
log_info "setting CNES quality get as default gate"
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/qualitygates/set_as_default?id=${GATEID}")
if [ "$( echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "successfully set CNES quality gate ID as default gate"
else
log_warning "impossible to set CNES quality gate as default gate" "$( echo ${RES} | jq '.errors[].msg' )"
fi
# add comment_lines condition
add_condition "Blocker violations" blocker_violations NE ${GATEID} 0 none 0
add_condition "Comment (%)" comment_lines_density LT ${GATEID} 0 30 20
add_condition "Comment (%)" comment_lines_density LT ${GATEID} 1 0 none
add_condition "Critical Issues" critical_violations NE ${GATEID} 0 none 0
add_condition "Duplicated Lines (%)" duplicated_lines_density GT ${GATEID} 0 10 15
add_condition "Duplicated Lines on New Code (%)" new_duplicated_lines_density GT ${GATEID} 1 0 10
add_condition "Major Issues" major_violations NE ${GATEID} 0 0 none
add_condition "New Major Issues" new_major_violations GT ${GATEID} 1 none 0
#add_condition "Technical Debt ratio" sqale_rating GT ${GATEID} 1 0 5
}
########################################################
# function add_profile
#
# Parameters :
# - 1 : Quality profile's file to import
#
# Description :
# Add quality profile in parameter in Sonarqube's
#
################################################################################
add_profile(){
file=$1
log_info "processing profile addition for file ${file}"
RES=$(curl POST -su admin:admin "${SONARQUBE_URL}/api/qualityprofiles/restore" --form backup=@${file})
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "quality profile ${file} successfully created."
else
log_warning "impossible to create ${file} quality profile" "$( echo ${RES} | jq '.errors[].msg' )"
fi
}
#############################################################################################
# function add_rules
# Status : NOT WORKING
#
# Parameters :
# - 1 : Rules file in JSON format correponding the the following format (Sonarqube 6.7.1 API /api/rules answer)
#
# Description :
# Read JSON formatted file and add each rules in it with it's parameters into SonarQube's configuration.
#
#
##############################################################################################
add_rules(){
file=$1
log_info "Processing rules addition from file ${file}"
total=$(jq '.total' ${file})
total=$((${total}-1))
for i in $(seq 0 ${total});
do
log_info "Adding custom rule $(jq '.rules['${i}'].key' ${file})"
############## /API/RULES/CREATE
#### Rules informations regristred using the rules creation API
custom_key=$(jq '.rules['${i}'].key' ${file})
markdown_description=$(jq '.rules['${i}'].mdDesc' ${file})
name=$(jq '.rules['${i}'].name' ${file})
severity=$(jq '.rules['${i}'].severity' ${file})
status=$(jq '.rules['${i}'].status' ${file})
template_key=$(jq '.rules['${i}'].templateKey' ${file})
type=$(jq '.rules['${i}'].type' ${file})
### Handling parameters
parameters="params="
for j in $(seq 0 $(($(jq '.rules['${i}'].params | length' ${file})-1)) );
do
log_info "The rule $name parameters contains $(jq '.rules['${i}'].params['${j}'] | length' ${file}) optional parameters parameters."
for k in $(seq 0 $(($(jq '.rules['${i}'].params['${j}'] | length' ${file})-1)));
do
param_key=$(jq '.rules['$i'].params['$j'] | keys['$k']' ${file})
param_value=$(jq '.rules['$i'].params['$j'].'${param_key} ${file})
parameters="${parameters}${param_key}=${param_value};"
done
done
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/rules/create?costum_key=${custom_key}&markdown_description=${markdown_description}&name=${name}&severity=${severity}&status=${status}&template_key=${template_key}&type=${type}&${parameters}")
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "rule ${name} created in Sonarqube.."
else
log_error "impossible to create the rule ${name}" "$( echo ${RES} | jq '.errors[].msg' )"
fi
############ /API/RULES/UPDATE
### Rules informations registred using the rule's update API.
remediation_fn_base_effort=$(jq '.rules['${i}'].remFnBaseEffort' ${file})
remediation_fn_type=$(jq '.rules['${i}'].defaultDebtRemFnType' ${file})
remediation_fy_gap_multiplier=$(jq '.rules['${i}'].debtRemFnOffset' ${file})
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/rules/update?key=$custom_key&remediation_fn_base_effort=${remediation_fn_base_effort}&remediation_fn_type=${remediation_fn_type}&remediation_fy_gap_multiplier=${remediation_fy_gap_multiplier}")
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "rule ${name} updated in Sonarqube."
else
log_error "impossible to update the rule ${name}" "$( echo ${RES} | jq '.errors[].msg' )"
fi
done
}
########################################################
# function create_quality_profiles
#
# Description :
# Periodically verify Sonarqube's server status and wait until it's UP
# Once run, import quality profiles from /tmp/conf directory (Regex : *-quality-profile.xml)
################################################################################
create_quality_profiles(){
sonar_status="DOWN"
log_info "initiating connection with Sonarqube"
sleep 15
while [ "${sonar_status}" != "UP" ]
do
sleep 8
log_info "retrieving Sonarqube's service status."
sonar_status=$(curl -s -X GET "${SONARQUBE_URL}/api/system/status" | jq -r '.status')
log_info "detected status ${sonar_status} for Sonarqube, expecting it to be UP."
done
log_info "detected status ${sonar_status} for Sonarqube, starting configuration of quality profiles."
# Not executed : expecting news from https://community.sonarsource.com/t/using-rules-create-api/1243
# API is returnin no detail informations
# Find all rules templates named "*-rules-template.json" in the folder /tmp/conf and add rules into sonarqube configuration.
#find /tmp/conf -name "*-rules-template.json" -type f -print0 | while IFS= read -rd $'\0' file; do
# add_rules ${file}
#done
# Find all files named "*-quality-profile.xml" in the folder /tmp/conf and add it in Sonarqube Quality profiles
find /tmp/conf -name "*-quality-profile.xml" -type f -print0 | while IFS= read -rd $'\0' file; do
add_profile "${file}"
done
log_info "finished to configure quality profiles".
}
run_sonarqube(){
set -e
if [ "${1:0:1}" != '-' ]; then
exec "$@"
fi
chown -R sonarqube:sonarqube $SONARQUBE_HOME
exec gosu sonarqube \
java -jar $SONARQUBE_HOME/lib/sonar-application-$SONAR_VERSION.jar \
-Dsonar.log.console=true \
-Dsonar.jdbc.username="$SONARQUBE_JDBC_USERNAME" \
-Dsonar.jdbc.password="$SONARQUBE_JDBC_PASSWORD" \
-Dsonar.jdbc.url="$SONARQUBE_JDBC_URL" \
-Dsonar.web.javaAdditionalOpts="$SONARQUBE_WEB_JVM_OPTS -Djava.security.egd=file:/dev/./urandom" \
"$@"
}
stop_sonarqube(){
pkill -u sonarqube
}
#run_sonarqube&
create_quality_profiles&&
create_quality_gates
#stop_sonarqube
exit 0