SonarQube 09 REST API实践

网友投稿 735 2022-11-02


SonarQube 09 REST API实践

SonarQube系统的API文档: ​​--location --request GET '\--header 'Authorization: Basic YWRtaW46YWRtaW4xMjM='

[root@jenkins-master ~]# curl --location --request GET '\> --header 'Authorization: Basic YWRtaW46YWRtaW4xMjM='{"paging":{"pageIndex":1,"pageSize":100,"total":1},"components":[{"key":"devops-maven-sonarqube","name":"devops-maven-sonarqube","qualifier":"TRK","visibility":"public","lastAnalysisDate":"2021-09-03T01:56:06+0000","revision":"1b40a1ef3391301102f1972109f26188f62d9581"}]}[root@jenkins-master ~]#

package org.devopsdef SonarRequest(apiUrl,method){ sonarApi=" respone=sh returnStdout: true, script: """ curl --location \ --request ${method} \ "${sonarApi}/${apiUrl}" \ --header 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' """ respone=respone -"\n" result=readJSON text: "${respone}" return result}def SerarchProject(projectName,method){ apiUrl="projects/search?projects=${projectName}" respone=SonarRequest(apiUrl,"GET") if (respone["paging"]["total"] == 1){ return true }else{ return false }}

@Library("devopslib@main") _def sonarqubeapi = new org.devops.sonarqubeapi()pipeline { agent any stages { stage('SeacrchProject') { steps { script{ println(sonarqubeapi.SerarchProject("devops-maven-service","GET")) } } } }}

创建项目:

curl --location --request POST '\--header 'Authorization: Basic YWRtaW46YWRtaW4xMjM='

{ "errors": [ { "msg": "Could not create Project, key already exists: sonar-maven" } ]}

package org.devopsdef SonarRequest(apiUrl,method){ sonarApi=" respone=sh returnStdout: true, script: """ curl --location \ --request ${method} \ "${sonarApi}/${apiUrl}" \ --header 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' """ respone=respone -"\n" result=readJSON text: "${respone}" return result}def SerarchProject(projectName,method){ apiUrl="projects/search?projects=${projectName}" respone=SonarRequest(apiUrl,"GET") if (respone["paging"]["total"] == 1){ return true }else{ return false }}def CreateProject(projectName,method){ apiUrl="projects/create?name=${projectName}&project=${projectName}" echo "${apiUrl}" respone=SonarRequest(apiUrl,"POST") try{ if (respone["project"]["name"] == "${projectName}"){ println("project create success") return true } }catch(e){ println(respone.errors) return false }}

最后就是给项目配置质量,现在这个项目里面什么都没有

catche来包围

def SonarRequest(apiUrl,method){ sonarApi=" respone=sh returnStdout: true, script: """ curl --location \ --request ${method} \ "${sonarApi}/${apiUrl}" \ --header 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' """ respone=respone -"\n" try{ result=readJSON text: "${respone}" }catch(e){ result="" } return result }def UpdateQualityProfile(language,projectName,qualityProfile){ apiUrl="qualityprofiles/add_project?language=${language}&project=${projectName}&qualityProfile=${qualityProfile}" respone=SonarRequest(apiUrl,"POST") if(respone==""){ println("language ${language} ${qualityProfile} bind ${projectName} success") }else{ println("language ${language} ${qualityProfile} bind ${projectName} failed") }}

如果不放在共享库,那么可以简化为下面一个脚本

@Library("devopslib@main") _def project = new org.devops.build()def buildTools = ["maven": "/usr/local/apache-maven-3.8.1"]def credentials = ["devops-maven-sonarqube": "f8b33d17-c1cf-428e-aa31-99d4038e59d0"]String buildType = "${env.buildType}"String projectDescription = "this is maven project"currentBuild.description = "maven project"pipeline { agent { label 'build' } stages { stage("createProject"){ steps{ script{ if(searchProject(env.JOB_NAME) == false){ createProject(env.JOB_NAME) } } } } stage('updateQualityProfile'){ steps{ script{ UpdateQualityProfile("java",env.JOB_NAME,"maven") } } } }}def sonarHttpRequest(method,apiUrl){ sonarApi = " response = sh returnStdout: true, script: """ curl --location \ --request ${method} \ "${sonarApi}/${apiUrl}" \ --header 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' """ response = response - "\n" try{ result = readJSON text: "${response}" }catch(e){ result = "" } return result}def searchProject(projectName){ apiUrl = "projects/search?projects=${projectName}" response = sonarHttpRequest("GET",apiUrl) println(response) if (response["paging"]["total"] == 1){ return true }else{ return false }}def createProject(projectName){ apiUrl="projects/create?name=${projectName}&project=${projectName}" response=sonarHttpRequest("POST",apiUrl) println(response) try{ if (response["project"]["name"] == "${projectName}"){ println("project create success") return true } }catch(e){ println(response.errors) return false }}def UpdateQualityProfile(language,projectName,qualityProfile){ apiUrl="qualityprofiles/add_project?language=${language}&project=${projectName}&qualityProfile=${qualityProfile}" response=sonarHttpRequest("POST",apiUrl) if(response==""){ println("language ${language} ${qualityProfile} bind ${projectName} success") }else{ println("language ${language} ${qualityProfile} bind ${projectName} failed") }}

Running on build-01 in /data/cicd/jenkinsagent/workspace/sonar-api[Pipeline] {[Pipeline] stage[Pipeline] { (createProject)[Pipeline] script[Pipeline] {[Pipeline] sh+ curl --location --request GET '--header 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 145 100 145 0 0 734 0 --:--:-- --:--:-- --:--:-- 732100 145 100 145 0 0 733 0 --:--:-- --:--:-- --:--:-- 732[Pipeline] readJSON[Pipeline] echo{"paging":{"pageIndex":1,"pageSize":100,"total":1},"components":[{"key":"sonar-api","name":"sonar-api","qualifier":"TRK","visibility":"public"}]}[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (updateQualityProfile)[Pipeline] script[Pipeline] {[Pipeline] sh+ curl --location --request POST '--header 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0[Pipeline] readJSON[Pipeline] echolanguage java maven bind sonar-api success[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决@Transactional注解事务不回滚不起作用的问题
下一篇:Jenkins CD 流水线设计 Gitops CI制品信息保存为CD准备
相关文章

 发表评论

暂时没有评论,来抢沙发吧~