Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

When playing with global configuration of Sonar instance (adding, removing or renaming) you may end up with jobs that are no more associated with an existing Sonar instance.

Here is a script to get the list of these jobs:

Code Block
import hudson.model.*
import hudson.maven.*
import hudson.tasks.*
import hudson.plugins.sonar.*

def listOfSonarInstance=SonarInstallation.all()*.name;
println("Available Sonar instances: "+ listOfSonarInstance);
println("=======\n");

// For each project
for(item in Hudson.instance.items) {
  if(item instanceof FreeStyleProject) {
    for (builder in item.buildersList) {
      if (builder instanceof SonarRunnerBuilder && !listOfSonarInstance.contains(builder.installationName)) {
        println("JOB : "+item.name);
        println("Unknow Sonar instance referenced in build step: "+builder.installationName);
        println("=======\n");
      }
    }
  }
  if(item instanceof FreeStyleProject || item instanceof AbstractMavenProject) {
    for (publisher in item.publishersList){
      if (publisher instanceof SonarPublisher && !listOfSonarInstance.contains(publisher.installationName)) {
        println("JOB : "+item.name);
        println("Unknow Sonar instance referenced in post build action: "+publisher.installationName);
        println("=======\n");
      }
    }
  }
}

Here is a script to reassign broken jobs to a different Sonar instance (just remind to update newSonarInstance value to the name of the Sonar instance you want to use for broken jobs):

Code Block
import hudson.model.*
import hudson.maven.*
import hudson.tasks.*
import hudson.plugins.sonar.*

def newSonarInstance="Sonar Prod";

def listOfSonarInstance=SonarInstallation.all()*.name;

if (!listOfSonarInstance.contains(newSonarInstance)) {
  println('Please define newSonarInstance value to an existing Sonar installation. Possible values are: ' + listOfSonarInstance);
  return;
}

// For each project
for(item in Hudson.instance.items) {
  if(item instanceof FreeStyleProject) {
    for (builder in item.buildersList){
      if (builder instanceof SonarRunnerBuilder && !listOfSonarInstance.contains(builder.installationName)) {
        def newBuilder = new SonarRunnerBuilder(newSonarInstance,builder.sonarRunnerName,builder.project,builder.@properties,builder.javaOpts);
        item.buildersList.replace(newBuilder);
        println("JOB: " + item.name + " fixed");
      }
    }
  }
  if(item instanceof FreeStyleProject || item instanceof AbstractMavenProject) {
    for (publisher in item.publishersList){
      if (publisher instanceof SonarPublisher && !listOfSonarInstance.contains(publisher.installationName)) {
        def newPublisher = new SonarPublisher(newSonarInstance,publisher.branch,publisher.language,publisher.triggers,publisher.jobAdditionalProperties,publisher.mavenOpts,publisher.mavenInstallationName,publisher.rootPom);
        item.publishersList.replace(newPublisher);
        println("JOB: " + item.name + " fixed");
      }
    }
  }
}