Skip to content

Commit

Permalink
fix deprecated method and improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen committed Oct 19, 2020
1 parent 6c1c678 commit 316f5bf
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/main/java/io/jenkins/plugins/cascgroovy/GroovyScriptCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.Extension;
import io.jenkins.plugins.casc.*;
import io.jenkins.plugins.casc.Attribute;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.Configurator;
import io.jenkins.plugins.casc.ConfiguratorException;
import io.jenkins.plugins.casc.RootElementConfigurator;
import io.jenkins.plugins.casc.impl.attributes.MultivaluedAttribute;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.*;

import static io.vavr.API.Try;
import static io.vavr.API.unchecked;

Expand All @@ -27,6 +34,7 @@
public class GroovyScriptCaller implements RootElementConfigurator<Boolean[]> {

@Override
@Nonnull
public String getName() {
return "groovy";
}
Expand All @@ -37,6 +45,7 @@ public Class getTarget() {
}

@Override
@Nonnull
public Set<Attribute<Boolean[],?>> describe() {
return Collections.singleton(new MultivaluedAttribute("", GroovyScriptSource.class));
}
Expand All @@ -47,13 +56,13 @@ public Boolean[] getTargetComponent(ConfigurationContext context) {
}

@Override
@Nonnull
public Boolean[] configure(CNode config, ConfigurationContext context) throws ConfiguratorException {
final Configurator<GroovyScriptSource> c = context.lookup(GroovyScriptSource.class);
return config.asSequence().stream()
.map(source -> getActualValue(source, context))
.map(source -> Try(() -> c.configure(source, context).getScript())
.onSuccess(GroovyScriptCaller.this::runGroovyShell)
.isSuccess())
.map(source -> getScriptFromSource(source, context, c))
.map(script -> Try(script::getScript).onSuccess(GroovyScriptCaller.this::runGroovyShell).isSuccess())
.toArray(Boolean[]::new);
}

Expand All @@ -67,13 +76,21 @@ private CNode resolveSourceOrGetValue(Map.Entry<String, CNode> entry, Configurat
final Mapping m = new Mapping();
m.put(
entry.getKey(),
SecretSourceResolver.resolve(context, unchecked(() -> entry.getValue().asScalar().getValue()).apply())
context.getSecretSourceResolver().resolve(unchecked(() -> entry.getValue().asScalar().getValue()).apply())
);
return m;
}

private GroovyScriptSource getScriptFromSource(CNode source, ConfigurationContext context,
Configurator<GroovyScriptSource> configurator) {
return unchecked(() ->
Try(() -> configurator.configure(source, context))
.getOrElseThrow(t -> new ConfiguratorException(this,
"Failed to retrieve groovy script", t))).apply();
}

private void runGroovyShell(String script) {
final GroovyShell s = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, new Binding());
final GroovyShell s = new GroovyShell(Jenkins.get().getPluginManager().uberClassLoader, new Binding());
unchecked(() -> s.run(script, "ConfigurationAsCodeGroovy", new ArrayList()));
}

Expand Down

0 comments on commit 316f5bf

Please sign in to comment.