public static void toJSON(OutputWriter jsonOutputWriter, StageInstanceModel model, String pipelineName, String pipelineCounter) { jsonOutputWriter .addLinks(linkWriter -> { linkWriter.addLink("self", Routes.Stage.self(pipelineName, pipelineCounter, model.getName(), model.getCounter())); }) .add("name", model.getName()) .add("counter", model.getCounter()) .add("status", model.getState().name()) .add("approved_by", model.getApprovedBy()) .add("scheduled_at", model.getScheduledDate()); if (model.getPreviousStage() != null) { jsonOutputWriter.addChild("previous_stage", childWriter -> { StageRepresenter.toJSON(childWriter, model.getPreviousStage(), pipelineName, pipelineCounter); }); } } }
public static StageInstanceModel custom(String stageName, boolean isAutoApproved) { StageInstanceModel stageHistoryItem = new StageInstanceModel(); stageHistoryItem.setName(stageName); if (isAutoApproved) { stageHistoryItem.setApprovalType(GoConstants.APPROVAL_SUCCESS); } else { stageHistoryItem.setApprovalType(GoConstants.APPROVAL_MANUAL); } assertThat("Should create the correct stub", stageHistoryItem.isAutoApproved(), is(isAutoApproved)); return stageHistoryItem; } }
private List stageHistoryAsJson(PipelineInstanceModel pipelineInstanceModel, StageInstanceModels stageHistory) { List json = new ArrayList(); for (StageInstanceModel stageHistoryItem : stageHistory) { Map<String, Object> jsonMap = new LinkedHashMap<>(); jsonMap.put("stageName", stageHistoryItem.getName()); jsonMap.put("stageId", stageHistoryItem.getId()); jsonMap.put("stageStatus", stageHistoryItem.getState().toString()); StageIdentifier stageIdentifier = new StageIdentifier(pipelineInstanceModel.getPipelineIdentifier(), stageHistoryItem.getName(), stageHistoryItem.getCounter()); jsonMap.put("stageLocator", encodeInUtf8(stageIdentifier.stageLocator())); jsonMap.put("getCanRun", Boolean.toString(stageHistoryItem.getCanRun())); jsonMap.put("getCanCancel", Boolean.toString(stageHistoryItem.getCanCancel())); jsonMap.put("scheduled", Boolean.toString(stageHistoryItem.isScheduled())); jsonMap.put("stageCounter", stageHistoryItem.getCounter()); handleApproval(stageHistoryItem, jsonMap); json.add(jsonMap); } return json; }
public boolean getCanApprove() { return (getCanRun() && !isScheduled()); }
public Date getScheduledDate() { Date earliestScheduledDate = null; for (StageInstanceModel stage : this) { if (!stage.isScheduled()) { continue; } Date stageDate = stage.getScheduledDate(); if (earliestScheduledDate == null || stageDate.before(earliestScheduledDate)) { earliestScheduledDate = stageDate; } } return earliestScheduledDate; }
public void selectStage(String selectedStageName) { StageInstanceModel stage = stageHistory.byName(selectedStageName); if (stage != null && stage.isScheduled()) { stage.setSelected(true); } }
private String getPipelineInstanceModelAsKey(PipelineModel model) { final StringBuilder builder = new StringBuilder(); for (PipelineInstanceModel pim : model.getActivePipelineInstances()) { builder.append("["); builder.append(pim.getId()).append(DELIMITER); for (StageInstanceModel stageInstanceModel : pim.getStageHistory()) { builder.append(stageInstanceModel.getName()).append(DELIMITER); builder.append(stageInstanceModel.getId()).append(DELIMITER); builder.append(stageInstanceModel.getState()).append(DELIMITER); } builder.append("]"); } return builder.toString(); }
@Test public void shouldReturnNullIfJobHistoryIsBlank() throws Exception { StageInstanceModel stageHistoryItem = new StageInstanceModel(); stageHistoryItem.setBuildHistory(new JobHistory()); assertThat(stageHistoryItem.getScheduledDate(), is(nullValue())); }
for (JobHistoryItem job : runningStage.getBuildHistory()) { String stageName = runningStage.getName(); String stageCounter = runningStage.getCounter(); if (job.isRunning()) { JobIdentifier jobIdentifier = new JobIdentifier(pipelineName, pipelineCounter, pipelineLabel, stageName, stageCounter, job.getName());
public static StageInstanceModel toStageInstanceModel(Stage stage) { StageInstanceModel stageInstanceModel = new StageInstanceModel(stage.getName(), String.valueOf(stage.getCounter()), stage.getResult(), stage.getIdentifier()); stageInstanceModel.setApprovalType(stage.getApprovalType()); stageInstanceModel.setApprovedBy(stage.getApprovedBy()); stageInstanceModel.setRerunOfCounter(stage.getRerunOfCounter()); JobHistory jobHistory = new JobHistory(); for (JobInstance jobInstance : stage.getJobInstances()) { jobHistory.addJob(jobInstance.getName(), jobInstance.getState(), jobInstance.getResult(), jobInstance.getScheduledDate()); } stageInstanceModel.setBuildHistory(jobHistory); return stageInstanceModel; }
@Test public void shouldUnderstandPreviousStageState() { StageInstanceModel item = new StageInstanceModel("foo", "10", JobHistory.withJob("unit", JobState.Assigned, JobResult.Unknown, new Date())); StageInstanceModel previous = new StageInstanceModel("foo", "1", JobHistory.withJob("unit", JobState.Completed, JobResult.Passed, new Date())); assertThat(item.hasPreviousStage(), is(false)); item.setPreviousStage(previous); assertThat(item.hasPreviousStage(), is(true)); assertThat(item.getPreviousStage(), is(previous)); }
public StageInstanceModel activeStage() { for (StageInstanceModel stageInstanceModel : stageHistory) { if (stageInstanceModel.getState().isActive()) return stageInstanceModel; } return null; }
@Test public void shouldBeManualApproved() throws Exception { StageInstanceModel stageHistoryItem = new StageInstanceModel(); stageHistoryItem.setApprovalType(GoConstants.APPROVAL_MANUAL); assertThat(stageHistoryItem.isAutoApproved(), is(false)); }
/** * * @deprecated use add method. This is a test helper */ public void addStage(String name, JobHistory history) { add(new StageInstanceModel(name, "1", history)); }
public StageInstanceModel byName(String name) { for (StageInstanceModel stage : this) { if (stage.getName().equals(name)) { return stage; } } return null; }
public static StageInstanceModels stageHistory(PipelineConfig pipelineConfig, Date modificationDate) { StageInstanceModels history = new StageInstanceModels(); for (StageConfig stageConfig : pipelineConfig) { StageInstanceModel item = new StageInstanceModel(CaseInsensitiveString.str(stageConfig.name()), "1", buildHistory(stageConfig, modificationDate)); item.setCounter("1"); item.setApprovalType(new InstanceFactory().createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), md5, new TimeProvider()).getApprovalType()); if (stageConfig.requiresApproval()) { item.setApprovedBy(APPROVED_BY); } else { item.setApprovedBy(GoConstants.DEFAULT_APPROVED_BY); } history.add(item); } return history; }
public static StageInstanceModels stagePerJob(String baseName, List<JobHistory> histories) { StageInstanceModels stageInstanceModels = new StageInstanceModels(); for (int i = 0; i < histories.size(); i++) { String stageName = baseName + "-" + i; StageInstanceModel stage = new StageInstanceModel(stageName, "1", StageResult.Passed, new StageIdentifier("pipeline", 1, "1", stageName, "1")); stage.setBuildHistory(histories.get(i)); stageInstanceModels.add(stage); stageInstanceModels.latestStage().setApprovedBy("cruise"); } return stageInstanceModels; }
private void populatePreviousStageState(PipelineInstanceModel activePipeline) { if (activePipeline.isAnyStageActive()) { StageInstanceModel activeStage = activePipeline.activeStage(); StageInstanceModel latestActive = null; long id = activePipeline.getId(); do { PipelineTimelineEntry timelineEntry = pipelineTimeline.runBefore(id, new CaseInsensitiveString(activePipeline.getName())); if (timelineEntry == null) { break; } id = timelineEntry.getId(); PipelineInstanceModel instanceModel = pipelineDao.loadHistory(id); if (instanceModel != null && instanceModel.hasStageBeenRun(activeStage.getName())) { latestActive = instanceModel.getStageHistory().byName(activeStage.getName()); } } while (latestActive == null); activeStage.setPreviousStage(latestActive); } }
private void populateStageOperatePermission(PipelineInstanceModel pipelineInstanceModel, Username username) { for (StageInstanceModel stage : pipelineInstanceModel.getStageHistory()) { stage.setOperatePermission(securityService.hasOperatePermissionForStage(pipelineInstanceModel.getName(), stage.getName(), CaseInsensitiveString.str(username.getUsername()))); } }