前言
由于项目繁多且每个项目对应质量规则和质量域是不尽相同的,故根据gitlab项目要求自动在SonarQube上创建项目,且指定其质量规则和质量域
常用SonarQube Api:
1.查找项目
projects/search?projects=${projectName}
1)sonarapi.grooy
//搜索Sonar项目 def SerarchProject(projectName){ apiUrl = "projects/search?projects=${projectName}" response = HttpReq("GET",apiUrl,'') response = readJSON text: """${response.content}""" result = response["paging"]["total"] if(result.toString() == "0"){ return "false" } else { return "true" } }
2)jenkinsfile调用
stage("QA"){ steps { script{ tools.PrintMes("搜索项目","green") result = sonarapi.SerarchProject("${JOB_NAME}") println(result) if (result == "false"){ println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!") sonarapi.CreateProject("${JOB_NAME}") } else { println("${JOB_NAME}---项目已存在!") } } } }
2.新建项目
projects/create?name=${projectName}&project=${projectName}
1)sonarapi.grooy
//创建Sonar项目 def CreateProject(projectName){ apiUrl = "projects/create?name=${projectName}&project=${projectName}" response = HttpReq("POST",apiUrl,'') println(response) }
2)jenkinsfile调用
stage("QA"){ steps { script{ tools.PrintMes("搜索项目","green") result = sonarapi.SerarchProject("${JOB_NAME}") println(result) if (result == "false"){ println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!") sonarapi.CreateProject("${JOB_NAME}") } else { println("${JOB_NAME}---项目已存在!") } } } }
3.自动配置项目质量规则
qualityprofiles/add_project?language=${lang}&project=${projectName}&qualityProfile=${qpname}
1)sonarapi.grooy
//配置项目质量规则 def ConfigQualityProfiles(projectName,lang,qpname){ apiUrl = "qualityprofiles/add_project?language=${lang}&project=${projectName}&qualityProfile=${qpname}" response = HttpReq("POST",apiUrl,'') println(response) }
2)jenkinsfile调用
stage("QA"){ steps { script{ tools.PrintMes("配置项目质量规则","green") qpName="${JOB_NAME}".split("-")[0] //Sonar%20way sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName) } } }
4.自动配置项目质量域
1)sonarapi.grooy
//获取质量阈ID def GetQualtyGateId(gateName){ apiUrl= "qualitygates/show?name=${gateName}" response = HttpReq("GET",apiUrl,'') response = readJSON text: """${response.content}""" result = response["id"] return result } //配置项目质量阈 def ConfigQualityGates(projectName,gateName){ gateId = GetQualtyGateId(gateName) apiUrl = "qualitygates/select?gateId=${gateId}&projectKey=${projectName}" response = HttpReq("POST",apiUrl,'') println(response)println(response) }
2) jenkinsfile调用
stage("QA"){ steps { script{ tools.PrintMes("配置质量阈","green") sonarapi.ConfigQualityGates("${JOB_NAME}",qpName) } } }
5.全部代码
1)sonarapi.groovy
package org.devops //封装HTTP def HttpReq(reqType,reqUrl,reqBody){ def sonarServer = "http://192.168.1.200:30090/api" result = httpRequest authentication: 'sonar-admin-user', httpMode: reqType, contentType: "APPLICATION_JSON", consoleLogResponseBody: true, ignoreSslErrors: true, requestBody: reqBody, url: "${sonarServer}/${reqUrl}" //quiet: true return result } //获取Sonar质量阈状态 def GetProjectStatus(projectName){ apiUrl = "project_branches/list?project=${projectName}" response = HttpReq("GET",apiUrl,'') response = readJSON text: """${response.content}""" result = response["branches"][0]["status"]["qualityGateStatus"] //println(response) return result } //搜索Sonar项目 def SerarchProject(projectName){ apiUrl = "projects/search?projects=${projectName}" response = HttpReq("GET",apiUrl,'') response = readJSON text: """${response.content}""" result = response["paging"]["total"] if(result.toString() == "0"){ return "false" } else { return "true" } } //创建Sonar项目 def CreateProject(projectName){ apiUrl = "projects/create?name=${projectName}&project=${projectName}" response = HttpReq("POST",apiUrl,'') println(response) } //配置项目质量规则 def ConfigQualityProfiles(projectName,lang,qpname){ apiUrl = "qualityprofiles/add_project?language=${lang}&project=${projectName}&qualityProfile=${qpname}" response = HttpReq("POST",apiUrl,'') println(response) } //获取质量阈ID def GetQualtyGateId(gateName){ apiUrl= "qualitygates/show?name=${gateName}" response = HttpReq("GET",apiUrl,'') response = readJSON text: """${response.content}""" result = response["id"] return result } //配置项目质量阈 def ConfigQualityGates(projectName,gateName){ gateId = GetQualtyGateId(gateName) apiUrl = "qualitygates/select?gateId=${gateId}&projectKey=${projectName}" response = HttpReq("POST",apiUrl,'') println(response)println(response) }
2) jenkinsfile调用
stage("QA"){ steps { script{ tools.PrintMes("搜索项目","green") result = sonarapi.SerarchProject("${JOB_NAME}") println(result) if (result == "false"){ println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!") sonarapi.CreateProject("${JOB_NAME}") } else { println("${JOB_NAME}---项目已存在!") } tools.PrintMes("配置项目质量规则","green") qpName="${JOB_NAME}".split("-")[0] //Sonar%20way sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName) tools.PrintMes("配置质量阈","green") sonarapi.ConfigQualityGates("${JOB_NAME}",qpName) tools.PrintMes("代码扫描","green") sonar.SonarScan("test","${JOB_NAME}","${JOB_NAME}","src",branchName) sleep 30 tools.PrintMes("获取扫描结果","green") result = sonarapi.GetProjectStatus("${JOB_NAME}") println(result) if (result.toString() == "ERROR"){ toemail.Email("代码质量阈错误!请及时修复!",userEmail) error " 代码质量阈错误!请及时修复!" } else { println(result) } } } }
6.测试验证
Ps:至此可以自动根据不同项目指定不同质量规则和质量域
标签:实战,NAME,SonarQube,apiUrl,JOB,result,projectName,response From: https://www.cnblogs.com/wuxinchun/p/16799557.html