-
Notifications
You must be signed in to change notification settings - Fork 80
Enabling Sonar
This is a list of steps to enable Sonar checks on a project.
Install the Sonar github app to the organization that hosts the project of interest.
https://github.com/apps/sonarcloud
Use github to login to the sonarcloud.io space for the organization.
In the organization page, navigate to your avatar > My Account
In the Security tab, create a new token:
Make sure to save that token somewhere secure.
In the organization page, navigate to Administration > Permissions
Give yourself permissions to execute Analysis.
Open the specific project, and navigate to Administration > Permissions
And check all permissions to yourself and owners:
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"
...
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"
...
Add the following to build.gradle
:
plugins {
...
id 'org.sonarqube' version '2.6.2'
...
}
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
...