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

I am trying to write a tiny script that

  • Get the versions of all microservices that are in a specific environment (resolved)

  • For each project / version, it receives sonar code coverage for this exact version. I'm having trouble getting Sonarbub coverage for the exact version of a specific project.

I am using sonarqube 6.0 (according to my endpoint my / api / server / version) (I hope we can upgrade to 6.4 in the near future, but this is not in my direct control and I do not want to wait for it)

My problem: I cannot bind data together, because when I call the endpoint / api / events, I get only the project date and scope, not the version. Here is an example of working code (credentials and base url not included)

I would be happy to solve this in any language: ruby, python, php, java, js, no matter what 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
+4
source share
1 answer

Starting with SonarQube 6.3

To get measures of a specific version (because this is a 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"
                }
            ]
        }
    ]

}

SonarQube 6.3

, WS :

  • , , /api/events WS, JSON-.
    • . "" WS
  • /api/timemachine , WS
    • . "" WS
+3

Source: https://habr.com/ru/post/1681657/


All Articles