8
8
import io .jenkins .plugins .casc .ConfigurationContext ;
9
9
import io .jenkins .plugins .casc .ConfiguratorException ;
10
10
import io .jenkins .plugins .casc .Configurator ;
11
+ import io .jenkins .plugins .casc .SecretSourceResolver ;
11
12
import io .jenkins .plugins .casc .RootElementConfigurator ;
12
13
import io .jenkins .plugins .casc .impl .attributes .MultivaluedAttribute ;
13
14
import io .jenkins .plugins .casc .model .CNode ;
15
+ import io .jenkins .plugins .casc .model .Mapping ;
14
16
import io .jenkins .plugins .casc .model .Sequence ;
15
17
import org .kohsuke .accmod .Restricted ;
16
18
import org .kohsuke .accmod .restrictions .NoExternalUse ;
24
26
import java .util .ArrayList ;
25
27
import java .util .Collections ;
26
28
import java .util .List ;
29
+ import java .util .Map ;
30
+ import java .util .HashMap ;
27
31
import java .util .Set ;
28
32
33
+ import static io .vavr .API .unchecked ;
34
+ import static io .vavr .API .Try ;
35
+
29
36
30
37
/**
31
38
* @author <a href="mailto:tomasz.szandala@gmail.com">Tomasz Szandala</a>
32
39
*/
33
- @ Extension (optional = true )
40
+ @ Extension (optional = true , ordinal = - 50 )
34
41
@ Restricted (NoExternalUse .class )
35
42
public class GroovyScriptCaller implements RootElementConfigurator <Boolean []> {
36
43
@@ -56,41 +63,36 @@ public Boolean[] getTargetComponent(ConfigurationContext context) {
56
63
57
64
@ Override
58
65
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 ;
92
88
}
93
89
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
+
94
96
@ Override
95
97
public Boolean [] check (CNode config , ConfigurationContext context ) throws ConfiguratorException {
96
98
// Any way to dry-run a Groovy script ?
@@ -99,7 +101,7 @@ public Boolean[] check(CNode config, ConfigurationContext context) throws Config
99
101
100
102
@ Nonnull
101
103
@ Override
102
- public List <Configurator > getConfigurators (ConfigurationContext context ) {
104
+ public List <Configurator < Boolean []> > getConfigurators (ConfigurationContext context ) {
103
105
return Collections .singletonList (context .lookup (GroovyScriptSource .class ));
104
106
}
105
107
@@ -108,4 +110,5 @@ public List<Configurator> getConfigurators(ConfigurationContext context) {
108
110
public CNode describe (Boolean [] instance , ConfigurationContext context ) throws Exception {
109
111
return null ;
110
112
}
113
+
111
114
}
0 commit comments