Jenkins : Change Version-Number in SVN-path

This script changes the Version-Number in the SVN-path of the SVN-configuration for a specific SVN-repository and job.

This is useful, when a project often changes the SVN-path because of a new branch for every release.

From Scriptler 

import hudson.scm.*
//import hudson.model.*

/** Old Version for the SVN-Path */
oldVersion  = "1.28"

/** New Version for the SVN-Path */
newVersion  = "1.29"

/** Path to the SVN-Repo containing with the following */
svnpath = "repo/testjob"

 /** Jobs containing with */
jobs = "testjob"

/** Should we override existing values? */
overrideExistingValues = false

/** Display Konfiguration */
println "### Overwrite SVN-Version ###"
println "oldVersion:     " + oldVersion
println "newVersion:     " + newVersion
println "Jobs with:      " + jobs
println "Repo_with_Path: " + svnpath


// ----- Do the work. -----

// Access to the Hudson Singleton
hudsonInstance = hudson.model.Hudson.instance

// Retrieve all Jobs which starts with -jobs-
allItems = hudsonInstance.items
chosenJobs = allItems.findAll{job -> job.name.contains(jobs)}

// Table header
col1 = "Job".center(35)
col2 = "OldPath".center(120)
col3 = "NewPath".center(120)
header = "$col1 | $col2 | $col3"
line = header.replaceAll("[Change Version-Number in SVN-path^|]", "-").replaceAll("\\|", "+")

println line
println header
println line

// Do work and create the result table
chosenJobs.each { job ->
 if(!(job instanceof hudson.model.ExternalJob)) {
   // No SCM-Configuration possible for External Jobs!
   if (job.scm instanceof SubversionSCM) {
     // Job has a SubversionSCM-Konfiguration
     def oldSvnPath = [][]
     def newSvnPath = [][]
     job.scm.locations.each{
       //For every Subversion-Location
       if (it.remote.contains(svnpath)) {
         //SVN-Path contains the given Path
         oldRemote = it.remote.padRight(119)
         jobname  = job.name.padRight(35)
         newRemote = it.remote
         if (it.remote.contains(oldVersion)) {
           //SVN-Path contains the old Version, which should be replaced
           newRemote = it.remote.replaceAll(/$oldVersion/,"$newVersion")  //Replace
           //Build new SVN-Location (it is not possible to change a value in the existing configuration)
           newSvnPath.add(new hudson.scm.SubversionSCM.ModuleLocation(newRemote,it.local))

           //For Visualisation of the changed Value
           newRemote = newRemote.padRight(120,"<")
           // Output Result for this Subversion-Location
           println "$jobname | $oldRemote ]> $newRemote"
         } else {
           //Visualisation of value which is not changed
           newRemote = newRemote.padRight(120)
           // Output Result for this Subversion-Location
           println "$jobname | $oldRemote  | $newRemote"
         }
       }

     }
     // Every Location was checked. Building new SVN-Configuration with the new SVN-Locations
     newscm = new hudson.scm.SubversionSCM(newSvnPath, job.scm.workspaceUpdater, job.scm.browser,
     job.scm.excludedRegions, job.scm.excludedUsers, job.scm.excludedRevprop, job.scm.excludedCommitMessages, job.scm.includedRegions)
     if (overrideExistingValues){
       // Only write values, when overrideExistingValues is true
       job.scm = newscm;
     }
     println line
     //Job is done
   }
 }
}
println line
//done