| #!/usr/bin/env groovy |
| import org.eclipse.hudson.model.* |
| |
| |
| |
| void printAllProperties() { |
| println("\n= = = All Properties = = =\n") |
| def props = System.getProperties() |
| for (prop in props) { |
| println(prop) |
| } |
| println("\n= = = End of All Properties = = =\n") |
| } |
| |
| void dumpObjectProperties (anyObject) { |
| def filtered = ['class', 'active'] |
| println("\n= = = Dump Object Properties = = =\n") |
| println anyObject.properties |
| .sort{it.key} |
| .collect{it} |
| .findAll{!filtered.contains(it.key)} |
| .join('\n') |
| println("\n= = = End of Dump Object Properties = = =\n") |
| } |
| |
| void cancelIfAlsoQueued (String jobName) { |
| |
| //println("\n= = = = Checking if other Queued Jobs = = = = = = = =\n"); |
| |
| // Direct check of null does not work as expect. |
| // Can not use variable in "wrapped script" at all, unless defined, |
| // else MissingProperty exception |
| // is thrown: |
| // groovy.lang.MissingPropertyException: No such property: jobName for class: Script1 |
| if (! jobName){ |
| println("PROGRAMMING ERROR: This script must be provided a project name)"); |
| } |
| else { |
| //println("Job name to clear from queue, if any: " + jobName); |
| def q = hudson.model.Hudson.getInstance().getQueue(); |
| def qitems = q.getItems(); |
| def nJobsOfSameName = 0; |
| if (qitems.length == 0) { |
| println("\n\tNo other jobs in queue\n") |
| } else { |
| for (i=0;i<qitems.length;i++) { |
| job=qitems[i].task; |
| //dumpObjectProperties(job) |
| if (job.getName().equals(jobName)) { |
| nJobsOfSameName++; |
| // If we find at even one of same name, we do not need to keep checking. |
| break; |
| } |
| } |
| // in our case, we do not want to cancel whole queue |
| // if (qitems.length > 0) { |
| // qitems[i].doCancelQueue() |
| // Instead we will cancel, not fail, current job |
| if (nJobsOfSameName > 0) { |
| println("\n\tThis job was automatically cancelled, since another was in the queue, that will include the contribution.\n"); |
| throw new InterruptedException(); |
| } else { |
| println("\n\tNo other jobs, of same name (" + jobName + ") were in the queue"); |
| } |
| } |
| } |
| } |
| String thisProjectName = ""; |
| // println("**** Dump this") |
| // dumpObjectProperties(this); |
| // println("**** Dump this.getBinding()") |
| // dumpObjectProperties(this.getBinding()); |
| // println("**** Dump binding.getVariable('build')"); |
| // dumpObjectProperties(this.getBinding().getVariable("build")) |
| // thisBuildName = this.getBinding().getVariable("build").getFullName(); |
| thisProjectName = this.getBinding().getVariable("build").getParent().getFullName(); |
| // println("thisBuildName: " + thisBuildName); |
| // println("thisProjectName: " + thisProjectName); |
| //printAllProperties() |
| cancelIfAlsoQueued(thisProjectName) |
| |