| 1 | import hudson.matrix.* |
| 2 | import hudson.model.* |
| 3 | import jenkins.model.* |
| 4 | |
| 5 | import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause; |
| 6 | import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change; |
| 7 | |
| 8 | // Iterate over all jobs and find the ones that have a hudson.plugins.git.util.BuildData |
| 9 | // as an action. |
| 10 | // |
| 11 | // We then clean it by removing the useless array action.buildsByBranchName |
| 12 | // |
| 13 | |
| 14 | def jobPattern = "dev_gerrit_.*" |
| 15 | |
| 16 | def matchedJobs = Jenkins.instance.items.findAll { job -> |
| 17 | job.name =~ /$jobPattern/ |
| 18 | } |
| 19 | |
| 20 | for (job in matchedJobs) { |
| 21 | println("job: " + job.name); |
| 22 | |
| 23 | def changes = [] |
| 24 | |
| 25 | for (build in job.getBuilds()) { |
| 26 | println(" build: " + build.number); |
| 27 | |
| 28 | // Skip currently building builds |
| 29 | if (build.isBuilding()) { |
| 30 | println(" Is building, skip it."); |
| 31 | continue |
| 32 | } |
| 33 | |
| 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) { |
| 38 | |
| 39 | Change change = build.getCause(GerritCause.class).getEvent().getChange() |
| 40 | |
| 41 | if (changes.contains(change)) { |
| 42 | println(" Is not the latest for change " + change.getId() + ", delete it."); |
| 43 | build.delete() |
| 44 | continue |
| 45 | } else { |
| 46 | changes.add(change) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Delete successful and aborted builds |
| 51 | if (build.result.toString() == 'SUCCESS' || build.result.toString() == 'ABORTED') { |
| 52 | println(" Is SUCCESSFUL / ABORTED, delete it."); |
| 53 | build.delete() |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | |
| 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(); |
| 65 | if (r != null) { |
| 66 | for (branch in r.getBranches()) { |
| 67 | action.buildsByBranchName.put(branch.getName(), action.lastBuild) |
| 68 | } |
| 69 | } |
| 70 | build.actions.remove(action); |
| 71 | build.actions.add(action); |
| 72 | build.save(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (job instanceof MatrixProject) { |
| 77 | for (run in build.getRuns()) { |
| 78 | println(" run: " + run); |
| 79 | |
| 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(); |
| 85 | if (r != null) { |
| 86 | for (branch in r.getBranches()) { |
| 87 | action.buildsByBranchName.put(branch.getName(), action.lastBuild) |
| 88 | } |
| 89 | } |
| 90 | run.actions.remove(action); |
| 91 | run.actions.add(action); |
| 92 | run.save(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |