Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-17708] Expose scriptler scripts via token macro token
- Loading branch information
Showing
8 changed files
with
143 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/.project | ||
/bin | ||
/scriptler (org.jenkins-ci.plugins).iml | ||
/dummy.groovy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/jenkinsci/plugins/scriptler/tokenmacro/ScriptlerTokenMacro.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.jenkinsci.plugins.scriptler.tokenmacro; | ||
|
||
import hudson.Extension; | ||
import hudson.model.TaskListener; | ||
import hudson.model.AbstractBuild; | ||
|
||
import java.io.IOException; | ||
|
||
import org.jenkinsci.plugins.scriptler.Messages; | ||
import org.jenkinsci.plugins.scriptler.config.Script; | ||
import org.jenkinsci.plugins.scriptler.util.GroovyScript; | ||
import org.jenkinsci.plugins.scriptler.util.ScriptHelper; | ||
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro; | ||
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException; | ||
|
||
/** | ||
* TokenMacro that allows the execution of a scriptler script an any arbitrary location supporting TokenMacros e.g. <code>${SCRIPTLER, scriptId="superscript.groovy"}</code> | ||
* | ||
* @author Dominik Bartholdi (imod) | ||
* | ||
*/ | ||
@Extension | ||
public class ScriptlerTokenMacro extends DataBoundTokenMacro { | ||
|
||
@Parameter | ||
public String scriptId; | ||
|
||
@Override | ||
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException { | ||
|
||
final Script script = ScriptHelper.getScript(scriptId, true); | ||
if (script.nonAdministerUsing) { | ||
listener.getLogger().println(Messages.tokenmacro_AdminScriptOnly(scriptId)); | ||
throw new MacroEvaluationException(Messages.tokenmacro_ScriptDoesNotExist(scriptId)); | ||
} | ||
|
||
Object output = context.getWorkspace().getChannel().call(new GroovyScript(script.script, null, true, listener)); | ||
|
||
return output != null ? output.toString() : ""; | ||
} | ||
|
||
@Override | ||
public boolean acceptsMacroName(String macroName) { | ||
return macroName.equals("SCRIPTLER"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/test/java/org/jenkinsci/plugins/scriptler/ScriptlerManagementHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.jenkinsci.plugins.scriptler; | ||
|
||
import org.apache.commons.fileupload.FileItem; | ||
|
||
public class ScriptlerManagementHelper { | ||
|
||
private final ScriptlerManagment scriptler; | ||
|
||
public ScriptlerManagementHelper(ScriptlerManagment scriptler) { | ||
this.scriptler = scriptler; | ||
} | ||
|
||
public void saveScript(FileItem file, boolean nonAdministerUsing, String fileName) throws Exception { | ||
scriptler.saveScript(file, nonAdministerUsing, fileName); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/jenkinsci/plugins/scriptler/tokenmacro/ScriptlerTokenMacroTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.jenkinsci.plugins.scriptler.tokenmacro; | ||
|
||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FileParameterValue.FileItemImpl; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.util.StreamTaskListener; | ||
|
||
import java.io.File; | ||
|
||
import org.apache.commons.fileupload.FileItem; | ||
import org.apache.commons.io.FileUtils; | ||
import org.jenkinsci.plugins.scriptler.ScriptlerManagementHelper; | ||
import org.jenkinsci.plugins.scriptler.ScriptlerManagment; | ||
import org.jenkinsci.plugins.tokenmacro.TokenMacro; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class ScriptlerTokenMacroTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
public void testExecutesScript() throws Exception { | ||
|
||
final ScriptlerManagment scriptler = j.getInstance().getExtensionList(ScriptlerManagment.class).get(0); | ||
ScriptlerManagementHelper helper = new ScriptlerManagementHelper(scriptler); | ||
File f = new File("dummy.groovy"); | ||
FileUtils.writeStringToFile(f, "return 'hello world'"); | ||
FileItem fi = new FileItemImpl(f); | ||
helper.saveScript(fi, true, "dummy.groovy"); | ||
|
||
FreeStyleProject p = j.createFreeStyleProject("foo"); | ||
FreeStyleBuild b = p.scheduleBuild2(0).get(); | ||
|
||
final StreamTaskListener listener = new StreamTaskListener(System.out); | ||
|
||
Assert.assertEquals("hello world", TokenMacro.expand(b, listener, "${SCRIPTLER,scriptId=\"dummy.groovy\"}")); | ||
|
||
} | ||
} |