Using the Sonar REST API for weekly emails

At work, we wanted to create an automated weekly email to the team that shows how we’re doing on new code. Sonar (now SonarQube) has a Sonar REST API and a Sonar Java WS client wrapper for it. Here’s how I did:

First, you need to round up some jars. You need:

  • commons-logging
  • httpclient
  • httpcore
  • json-simple
  • sonar-ws-client

Once you have those, you can use it with something like this:

public class RetrieveStats {
 
    static String host = "http://myhost:8080/sonar";
    static String resourceKey = "org.blah:sonar_name";
    static String[] MEASURES_TO_GET = new String[]{"violations", "new_violations", 
                                                   "new_coverage", "tests"};
 
    public static void main(String[] args) {                
        try {       
            //setup
            DecimalFormat df = new DecimalFormat("#.##");            
            Date date = new Date();
 
            //header
            output("************************************");
            output("Code trend for the past 7 days as of "+date);
            output("************************************");
 
            //do the work of getting the data
            Sonar sonar = new Sonar(new HttpClient4Connector(new Host(host)));
            ResourceQuery query = ResourceQuery.createForMetrics(resourceKey, MEASURES_TO_GET);
            query.setIncludeTrends(true);
            Resource resource = sonar.find(query);
            //loop through them
            //getVariation2 for "7 days"
            List<measure> allMeasures = resource.getMeasures();
            for (Measure measure : allMeasures) {
                output(measure.getMetricKey()+": "+df.format(measure.getVariation2()));
            }            
            output("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
</measure>

and this will output something like:

************************************
Code trend for the past 7 days as of Wed Feb 20 17:42:17 EST 2013
************************************
tests: 4
new_coverage: 6.52
violations: -41
new_violations: 55

Of course, you can tweak the code to output any metrics you want with more details, etc. if you run this via Jenkins, you can have it email the output to the dev team. We do this once on Thursday morning (as a reminder to everyone), and once on Friday afternoon.

UPDATE: some people were confused about what a resourcekey is so here’s my explanation: The resourcekey is the identifier for your project in Sonar. You create it when you setup your project in Sonar… typically it’s of the format domain:projectname. For a given project, you can figure out what it is by going to the project in Sonar then clicking the the little link icon in the upper left then look at th far right of the URL. For example, this project in sonar, https://analysis.apache.org/dashboard/index/org.apache.directmemory.server:server has a resourcekey of org.apache.directmemory.server:server

8 thoughts on “Using the Sonar REST API for weekly emails

  1. Robin

    can you suggest me how to avoid this error and were to enter username and password for authentication
    Exception in thread “main” java.lang.NoSuchMethodError: org.apache.http.client.methods.HttpRequestBase.releaseConnection()V

    Reply
  2. Brian

    The resourcekey is the identifier for your project in Sonar. You create it when you setup your project in Sonar… typically it’s of the format domain:projectname. For a given project, you can figure out what it is by going to the project in Sonar then clicking the the little link icon in the upper left then look at th far right of the URL. For example, hsi project in sonar, https://analysis.apache.org/dashboard/index/org.apache.directmemory.server:server has a resourcekey of org.apache.directmemory.server:server

    Reply
  3. klamali

    I want to retrive data (data mensioned in your article ) and store them in a mysql database for further analysis. This must be implemented in javascript or nodejs.
    Please help me
    P.S->>the link ‘sonar rest api’ just goes to the sonarqube documentation
    please give me the correct link too

    Reply
  4. phani

    Getting the error
    Exception in thread “main” java.lang.NoSuchMethodError: org.apache.http.client.methods.HttpRequestBase.releaseConnection()V

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *