When you do your first Sonar run on your project, you get a lot of new quality numbers to play with, but no trends. You only have one data set for comparison, the now picture.
Wouldn't it be nice if you could see the current trend of the project without waiting a couple of month for the 'daily/weekly' Sonar runs to fill up the data? Well, you're in luck! And if you're using git as a version system as well, this is your day. :)
In the Sonar Advanced Parameter documentation you will find a System Property called sonar.projectDate. The property let you tell Sonar when in time the running analysis was ran.
By combining this property and what your version system does best, track changes to source, we can now play back the history of the project as far as Sonar is concerned.
This little Bash script illustrates the concept. To spell out what it does in human readable form:
for each tag in the given git repository, checkout the source and run sonar, using the tag date as projectDate
GIT_REPO=$1
START_TAG=$2
MVN_COMMAND="mvn clean install"
SONAR_COMMAND="mvn org.codehaus.sonar:sonar-maven3-plugin:3.0:sonar"
if [ -z "$GIT_REPO" ]; then
echo "Missing program argument: repository"
echo "Usage: ./sonar_history.sh git_repository_path [start-tag]"
exit
fi
pushd $GIT_REPO
for tag in `git tag`
do
if [[ -n "$START_TAG" && "$START_TAG" > "$tag" ]] ; then
echo "Skipping $tag (start tag $START_TAG)"
continue
fi
TAG_DATE=`git show $tag --date=iso | grep Date: -m1 | cut -d' ' -f 4`
echo "Checking out source from $TAG_DATE tagged as $tag"
git checkout $tag > /dev/null 2>&1
git clean -df > /dev/null 2>&1
SONAR_PROJECT_COMMAND="$SONAR_COMMAND -Dsonar.projectDate=$TAG_DATE"
echo "Executing Maven: $MVN_COMMAND"
$MVN_COMMAND > /dev/null 2>&1
echo "Executing Sonar: $SONAR_PROJECT_COMMAND"
$SONAR_PROJECT_COMMAND > /dev/null 2>&1
done
popd
You can of course modify this script to fit your own need. Maybe you want to checkout the history pr week or pr month instead of using tags. It's up to you.
Have fun! :)
Thanks for sharing this valuable example.