sonarqube java api_sonar:soanrqube接口api

本文介绍了如何结合Jenkins和SonarQube进行代码扫描,并通过SonarQube的API获取项目质量检查状态。重点讨论了利用`api/qualitygates/project_status`接口获取项目错误详情,包括安全、可靠性、可维护性、覆盖率和重复代码密度等指标,以定制Jenkins邮件通知的主题。
摘要由CSDN通过智能技术生成

背景:

jenkins+sonar集成了代码扫描,但是发出的邮件不管项目质量是通过还是失败,每次邮件的标题都是jenkins的构建状态,所以需要获取sonar中该项目的扫描结果。

57b10e635a469c543212f5a6755dc1a6.png

解决:

在sonarqube的服务器上输入

就可查看sonar提供的接口

34724983a542a6d21e0dbea10cba620a.png

里面有详细的介绍,本次主要想获取某个项目的执行结果,使用这个接口:

api/qualitygates/project_status

dd5936ee5471cbb2e276585af387e464.png

传入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`='项目名'

b3a4cad748fe7b921c46aa906c423fc4.png

将返回的json解析就可得到扫描结果,然后添加到jenkins邮件配置的主题中

后续其他接口待研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值