Skip to content

Commit

Permalink
add custom quality gates
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-musallam committed May 15, 2019
1 parent 1f2e76a commit 5a29820
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ VOLUME "$SONARQUBE_HOME/data"

WORKDIR $SONARQUBE_HOME
COPY run.sh $SONARQUBE_HOME/bin/
COPY quality-gates.sh $SONARQUBE_HOME/bin/
USER sonarqube
ENTRYPOINT ["./bin/run.sh"]
ENTRYPOINT ./bin/quality-gates.sh & ./bin/run.sh
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ docker run --rm -p 9000:9000 ahmedmusallam/sonarqube-aem:latest
From Source:

Clone the repo and run the `build-and-run-container.sh` script. Or open it and run the commands manually. Sonar will run on port 9000.

## Custom Quality Gates

Take a look at `quality-gates.sh` in source code and adjust it to your needs.

By default, that script will create a new `aem-gate` Gate and set the following Conditions:

- Code Coverage - 75% required
- Code Smells - A require
- Maintainability Rating - A required
- Reliability Rating - A required
- Security Rating - A required

3 changes: 3 additions & 0 deletions kill-sonarqube-aem-container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker ps | grep sonarqube-aem | awk '{print $1}' | xargs docker kill
105 changes: 105 additions & 0 deletions quality-gates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env bash

#########
# Creates Sonar Qube Custom Quality Gate.
#########

#set -x

# print in blue color
info () {
printf "\e[1;34m[quality-gates.sh]:: %s ::\e[0m\n" "$*"
}
# print in red color
error () {
printf "\e[1;31m[quality-gates.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
}

# create quality gate
create () {
post localhost:9000/api/qualitygates/create "$@"
}

# set gate as default
set_as_default () {
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 -d name=aem-gate

info "Setting aem-gate as the default Quality Gate."
set_as_default -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!"

0 comments on commit 5a29820

Please sign in to comment.