5 import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause;
6 import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
8 // Iterate over all jobs and find the ones that have a hudson.plugins.git.util.BuildData
11 // We then clean it by removing the useless array action.buildsByBranchName
14 def jobPattern = "dev_gerrit_.*"
16 def matchedJobs = Jenkins.instance.items.findAll { job ->
17 job.name =~ /$jobPattern/
20 for (job in matchedJobs) {
21 println("job: " + job.name);
25 for (build in job.getBuilds()) {
26 println(" build: " + build.number);
28 // Skip currently building builds
29 if (build.isBuilding()) {
30 println(" Is building, skip it.");
34 // Keep only the last build of a Gerrit Change
35 if (build.getCause(GerritCause.class) != null &&
36 build.getCause(GerritCause.class).getEvent() != null &&
37 build.getCause(GerritCause.class).getEvent().getChange() != null) {
39 Change change = build.getCause(GerritCause.class).getEvent().getChange()
41 if (changes.contains(change)) {
42 println(" Is not the latest for change " + change.getId() + ", delete it.");
50 // Delete successful and aborted builds
51 if (build.result.toString() == 'SUCCESS' || build.result.toString() == 'ABORTED') {
52 println(" Is SUCCESSFUL / ABORTED, delete it.");
58 // It is possible for a build to have multiple BuildData actions
59 // since we can use the Mulitple SCM plugin.
60 def gitActions = build.getActions(hudson.plugins.git.util.BuildData.class)
61 if (gitActions != null) {
62 for (action in gitActions) {
63 action.buildsByBranchName = new HashMap<String, Build>();
64 hudson.plugins.git.Revision r = action.getLastBuiltRevision();
66 for (branch in r.getBranches()) {
67 action.buildsByBranchName.put(branch.getName(), action.lastBuild)
70 build.actions.remove(action);
71 build.actions.add(action);
76 if (job instanceof MatrixProject) {
77 for (run in build.getRuns()) {
78 println(" run: " + run);
80 gitActions = run.getActions(hudson.plugins.git.util.BuildData.class)
81 if (gitActions != null) {
82 for (action in gitActions) {
83 action.buildsByBranchName = new HashMap<String, Build>();
84 hudson.plugins.git.Revision r = action.getLastBuiltRevision();
86 for (branch in r.getBranches()) {
87 action.buildsByBranchName.put(branch.getName(), action.lastBuild)
90 run.actions.remove(action);
91 run.actions.add(action);