10

We currently have a jenkins pipeline using Multibranch Workflow plugin. Each git branch execute a sonarqube analysis creating a sonarqube project using the sonar.branch property. This is very useful because each branch is being analyzed before merge it, the problem appear when a branch is merged with master and disappear on GIT, the project continues on sonarqube and need to be deleted manually. Is There a way to do it automatically? or any other recomendation?

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
F.Rosado
  • 487
  • 4
  • 20

3 Answers3

4

The best way to remove SonarQube branch projects is to not push them to the server in the first place. Here are your options:

At SonarSource, we use SonarLint and the GitHub Plugin on a daily basis. One last solution is to delete manually the project using the api/projects/delete web service.

  • 1
    If you use a continuous integration system like Jenkins, when you create a branch on git from master the jenkinsfile it is also copied, avoid sonarque analysis imply modify this file. In some cases the developer forget this step. – F.Rosado Feb 07 '17 at 11:42
  • 2
    We have the same "problem" with Travis. That's why our script do some conditional checks (are we on master branch or not) before running the SonarQube analysis. – Julien H. - SonarSource Team Feb 08 '17 at 12:40
  • 1
    I want my branch to be on sonar but I would like them to be deleted when branch is merged / deleted. So you use a git hook to call sonar api / delete on branch merge / delete ? – TheBakker Oct 17 '18 at 09:57
3

If you happen to be using Bitbucket Server and the Sonar for Bitbucket Server add-on, there's an automatic way to perform this cleanup. To enable this setting from Bitbucket Server, follow the 3 steps shown in the screenshots below.

Screenshot of 3 steps

ASnyder
  • 83
  • 5
  • Perhaps is the best solution if you can buy the plugin, it is not always possible. – F.Rosado Mar 22 '17 at 08:09
  • if you cannot invest 10 bucks / 10 users you probably also should'nt be investing time into continuous integration / code QA. Extreme programming and a very simple SCM is probably all you need for such a project. Just sayian. SonarQube and the likes make sense in business-oriented teams only - for everything else a simple cronjob might do the trick. – specializt Jun 20 '17 at 09:50
  • 1
    My case is different, the number of users for bitbucket is large (+2000) but the number of sonarqube users is small (less than 100). Perhaps a custom scheduled task that check projects and repositories is a solution. – F.Rosado Aug 08 '17 at 16:25
2

You can define a method as below, to do the job and then call the method whenever you need according to your need or on some event like git branch merged/removed, etc.

def deleteSonarProject() {
    def sonarToken = "-your admin credential token generate it from sonarServer-"
    def url = "http://yourBaseURL:portNumber/api/projects/delete"
    sh "curl -u ${sonarToken}: ${url} -d 'project=projectKey'"
}
Gentle
  • 519
  • 6
  • 12