SonarQube 6.0 get code coverage for a specific version of a project

7.1k Views Asked by At

I am trying to write a tiny script that

  1. Gets the versions of all microserves that are in a particular environment (solved)

  2. For each project/version gets the sonarqube code coverage for that exact version. I am having problems getting sonarqube coverage for an exact version of a particular project.

I am using sonarqube 6.0 (as per my /api/server/version endpoint) (I hope that we can upgrade to 6.4 the latest soon, but that's not in my direct control and I don't want to wait for it)

My problem is- I can't tie the data together, because when I call the /api/events endpoint, I only get back date and coverage for a project, not version. Here is a working code sample (credentials and base url not included)

I would be happy to solve this in any language- ruby, python, php, java, js, whatever works.

#!/usr/bin/ruby
require 'rest-client'
require 'json'
require 'ostruct'
require 'date'
require 'nokogiri'

projects_endpoint='/api/projects/'
time_machine_endpoint='/api/timemachine/'
events_endpoint='/api/events'

rc = RestClient::Resource.new(server_url, user, pass)
sonarqube_projects = JSON.parse(rc["#{projects_endpoint}index?format=json"].get, object_class: OpenStruct)

coverage_per_project = sonarqube_projects.map {|sq_project|
  # data shape: #<OpenStruct id="1687", k="foo-project", nm="foo-project", sc="PRJ", qu="TRK", lv="0.0.617", v=#<OpenStruct 0.0.617=#<OpenStruct sid="4197", d="2017-07-18T03:50:48+0000">>>
  project_name = sq_project.k
  url = "#{time_machine_endpoint}?format=json&resource=#{project_name}&metrics=coverage"
  events = JSON.parse(rc[url].get, object_class: OpenStruct)

  # data shape:
  # [#<OpenStruct cols=[#<OpenStruct metric="coverage">], cells=[#<OpenStruct d="2016-12-08T19:26:24+0000", v=[68.0]>, #<OpenStruct d="2016-12-08T19:36:46+0000", v=[68.0]>, #<OpenStruct d="2016-12-08T20:26:28+0000", v=[79.5]>, #<OpenStruct d="2016-12-08T20:36:53+0000", v=[79.5]>]
  # my problem is right here- I need a version of the app in each cells object so that I can search for one that matches my deployed version!

  # idealistic pseudo-code that doesn't work:
  correct_event = events.first {|event|
    event.version == my_deployed_app_version
  }

  return {project_name: project_name, coverage: correct_event.coverage}
}
puts coverage_per_project
1

There are 1 best solutions below

2
On BEST ANSWER

Starting with SonarQube 6.3

To get the measures of a specific version (because it's the use case), you need to use 2 Web Services:

{

    "paging": {
        "pageIndex": 1,
        "pageSize": 100,
        "total": 10
    },
    "analyses": [
        {
            "key": "AV07Vpk4NAVDjyrgWPAw",
            "date": "2017-07-13T11:45:12+0200",
            "events": [
                {
                    "key": "AV07VpslNAVDjyrgWPAx",
                    "category": "VERSION",
                    "name": "1.4.0"
                }
            ]
        },
...
}
{

    "paging": {
        "pageIndex": 1,
        "pageSize": 100,
        "total": 1
    },
    "measures": [
        {
            "metric": "coverage",
            "history": [
                {
                    "date": "2017-07-13T11:45:12+0200",
                    "value": "51.1"
                }
            ]
        }
    ]

}

Prior to SonarQube 6.3

The principle is the same, but the WS differ:

  • For the first call, you probably want to use the /api/events WS that returns a slightly different JSON document.
  • For the second call, /api/timemachine should do the work with the date that was returned by the first WS call