我正在尝试编写一个
的小脚本获取特定环境中所有微服务的版本(已解决)
对于每个项目/版本,获取该确切版本的sonarqube代码覆盖率。我在为特定项目的确切版本获取sonarqube覆盖时遇到问题。
我正在使用sonarqube 6.0(根据我的/ api /服务器/版本端点)(我希望我们可以很快升级到6.4,但这不是我直接控制的,我不会'我想等待它)
我的问题是 - 我无法将数据绑定在一起,因为当我调用/ api / events端点时,我只返回项目的日期和覆盖范围,而不是版本。这是一个工作代码示例(不包括凭据和基本URL)
我很乐意用任何语言解决这个问题 - ruby,python,php,java,js,无论什么工作。
#!/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
答案 0 :(得分:3)
从SonarQube 6.3开始
要获取特定版本的度量(因为它是用例),您需要使用2个Web服务:
{
"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。
/api/timemachine
应该使用第一个WS调用返回的日期