sonarQube的Api调用总结

       最近公司开发涉及到对Sonar获取数据和创建用户等操作,Sonar为我们提供了web Api,同时,在sonar5.x版本之前还贴心的为我们提供了sonar-ws-client,但是由于之后版本迭代比较快,接口差异大,sonar-ws-client在较新的版本中已不在适用。

        有人说官网上不是提供了Api么,是的,我们需要自己再封装一下,本帖为扫盲贴,大神请绕道,首先在适用api之前,我们需要知道

1:我们需要做安全校验


sonar默认采用 Basic Auth 验证,需要在请求头中带上校验信息

2:我们需要注意传参格式


sonar传参不能讲参数放在我们之前习惯的head或者body,需要我们将参数拼接在url中,不管是get还是post

下面我们看看具体实现:

this.encoding = DatatypeConverter.printBase64Binary((username+":"+password).getBytes("UTF-8"));

扫描二维码关注公众号,回复: 9615931 查看本文章
httpPost.setHeader("Authorization", "Basic " + this.encoding);

如此将校验信息头加上


传参格式如图所示,如此所有接口都能轻松掉通

另外由于sonar在国内信息都不多,我这里再啰嗦几句

sonar有自己的回调接口,每次完成代码检查后会调用此接口,我们只需要在sonar上配置回调地址,默认可配置10个,每个接口都能收到sonar的回调


同时注意sonar的回调入参是一个json字符串对象,我们需要以此对象去接收,只需将此格式转为对象,spring的话用如下方式接收即可

@RequestBody SonarCallBackDto sonarCallBackDto

{
     "analysedAt" "2016-11-18T10:46:28+0100" ,
     "project" : {
         "key" "org.sonarqube:example" ,
         "name" "Example"
     },
     "properties" : {
     },
     "qualityGate" : {
         "conditions" : [
             {
                 "errorThreshold" "1" ,
                 "metric" "new_security_rating" ,
                 "onLeakPeriod" true ,
                 "operator" "GREATER_THAN" ,
                 "status" "OK" ,
                 "value" "1"
             },
             {
                 "errorThreshold" "1" ,
                 "metric" "new_reliability_rating" ,
                 "onLeakPeriod" true ,
                 "operator" "GREATER_THAN" ,
                 "status" "OK" ,
                 "value" "1"
             },
             {
                 "errorThreshold" "1" ,
                 "metric" "new_maintainability_rating" ,
                 "onLeakPeriod" true ,
                 "operator" "GREATER_THAN" ,
                 "status" "OK" ,
                 "value" "1"
             },
             {
                 "errorThreshold" "80" ,
                 "metric" "new_coverage" ,
                 "onLeakPeriod" true ,
                 "operator" "LESS_THAN" ,
                 "status" "NO_VALUE"
             }
         ],
         "name" "SonarQube way" ,
         "status" "OK"
     },
     "serverUrl" "<a href="http://localhost:9000/" "="" style="border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important; background: 0px 50%;">http://localhost:9000" ,
     "status" "SUCCESS" ,
     "taskId" "AVh21JS2JepAEhwQ-b3u"
}

都是些简单的道理,由于官方文档不够详细,所以都琢磨了一会,希望对大家有所帮助


发布了6 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tushuping/article/details/81009723