@Override public boolean hasBuildPlan(final CaseInsensitiveString pipelineName, final CaseInsensitiveString stageName, String buildName, boolean ignoreCase) { if (!hasStageConfigNamed(pipelineName, stageName, ignoreCase)) { return false; } StageConfig stageConfig = stageConfigByName(pipelineName, stageName); return stageConfig != null && stageConfig.jobConfigByInstanceName(buildName, ignoreCase) != null; }
@Override public JobConfig jobConfigByName(String pipelineName, String stageName, String jobInstanceName, boolean ignoreCase) { JobConfig jobConfig = stageConfigByName(new CaseInsensitiveString(pipelineName), new CaseInsensitiveString(stageName)).jobConfigByInstanceName(jobInstanceName, ignoreCase); bombIfNull(jobConfig, String.format("Job [%s] is not found in pipeline [%s] stage [%s].", jobInstanceName, pipelineName, stageName)); return jobConfig; }
@Test public void shouldLoadStageFromXmlPartial() throws Exception { String stageXmlPartial = "<stage name=\"mingle\">\n" + " <jobs>\n" + " <job name=\"functional\">\n" + " <artifacts>\n" + " <log src=\"artifact1.xml\" dest=\"cruise-output\" />\n" + " </artifacts>\n" + " </job>\n" + " </jobs>\n" + "</stage>\n"; StageConfig stage = xmlLoader.fromXmlPartial(stageXmlPartial, StageConfig.class); assertThat(stage.name(), is(new CaseInsensitiveString("mingle"))); assertThat(stage.allBuildPlans().size(), is(1)); assertThat(stage.jobConfigByInstanceName("functional", true), is(notNullValue())); }
@Test public void shouldLoadPipelineFromXmlPartial() throws Exception { String pipelineXmlPartial = "<pipeline name=\"pipeline\">\n" + " <materials>\n" + " <hg url=\"/hgrepo\"/>\n" + " </materials>\n" + " <stage name=\"mingle\">\n" + " <jobs>\n" + " <job name=\"functional\">\n" + " <artifacts>\n" + " <log src=\"artifact1.xml\" dest=\"cruise-output\" />\n" + " </artifacts>\n" + " </job>\n" + " </jobs>\n" + " </stage>\n" + "</pipeline>\n"; PipelineConfig pipeline = xmlLoader.fromXmlPartial(pipelineXmlPartial, PipelineConfig.class); assertThat(pipeline.name(), is(new CaseInsensitiveString("pipeline"))); assertThat(pipeline.size(), is(1)); assertThat(pipeline.findBy(new CaseInsensitiveString("mingle")).jobConfigByInstanceName("functional", true), is(notNullValue())); }
public void createRerunInstances(JobInstance oldJob, JobInstances jobInstances, SchedulingContext context, StageConfig stageConfig, final Clock clock, InstanceFactory instanceFactory) { String jobName = oldJob.getName(); JobConfig jobConfig = stageConfig.jobConfigByInstanceName(jobName, true); if (jobConfig == null) { throw new CannotRerunJobException(jobName, "Configuration for job doesn't exist."); } if (jobConfig.isRunMultipleInstanceType()) { String runType = "'run multiple instance'"; throw new CannotRerunJobException(jobName, "Run configuration for job has been changed to " + runType + "."); } RunOnAllAgents.CounterBasedJobNameGenerator nameGenerator = new RunOnAllAgents.CounterBasedJobNameGenerator(CaseInsensitiveString.str(jobConfig.name())); JobInstances instances = instanceFactory.createJobInstance(stageConfig.name(), jobConfig, context, clock, nameGenerator); for (JobInstance instance : instances) { instance.setRerun(true); } jobInstances.addAll(instances); } }
@Test public void shouldLoadPartialConfigWithPipeline() throws Exception { String partialConfigWithPipeline = "<cruise schemaVersion='" + CONFIG_SCHEMA_VERSION + "'>\n" + "<pipelines group=\"first\">\n" + "<pipeline name=\"pipeline\">\n" + " <materials>\n" + " <hg url=\"/hgrepo\"/>\n" + " </materials>\n" + " <stage name=\"mingle\">\n" + " <jobs>\n" + " <job name=\"functional\">\n" + " <artifacts>\n" + " <log src=\"artifact1.xml\" dest=\"cruise-output\" />\n" + " </artifacts>\n" + " </job>\n" + " </jobs>\n" + " </stage>\n" + "</pipeline>\n" + "</pipelines>\n" + "</cruise>\n"; PartialConfig partialConfig = xmlLoader.fromXmlPartial(partialConfigWithPipeline, PartialConfig.class); assertThat(partialConfig.getGroups().size(), is(1)); PipelineConfig pipeline = partialConfig.getGroups().get(0).getPipelines().get(0); assertThat(pipeline.name(), is(new CaseInsensitiveString("pipeline"))); assertThat(pipeline.size(), is(1)); assertThat(pipeline.findBy(new CaseInsensitiveString("mingle")).jobConfigByInstanceName("functional", true), is(notNullValue())); }
@Test public void shouldFindCorrectJobIfJobIsOnAllAgents() throws Exception { JobConfig allAgentsJob = new JobConfig("job-for-all-agents"); allAgentsJob.setRunOnAllAgents(true); JobConfigs jobs = new JobConfigs(); jobs.add(allAgentsJob); jobs.add(new JobConfig("job")); StageConfig stage = new StageConfig(new CaseInsensitiveString("stage-name"), jobs); JobConfig found = stage.jobConfigByInstanceName("job-for-all-agents-" + RunOnAllAgentsJobTypeConfig.MARKER + "-1", true); assertThat(found, is(allAgentsJob)); }
@Test public void shouldFindCorrectJobIfJobIsOnAllAgentsAndAmbiguousName() throws Exception { JobConfig allAgentsJob = new JobConfig("job-for-all-agents"); JobConfig ambiguousJob = new JobConfig("job-for-all"); allAgentsJob.setRunOnAllAgents(true); ambiguousJob.setRunOnAllAgents(true); JobConfigs jobs = new JobConfigs(); jobs.add(ambiguousJob); jobs.add(allAgentsJob); StageConfig stage = new StageConfig(new CaseInsensitiveString("stage-name"), jobs); JobConfig found = stage.jobConfigByInstanceName(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("job-for-all-agents", 1), true); assertThat(found, is(allAgentsJob)); }
PipelineConfig pipelineConfig = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")); StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString("stage")); JobConfig build = stageConfig.jobConfigByInstanceName("functional", true);
private boolean validateExistenceAndOrigin(PipelineConfig currentPipeline, ValidationContext validationContext) { PipelineConfig srcPipeline = validationContext.getPipelineConfigByName(pipelineName.getAncestorName()); if (srcPipeline == null) { //"ProdDeploy :: deploy :: scp" tries|attempts to fetch artifact from pipeline "not-found" which does not exist. addError(PIPELINE, String.format("\"%s :: %s :: %s\" tries to fetch artifact from pipeline \"%s\" which does not exist." , currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName())); return true; } else { StageConfig srcStage = srcPipeline.findBy(stage); if (srcStage == null) { addError(STAGE, String.format("\"%s :: %s :: %s\" tries to fetch artifact from stage \"%s :: %s\" which does not exist." , currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName(), stage)); return true; } else { if (srcStage.jobConfigByInstanceName(CaseInsensitiveString.str(job), true) == null) { addError(JOB, String.format("\"%s :: %s :: %s\" tries to fetch artifact from job \"%s :: %s :: %s\" which does not exist.", currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName(), stage, job)); return true; } } if (validationContext.shouldCheckConfigRepo()) { if (!validationContext.getConfigRepos().isReferenceAllowed(currentPipeline.getOrigin(), srcPipeline.getOrigin())) { addError(ARTIFACT_ORIGIN, String.format("\"%s :: %s :: %s\" tries to fetch artifact from job \"%s :: %s :: %s\" which is defined in %s - reference is not allowed", currentPipeline.name(), validationContext.getStage().name(), validationContext.getJob().name(), pipelineName.getAncestorName(), stage, job, displayNameFor(srcPipeline.getOrigin()))); return true; } } } return false; }
public JobConfigIdentifier translateToActualCase(JobConfigIdentifier identifier) { PipelineConfig pipelineConfig = getCurrentConfig().pipelineConfigByName(new CaseInsensitiveString(identifier.getPipelineName())); String translatedPipelineName = CaseInsensitiveString.str(pipelineConfig.name()); StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(identifier.getStageName())); if (stageConfig == null) { throw new StageNotFoundException(new CaseInsensitiveString(identifier.getPipelineName()), new CaseInsensitiveString(identifier.getStageName())); } String translatedStageName = CaseInsensitiveString.str(stageConfig.name()); JobConfig plan = stageConfig.jobConfigByInstanceName(identifier.getJobName(), true); if (plan == null) { throw new JobNotFoundException(identifier.getPipelineName(), identifier.getStageName(), identifier.getJobName()); } String translatedJobName = plan.translatedName(identifier.getJobName()); return new JobConfigIdentifier(translatedPipelineName, translatedStageName, translatedJobName); }
assertThat("Should not require approval", stage2.requiresApproval(), is(false)); JobConfig plan = stage1.jobConfigByInstanceName("plan1", true); assertThat(plan.name(), is(new CaseInsensitiveString("plan1"))); assertThat(plan.resourceConfigs(), is(new ResourceConfigs("tiger, lion")));