forked from ahmed-musallam/sonarqube-aem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quality.sh
executable file
·164 lines (129 loc) · 3.39 KB
/
quality.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
#!/usr/bin/env bash
#########
# Creates Sonar Qube Custom Quality Gate.
#########
#set -x
# print in blue color
info () {
printf "\e[1;34m[quality.sh]:: %s ::\e[0m\n" "$*"
}
# print in red color
error () {
printf "\e[1;31m[quality.sh]:: %s ::\e[0m\n" "$*"
}
# wait here untill sonar is up. sleep 5 seconds between checks.
while [[ "$(curl -s localhost:9000/api/system/status)" != *'"status":"UP"'* ]]; do
info "Waiting for Sonar to be ready.."
sleep 5;
done
info "Sonar is UP! Configuring Quality Gates..."
# default curl options
curl_opts=(
-X POST
--user admin:admin
-s
-o /dev/null
-w '%{http_code}'
)
# Send a post request
post () {
STATUS=$(curl "${curl_opts[@]}" "$@")
# some APIs like set_as_default return a 204 for success ops
if [ $STATUS -eq 200 ] || [ $STATUS -eq 204 ]; then
info "==> Success!"
else
error "==> Failed."
fi
}
#######
## Quality Gates
#######
# create quality gate
create_gate () {
post localhost:9000/api/qualitygates/create "$@"
}
# set gate as default
set_as_default_gate () {
post localhost:9000/api/qualitygates/set_as_default "$@"
}
# create gate condition
create_condition () {
post localhost:9000/api/qualitygates/create_condition "$@"
}
# by default, the newly created gate ID will be 2; Since Sonar Way gate is 1.
gate_id=2
info "Creating Quality Gate: aem-gate"
create_gate -d name=aem-gate
info "Setting aem-gate as the default Quality Gate."
set_as_default_gate -d id=$gate_id
info "Creating Condition: Code Coverage - 75% required"
create_condition \
-d metric=coverage \
-d gateId=$gate_id \
-d error=75 \
-d op=LT
info "Creating Condition: Code Smells - A required"
create_condition \
-d metric=code_smells \
-d gateId=$gate_id \
-d error=1 \
-d op=GT
info "Creating Condition: Maintainability Rating - A required"
create_condition \
-d metric=sqale_rating \
-d gateId=$gate_id \
-d error=1 \
-d op=GT
info "Creating Condition: Reliability Rating - A required"
create_condition \
-d metric=reliability_rating \
-d gateId=$gate_id \
-d error=1 \
-d op=GT
info "Creating Condition: Security Rating - A required"
create_condition \
-d metric=security_rating \
-d gateId=$gate_id \
-d error=1 \
-d op=GT
info "Quality Gate Creation Done!"
#######
## Quality Profiles
#######
create_profile () {
post localhost:9000/api/qualityprofiles/create "$@"
}
change_profile_parent () {
post localhost:9000/api/qualityprofiles/change_parent "$@"
}
activate_rules () {
post localhost:9000/api/qualityprofiles/activate_rules "$@"
}
set_default () {
post localhost:9000/api/qualityprofiles/set_default "$@"
}
get_aem_profile_id () {
OUT="$(curl -X POST --user admin:admin -s /dev/null localhost:9000/api/qualityprofiles/search -d qualityProfile=aem-way-java 2>/dev/null)"
pat='.*"key":"([^"]+)",.*'
[[ "$OUT" =~ $pat ]]
echo "${BASH_REMATCH[1]}"
}
#info "Create AEM Profile - Java"
create_profile \
-d language=java \
-d name=aem-way-java
info "Make AEM profile Inherit Sonar way"
change_profile_parent \
-d language=java \
-d parentQualityProfile="Sonar way" \
-d qualityProfile="aem-way-java"
info "setting aem-way-java as default"
set_default \
-d language=java \
-d qualityProfile="aem-way-java"
PROFILE_KEY=$(get_aem_profile_id)
info "Activating AEM rules"
activate_rules \
-d repositories="AEM Rules,Common HTL," \
-d targetKey=$PROFILE_KEY
info "Quality Profile Creation done!"