Horst Krause
Horst Krause

Reputation: 686

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

We use jenkins, sonarqube 5.5, maven and git. When developers create a new git branch and push it, jenkins analyses the branch too, so the developers can fix everything before merging. To avoid this development branch analysis mixing up with the master branch analysis, jenkins passes the branch name into the analysis. The causes sonarqube to create a new project for each branch. 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. But now this is deprecated. So what's the new way to define the proper quality gate for a newly created project?

Upvotes: 4

Views: 3830

Answers (1)

Tim
Tim

Reputation: 2066

You can use the rest api provided by Sonar.

Step 1. Create gate

        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

    ["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

Get project 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

        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.

Br,

Tim

Upvotes: 4

Related Questions