use of io.jenkins.plugins.casc.model.Source 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.model.Source 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.model.Source in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method collectProblems.
private JSONArray collectProblems(Map<Source, String> issues, String severity) {
final JSONArray problems = new JSONArray();
issues.entrySet().stream().map(e -> new JSONObject().accumulate("line", e.getKey().line).accumulate(severity, e.getValue())).forEach(problems::add);
return problems;
}
use of io.jenkins.plugins.casc.model.Source in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doCheck.
@RequirePOST
@Restricted(NoExternalUse.class)
public void doCheck(StaplerRequest req, StaplerResponse res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final Map<Source, String> issues = checkWith(YamlSource.of(req));
res.setContentType("application/json");
final JSONArray warnings = new JSONArray();
issues.entrySet().stream().map(e -> new JSONObject().accumulate("line", e.getKey().line).accumulate("warning", e.getValue())).forEach(warnings::add);
warnings.write(res.getWriter());
}
use of io.jenkins.plugins.casc.model.Source 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