本文介绍: (7)Sonarqube已新增gradle项目。(3)Sonarqube已新增go项目。(2)Jenkins添加凭证。(4)任意项目中回放进行测试。(5)任意项目中回放进行测试。(1)Postman测试。(1)Postman测试。
一、实验
1.SonarQube REST API 查找项目
pipeline {
agent { label "build"}
stages {
stage("Sonar"){
steps{
script {
projectName = "devops03-maven5-service"
if (SearchProject(projectName)){
println("${projectName} exist")
} else {
println("${projectName} not fonud")
}
}
}
}
}
}
def SonarRequest(apiUrl,method){
withCredentials([string(credentialsId: "855da77c-ad38-4f06-a941-3a5afcf6bbc6", variable: 'SONAR_TOKEN')]) {
sonarApi = "http://192.168.204.8:9000/api"
response = sh returnStdout: true,
script: """
curl --location
--request ${method}
"${sonarApi}/${apiUrl}"
--header "Authorization: Basic ${SONAR_TOKEN}"
"""
// json格式化
try {
response = readJSON text: """ ${response - "n"} """
} catch(e){
response = readJSON text: """{"errors" : true}"""
}
return response
}
}
//查找项目
def SearchProject(projectName){
apiUrl = "projects/search?projects=${projectName}"
response = SonarRequest(apiUrl,"GET")
if (response.paging.total == 0){
return false
}
return true
}
(4)任意项目中回放进行测试
(5)成功
2.SonarQube REST API 新增项目
pipeline {
agent { label "build"}
stages {
stage("Sonar"){
steps{
script {
// projectName = "devops03-maven5-service"
projectName = "devops03-gradle-service"
if (SearchProject(projectName)){
println("${projectName} exist")
} else {
println("${projectName} not fonud")
//项目不存在
CreateProject(projectName)
}
}
}
}
}
}
def SonarRequest(apiUrl,method){
withCredentials([string(credentialsId: "855da77c-ad38-4f06-a941-3a5afcf6bbc6", variable: 'SONAR_TOKEN')]) {
sonarApi = "http://192.168.204.8:9000/api"
response = sh returnStdout: true,
script: """
curl --location
--request ${method}
"${sonarApi}/${apiUrl}"
--header "Authorization: Basic ${SONAR_TOKEN}"
"""
// json格式化
try {
response = readJSON text: """ ${response - "n"} """
} catch(e){
response = readJSON text: """{"errors" : true}"""
}
return response
}
}
//查找项目
def SearchProject(projectName){
apiUrl = "projects/search?projects=${projectName}"
response = SonarRequest(apiUrl,"GET")
if (response.paging.total == 0){
return false
}
return true
}
//创建项目
def CreateProject(projectName){
apiUrl = "projects/create?name=${projectName}&project=${projectName}"
response = SonarRequest(apiUrl,"POST")
try{
if (response.project.key == projectName ) {
println("Project Create success!...")
return true
}
}catch(e){
println(response.errors)
return false
}
}
(5)任意项目中回放进行测试
(6)成功
(7)Sonarqube已新增gradle项目
原文地址:https://blog.csdn.net/cronaldo91/article/details/134823035
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_48832.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。