public Queue.Item run(String stageName) { if (stageName == null || stageName.equals("")) { throw new IllegalStateException(Messages.RestartDeclarativePipelineAction_NullStageName()); } if (!run.hasPermission(Item.BUILD) || !run.getParent().isBuildable()) { throw new IllegalStateException(Messages.RestartDeclarativePipelineAction_ProjectNotBuildable(run.getParent().getFullName())); } if (run.isBuilding()) { throw new IllegalStateException(Messages.RestartDeclarativePipelineAction_OriginRunIncomplete(run.getFullDisplayName())); } ExecutionModelAction execAction = run.getAction(ExecutionModelAction.class); if (execAction == null) { throw new IllegalStateException(Messages.RestartDeclarativePipelineAction_OriginWasNotDeclarative(run.getFullDisplayName())); } if (!getRestartableStages().contains(stageName)) { throw new IllegalStateException(Messages.RestartDeclarativePipelineAction_StageNameNotPresent(stageName, run.getFullDisplayName())); } List<Action> actions = new ArrayList<>(); CpsFlowExecution execution = getExecution(); if (execution == null) { throw new IllegalStateException(Messages.RestartDeclarativePipelineAction_OriginRunMissingExecution(run.getFullDisplayName())); } actions.add(new RestartFlowFactoryAction(run.getExternalizableId())); actions.add(new CauseAction(new Cause.UserIdCause(), new RestartDeclarativePipelineCause(run, stageName))); return ParameterizedJobMixIn.scheduleBuild2(run.getParent(), 0, actions.toArray(new Action[actions.size()])); }
@Override public String getDisplayName() { return Messages.Restart_from_Stage(); }
public FormValidation doCheckStageName(@QueryParameter String value) { String s = Util.fixEmptyAndTrim(value); if (s == null || s.equals("")) { return FormValidation.error(Messages.RestartDeclarativePipelineAction_NullStageName()); } if (!getRestartableStages().contains(s)) { return FormValidation.error(Messages.RestartDeclarativePipelineAction_StageNameNotPresent(s, run.getFullDisplayName())); } return FormValidation.ok(); }
@Issue("JENKINS-45455") @Test public void emptyStageName() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); Exception runFailure = null; try { Queue.Item queueItem = action.run(""); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_NullStageName(), runFailure.getMessage()); }
@Issue("JENKINS-45455") @Test public void notPresentStageName() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); Exception runFailure = null; try { Queue.Item queueItem = action.run("not-present"); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_StageNameNotPresent("not-present", original.getFullDisplayName()), runFailure.getMessage()); }
@Issue("JENKINS-45455") @Test public void projectNotBuildable() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); WorkflowJob p = original.getParent(); p.setDisabled(true); Exception runFailure = null; try { Queue.Item queueItem = action.run("restart"); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_ProjectNotBuildable(p.getFullName()), runFailure.getMessage()); }
assertEquals(Messages.RestartDeclarativePipelineAction_OriginRunIncomplete(b.getFullDisplayName()), runFailure.getMessage());
@Issue("JENKINS-45455") @Test public void nullStageName() throws Exception { WorkflowRun original = expect(Result.FAILURE, "restart", "simpleRestart") .logContains("Odd numbered build, failing", "This shouldn't show up on second run") .go(); RestartDeclarativePipelineAction action = original.getAction(RestartDeclarativePipelineAction.class); assertNotNull(action); assertTrue(action.isRestartEnabled()); Exception runFailure = null; try { Queue.Item queueItem = action.run(null); // Because we should definitely have thrown an exception in run. assertNull(queueItem); } catch (Exception e) { runFailure = e; } assertNotNull(runFailure); assertTrue(runFailure instanceof IllegalStateException); assertEquals(Messages.RestartDeclarativePipelineAction_NullStageName(), runFailure.getMessage()); }