I am trying to write a tiny script that
Gets the versions of all microserves that are in a particular environment (solved)
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