背景:
jenkins+sonar集成了代碼掃描,但是發出的郵件不管項目質量是通過還是失敗,每次郵件的標題都是jenkins的構建狀態,所以需要獲取sonar中該項目的掃描結果。
解決:
在sonarqube的服務器上輸入
http://192.168.207.160:9000/web_api
就可查看sonar提供的接口
里面有詳細的介紹,本次主要想獲取某個項目的執行結果,使用這個接口:
api/qualitygates/project_status
傳入projectKey或者projectId或者analysisId任意一個參數就行了
如:
http://192.168.207.160:9000/api/qualitygates/project_status?projectId=AWsLgu8jQ0UZgfmTXkJs
接口返回
{ "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 } }
http://192.168.207.160:9000/api/qualitygates/project_status?projectKey=A-ordnotify-other
接口返回
{ "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郵件配置的主題中
后續其他接口待研究