在sonarqube系統中,可以通過webhook實現代碼分析結果的通知。比如我們希望將分析結果發送到釘釘群,那么可以新建一個web應用,編寫發送消息到釘釘群的邏輯,然后提供一個http接口,設置到sonarqube的webhook中。這樣,當sonarqube分析完代碼,會調用設置的http接口。這樣就實現了代碼質量分析結果通知到釘釘群的功能。
jenkins執行命名示例
mvn clean compile -Dmaven.test.failure.ignore=true \
sonar:sonar -Dsonar.host.url=http://10.0.0.1:9000 -Dsonar.login=xxxxxxx
sonarqube分析結果json示例
{
"serverUrl": "http://localhost:9000",
"taskId": "AVh21JS2JepAEhwQ-b3u",
"status": "SUCCESS",
"analysedAt": "2016-11-18T10:46:28+0100",
"revision": "c739069ec7105e01303e8b3065a81141aad9f129",
"project": {
"key": "myproject",
"name": "My Project",
"url": "https://mycompany.com/sonarqube/dashboard?id=myproject"
},
"branch": {
"name": "master",
"url": "http://10.0.0.10:9000/dashboard?id=xxxx
},
"properties": {
},
"qualityGate": {
"conditions": [
{
"errorThreshold": "1",
"metric": "new_security_rating",
"onLeakPeriod": true,
"operator": "GREATER_THAN",
"status": "OK",
"value": "1"
},
],
"name": "SonarQube way",
"status": "OK"
}
}
因為開源版的sonarqube不支持代碼多分支管理,分析結果的json中,branch始終為master。為了解決這個問題,我們可以使用sonarqube的自定義參數來解決。方法是在sonar掃描時指定自定義參數,如下:
mvn clean compile sonar:sonar -Dsonar.analysis.branch=test
參考
https://docs.sonarqube.org/latest/project-administration/webhooks/