88import io .jenkins .plugins .casc .ConfigurationContext ;
99import io .jenkins .plugins .casc .ConfiguratorException ;
1010import io .jenkins .plugins .casc .Configurator ;
11+ import io .jenkins .plugins .casc .SecretSourceResolver ;
1112import io .jenkins .plugins .casc .RootElementConfigurator ;
1213import io .jenkins .plugins .casc .impl .attributes .MultivaluedAttribute ;
1314import io .jenkins .plugins .casc .model .CNode ;
15+ import io .jenkins .plugins .casc .model .Mapping ;
1416import io .jenkins .plugins .casc .model .Sequence ;
1517import org .kohsuke .accmod .Restricted ;
1618import org .kohsuke .accmod .restrictions .NoExternalUse ;
2426import java .util .ArrayList ;
2527import java .util .Collections ;
2628import java .util .List ;
29+ import java .util .Map ;
30+ import java .util .HashMap ;
2731import java .util .Set ;
2832
33+ import static io .vavr .API .unchecked ;
34+ import static io .vavr .API .Try ;
35+
2936
3037/**
3138 * @author <a href="mailto:tomasz.szandala@gmail.com">Tomasz Szandala</a>
3239 */
33- @ Extension (optional = true )
40+ @ Extension (optional = true , ordinal = - 50 )
3441@ Restricted (NoExternalUse .class )
3542public class GroovyScriptCaller implements RootElementConfigurator <Boolean []> {
3643
@@ -56,41 +63,36 @@ public Boolean[] getTargetComponent(ConfigurationContext context) {
5663
5764 @ Override
5865 public Boolean [] configure (CNode config , ConfigurationContext context ) throws ConfiguratorException {
59- //JenkinsJobManagement mng = new JenkinsJobManagement(System.out, new EnvVars(), null, null, LookupStrategy.JENKINS_ROOT);
60- final Sequence sources = config .asSequence ();
61- final Configurator <GroovyScriptSource > con = context .lookup (GroovyScriptSource .class );
62- List <Boolean > generated = new ArrayList <>();
63- for (CNode source : sources ) {
64- final String script ;
65- try {
66- script = con .configure (source , context ).getScript ();
67- } catch (IOException e ) {
68- throw new ConfiguratorException (this , "Failed to retrieve Groovy script" , e );
69- }
70- try {
71- //Binding binding = new Binding();
72- //binding.setVariable("foo", new Integer(2));
73- //GroovyShell shell = new GroovyShell();
74- //shell.evaluate(script);
75-
76- Binding binding = new Binding ();
77- //binding.setProperty("out",new PrintWriter(stdout,true));
78- //binding.setProperty("stdin",stdin);
79- //binding.setProperty("stdout",stdout);
80- //binding.setProperty("stderr",stderr);
81-
82- GroovyShell groovy = new GroovyShell (Jenkins .getActiveInstance ().getPluginManager ().uberClassLoader , binding );
83- groovy .run (script , "Configuration-as-Code-Groovy" , new ArrayList ());
84-
85- generated .add (true );
86-
87- } catch (Exception ex ) {
88- throw new ConfiguratorException (this , "Failed to execute script with hash " + script .hashCode (), ex );
89- }
90- }
91- return generated .toArray (new Boolean [generated .size ()]);
66+ final Configurator <GroovyScriptSource > c = context .lookup (GroovyScriptSource .class );
67+ return config .asSequence ().stream ()
68+ .map (source -> getActualValue (source , context ))
69+ .map (source -> Try (() -> c .configure (source , context ).getScript ())
70+ .onSuccess (GroovyScriptCaller .this ::runGroovyShell )
71+ .isSuccess ())
72+ .toArray (Boolean []::new );
73+ }
74+
75+ private CNode getActualValue (CNode source , ConfigurationContext context ) {
76+ return unchecked (() -> source .asMapping ().entrySet ().stream ().findFirst ()).apply ()
77+ .map (entry -> resolveSourceOrGetValue (entry , context ))
78+ .orElse (source );
79+ }
80+
81+ private CNode resolveSourceOrGetValue (Map .Entry <String , CNode > entry , ConfigurationContext context ) {
82+ final Mapping m = new Mapping ();
83+ m .put (
84+ entry .getKey (),
85+ SecretSourceResolver .resolve (context , unchecked (() -> entry .getValue ().asScalar ().getValue ()).apply ())
86+ );
87+ return m ;
9288 }
9389
90+ private Boolean runGroovyShell (String script ) {
91+ final GroovyShell s = new GroovyShell (Jenkins .getActiveInstance ().getPluginManager ().uberClassLoader , new Binding ());
92+ return Try (() -> s .run (script , "Configuration-as-Code-Groovy" , new ArrayList ())).isSuccess ();
93+ }
94+
95+
9496 @ Override
9597 public Boolean [] check (CNode config , ConfigurationContext context ) throws ConfiguratorException {
9698 // Any way to dry-run a Groovy script ?
@@ -99,7 +101,7 @@ public Boolean[] check(CNode config, ConfigurationContext context) throws Config
99101
100102 @ Nonnull
101103 @ Override
102- public List <Configurator > getConfigurators (ConfigurationContext context ) {
104+ public List <Configurator < Boolean []> > getConfigurators (ConfigurationContext context ) {
103105 return Collections .singletonList (context .lookup (GroovyScriptSource .class ));
104106 }
105107
@@ -108,4 +110,5 @@ public List<Configurator> getConfigurators(ConfigurationContext context) {
108110 public CNode describe (Boolean [] instance , ConfigurationContext context ) throws Exception {
109111 return null ;
110112 }
113+
111114}
0 commit comments