Integrating SonarQube and Reporting Services, by Vinicius Moura

This walkthrough, authored and translated from its Portuguese counterpart by Vinicius Moura, aims to provide insightful and practical guidance about integrating the SonarQube platform and Reporting Services installed on existing Team Foundation Server 2013. It presents the configuration and development done to merge Code Quality metrics provided by Sonar and Work Item metrics provided by TFS on same report.
image


Prerequisites

Team Foundation Server 2013

This guide requires a Team Foundation Server 2013 installation with the following setup:

  • Build Configuration;
  • Reporting Services Configuration.

SonarQube

SonarQube Installation Guide from ALM Rangers was used as basis to this walk-through with an objective to integrate the SonarQube platform with an existing Team Foundation Server 2013 setup.
After completing the setup you should have access to the SonarQube portal:
image001


ALM Configurations

This walkthrough will demonstrate ALM on Team Foundation Server based on following:

  • Backlog Portfolio Management structure based on “One Team Project”
  • Source Control planning based on TFVC
  • Build Automation.

Backlog Portfolio Management

The structure for Areas on TFS is based on “One Team Project”. As shown below the root level contains the OneTeamProject node, with three different application system sub-areas.
image002

To filter one Team (e.g. OneTeamProject\SystemA) we set a Default Area, for example SystemA.
image003

The iteration structure represents all Sprints for the development team working on the system.
To filter one Team, we use a default backlog iteration i.e. SystemA
image004

After these configurations, the development team can select an appropriate area and iteration
image005

Source Control Planning

The Source Control reflects the same structure as the Area Path. As with the area and iteration path, SystemA is the node for our development team.
image006

For this walkthrough we are using a branch strategy that allocates a branch for each sprint as shown below: 
image007image008

Build Automation

A Build Automation process uses the Build Process Template “TfvcTemplate12.xaml” (see example in SonarQube Installation Guide), which is versioned on BuildProcessTemplate folder and customized to encapsulate all Sonar calls.
image009

The following changes were made to encapsulate Sonar calls:

  • Variable’s creation named BuildDetail.
    image010
  • Assign value to variable BuildDetail.
    image011
    image012
  • Created arguments:
    image013
    • BuildNumberSuffix – this argument receive a number version of Build;
    • BuildRunnerPath – this argument receive an installation path of “SonarQube.Msbuild.Runner.exe”.
  • Configuring metadata to not allow the change of the arguments.
    image014
  • On Sequence “Compile, Test and Publish”, include on Build Activity “Assign” called “Assign BuildNumberSuffix”.
    image015
    • Value = BuildDetail.BuildNumber.Replace(BuildDetail.BuildDefinition.Name + "_", "")
      image016
      image017
  • On Build Activity ”Run optional script before MSBuild”, make the Sonar Runner call passing the following parameters.
    • FilePath = SonarRunnerPath
      image018
    • Arguments = String.Format("/key:""{0}"" /name:""{1}"" /version:{2}", BuildDetail.BuildDefinition.Name, BuildDetail.BuildDefinition.Name, BuildNumberSuffix)
      image019
      image020
      The parameters listed above are the arguments of “SonarQube.Msbuild.Runner.exe” command line.
      Each Build Definition will match a “Project Name” on Sonar and each build queuing will be a new version.
  • On Build Activity ”Run optional script after Test Runner”, make sure the Sonar Runner uses the following parameter.
    • FilePath = SonarRunnerPath
      image021
      image022

Sonar Service API

The Sonar Service API is a WCF service that will be access a REST API of Sonar to list metrics of Builds. This service should be enabled on IIS (Internet Information Services). In short, the service will connect to the REST API and return metrics from a respective “projectKey”. A web.config example shows a connection of Sonar and the list of returned metrics.
image023

  • sonar.api.url – URL installation of Sonar;
  • sonar.api.metrics – list of metrics.

When we access “Sonar Service API”, it returns a list of metric and respective last and first matched values as example:
image024


Executing Sprint

After executing ALM Configurations, we must perform the initial setup on each project.

For the example below, we will have the following configuration:

System Name SystemA
Area Path OneTeamProject\SystemA
Iteration OneTeamProject\SystemA\Sprint2
Branch $\OneTeamProject\SystemA\Dev\Sprint2
Build Definition OneTeamProject.SystemA.Sprint2 Build Definition Name is based on “Iteration Path” and a Sonar Project.

Initial Sprint Setup

In our example, we have already created the Area and Iteration Path and can create the Work Items using the following process:
image025

We create a branch for isolation of implementing new work that represents the Sprint, with the MAIN branch as parent.
image026

After creating the branch, we define a Build Definition to build the respective branch.

  • Access Team Explorer menu and click on Build. After click “New Build Definition”
    image027
  • On Build definition name, type a same name of Iteration Path, changing “\” by “.”
    OneTeamProject.SystemA.Sprint
    image028
  • On “Trigger” menu, choose “Continuous Integration” option
    image029
  • On “Source Settings” menu, point to their source control of your project.
    image030
  • On “Build Defaults” menu, point to Build Controller and defines a Drop Location
    image031
  • On “Process” menu, choose Build Process Template “TfvcTemplate.12_Sonar.xaml”
    image032
  • On “Process” menu, point to respective solution through “2. Build” “1.Projects” option
    image033
  • Save the Build Definition;
  • To create a project on Sonar, queue the Build.
  • At end of execution, the link to SonarQube will be shown.
    image034
    image035

Each queuing will generate a new version of analysis for this Build Definition.

Important Tips

Creating a new Sprints and Build Definitions, we can use two Visual Studio plug-ins that will help us to manage Build Process.

  • Using the example above, if it is necessary create a Build Definition that represent “Sprint3”, we can clone the Build Definition “Sprint2”. See an example below:
    • Find a Build Definition “Sprint2” and click on “Branch Build Definition”
      image036
    • Remap a Source Control
      image037
    • Rename a Build Definition
      image038
  • Team Explorer Builds Tree – To organize a Build Definition list, install a Team Explorer Builds Tree. It is shown all builds on Hierarchy structure as example below:
    image039

Integrating Reporting Services and SonarQube

After configurations and build execution, we can create a report on Reporting Services to show a SonarQube analysis.

Reporting Services will connect WCF Services and show all metrics configured on Web.config.

Follow these steps to create a report:

Connect Reporting Services to WCF Services

  • Create a new parameter called “projectKey”.
    image040
  • Create a DataSource that connect on WCF Services
    image041
  • Create a DataSet that uses a DataSource created above
    image042

<Query>
<Method Namespace="https://tempuri.org/" Name="TimeMachineByProjectKey">
<Parameters>
<Parameter Name="projectKey"></Parameter>
</Parameters>
</Method>
<ElementPath xmlns:a="https://schemas.datacontract.org/2004/07/SonarServiceApi.Contracts" xmlns:i="https://www.w3.org/2001/XMLSchema-instance">
</ElementPath>
<SoapAction>https://tempuri.org/ISonarServiceContract/TimeMachineByProjectKey</SoapAction>
</Query>
image043

  • After we run the query, we have a following result:
    image044

Create “Sprint Project” Report

According to our process where the Iteration Path is the respective Build Definition, we can create a “Sprint Project” Report that shows information about Work Items and SonarQube.

On example bellow, we created a report that lists all Iterations. Therefore, it is possible to connect on SonarQube and show information about “Code Quality” along with Work Item information.
image045

Therefore, we can show a single report that show all information about any project, including planning information and code quality evaluation.


Authored by Vinicius Moura
, an ALM Consultant since 2010 and ALM Ranger since 2012. He has translated guides, such as Extracting effective permissions from TFS and TFS Reporting Guide for the Portuguese community, and is the author of the Work Item Field History VS2013 widget.