SonarQube is a code analysis tool that shows key numbers about code quality, e.g. code coverage, code complexity and various code practices.
SonarQube has a web GUI that allows exploring the analysis results.
However, SonarQube has no storage of build quality history. To keep statistics about code quality one either have to manually type the key numbers of each analysed application into a spreadsheet, or use something like this tool.
This utility consists of a servlet that serves as a webhook that is called by Sonar when completing an analysis. The webhook POST data doesn’t have the necessary information (which are some key metrics of the build).
So when receiving a POST, the servlet will do a callback to the SonarQube REST API to retrieve the metrics, which will then be stored in a PostgreSQL database.
The servlet has been written as a microservice that can be installed into an apache karaf instance.
Version | Date | Description |
---|---|---|
1.5.29 | <2024-12-14 Sat> | Use liquibase 4.30.0, postgresql jdbc 42.7.4 |
1.5.28 | <2024-08-03 Sat> | Use jackson 2.17.2 |
1.5.27 | <2024-08-01 Thu> | Use liquibase 4.29.0 |
1.5.26 | <2024-07-05 Fri> | Use liquibase 4.28.0 |
1.5.25 | <2024-04-06 Sat> | Use liquibase 4.27.0 and to build with karaf 4.4.5 |
1.5.24 | <2024-03-25 Mon> | Use postgresql JDBC 42.7.3, jackson 2.16.2 |
1.5.23 | <2024-03-02 Sat> | Use postgresql JDBC 42.7.2 |
1.5.22 | <2023-12-14 Thu> | Use postgresql JDBC 42.7.1 and mockito 5.8.0 |
1.5.21 | <2023-12-12 Tue> | Use liquibase 4.24.0 |
1.5.20 | <2023-11-05 Sun> | Use jackson 2.15.3, junit jupiter 5.10.0, and mockito 5.7.0 |
1.5.19 | <2023-10-31 Tue> | Use karaf 4.4.4 |
1.5.18 | <2023-07-30 Sun> | Use liquibase 2.15.2 |
1.5.17 | <2023-07-08 Sat> | Use Java 17 |
1.5.16 | <2023-07-02 Sun> | Use liquibase 4.23.0 |
1.5.15 | <2023-04-26 Wed> | Use jackson 2.15.0 |
1.5.14 | <2023-04-24 Mon> | Use jackson 2.14.2 |
1.5.13 | <2023-04-23 Sun> | Build karaf feature files in the maven “compile” phase |
1.5.12 | <2023-03-06 Mon> | Use liquibase 4.19.0, pax-jdbc 1.5.5, postgresql jdbc 42.5.4, karaf 4.4.3 |
1.5.11 | <2022-12-04 Sun> | Use jackson 2.14.1 to fix CVE-2022-42003 and CVE-2022-42004 |
1.5.10 | <2022-11-26 Sat> | Use postgresql jdbc 42.5.1 to fix CVE-2022-41946 |
1.5.9 | <2022-11-01 Tue> | Use liquibase 4.17.1, postgresql jdbc 42.5.0 |
1.5.8 | <2022-10-20 Thu> | Add support for maintainability, security and reliability ratings |
1.5.7 | <2022-10-12 Wed> | Work with SonarQube with version >= 8 (no longer works with version 7.x and older) |
1.5.6 | <2022-10-09 Sun> | Github actions CI build, sonar report cleanup, support for sonar user token |
1.5.5 | <2022-08-21 Sun> | Use liquibase 4.15.0 |
1.5.4 | <2022-08-10 Wed> | Use maven-bundle-plugin 5.1.8, junit jupiter 5.9.0, mockito 4.6.1, and assertj 2.23.1 |
1.5.3 | <2022-08-10 Wed> | Use postgresql jdbc driver 42.4.1 |
1.5.2 | <2022-07-25 Mon> | Use karaf 4.4.1 |
1.5.1 | <2022-05-31 Tue> | Use jackson jackson 2.13.3 to fix security issue |
1.5.0 | <2022-05-29 Sun> | Use karaf 4.4.0 and OSGi 8 |
1.4.8 | <2022-02-21 Mon> | Use Java 11, karaf 4.3.6, junit 5.8.2, assertj 3.22.0 and mockito 4.3.1 |
1.4.7 | <2021-10-14 Thu> | Use karaf 4.3.3 and postgresql JDBC 4.2.24 |
1.4.6 | <2021-07-25 Sun> | Use PostgreSQL JDBC driver 42.2.23 |
1.4.5 | <2021-06-15 Tue> | Use jackson 12.3 |
1.4.4 | <2021-06-13 Sun> | Align dependency versions with other applications |
1.4.3 | <2021-06-01 Tue> | Get versions for the OSGi 7 framework and the OSGi compendium from the karaf BoM |
1.4.2 | <2021-04-19 Mon> | Get OSGi adapters dependency from a BoM |
1.4.1 | <2021-04-17 Sat> | Get maven dependency versions and maven plugin config from a parent POM |
1.4.0 | <2021-04-12 Mon> | Built with karaf 4.3.0 and OSGi 7 |
1.3.4 | <2021-03-21 Sun> | Bugfix: avoid loading junit and hamcrest in karaf |
1.3.3 | <2021-03-21 Sun> | Get maven dependencies from the karaf 4.2.11 BoM |
1.3.2 | <2020-10-10 Sat> | Use PostgreSQL JDBC driver 42.2.17 |
1.3.1 | <2020-09-26 Sat> | Use PostgreSQL JDBC driver 42.2.12 |
1.3.0 | <2020-07-24 Fri> | Remove use of cobertura, upgrade liquibase from 3.5.3 to 3.8.0 |
1.2.0 | <2020-04-20 Mon> | Use jackson-databind 2.9.10.3, make liquibase script work with h2 |
1.1.0 | <2019-11-14 Thu> | Use pax-jdbc-config to set up the database, build with openjdk 11, jackson security upgrade |
1.0.0 | <2017-12-18 Mon> | First release |
(this assumes that you have an apache karaf already installed on your development computer, as well as git, maven, and a Java development kit)
Procedure:
- Clone the project
mkdir -p ~/git cd ~/git git clone https://github.com/steinarb/sonar-collector.git
- Build the project with maven
cd ~/git/sonar-collector mvn clean install
- Give the following commands from the karaf console to install the REST service:
feature:repo-add mvn:no.priv.bang.sonar.sonar-collector/sonar-collector-webhook/LATEST/xml/features feature:install sonar-collector-webhook-with-postgresql
Note: You will need to have a suitable PostgreSQL database to write to for this servlet to be meaningful. See the sections Create the database and Using a database running on a different host for more detail. The database just have to be a blank, freshly created database, that the servlet can write to, either on localhost with the curent user, or using JDBC credentials configured from the karaf console (this is what’s covered in Using a database running on a different host).
The latest version of the sonar-collector is available from docker hub.
Note: The docker image is actually provisioned at startup time by the latest sonar-collector release to maven central, so don’t be mislead by the creation date of the image. The most recent relase to maven central is what will be run.
To run the sonar-collector in the container:
- get hold of an RDBMS (preferrably PostgreSQL but any RDMS that has its driver deployed to maven central (i.e. not Oracle) can probably be made to work)
- The database could be an AWS MicroDB (preferraby PostgreSQL)
- The database could potentially be an aurora instance (however, this heas not been tried, since AWS aurora instances costs money from day one)
- add a user to the RDBMS (e.g. “myuser” with password “sosecret”)
- add a blank database to the RDBMS, e.g. named “sonar-collector”, with the user created above as owner
- In the docker container, get the image from docker hub:
pull steinarb/sonar-collector:latest
- Start a container with the image, setting the JDBC credentials as environment variables:
docker run -p 8101:8101 -p 8181:8181 -e "JDBC_URL=jdbc:postgresql://somehost.com/sonar-collector" -e "JDBC_USER=myuser" -e "JDBC_USER=sosecret" -d steinarb/sonar-collector:latest
- Add the URL of the sonar-collector running inside the container as a webhook to sonar (e.g. http://somecontainer.somecompany.com:8181/sonar-collector)
- After this the key measurements from each sonar run will be stored as a row in the measures table with the time of the run
- There is no UI, use a database explorer tool to run queries on the measures table to get the desired number
The values that can be overridden by setting environment variables on container start, are:
Variable | Default value | Description |
---|---|---|
JDBC_DRIVER_FEATURE | postgresql | Karaf feature to load the driver |
JDBC_DRIVER_NAME | PostgreSQL JDBC Driver | Identify correct driver for OSGi service dependency injection |
JDBC_URL | jdbc:postgresql:///sonar-collector | |
JDBC_USER | <none> | No default to make it possible to have no username |
JDBC_PASSWORD | <none> | No default to make it possible to have no password |
Example docker run command for using a h2 database instead of postgresl
docker run -p 8101:8101 -p 8181:8181 -e 'JDBC_DRIVER_FEATURE=pax-jdbc-h2' -e 'JDBC_DRIVER_NAME=H2 JDBC Driver' -e 'JDBC_URL=jdbc:h2:tcp://somehost.company.com/~/sonar-collector' -e 'JDBC_USER=sa' -e JDBC_PASSWORD='' -d steinarb/sonar-collector:latest
RDBMS type | Karaf feature | JDBC_DRIVER_NAME | example JDBC_URL | Default port |
---|---|---|---|---|
PostgreSQL | postgresql | PostgreSQL JDBC Driver | jdbc:postgresql://somehost.company.com/sonar-collector | 5432 |
Derby (aka. JavaDB) | pax-jdbc-derby | derby | jdbc:derby://somehost.company.com:1527/sonar-collector | 1527 |
H2 | pax-jdbc-h2 | H2 JDBC Driver | jdbc:h2:tcp://somehost.company.com/~/sonar-collector | 9092 |
MSSQL | pax-jdbc-mssql | Microsoft JDBC Driver for SQL Server | jdbc:sqlserver://somehost.company.com:1433;databaseName=sonar-collector | 1433 |
mariadb | pax-jdbc-mariadb | mariadb | jdbc:mariadb://somehost.company.com:3306/sonar-collector | 3306 |
mysql | pax-jdbc-mysql | mysql | jdbc:mysql://somehost.company.com:3306/sonar-collector | 3306 |
(This procedure doesn’t require development tools or building the project yourself. The servlet, and its attached karaf feature has been deployed to maven central)
This describes how to install and run the program on a debian GNU/linux system.
As root, do the following command:
apt-get update apt-get install postgresql
Procedure:
- Create a PostgreSQL user matching the karaf user:
/usr/bin/sudo -u postgres createuser --pwprompt karaf
- At the prompt “Enter password for new role”, enter the JDBC password for user “karaf”
- At the prompt “Enter it again”, enter the same password again
Make a note of this password, since it will be needed later, when setting up a password authenticated connection
- Create an empty database owned by the karaf user:
/usr/bin/sudo -u postgres createdb -O karaf sonarcollector
Do the following steps as root
- Add a key for the apt repo containing the karaf package
wget -O - https://apt.bang.priv.no/apt_pub.gpg | apt-key add -
- Add the repo containing karaf by adding the following lines to /etc/apt/sources.list :
# APT archive for apache karaf deb http://apt.bang.priv.no/public stable main
- Install the debian package
apt-get update apt-get install karaf
Procedure:
- SSH into karaf
ssh -p 8101 karaf@localhost
The password is “karaf” (without the quotes)
- Install the application
feature:repo-add mvn:no.priv.bang.sonar.sonar-collector/sonar-collector-webhook/LATEST/xml/features feature:install sonar-collector-webhook-with-postgresql
(sonar-collector has been deployed to maven central, which is a repository that is builtin to karaf)
The above example shows connecting to a PostgreSQL database running on localhost, authenticating with ident authentication (ie. no password).
This example shows how to connect to a PostgreSQL database running on a different host, authenticating using username and password.
Procedure:
- SSH into karaf
ssh -p 8101 karaf@localhost
The password is “karaf” (without the quotes)
- In the karaf command shell, create configuration for the JDBC connection:
config:edit org.ops4j.datasource-sonar-collector config:property-set osgi.jdbc.driver.name "PostgreSQL JDBC Driver" config:property-set dataSourceName "jdbc/sonar-collector" config:property-set url "jdbc:postgresql://lorenzo.hjemme.lan/sonarcollector" config:property-set user "karaf" config:property-set password "karaf" config:property-set org.apache.karaf.features.configKey "org.ops4j.datasource-sonar-collector" config:update
(this assumes the username/password combination karaf/karaf, it is recommended to use a different password in a real setting with PostgreSQL accepting network connections)
The “config:update” command will cause the sonar collector to be restarted, it will pick up the new configuration, and connect to the remote server, and if the “sonar-collector” database exists as a blank database, create the schema and be ready to store data there.
Side note: The configuration will be stored in standard .properties file format, in the file /etc/karaf/no.priv.bang.sonar.collector.webhook.SonarCollectorServlet.cfg and be persistent across restarts and reinstallations of the karaf .deb package (the .deb package will only uninstall/reinstall unchanged known files in this directory, and won’t touch unknown files at all).
Note that PostgreSQL out of the box on debian only accepts domain connections and localhost connections.
To make PostgreSQL listen on all network connections, two files must be edited and the PostgreSQL daemon must be restarted.
Procedure, do the following, logged in as root on the server:
- Do “su” to user postgres to get the right ownership on the files
su - postgres
- Edit the /etc/postgresql/9.6/main/postgresql.conf file, uncomment the listen_address line and edit it to look like this
listen_addresses = '*' # what IP address(es) to listen on;
- Edit the /etc/postgresql/9.6/main/pg_hba.conf, add the following lines
# IPv4 network connection allow password authentication host all all 0.0.0.0/0 md5
- Log out from user postgres (only root can restart the daemon):
exit
- Restart the postgresql daemon
systemctl restart postgresql
WARNING! This is not regularily tested (i.e. won’t be tested before releases) and I don’t plan to actually use sonar-collector with anything except PostgreSQL myself.
To use JDBC against a RDBMS other than PostgreSQL, do the following from the karaf console command line (derby in-memory database used in the examples):
- Load the component providing the DataSourceFactory OSGi service:
feature:install pax-jdbc-derby
- Add karaf configuration selecting the correct DataSourceFactory and JDBC connection info (url, user and password):
config:edit org.ops4j.datasource-sonar-collector config:property-set osgi.jdbc.driver.name "PostgreSQL JDBC Driver" config:property-set dataSourceName "jdbc/sonar-collector" config:property-set url "jdbc:derby:data/example/derby;create=true" config:property-set osgi.jdbc.driver.name derby config:property-set org.apache.karaf.features.configKey "org.ops4j.datasource-sonar-collector" config:update
- Load sonar-collector using a feature that doesn’t unnecessarily pull in the PostgreSQL DataSourceFactory:
feature:repo-add mvn:no.priv.bang.sonar.sonar-collector/sonar-collector-webhook/LATEST/xml/features feature:install sonar-collector-webhook-with-jdbc
Procedure:
- Open your SonarCloud project in a web browser and log in as a user with ownership to the project (I do login as github user)
- In the project select the menu Administration->General Settings
- Select the webhooks tab in the tab bar on the left side of the page (you may have to scroll down to see it)
- In “Name:”, write:
sonar-collecttor
- In “URL”, write:
https://mydowmain.com:8181/sonar-collector
- Click the button “Save”
In a hosted SonarQube the webhook can be set globally across all projects.
Procedure:
- Open your SonarCloud instance in a web browser, e.g. http://localhost:9000 and log in as an admin user (admin/admin in a test instance)
- In the top menu, select Administration
- Select the tab “Webhooks” in the list to the left of the page (you may have to scroll down to see the tab)
- In “Name”, type:
sonar-collector
- In “URL”, type:
http://localhost:8181/sonar-collector
- Click the button “Save”
If you get 401 when sonar-collector is doing web api callbacks to sonar to get numbers that aren’t in the webhook call, then you can add a sonar user token to use with the sonar web api.
Procedure:
- In Sonar, go to My Account->Security, and create and retrieve a user token (Note: you only get one chance to copy the token after creating it)
- Add the user token to the sonar-collector config. replace “squ_3869fbac07cc388306804e35fb72ca7c4baff275” with the token retrieved from sonar:
config:edit no.priv.bang.sonar.collector.webhook.SonarCollectorServlet config:property-set sonar_user_token squ_3869fbac07cc388306804e35fb72ca7c4baff275 config:update
This utility is licensend under the Apache license v. 2. See the LICENSE file for details.
To run the servlet locally and debug into the servlet, the following software is required:
- A locally installed apache karaf (see the apache karaf quick start guide )
- A locally installed SonarQube (see SonarQube Get Started in Two Minutes )
- A locally installed (or at least reachable, see Using a database running on a different host ) PostgreSQL database
- An IDE that can do remote debugging
Preparation for debugging
- create user and empty database in PostgreSQL
- Add http://localhost:8181/sonar-collector as a webhook in SonarQube
- Clone and build the sonar-collector
mkdir -p ~/git cd ~/git/ git clone https://github.com/steinarb/sonar-collector.git cd ~/git/sonar-collector/ mvn clean install
- Start karaf with setup for remote debugging (cd to an unpacked downloaded karaf installation, start karaf as the user you used to do “mvn clean install”)
cd ~/Downloads/apache-karaf-4.1.4/ bin/karaf debug
- Install the sonar-collector in karaf, with the following commands in the karaf console:
feature:repo-add mvn:no.priv.bang.sonar.sonar-collector/sonar-collector-webhook/LATEST/xml/features feature:install sonar-collector-webhook
- Connect the IDE to a debugging connection on localhost port 5005 (see your IDE’s documentation for this) and set the breakpoint at the desired code
Then just trigger an analysis in the locally installed SonarQube and debug when the breakpoint is triggered:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=a51f2ab9a8790abd91773f0a7d2f6d2dc9d97975
(as the sonar.login argument, use the token that SonarQube returns when using the setup wizard of the quick start)
Precondition: docker running on the build server
Procedure:
- Move to the build directory:
cd docker/
- Build the image:
mvn clean install
- Verify with “docker images” that the image has been rebuilt (if the CREATED column shows an old time the image probably hasn’t been rebuilt):
sb@lorenzo:~/workspaces/ws02/sonar-collector/docker$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE steinarb/sonar-collector latest 6c578e16f6e0 3 seconds ago 291MB sb@lorenzo:~/workspaces/ws02/sonar-collector/docker$