Sonar High-Level-API

Today I’d like to introduce a new Open Source software project. It’s not a big thing yet. However, I think it will be already helpful in some cases. It’s called Sonar-HLA which reads Sonar High-Level-API. This post is an excerpt from Sonar-HLA’s wiki page and gives a short introduction and provides some code examples as well.

So what is Sonar-HLA? It’s an easy way to access resources from a SonarQube server. Since it is a high level API, don’t expect to get every detail which is available inside Sonar. This project was raised since I needed an easy way to access Sonar projects and its most important measures for creating some charts for management issues. So with the ease of accessing Sonar comes the cost of not having all information available which resides inside Sonar. From my experience and the use cases I had so far, the first release (0.3.1) which was deployed in August 2014 already provides some good features.

Basically, with Sonar-HLA you can do this:

  • Get a list of all projects from a SonarQube server along with the project’s measures and convert them to a CSV string or stream for further processing or for directly importing the CSV data into a calc sheet.
  • One may get the data using
    • the Java API directly or
    • use the Maven plugin.

Sonar-HLA is a Maven project. So integrate it into your own Maven project by using this dependency:

<dependency>
  <groupId>com.github.badamowicz</groupId>
  <artifactId>sonar-hla</artifactId>
  <version>0.3.1</version>
</dependency>

And now, with these few lines of Java code, just extract all projects from Sonar and generate CSV from it:

import java.util.Arrays;
import java.util.List;

import com.github.badamowicz.sonar.hla.api.HLAMeasure;
import com.github.badamowicz.sonar.hla.api.IProject;
import com.github.badamowicz.sonar.hla.api.ISonarConverter;
import com.github.badamowicz.sonar.hla.api.ISonarExtractor;
import com.github.badamowicz.sonar.hla.impl.SonarHLAFactory;

public class ExtractProjectsExample {

    public void extractProjects() {

        ISonarExtractor extractor = null;
        ISonarConverter converter = null;
        List<IProject> projects = null;
        String csvData = null;

        extractor = SonarHLAFactory.getExtractor("http://localhost:9000");
        converter = SonarHLAFactory.getConverterInstance();
        projects = extractor.getAllProjects();
        csvData = converter.getCSVData(projects, Arrays.asList(HLAMeasure.values()), true);

        // Now do something with CSV...
    }
}

You will find a more detailed description of the used classes and interfaces here.

If you want it even more easy, just use the Maven plugin of Sonar-HLA. If Maven is installed on your computer and you have a SonarQube server running on localhost with port 9000 (default), just issue:

mvn com.github.badamowicz:sonar-hla-maven-plugin:0.3.1:extract

This command will use all default values from the plugin and create an output like this:

Project;Lines of Code;Coverage [%];Complexity;Duplicate Lines;Issues;Blocker;Critical;Major;Minor;Info
com.github.badamowicz.sonar.hla:sonar-hla-parent;600;87.7%;102;0;27;0;0;27;0;0
com.github.badamowicz:sonar-hla-parent;600;87.7%;102;0;27;0;0;27;0;0

Actually some more output will be generated. Get the details from the plugin’s quick start guide. But now simply copy the lines containing CSV data, save them as a file and import into OpenOffice, Excel or whatever.

If you find all that useful, you may now:

There are already some more features in my mind which will further improve Sonar-HLA. Some of them are already listed in the issue tracker of Github. If you have some improvements you think would make Sonar-HLA more useful or if you just encounter a bug, also just drop me an email or leave a comment.

Thanks!

Subscribe to new posts

If you're interested in new posts and updates, just subscribe here.

, , , ,

No comments yet.

Leave a Reply

* Copy This Password *

* Type Or Paste Password Here *