背景:
jenkins+sonar集成了代码扫描,但是发出的邮件不管项目质量是通过还是失败,每次邮件的标题都是jenkins的构建状态,所以需要获取sonar中该项目的扫描结果。
解决:
在sonarqube的服务器上输入
就可查看sonar提供的接口
里面有详细的介绍,本次主要想获取某个项目的执行结果,使用这个接口:
api/qualitygates/project_status
传入projectKey或者projectId或者analysisId任意一个参数就行了
如:
接口返回
{"projectStatus": {"status": "ERROR","conditions": [{"status": "ERROR","metricKey": "new_security_rating","comparator": "GT","periodIndex": 1,"errorThreshold": "1","actualValue": "5"}, {"status": "ERROR","metricKey": "new_reliability_rating","comparator": "GT","periodIndex": 1,"errorThreshold": "1","actualValue": "4"}, {"status": "OK","metricKey": "new_maintainability_rating","comparator": "GT","periodIndex": 1,"errorThreshold": "1","actualValue": "1"}, {"status": "ERROR","metricKey": "new_coverage","comparator": "LT","periodIndex": 1,"errorThreshold": "80","actualValue": "0.0"}, {"status": "ERROR","metricKey": "new_duplicated_lines_density","comparator": "GT","periodIndex": 1,"errorThreshold": "3","actualValue": "5.967688757006265"}],"periods": [{"index": 1,"mode": "previous_version","date": "2019-05-31T09:35:58+0800"}],"ignoredConditions": false}
}
接口返回
{"projectStatus": {"status": "ERROR","conditions": [{"status": "ERROR","metricKey": "new_security_rating","comparator": "GT","periodIndex": 1,"errorThreshold": "1","actualValue": "5"}, {"status": "ERROR","metricKey": "new_reliability_rating","comparator": "GT","periodIndex": 1,"errorThreshold": "1","actualValue": "4"}, {"status": "OK","metricKey": "new_maintainability_rating","comparator": "GT","periodIndex": 1,"errorThreshold": "1","actualValue": "1"}, {"status": "ERROR","metricKey": "new_coverage","comparator": "LT","periodIndex": 1,"errorThreshold": "80","actualValue": "0.0"}, {"status": "ERROR","metricKey": "new_duplicated_lines_density","comparator": "GT","periodIndex": 1,"errorThreshold": "3","actualValue": "5.967688757006265"}],"periods": [{"index": 1,"mode": "previous_version","date": "2019-05-31T09:35:58+0800"}],"ignoredConditions": false}
}
projectKey和projectId可以在sonar的project表中查询到
SELECT * FROM projects WHERE `name`='项目名'
将返回的json解析就可得到扫描结果,然后添加到jenkins邮件配置的主题中
后续其他接口待研究