public String getConfigurationType() { if (hasTemplate()) { return CONFIGURATION_TYPE_TEMPLATE; } return CONFIGURATION_TYPE_STAGES; }
public void process(CruiseConfig cruiseConfig) { for (PipelineConfig pipelineConfig : cruiseConfig.getAllPipelineConfigs()) { if (pipelineConfig.hasTemplate()) { CaseInsensitiveString templateName = pipelineConfig.getTemplateName(); PipelineTemplateConfig pipelineTemplate = cruiseConfig.findTemplate(templateName); pipelineConfig.validateTemplate(pipelineTemplate); if (pipelineConfig.errors().isEmpty() && !pipelineConfig.hasTemplateApplied()) { pipelineConfig.usingTemplate(pipelineTemplate); } } } } }
@Override public boolean hasTemplate() { for (PipelineConfig pipelineConfig : this) { if (pipelineConfig.hasTemplate()) { return true; } } return false; }
public void validateTemplate(PipelineTemplateConfig templateConfig) { if (hasTemplate()) { if (new NameTypeValidator().isNameInvalid(templateName.toString())) { errors().add(TEMPLATE_NAME, NameTypeValidator.errorMessage("template", templateName)); } if (hasStages() && !hasTemplateApplied()) { addError("stages", format("Cannot add stages to pipeline '%s' which already references template '%s'", this.name(), this.getTemplateName())); addError("template", format("Cannot set template '%s' on pipeline '%s' because it already has stages defined", this.getTemplateName(), this.name())); } if (templateConfig == null) { addError("pipeline", format("Pipeline '%s' refers to non-existent template '%s'.", name(), templateName)); } } }
private void ensureNoTemplateDefined(CaseInsensitiveString stageName) { if (hasTemplate()) { throw new IllegalStateException(format("Cannot add stage '%s' to pipeline '%s', which already references template '%s'.", stageName, name, templateName)); } }
@Test public void shouldTemplatizeAPipeline() { PipelineConfig config = PipelineConfigMother.createPipelineConfigWithStages("pipeline", "stage1", "stage2"); config.templatize(new CaseInsensitiveString("template")); assertThat(config.hasTemplate(), is(true)); assertThat(config.hasTemplateApplied(), is(false)); assertThat(config.getTemplateName(), is(new CaseInsensitiveString("template"))); assertThat(config.isEmpty(), is(true)); config.templatize(new CaseInsensitiveString("")); assertThat(config.hasTemplate(), is(false)); config.templatize(null); assertThat(config.hasTemplate(), is(false)); }
@Override public ArrayList<PipelineConfig> pipelineConfigsAssociatedWithTemplate(CaseInsensitiveString templateName) { ArrayList<PipelineConfig> pipelines = new ArrayList<>(); if (templateName != null) { for (PipelineConfig pipelineConfig : getAllPipelineConfigs()) { if (pipelineConfig.hasTemplate() && pipelineConfig.getTemplateName().equals(templateName)) { pipelines.add(pipelineConfig); } } } return pipelines; }
public PipelineConfig getCopyForEditing() { PipelineConfig pipelineConfig = (PipelineConfig) clone(); if (pipelineConfig.hasTemplate()) { pipelineConfig.clear(); } return pipelineConfig; }
@Override public ArrayList<CaseInsensitiveString> pipelinesAssociatedWithTemplate(CaseInsensitiveString templateName) { ArrayList<CaseInsensitiveString> pipelines = new ArrayList<>(); if (templateName != null) { for (PipelineConfig pipelineConfig : getAllPipelineConfigs()) { if (pipelineConfig.hasTemplate() && pipelineConfig.getTemplateName().equals(templateName)) { pipelines.add(pipelineConfig.getName()); } } } return pipelines; }
public List<PipelineConfig> allPipelinesNotUsingTemplates(Username username, LocalizedOperationResult result) { if (!(securityService.isUserAdmin(username) || securityService.isUserGroupAdmin(username))) { result.forbidden(LocalizedMessage.forbiddenToEdit(), HealthStateType.forbidden()); return null; } List<PipelineConfig> allPipelineConfigs = goConfigService.getAllPipelineConfigsForEditForUser(username); List<PipelineConfig> allPipelinesNotUsingTemplates = new ArrayList<>(); for (PipelineConfig pipeline : allPipelineConfigs) { if (!pipeline.hasTemplate()) { allPipelinesNotUsingTemplates.add(pipeline); } } return allPipelinesNotUsingTemplates; }
public void encryptSecureProperties(CruiseConfig preprocessedConfig, PipelineConfig preprocessedPipelineConfig) { if (hasTemplate() || doesNotHavePublishAndFetchExternalConfig()) { return; } for (StageConfig stageConfig : getStages()) { stageConfig.encryptSecureProperties(preprocessedConfig, preprocessedPipelineConfig, preprocessedPipelineConfig.getStage(stageConfig.name())); } }
public void validate(ValidationContext validationContext) { validateLabelTemplate(); validatePipelineName(); validateStageNameUniqueness(); validateLockBehaviorValues(); if (!hasTemplate() && isEmpty()) { addError("pipeline", format("Pipeline '%s' does not have any stages configured. A pipeline must have at least one stage.", name())); } }
@Test public void shouldUseTemplateWhenSetConfigAttributesContainsTemplateName() { PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig("pipeline"); assertThat(pipelineConfig.hasTemplate(), is(false)); Map map = new HashMap(); map.put(PipelineConfig.CONFIGURATION_TYPE, PipelineConfig.CONFIGURATION_TYPE_TEMPLATE); map.put(PipelineConfig.TEMPLATE_NAME, "foo-template"); pipelineConfig.setConfigAttributes(map); assertThat(pipelineConfig.getConfigurationType(), is(PipelineConfig.CONFIGURATION_TYPE_TEMPLATE)); assertThat(pipelineConfig.getTemplateName(), is(new CaseInsensitiveString("foo-template"))); }
@Override public Map<CaseInsensitiveString, Map<CaseInsensitiveString, Authorization>> templatesWithAssociatedPipelines() { if (allTemplatesWithAssociatedPipelines == null) { allTemplatesWithAssociatedPipelines = new AllTemplatesWithAssociatedPipelines(); for (PipelineTemplateConfig templateConfig : getTemplates()) { if (!allTemplatesWithAssociatedPipelines.containsKey(templateConfig.name())) { allTemplatesWithAssociatedPipelines.put(templateConfig.name(), new HashMap<>()); } } for (PipelineConfigs pipelineConfigs : getGroups()) { List<PipelineConfig> pipelines = pipelineConfigs.getPipelines(); for (PipelineConfig pipeline : pipelines) { if (pipeline.hasTemplate()) { Map<CaseInsensitiveString, Authorization> authorizationMap = allTemplatesWithAssociatedPipelines.get(pipeline.getTemplateName()); authorizationMap.put(pipeline.getName(), pipelineConfigs.getAuthorization()); } } } } return allTemplatesWithAssociatedPipelines; }