diff --git a/src/main/java/com/sonyericsson/rebuild/RebuildAction.java b/src/main/java/com/sonyericsson/rebuild/RebuildAction.java index 240e9b3..09d856a 100644 --- a/src/main/java/com/sonyericsson/rebuild/RebuildAction.java +++ b/src/main/java/com/sonyericsson/rebuild/RebuildAction.java @@ -229,9 +229,7 @@ public void parameterizedRebuild(Run currentBuild, StaplerResponse response) thr project.checkPermission(Item.BUILD); if (isRebuildAvailable()) { - List actions = copyBuildCausesAndAddUserCause(currentBuild); - ParametersAction action = currentBuild.getAction(ParametersAction.class); - actions.add(action); + List actions = constructRebuildActions(build, currentBuild.getAction(ParametersAction.class)); Hudson.getInstance().getQueue().schedule((Queue.Task) build.getParent(), 0, actions); response.sendRedirect("../../"); @@ -251,7 +249,7 @@ public void nonParameterizedRebuild(Run currentBuild, StaplerResponse response) throws ServletException, IOException, InterruptedException { getProject().checkPermission(Item.BUILD); - List actions = constructRebuildCause(build, null); + List actions = constructRebuildActions(build, null); Hudson.getInstance().getQueue().schedule((Queue.Task) currentBuild.getParent(), 0, actions); response.sendRedirect("../../"); } @@ -310,7 +308,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl } } - List actions = constructRebuildCause(build, new ParametersAction(values)); + List actions = constructRebuildActions(build, new ParametersAction(values)); Hudson.getInstance().getQueue().schedule((Queue.Task) build.getParent(), 0, actions); rsp.sendRedirect("../../"); @@ -320,29 +318,27 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl /** * Extracts the build causes and adds or replaces the {@link hudson.model.Cause.UserIdCause}. The result is a * list of all build causes from the original build (might be an empty list), plus a - * {@link hudson.model.Cause.UserIdCause} for the user who started the rebuild. + * {@link hudson.model.Cause.UserIdCause} for the user who started the rebuild, plus a + * {@link RebuildCause} for this rebuild. * * @param fromBuild the build to copy the causes from. * @return list with all original causes and a {@link hudson.model.Cause.UserIdCause}. */ - private List copyBuildCausesAndAddUserCause(Run fromBuild) { - List currentBuildCauses = fromBuild.getCauses(); + private List constructRebuildCauses(Run fromBuild) { + List currentBuildCauses = new ArrayList(fromBuild.getCauses()); - List actions = new ArrayList(currentBuildCauses.size()); boolean hasUserCause = false; for (Object buildCause : currentBuildCauses) { if (buildCause instanceof Cause.UserIdCause) { hasUserCause = true; - actions.add(new CauseAction(new Cause.UserIdCause())); - } else { - actions.add(new CauseAction((Cause)buildCause)); } } if (!hasUserCause) { - actions.add(new CauseAction(new Cause.UserIdCause())); + currentBuildCauses.add(new Cause.UserIdCause()); } + currentBuildCauses.add(new RebuildCause(fromBuild)); - return actions; + return currentBuildCauses; } /** @@ -374,14 +370,14 @@ public boolean isRebuildAvailable() { && project.hasPermission(Item.BUILD) && project.isBuildable() && project instanceof Queue.Task - && !isMatrixRun() + && !isMatrixRun() && !isRebuildDisabled(); } private boolean isRebuildDisabled() { RebuildSettings settings = (RebuildSettings)getProject().getProperty(RebuildSettings.class); - + if (settings != null && settings.getRebuildDisabled()) { return true; } @@ -460,15 +456,16 @@ private ParameterValue cloneParameter(ParameterValue oldValue, String newValue) throw new IllegalArgumentException("Unrecognized parameter type: " + oldValue.getClass()); } /** - * Method for constructing Rebuild cause. + * Method for constructing Rebuild actions. * * @param up AbsstractBuild * @param paramAction ParametersAction. * @return actions List */ - private List constructRebuildCause(Run up, ParametersAction paramAction) { - List actions = copyBuildCausesAndAddUserCause(up); - actions.add(new CauseAction(new RebuildCause(up))); + private List constructRebuildActions(Run up, ParametersAction paramAction) { + List causes = constructRebuildCauses(up); + List actions = new ArrayList(); + actions.add(new CauseAction(causes)); if (paramAction != null) { actions.add(paramAction); } diff --git a/src/test/java/com/sonyericsson/rebuild/RebuildValidatorTest.java b/src/test/java/com/sonyericsson/rebuild/RebuildValidatorTest.java index 58d51db..730691a 100644 --- a/src/test/java/com/sonyericsson/rebuild/RebuildValidatorTest.java +++ b/src/test/java/com/sonyericsson/rebuild/RebuildValidatorTest.java @@ -47,7 +47,6 @@ import org.kohsuke.stapler.DataBoundConstructor; import java.io.IOException; -import java.util.List; import java.util.concurrent.ExecutionException; /** @@ -253,25 +252,23 @@ public void testWhenProjectWithCauseThenCauseIsCopiedAndUserCauseAdded() while (project.isBuilding()) { Thread.sleep(DELAY); } - List actions = project.getLastCompletedBuild().getActions(); boolean hasRebuildCause = false; boolean hasRemoteCause = false; boolean hasUserIdCause = false; - for (Action action : actions) { + for (Action action : project.getLastCompletedBuild().getAllActions()) { if (action instanceof CauseAction) { - CauseAction causeAction = (CauseAction) action; - if (causeAction.getCauses().get(0).getClass() - .equals(RebuildCause.class)) { - hasRebuildCause = true; - } - if (causeAction.getCauses().get(0).getClass() - .equals(Cause.RemoteCause.class)) { - hasRemoteCause = true; - } - if (causeAction.getCauses().get(0).getClass() - .equals(Cause.UserIdCause.class)) { - hasUserIdCause = true; + for(Cause cause : ((CauseAction)action).getCauses()) { + if (cause.getClass().equals(RebuildCause.class)) { + hasRebuildCause = true; + } + if (cause.getClass().equals(Cause.RemoteCause.class)) { + hasRemoteCause = true; + } + if (cause.getClass().equals(Cause.UserIdCause.class)) { + hasUserIdCause = true; + } } + } } assertTrue("Build should have user, remote and rebuild causes",