Source:
<code>private Stage lockAndRerunStage(String pipelineName, Integer counter, String stageName, StageInstanceCreator creator, final ErrorConditionHandler errorHandler) { synchronized (mutexForPipeline(pipelineName)) { OperationResult result = new ServerHealthStateOperationResult(); if (!schedulingChecker.canSchedule(result)) { errorHandler.cantSchedule(result.getServerHealthState().getDescription(), pipelineName, stageName); } String username = CaseInsensitiveString.str(SessionUtils.currentUsername().getUsername()); if (!securityService.hasOperatePermissionForStage(pipelineName, stageName, username)) { errorHandler.noOperatePermission(pipelineName, stageName); } Pipeline pipeline = pipelineService.fullPipelineByCounter(pipelineName, counter); if (pipeline == null) { errorHandler.nullPipeline(pipelineName, counter, stageName); } if (!pipeline.hasStageBeenRun(stageName)) { if (goConfigService.hasPreviousStage(pipelineName, stageName)) { CaseInsensitiveString previousStageName = goConfigService.previousStage(pipelineName, stageName).name(); if (!pipeline.hasStageBeenRun(CaseInsensitiveString.str(previousStageName))) { errorHandler.previousStageNotRun(pipeline.getName(), stageName); } <START> StageConfig currentStageConfig = goConfigService.nextStage(pipelineName, previousStageName.toString()); <END> Approval approval = currentStageConfig.getApproval(); if (approval.getType().equals(Approval.MANUAL) && approval.isAllowOnlyOnSuccess()) { Stage previousStage = pipeline.getStages().byName(previousStageName.toString()); StageResult previousStageResult = previousStage.getResult(); if (previousStageResult != StageResult.Passed) { String errorMessage = String.format("Cannot schedule %s as the previous stage %s has %s!", stageName, previousStageName.toString(),previousStageResult.name()); errorHandler.cantSchedule(errorMessage, pipelineName, stageName); } } } } Stage stage = internalRerun(pipeline, stageName, username, creator, errorHandler); if (stage == null) { errorHandler.nullStage(stageName); } return stage; } }</code><technical_language>Extract code a new method</technical_language>

Target:
private Stage lockAndRerunStage(String pipelineName, Integer counter, String stageName, StageInstanceCreator creator, final ErrorConditionHandler errorHandler) { synchronized (mutexForPipeline(pipelineName)) { OperationResult result = new ServerHealthStateOperationResult(); if (!schedulingChecker.canSchedule(result)) { errorHandler.cantSchedule(result.getServerHealthState().getDescription(), pipelineName, stageName); } String username = CaseInsensitiveString.str(SessionUtils.currentUsername().getUsername()); if (!securityService.hasOperatePermissionForStage(pipelineName, stageName, username)) { errorHandler.noOperatePermission(pipelineName, stageName); } Pipeline pipeline = pipelineService.fullPipelineByCounter(pipelineName, counter); ScheduleStageResult canScheduleStage = schedulingChecker.shouldAllowSchedulingStage(pipeline, stageName); switch (canScheduleStage) { case PipelineNotFound: errorHandler.nullPipeline(pipelineName, counter, stageName); break; case PreviousStageNotRan: errorHandler.previousStageNotRun(pipelineName, stageName); break; case PreviousStageNotPassed: String errorMessage = String.format("Cannot schedule %s as the previous stage %s has %s!", stageName, canScheduleStage.getPreviousStageName(), canScheduleStage.getPreviousStageResult()); errorHandler.cantSchedule(errorMessage, pipelineName, stageName); break; } Stage stage = internalRerun(pipeline, stageName, username, creator, errorHandler); if (stage == null) { errorHandler.nullStage(stageName); } return stage; } }