Search in sources :

Example 1 with Source

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));
}
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 2 with Source

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());
    }
}
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 3 with Source

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;
}
Also used : Sequence(io.jenkins.plugins.casc.model.Sequence) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) URL(java.net.URL) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) Scanner(java.util.Scanner) CNode(io.jenkins.plugins.casc.model.CNode) Mapping(io.jenkins.plugins.casc.model.Mapping) Which(hudson.remoting.Which) Node(org.yaml.snakeyaml.nodes.Node) Initializer(hudson.init.Initializer) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) Scalar(io.jenkins.plugins.casc.model.Scalar) URI(java.net.URI) GlobalConfiguration(jenkins.model.GlobalConfiguration) Path(java.nio.file.Path) Functions(hudson.Functions) Restricted(org.kohsuke.accmod.Restricted) Collection(java.util.Collection) Jenkins(jenkins.model.Jenkins) Set(java.util.Set) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Logger(java.util.logging.Logger) StandardCharsets(java.nio.charset.StandardCharsets) String.format(java.lang.String.format) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Serializer(org.yaml.snakeyaml.serializer.Serializer) YamlUtils(io.jenkins.plugins.casc.yaml.YamlUtils) List(java.util.List) POST(org.kohsuke.stapler.verb.POST) Stream(java.util.stream.Stream) Writer(java.io.Writer) SchemaGeneration.writeJSONSchema(io.jenkins.plugins.casc.SchemaGeneration.writeJSONSchema) InitMilestone(hudson.init.InitMilestone) Source(io.jenkins.plugins.casc.model.Source) JSONObject(net.sf.json.JSONObject) Permission(hudson.security.Permission) YAMLException(org.yaml.snakeyaml.error.YAMLException) IntStream(java.util.stream.IntStream) Format(io.jenkins.plugins.casc.model.Scalar.Format) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QueryParameter(org.kohsuke.stapler.QueryParameter) StaplerRequest(org.kohsuke.stapler.StaplerRequest) HashMap(java.util.HashMap) PLAIN(org.yaml.snakeyaml.DumperOptions.ScalarStyle.PLAIN) TreeSet(java.util.TreeSet) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Inject(javax.inject.Inject) DumperOptions(org.yaml.snakeyaml.DumperOptions) ACLContext(hudson.security.ACLContext) ACL(hudson.security.ACL) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Extension(hudson.Extension) OutputStreamWriter(java.io.OutputStreamWriter) Resolver(org.yaml.snakeyaml.resolver.Resolver) DOUBLE_QUOTED(org.yaml.snakeyaml.DumperOptions.ScalarStyle.DOUBLE_QUOTED) LinkedHashSet(java.util.LinkedHashSet) Util(hudson.Util) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) FormValidation(hudson.util.FormValidation) HttpServletResponse(javax.servlet.http.HttpServletResponse) ManagementLink(hudson.model.ManagementLink) NoExternalUse(org.kohsuke.accmod.restrictions.NoExternalUse) StaplerResponse(org.kohsuke.stapler.StaplerResponse) IOException(java.io.IOException) Tag(org.yaml.snakeyaml.nodes.Tag) File(java.io.File) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Klass(org.kohsuke.stapler.lang.Klass) Collectors.toList(java.util.stream.Collectors.toList) PluginManager(hudson.PluginManager) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) JSONArray(net.sf.json.JSONArray) Emitter(org.yaml.snakeyaml.emitter.Emitter) DefaultConfiguratorRegistry(io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ServletContext(javax.servlet.ServletContext) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull) LITERAL(org.yaml.snakeyaml.DumperOptions.ScalarStyle.LITERAL) Collections(java.util.Collections) BLOCK(org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK) FileSystems(java.nio.file.FileSystems) JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray)

Example 4 with Source

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());
}
Also used : Sequence(io.jenkins.plugins.casc.model.Sequence) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) URL(java.net.URL) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) Scanner(java.util.Scanner) CNode(io.jenkins.plugins.casc.model.CNode) Mapping(io.jenkins.plugins.casc.model.Mapping) Which(hudson.remoting.Which) Node(org.yaml.snakeyaml.nodes.Node) Initializer(hudson.init.Initializer) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) Scalar(io.jenkins.plugins.casc.model.Scalar) URI(java.net.URI) GlobalConfiguration(jenkins.model.GlobalConfiguration) Path(java.nio.file.Path) Functions(hudson.Functions) Restricted(org.kohsuke.accmod.Restricted) Collection(java.util.Collection) Jenkins(jenkins.model.Jenkins) Set(java.util.Set) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Logger(java.util.logging.Logger) StandardCharsets(java.nio.charset.StandardCharsets) String.format(java.lang.String.format) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Serializer(org.yaml.snakeyaml.serializer.Serializer) YamlUtils(io.jenkins.plugins.casc.yaml.YamlUtils) List(java.util.List) POST(org.kohsuke.stapler.verb.POST) Stream(java.util.stream.Stream) Writer(java.io.Writer) SchemaGeneration.writeJSONSchema(io.jenkins.plugins.casc.SchemaGeneration.writeJSONSchema) InitMilestone(hudson.init.InitMilestone) Source(io.jenkins.plugins.casc.model.Source) JSONObject(net.sf.json.JSONObject) Permission(hudson.security.Permission) YAMLException(org.yaml.snakeyaml.error.YAMLException) IntStream(java.util.stream.IntStream) Format(io.jenkins.plugins.casc.model.Scalar.Format) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QueryParameter(org.kohsuke.stapler.QueryParameter) StaplerRequest(org.kohsuke.stapler.StaplerRequest) HashMap(java.util.HashMap) PLAIN(org.yaml.snakeyaml.DumperOptions.ScalarStyle.PLAIN) TreeSet(java.util.TreeSet) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Inject(javax.inject.Inject) DumperOptions(org.yaml.snakeyaml.DumperOptions) ACLContext(hudson.security.ACLContext) ACL(hudson.security.ACL) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Extension(hudson.Extension) OutputStreamWriter(java.io.OutputStreamWriter) Resolver(org.yaml.snakeyaml.resolver.Resolver) DOUBLE_QUOTED(org.yaml.snakeyaml.DumperOptions.ScalarStyle.DOUBLE_QUOTED) LinkedHashSet(java.util.LinkedHashSet) Util(hudson.Util) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) FormValidation(hudson.util.FormValidation) HttpServletResponse(javax.servlet.http.HttpServletResponse) ManagementLink(hudson.model.ManagementLink) NoExternalUse(org.kohsuke.accmod.restrictions.NoExternalUse) StaplerResponse(org.kohsuke.stapler.StaplerResponse) IOException(java.io.IOException) Tag(org.yaml.snakeyaml.nodes.Tag) File(java.io.File) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Klass(org.kohsuke.stapler.lang.Klass) Collectors.toList(java.util.stream.Collectors.toList) PluginManager(hudson.PluginManager) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) JSONArray(net.sf.json.JSONArray) Emitter(org.yaml.snakeyaml.emitter.Emitter) DefaultConfiguratorRegistry(io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ServletContext(javax.servlet.ServletContext) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull) LITERAL(org.yaml.snakeyaml.DumperOptions.ScalarStyle.LITERAL) Collections(java.util.Collections) BLOCK(org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK) FileSystems(java.nio.file.FileSystems) JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Source(io.jenkins.plugins.casc.model.Source) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Restricted(org.kohsuke.accmod.Restricted)

Example 5 with Source

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));
}
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

Source (io.jenkins.plugins.casc.model.Source)5 YamlSource (io.jenkins.plugins.casc.yaml.YamlSource)5 File (java.io.File)3 ArrayList (java.util.ArrayList)3 JSONArray (net.sf.json.JSONArray)3 Restricted (org.kohsuke.accmod.Restricted)3 RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)3 POST (org.kohsuke.stapler.verb.POST)3 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Extension (hudson.Extension)2 Functions (hudson.Functions)2 PluginManager (hudson.PluginManager)2 Util (hudson.Util)2 InitMilestone (hudson.init.InitMilestone)2 Initializer (hudson.init.Initializer)2 ManagementLink (hudson.model.ManagementLink)2 Which (hudson.remoting.Which)2 ACL (hudson.security.ACL)2 ACLContext (hudson.security.ACLContext)2