@@ -3,11 +3,14 @@
import groovy .lang .GroovyShell ;
import hudson .Util ;
import hudson .PluginManager ;
import hudson .model .Item ;
import hudson .model .AbstractProject ;
import hudson .model .Hudson ;
import hudson .model .Node ;
import hudson .remoting .Callable ;
import hudson .util .IOUtils ;
import java .io .File ;
import java .io .FileInputStream ;
import java .io .IOException ;
import java .util .Map ;
@@ -24,15 +27,15 @@ public GroovyScriptTriggerExecutor(XTriggerLog log) {
super (log );
}
public boolean evaluateGroovyScript (Node executingNode , final Item job , final String scriptContent , final Map <String , String > envVars , boolean groovySystemScript ) throws ScriptTriggerException {
public boolean evaluateGroovyScript (Node executingNode , final AbstractProject proj , final String scriptContent , final Map <String , String > envVars , boolean groovySystemScript ) throws ScriptTriggerException {
if (scriptContent == null ) {
throw new NullPointerException ("The script content object must be set." );
}
try {
if (groovySystemScript ) {
log .info ("Running as system script" );
return evaluateGroovyScript (job , scriptContent , envVars );
return evaluateGroovyScript (proj , scriptContent , envVars );
}
return executingNode .getRootPath ().act (new Callable <Boolean , ScriptTriggerException >() {
@@ -56,24 +59,29 @@ public Boolean call() throws ScriptTriggerException {
}
}
private boolean evaluateGroovyScript (final Item job , final String scriptContent , final Map <String , String > envVars ) {
private boolean evaluateGroovyScript (final AbstractProject proj , final String scriptContent , final Map <String , String > envVars ) {
final StringBuilder envDebug = new StringBuilder ("Replacing script vars using:" );
for (final Map .Entry <String , String > envEntry : envVars .entrySet ()) {
envDebug .append ("\n\t" ).append (envEntry .getKey ()).append ("=" ).append (envEntry .getValue ());
}
log .info (envDebug .toString ());
final String groovyExpressionResolved = Util .replaceMacro (scriptContent , envVars );
log .info (String .format ("Evaluating the groovy script: \n----------------------------------------\n%s\n----------------------------------------\n\n" , groovyExpressionResolved ));
log .info ("Evaluating the groovy script:" );
log .info ("---------- Base Script -----------------" );
log .info (scriptContent );
log .info ("---------- Resolved Script -------------" );
log .info (groovyExpressionResolved );
log .info ("----------------------------------------\n" );
final ClassLoader cl = getClassLoader ();
GroovyShell shell = new GroovyShell (cl );
shell .setVariable ("log" , log );
shell .setVariable ("out" , log .getListener ().getLogger ());
if (job != null ) {
shell .setVariable ("job " , job );
if (proj != null ) {
shell .setVariable ("project " , proj );
}
//Evaluate the new script content
@@ -105,17 +113,50 @@ protected ClassLoader getClassLoader() {
return cl ;
}
public boolean evaluateGroovyScriptFilePath (Node executingNode , Item job , String scriptFilePath , Map <String , String > envVars , boolean groovySystemScript ) throws ScriptTriggerException {
public boolean evaluateGroovyScriptFilePath (Node executingNode , AbstractProject proj , String scriptFilePath , Map <String , String > envVars , boolean groovySystemScript ) throws ScriptTriggerException {
if (scriptFilePath == null ) {
throw new NullPointerException ("The scriptFilePath object must be set." );
}
if (!existsScript (executingNode , scriptFilePath )) {
return false ;
final String scriptContent ;
if (groovySystemScript ) {
String expandedScriptFile = Util .replaceMacro (scriptFilePath , envVars );
final File file = new File (expandedScriptFile );
final String scriptPath = file .getAbsolutePath ();
if (!file .exists ()) {
log .info (String .format ("Can't load the file '%s'. It doesn't exist." , scriptPath ));
return false ;
}
log .info ("Reading script from: " + file .getAbsolutePath ());
try {
final FileInputStream fis = new FileInputStream (file );
try {
scriptContent = IOUtils .toString (fis );
}
finally {
fis .close ();
}
log .info ("Read " + scriptContent .length () + " character long script from: " + scriptPath );
}
catch (IOException e ) {
final String msg = "Failed to read system groovy script file '" + scriptFilePath + "' from '" + scriptPath + "'" ;
log .info (msg );
e .printStackTrace (log .getListener ().getLogger ());
throw new RuntimeException (msg , e );
}
}
else {
if (!existsScript (executingNode , scriptFilePath )) {
return false ;
}
scriptContent = getStringContent (executingNode , scriptFilePath );
}
String scriptContent = getStringContent (executingNode , scriptFilePath );
return evaluateGroovyScript (executingNode , job , scriptContent , envVars , groovySystemScript );
return evaluateGroovyScript (executingNode , proj , scriptContent , envVars , groovySystemScript );
}
}