Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

When editing global configuration of a SonarQube instance in Jenkins (adding, removing or renaming) you may end up with jobs that are no longer associated with an existing SonarQube 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 SonarQube instance (just remember to update the newSonarInstance value to the name of the SonarQube 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");
      }
    }
  }
}