public PipelineConfig getPipelineConfigByName(CaseInsensitiveString pipelineName) { try { return cruiseConfig.pipelineConfigByName(pipelineName); } catch (PipelineNotFoundException e) { return null; } }
@Override public void makePipelineUseTemplate(CaseInsensitiveString pipelineName, CaseInsensitiveString templateName) { pipelineConfigByName(pipelineName).templatize(templateName); }
@Override public MaterialConfig materialConfigFor(CaseInsensitiveString pipelineName, String fingerprint) { PipelineConfig pipelineConfig = pipelineConfigByName(pipelineName); MaterialConfigs materialConfigs = pipelineConfig.materialConfigs(); for (MaterialConfig materialConfig : materialConfigs) { if (materialConfig.getFingerprint().equals(fingerprint)) { return materialConfig; } } return null; }
@Override public List<PipelineConfig> pipelinesForFetchArtifacts(String pipelineName) { PipelineConfig currentPipeline = pipelineConfigByName(new CaseInsensitiveString(pipelineName)); List<PipelineConfig> pipelinesForFetchArtifact = currentPipeline.allFirstLevelUpstreamPipelines(this); pipelinesForFetchArtifact.add(currentPipeline); return pipelinesForFetchArtifact; }
@Override public StageConfig stageConfigByName(final CaseInsensitiveString pipelineName, final CaseInsensitiveString stageName) { StageConfig stageConfig = pipelineConfigByName(pipelineName).findBy(stageName); StageNotFoundException.bombIfNull(stageConfig, pipelineName, stageName); return stageConfig; }
@Override public boolean isPipelineUnlockableWhenFinished(String pipelineName) { return pipelineConfigByName(new CaseInsensitiveString(pipelineName)).isPipelineUnlockableWhenFinished(); }
@Override public StageConfig nextStage(final CaseInsensitiveString pipelineName, final CaseInsensitiveString lastStageName) { StageConfig stageConfig = pipelineConfigByName(pipelineName).nextStage(lastStageName); bombIfNull(stageConfig, "Build stage after '" + lastStageName + "' not found."); return stageConfig; }
@Override public StageConfig previousStage(final CaseInsensitiveString pipelineName, final CaseInsensitiveString lastStageName) { StageConfig stageConfig = pipelineConfigByName(pipelineName).previousStage(lastStageName); bombIfNull(stageConfig, "Build stage after '" + lastStageName + "' not found."); return stageConfig; }
@Override public Boolean isPipelineLockable(String pipelineName) { PipelineConfig pipelineConfig = pipelineConfigByName(new CaseInsensitiveString(pipelineName)); if (pipelineConfig.hasExplicitLock()) { return pipelineConfig.explicitLock(); } return false; }
@Override public JobConfig findJob(String pipelineName, String stageName, String jobName) { return pipelineConfigByName(new CaseInsensitiveString(pipelineName)) .findBy(new CaseInsensitiveString(stageName)) .jobConfigByConfigName(new CaseInsensitiveString(jobName)); }
@Override public boolean hasVariableInScope(String pipelineName, String variableName) { EnvironmentConfig environmentConfig = environments.findEnvironmentForPipeline(new CaseInsensitiveString(pipelineName)); if (environmentConfig != null) { if (environmentConfig.hasVariable(variableName)) { return true; } } return pipelineConfigByName(new CaseInsensitiveString(pipelineName)).hasVariableInScope(variableName); }
@Override public EnvironmentVariablesConfig variablesFor(String pipelineName) { EnvironmentVariablesConfig pipelineVariables = pipelineConfigByName(new CaseInsensitiveString(pipelineName)).getVariables(); EnvironmentConfig environment = this.environments.findEnvironmentForPipeline(new CaseInsensitiveString(pipelineName)); return environment != null ? environment.getVariables().overrideWith(pipelineVariables) : pipelineVariables; }
@Test public void shouldNotClonePipelineNameToConfigMap() { BasicCruiseConfig config = GoConfigMother.configWithPipelines("p1", "p2"); //to prime cache config.pipelineConfigByName(new CaseInsensitiveString("p1")); //change state config.findGroup("defaultGroup").remove(0); BasicCruiseConfig cloned = new GoConfigCloner().deepClone(config); assertThat(ReflectionUtil.getField(config, "pipelineNameToConfigMap"), is(not((nullValue())))); assertThat(ReflectionUtil.getField(cloned, "pipelineNameToConfigMap"), is(nullValue())); assertThat(cloned.pipelineConfigsAsMap().size(), is(1)); }
@Test public void shouldGetPipelineGroupForPipelineInContext(){ String pipelineName = "p1"; BasicCruiseConfig cruiseConfig = GoConfigMother.configWithPipelines(pipelineName); PipelineConfig p1 = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString(pipelineName)); PipelineConfigSaveValidationContext context = PipelineConfigSaveValidationContext.forChain(true, PipelineConfigs.DEFAULT_GROUP, cruiseConfig, p1); assertThat(context.getPipelineGroup(), is(cruiseConfig.findGroup(PipelineConfigs.DEFAULT_GROUP))); }
@Test public void shouldValidateTreeForAllTasks() { AntTask antTask = antTask("build.xml", "compile", "/abc"); ExecTask execTask = new ExecTask("foo", new Arguments(new Argument("arg"))); Tasks tasks = new Tasks(antTask, execTask); String pipelineName = "p1"; PipelineConfig pipelineConfig = GoConfigMother.configWithPipelines(pipelineName).pipelineConfigByName(new CaseInsensitiveString(pipelineName)); StageConfig stageConfig = pipelineConfig.getStages().get(0); JobConfig jobConfig = stageConfig.getJobs().get(0); jobConfig.setTasks(tasks); PipelineConfigSaveValidationContext context = PipelineConfigSaveValidationContext.forChain(true, "group", pipelineConfig, stageConfig, jobConfig); assertFalse(tasks.validateTree(context)); assertThat(tasks.errors().isEmpty(), is(true)); assertThat(antTask.errors().isEmpty(), is(false)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).size(), is(1)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).contains("Task of job 'job' in stage 'stage' of pipeline 'p1' has path '/abc' which is outside the working directory."), is(true)); assertThat(execTask.errors().get(ExecTask.ARG_LIST_STRING).size(), is(1)); assertThat(execTask.errors().get(ExecTask.ARG_LIST_STRING).contains("Can not use both 'args' attribute and 'arg' sub element in 'exec' element!"), is(true)); assertThat(execTask.errors().get(ExecTask.ARGS).size(), is(1)); assertThat(execTask.errors().get(ExecTask.ARGS).contains("Can not use both 'args' attribute and 'arg' sub element in 'exec' element!"), is(true)); }
@Test public void shouldValidateTree(){ String pipelineName = "p1"; PipelineConfig pipelineConfig = GoConfigMother.configWithPipelines(pipelineName).pipelineConfigByName(new CaseInsensitiveString(pipelineName)); StageConfig stageConfig = pipelineConfig.getStages().get(0); JobConfig jobConfig = stageConfig.getJobs().get(0); AbstractTask execTask = new ExecTask("ls", "-la", "42"); AntTask antTask = new AntTask(); antTask.setWorkingDirectory("/abc"); execTask.setCancelTask(antTask); PipelineConfigSaveValidationContext context = PipelineConfigSaveValidationContext.forChain(true, "group", pipelineConfig, stageConfig, jobConfig); assertThat(execTask.validateTree(context), is(false)); assertThat(antTask.errors().isEmpty(), is(false)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).size(), is(1)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).contains("Task of job 'job' in stage 'stage' of pipeline 'p1' has path '/abc' which is outside the working directory."), is(true)); }
new ConfigParamPreprocessor().process(preprocessed); config.encryptSecureProperties(preprocessed); PipelineConfig ancestor = config.pipelineConfigByName(new CaseInsensitiveString("ancestor")); PipelineConfig child = config.pipelineConfigByName(new CaseInsensitiveString("child"));
private BasicCruiseConfig setupPipelines() { BasicCruiseConfig config = GoConfigMother.configWithPipelines("ancestor", "parent", "child"); config.getArtifactStores().add(new ArtifactStore("cd.go.s3", "cd.go.s3")); PipelineConfig ancestor = config.pipelineConfigByName(new CaseInsensitiveString("ancestor")); ancestor.add(StageConfigMother.stageConfig("stage1", new JobConfigs(new JobConfig("job1")))); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("art_1", "cd.go.s3", ancestor.getStage("stage1").getJobs().first().artifactConfigs().add(pluggableArtifactConfig); PipelineConfig parent = config.pipelineConfigByName(new CaseInsensitiveString("parent")); parent.add(StageConfigMother.stageConfig("stage1", new JobConfigs(new JobConfig("job1")))); parent.getStage("stage1").jobConfigByConfigName("job1").artifactConfigs() .add(new PluggableArtifactConfig("art_2", "cd.go.s3")); PipelineConfig child = config.pipelineConfigByName(new CaseInsensitiveString("child")); child.addParam(new ParamConfig("UPSTREAM_PIPELINE", "ancestor/parent")); child.addParam(new ParamConfig("UPSTREAM_STAGE", "stage1"));