简体   繁体   English

如何为新的声纳项目设置非默认质量门

[英]How to set non-default quality gate for new sonar projects

We use jenkins, sonarqube 5.5, maven and git. 我们使用jenkins,sonarqube 5.5,maven和git。 When developers create a new git branch and push it, jenkins analyses the branch too, so the developers can fix everything before merging. 当开发人员创建一个新的git分支并推送它时,jenkins也会对分支进行分析,因此开发人员可以在合并之前修复所有内容。 To avoid this development branch analysis mixing up with the master branch analysis, jenkins passes the branch name into the analysis. 为了避免这种开发分支分析与主分支分析混合,jenkins将分支名称传递给分析。 The causes sonarqube to create a new project for each branch. 导致sonarqube为每个分支创建一个新项目。 So far that's ok. 到目前为止还可以。

But recently we switched from one default quality gate for all projects to different quality gates for projects under active development and projects which are just in maintenance. 但最近我们从所有项目的一个默认质量门切换到正在开发的项目和刚刚维护的项目的不同质量门。

So how can we tell sonar when creating an new project for a new branch which quality gate to use? 那么我们如何在为新分支创建新项目时告诉声纳哪个质量门要使用? Until some versions ago, there was a sonar.qualitygate property which could be set. 在某些版本之前,有一个可以设置的sonar.qualitygate属性。 But now this is deprecated. 但现在这已被弃用了。 So what's the new way to define the proper quality gate for a newly created project? 那么为新创建的项目定义适当质量门的新方法是什么?

You can use the rest api provided by Sonar. 您可以使用Sonar提供的其余api。

Step 1. Create gate 步骤1.创建门

        def result = ["curl", "--user", auth, "-X", "POST", "-H", "Content-Type: application/json", "-d", "{'name':'" + qualityGateName + "'}", "https://yoursonarserver/api/qualitygates/create"].execute().text

Step 2 Bind project into the gate 步骤2将项目绑定到门口

    ["curl", "--user", auth, "-X", "POST", "-H", "Content-Type: application/json", "-d", "{'gateId':'"+qualityGateId+"','projectId':'"+projectId+"'}", "https://yoursonarserver/qualitygates/select"].execute().text

About how to get the projectId and qualityGateId, you can use the following two apis 关于如何获取projectId和qualityGateId,您可以使用以下两个api

Get project ID 获取项目ID

        String result = ["curl", "--user", auth , "-X", "GET", "-H", "Accept: application/json", "https://yoursonarserver/api/projects/index", "-d", "search=" + projectName ].execute().text

Get Quality gate id 获取质量门ID

        def result = ["curl", "--user", auth, "-X", "GET", "-H", "Accept: application/json", "https://yoursonarserver/api/qualitygates/list"].execute().text

The above two apis will get a list of ids, so you need parse them based the project name. 以上两个api将获得一个id列表,因此您需要根据项目名称解析它们。

Br, BR,

Tim 蒂姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM