本节SonarQube版本为V6.7-community。

安装徽章插件

使用默认账号密码admin/admin登录系统 http://localhost:9000 后,在页面的底部可以看到Web API链接,在搜索框中输入"api/badges",默认是搜索不出东西的。此时依次点击Administration --> Marketplace,或直接访问 http://localhost:9000/admin/marketplace ,在当前页面中使用ctrl+f搜索"SVG Badges",点击其右方的"Install"按钮,等待安装成功。

若在Marketplace里查找不到,可以去https://github.com/QualInsight/qualinsight-plugins-sonarqube-badges/releases这里下载,并手工复制到容器的/opt/sonarqube/extensions/plugins/目录下,再重启。

使用

此时假如项目列表中已有一个项目com.example:sonar-demo,这时候我们访问 http://localhost:9000/api/badges/measure?key=com.example%3Asonar-demo&metric=coverage 即可看到测试覆盖率的数值,同时还可以使用 http://localhost:9000/api/badges/measure?key=com.example%3Asonar-demo&metric=alert_status 来查看项目状态。

[![Sonar Quality Gate](http://localhost:9000/api/badges/measure?key=com.example%3Asonar-demo&metric=alert_status)](http://localhost:9000/dashboard?id=com.example%3Asonar-demo)
[![Sonar Coverage](http://localhost:9000/api/badges/measure?key=com.example%3Asonar-demo&metric=coverage)](http://localhost:9000/dashboard?id=com.example%3Asonar-demo)

是不是很有意思?

参考页面

  • https://stackoverflow.com/questions/43743141/using-gitlab-variables-in-gitlab-readme-md-for-sonarqube-badges
  • https://github.com/selenide/selenide (看到这个项目的徽章导致我想在自己的项目中也添加一下~)