Skip to content

Enabling Sonar

matthieun edited this page Dec 18, 2018 · 2 revisions

This is a list of steps to enable Sonar checks on a project.

Sonar setup

Sonar github app

Install the Sonar github app to the organization that hosts the project of interest.

https://github.com/apps/sonarcloud

Sonar App

Sonarcloud permissions

Use github to login to the sonarcloud.io space for the organization.

Sonar token

In the organization page, navigate to your avatar > My Account

sonarcloud avatar

In the Security tab, create a new token:

sonarcloud avatar

Make sure to save that token somewhere secure.

Sonar organization permissions

In the organization page, navigate to Administration > Permissions

sonarcloud org permissions

Give yourself permissions to execute Analysis.

sonarcloud org permissions check

Sonar project permissions

Open the specific project, and navigate to Administration > Permissions

sonarcloud permissions

And check all permissions to yourself and owners:

sonarcloud permissions check

Travis setup

Sonar token

Encrypt the sonar token and send it to Travis:

cd <project clone>
travis encrypt SONAR_TOKEN=XXXXXXXXX

which returns a "secure" phrase to add to the .travis.yml file:

env:
  global:
      ...
    - # SONAR_TOKEN
    - secure: "YYYYYY"
      ...

Sonar PR decoration github token

In your personal github settings > Developer settings > personal access tokens, create a new personal access token. Put "Sonar" in its name to remember what it is used for.

cd <project clone>
travis encrypt SONAR_PR_DECORATION_GITHUB_TOKEN=XXXXXXXXX

which returns a "secure" phrase to add to the .travis.yml file:

env:
  global:
      ...
    - # SONAR_PR_DECORATION_GITHUB_TOKEN
    - secure: "YYYYYY"
      ...

Gradle script

Add the following to build.gradle:

plugins {
    ...
    id 'org.sonarqube' version '2.6.2'
    ...
}

Travis script

Add a new .travis/sonar.sh script. Make sure to update the parameters -Dsonar.organization=osmlab and -Dsonar.pullrequest.github.repository=osmlab/atlas with the right project name!

#!/usr/bin/env sh

if [ "$TRAVIS_PULL_REQUEST" != "false" ];
then
	SONAR_PULLREQUEST_BRANCH="$(echo $TRAVIS_PULL_REQUEST_SLUG | awk '{split($0,a,"/"); print a[1]}')/$TRAVIS_PULL_REQUEST_BRANCH"
	echo "Running sonarqube in Pull Request $TRAVIS_PULL_REQUEST"
	echo "sonar.pullrequest.key=$TRAVIS_PULL_REQUEST"
	echo "sonar.pullrequest.branch=$SONAR_PULLREQUEST_BRANCH"
	echo "sonar.pullrequest.base=$TRAVIS_BRANCH"
	./gradlew sonarqube \
		-Dsonar.organization=osmlab \
		-Dsonar.host.url=https://sonarcloud.io \
		-Dsonar.login=$SONAR_TOKEN \
		-Dsonar.junit.reportPaths=build/test-results/test \
		-Dsonar.jacoco.reportPaths=build/jacoco/test.exec \
		-Dsonar.pullrequest.key=$TRAVIS_PULL_REQUEST \
		-Dsonar.pullrequest.branch=$SONAR_PULLREQUEST_BRANCH \
		-Dsonar.pullrequest.base=$TRAVIS_BRANCH \
		-Dsonar.pullrequest.provider=github \
		-Dsonar.pullrequest.github.repository=osmlab/atlas \
		-Dsonar.pullrequest.github.endpoint=https://api.github.com/ \
		-Dsonar.pullrequest.github.token.secured=$SONAR_PR_DECORATION_GITHUB_TOKEN
else
	echo "Running sonarqube in a regular build"
	./gradlew sonarqube \
		-Dsonar.branch.name=$TRAVIS_BRANCH \
		-Dsonar.organization=osmlab \
		-Dsonar.host.url=https://sonarcloud.io \
		-Dsonar.login=$SONAR_TOKEN \
		-Dsonar.junit.reportPaths=build/test-results/test \
		-Dsonar.jacoco.reportPaths=build/jacoco/test.exec
fi

and wrap it in a .travis/sonar-gate.sh script:

#!/usr/bin/env sh

if [ $TRAVIS_TEST_RESULT -eq 0 ];
then
	.travis/sonar.sh
	RETURN_VALUE=$?
	if [ "$RETURN_VALUE" != "0" ];
	then
		exit $RETURN_VALUE
	fi
fi

Finally add that script to the .travis.yml list:

script:
    ...
  - .travis/sonar-gate.sh
    ...