Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9f186ff09e20e8a9dcc0d0ea9b91f91978c30ea8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env groovy

/* Remember for our Hudson jobs this needs to be ran as a 
   _system_ groovy build stem, not a simple groovy build step. 
*/

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");
      }
    }
  }
}

println("[INFO] Beginning Groovy script"); 
println("Groovy Version: " + GroovySystem.version);
printAllProperties();
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);

cancelIfAlsoQueued(thisProjectName);
println("[INFO] Ending Groovy script"); 

Back to the top