數(shù)據(jù)庫(kù)定義,但 不建議直接查詢DB,使用Sonar提供的Web_Api,默認(rèn)在SonarQube服務(wù)的/web_api下可以查看到提供的所有API信息,如http://localhost:9000/web_api/api/project_analyses
對(duì)應(yīng)的屬性含義參考: https://docs.sonarqube.org/7.6/user-guide/metric-definitions/
數(shù)據(jù)來(lái)源方法:
- 獲取項(xiàng)目總數(shù):
http://example.sonar.com/api/components/search_projects?pageIndex=2&ps=500
- 獲取最近一次的分析結(jié)果時(shí)間:
API: /api/components/search_projects
Para:
ps? #大小,最大500
f? #
example:
http://example.sonar.com/api/components/search_projects?ps=50&f=analysisDate
result:
{
"paging":{
"pageIndex":1,
"pageSize":50,
"total":2337
},
"organizations":[
],
"components":Array[50],
"facets":[
]
}
# 其中components中包含項(xiàng)目name 和時(shí)間戳,如
{
"organization":"default-organization",
"id":"AW0j4CH4yaNpfFfxGPl2",
"key":"projectGroup1:projectNameExample",
"name":"projectGroup1:projectNameExample",
"isFavorite":false,
"analysisDate":"2019-11-27T20:37:05+0800",
"tags":[],
"visibility":"public"
}
- 根據(jù)component,查詢不同的metrics:
API: /api/measures/search
Para:
projectKeys? # 逗號(hào)分隔多個(gè)項(xiàng)目
metricKeys? # 查詢的各個(gè)標(biāo)準(zhǔn)
example:
http://example.sonar.com/api/measures/search?projectKeys=projectGroup1:projectNameExample&metricKeys=alert_status,bugs,reliability_rating,vulnerabilities,security_rating,code_smells,sqale_rating,duplicated_lines_density,coverage,ncloc,ncloc_language_distribution
result:
{
"measures":[
{
"metric":"alert_status",
"value":"OK",
"component":"projectGroup1:projectNameExample"
},
{
"metric":"bugs",
"value":"6",
"component":"projectGroup1:projectNameExample",
"bestValue":false
},
{
"metric":"code_smells",
"value":"185",
"component":"projectGroup1:projectNameExample",
"bestValue":false
},
{
"metric":"coverage",
"value":"0.0",
"component":"projectGroup1:projectNameExample",
"bestValue":false
},
{
"metric":"duplicated_lines_density",
"value":"64.6",
"component":"projectGroup1:projectNameExample",
"bestValue":false
},
{
"metric":"ncloc",
"value":"14566",
"component":"projectGroup1:projectNameExample"
},
{
"metric":"ncloc_language_distribution",
"value":"java=4756;js=9582;web=228",
"component":"projectGroup1:projectNameExample"
},
{
"metric":"reliability_rating",
"value":"3.0",
"component":"projectGroup1:projectNameExample",
"bestValue":false
},
{
"metric":"security_rating",
"value":"2.0",
"component":"projectGroup1:projectNameExample",
"bestValue":false
},
{
"metric":"sqale_rating",
"value":"1.0",
"component":"projectGroup1:projectNameExample",
"bestValue":true
},
{
"metric":"vulnerabilities",
"value":"27",
"component":"projectGroup1:projectNameExample",
"bestValue":false
}
]
}
- 查詢?cè)隽績(jī)?nèi)容——
接口調(diào)用可以使用Basic的認(rèn)證方式:參考
- Use token
curl -u admin:SuPeRsEcReT "https://sonar.mydomain.com/api/resources?resource=com.mydomain.project:MY&metrics=ncloc&format=json"curl -u THIS_IS_MY_TOKEN: https://sonarqube.com/api/user_tokens/search- note that the colon after the token is required in curl to set an empty password