use of io.jenkins.plugins.casc.yaml.YamlSource in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doReplace.
@RequirePOST
@Restricted(NoExternalUse.class)
public void doReplace(StaplerRequest request, StaplerResponse response) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
String newSource = request.getParameter("_.newSource");
String normalizedSource = Util.fixEmptyAndTrim(newSource);
List<String> candidateSources = new ArrayList<>();
for (String candidateSource : inputToCandidateSources(normalizedSource)) {
File file = new File(candidateSource);
if (file.exists() || ConfigurationAsCode.isSupportedURI(candidateSource)) {
candidateSources.add(candidateSource);
} else {
LOGGER.log(Level.WARNING, "Source {0} could not be applied", candidateSource);
// todo: show message in UI
}
}
if (!candidateSources.isEmpty()) {
List<YamlSource> candidates = getConfigFromSources(candidateSources);
if (canApplyFrom(candidates)) {
sources = candidateSources;
configureWith(getConfigFromSources(getSources()));
CasCGlobalConfig config = GlobalConfiguration.all().get(CasCGlobalConfig.class);
if (config != null) {
config.setConfigurationPath(normalizedSource);
config.save();
}
LOGGER.log(Level.FINE, "Replace configuration with: " + normalizedSource);
} else {
LOGGER.log(Level.WARNING, "Provided sources could not be applied");
// todo: show message in UI
}
} else {
LOGGER.log(Level.FINE, "No such source exists, applying default");
// May be do nothing instead?
configure();
}
response.sendRedirect("");
}
use of io.jenkins.plugins.casc.yaml.YamlSource in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCodeTest method checkWith_should_pass_against_input_which_has_same_entries_with_initial_config.
@Test
@Issue("Issue #653")
@ConfiguredWithCode("aNonEmpty.yml")
public void checkWith_should_pass_against_input_which_has_same_entries_with_initial_config() throws Exception {
String rawConf = getClass().getResource("JenkinsConfigTest.yml").toExternalForm();
YamlSource input = YamlSource.of(rawConf);
Map<Source, String> actual = ConfigurationAsCode.get().checkWith(input);
assertThat(actual.size(), is(0));
}
use of io.jenkins.plugins.casc.yaml.YamlSource in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doCheckNewSource.
@POST
@Restricted(NoExternalUse.class)
public FormValidation doCheckNewSource(@QueryParameter String newSource) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
String normalizedSource = Util.fixEmptyAndTrim(newSource);
if (normalizedSource == null) {
// empty, do nothing
return FormValidation.ok();
}
List<String> candidateSources = new ArrayList<>();
for (String candidateSource : inputToCandidateSources(normalizedSource)) {
File file = new File(candidateSource);
if (!file.exists() && !ConfigurationAsCode.isSupportedURI(candidateSource)) {
return FormValidation.error("Configuration cannot be applied. File or URL cannot be parsed or do not exist.");
}
candidateSources.add(candidateSource);
}
try {
List<YamlSource> candidates = getConfigFromSources(candidateSources);
final Map<Source, String> issues = checkWith(candidates);
final JSONArray errors = collectProblems(issues, "error");
if (!errors.isEmpty()) {
return FormValidation.error(errors.toString());
}
final JSONArray warnings = collectProblems(issues, "warning");
if (!warnings.isEmpty()) {
return FormValidation.warning(warnings.toString());
}
return FormValidation.okWithMarkup("The configuration can be applied");
} catch (ConfiguratorException | IllegalArgumentException e) {
return FormValidation.error(e, e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
}
}
use of io.jenkins.plugins.casc.yaml.YamlSource in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCodeTest method checkWith_should_pass_against_valid_input.
@Test
@Issue("Issue #653")
public void checkWith_should_pass_against_valid_input() throws Exception {
String rawConf = getClass().getResource("JenkinsConfigTest.yml").toExternalForm();
YamlSource input = YamlSource.of(rawConf);
Map<Source, String> actual = ConfigurationAsCode.get().checkWith(input);
assertThat(actual.size(), is(0));
}
Aggregations