Search in sources :

Example 1 with YamlSource

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("");
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Restricted(org.kohsuke.accmod.Restricted)

Example 2 with YamlSource

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));
}
Also used : Util.toYamlString(io.jenkins.plugins.casc.misc.Util.toYamlString) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Source(io.jenkins.plugins.casc.model.Source) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 3 with YamlSource

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());
    }
}
Also used : ArrayList(java.util.ArrayList) JSONArray(net.sf.json.JSONArray) File(java.io.File) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Source(io.jenkins.plugins.casc.model.Source) POST(org.kohsuke.stapler.verb.POST) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Restricted(org.kohsuke.accmod.Restricted)

Example 4 with YamlSource

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));
}
Also used : Util.toYamlString(io.jenkins.plugins.casc.misc.Util.toYamlString) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Source(io.jenkins.plugins.casc.model.Source) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

YamlSource (io.jenkins.plugins.casc.yaml.YamlSource)4 Source (io.jenkins.plugins.casc.model.Source)3 Util.toYamlString (io.jenkins.plugins.casc.misc.Util.toYamlString)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Issue (org.jvnet.hudson.test.Issue)2 Restricted (org.kohsuke.accmod.Restricted)2 RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)2 ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)1 JSONArray (net.sf.json.JSONArray)1 POST (org.kohsuke.stapler.verb.POST)1