Generating Sonar-Reports

This post gives a short tutorial/example of how to extract data from a Sonar server and convert them to CSV for further processing. Not a big issue, but I found this very useful and easy to accomplish. So I thought this might be worth a blog post.

Extract the Data from Sonar

Sonar provides a set of Web Services for accessing internal data. The whole list is found on Sonar’s documentation page here:

In this example I was only interested in the worst classes of one of my projects concerning line coverage. The appropriate URL for retrieving this data is:

  • http://your.sonar.server.com:9000/api/resources?resource=your.groupID:yourArtifactId:yourVersion&depth=-1&scopes=FIL&metrics=coverage&limit=100

So here we’re using the Web Service resource of the project with ID your.groupID:yourArtifactId:yourVersion and we’re interested in line coverage by specifying scopes=FIL&metrics=coverage. By specifying limit=100 I got only 100 classes. Obviously the output is sorted such that classes with low coverages are the first to be in the list.

This URL retrieves an XML structure that looks like this:

<resources>
  <resource>
    <id>781</id>
    <key>yourGroupId:yourProjectId:yourVersion:some.package.SomeClass1
    </key>
    <name>SomeClass1</name>
    <lname>some.package.SomeClass1</lname>
    <scope>FIL</scope>
    <qualifier>CLA</qualifier>
    <lang>java</lang>
    <date>2013-05-28T11:12:53+0000</date>
    <msr>
      <key>coverage</key>
      <val>0.0</val>
      <frmt_val>0.0%</frmt_val>
    </msr>
  </resource>
  <resource>
    <id>761</id>
    <key>yourGroupId:yourProjectId:yourVersion:some.package.SomeClass2
    </key>
    <name>SomeClass2</name>
    <lname>some.package.SomeClass2</lname>
    <scope>FIL</scope>
    <qualifier>CLA</qualifier>
    <lang>java</lang>
    <date>2013-05-28T11:12:53+0000</date>
    <msr>
      <key>coverage</key>
      <val>0.0</val>
      <frmt_val>0.0%</frmt_val>
    </msr>
  </resource>

Convert XML data to CSV
In order to do some analysis and maybe also create some charts I wanted to have the data in CSV format in order to import them into OpenOffice. Some “googling” brought up xml2csv-conv which is a command line based Java program which does the job. Find it here:

In this particular case, I was only interested in the XML fields name, which contains the class name, and val, which contains the actual line coverage in percent. After downloading and some reading of the docs, this was my command:

java -jar xml2csv-conv.jar -k "name,val" report.xml report.csv

The generated CSV looks like this:

name,val
SomeClass1,0.0
SomeClass2,0.0
SomeClass3,0.0
SomeClass4,0.0
...

I will not show the further proceedings for importing into Open Office and creating a chart. Anyone reading this should be able to do so.

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 *