Skip to content

Commit

Permalink
[FIXED JENKINS-17708] Expose scriptler scripts via token macro token
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed May 19, 2013
1 parent 587086f commit 1f5a83c
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@
/.project
/bin
/scriptler (org.jenkins-ci.plugins).iml
/dummy.groovy
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.451</version>
<version>1.480</version>
<!-- <version>1.460</version> version caused JENKINS-13518 -->
</parent>

Expand Down
Expand Up @@ -358,27 +358,15 @@ public HttpResponse doRemoveScript(StaplerRequest res, StaplerResponse rsp, @Que
public HttpResponse doUploadScript(StaplerRequest req) throws IOException, ServletException {
checkPermission(Hudson.ADMINISTER);
try {
File rootDir = getScriptDirectory();


FileItem fileItem = req.getFileItem("file");
boolean nonAdministerUsing = req.getSubmittedForm().getBoolean("nonAdministerUsing");
String fileName = Util.getFileName(fileItem.getName());
if (StringUtils.isEmpty(fileName)) {
return new HttpRedirect(".");
}
// upload can only be to/from local catalog
fileName = fixFileName(null, fileName);

fileItem.write(new File(rootDir, fileName));

commitFileToGitRepo(fileName);

Script script = ScriptHelper.getScript(fileName, false);
if (script == null) {
script = new Script(fileName, fileName, true, nonAdministerUsing, false);
}
ScriptlerConfiguration config = getConfiguration();
config.addOrReplace(script);
saveScript(fileItem, nonAdministerUsing, fileName);

return new HttpRedirect("index");
} catch (IOException e) {
Expand All @@ -388,6 +376,28 @@ public HttpResponse doUploadScript(StaplerRequest req) throws IOException, Servl
}
}

/**
* Protected only for testing
*/
/*private*/ void saveScript(FileItem fileItem, boolean nonAdministerUsing, String fileName) throws Exception, IOException {
// upload can only be to/from local catalog
fileName = fixFileName(null, fileName);

File rootDir = getScriptDirectory();
final File f = new File(rootDir, fileName);

fileItem.write(f);

commitFileToGitRepo(fileName);

Script script = ScriptHelper.getScript(fileName, false);
if (script == null) {
script = new Script(fileName, fileName, true, nonAdministerUsing, false);
}
ScriptlerConfiguration config = getConfiguration();
config.addOrReplace(script);
}

/**
* Display the screen to trigger a script. The source of the script get loaded from the filesystem and placed in the request to display it on the page before execution.
*
Expand Down
@@ -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");
}

}
Expand Up @@ -78,12 +78,14 @@ public Object call() throws RuntimeException {
PrintStream logger = listener.getLogger();
GroovyShell shell = new GroovyShell(cl);

for (Parameter param : parameters) {
final String paramName = param.getName();
if (DEFAULT_VARIABLES.contains(paramName)) {
logger.println(Messages.skipParamter(paramName));
} else {
shell.setVariable(paramName, param.getValue());
if(parameters != null) {
for (Parameter param : parameters) {
final String paramName = param.getName();
if (DEFAULT_VARIABLES.contains(paramName)) {
logger.println(Messages.skipParamter(paramName));
} else {
shell.setVariable(paramName, param.getValue());
}
}
}

Expand Down
Expand Up @@ -37,4 +37,6 @@ parameterExtractionFailed = failed to read parameters from request
scriptSourceNotFound = not able to load sources for script [{0}]
skipParamter = skipping parameter [{0}] this name is used internal, please rename!"
resultPrefix = Result:
no_parameters_defined = There is not any parameter defined for this job.
no_parameters_defined = There is not any parameter defined for this job.
tokenmacro_AdminScriptOnly = The script [{0}] exists, but is marked to be used by admins only and is therefore not allowed for usage in the TokenMacro.
tokenmacro_ScriptDoesNotExist = No script with the id [{0}] could be found.
@@ -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);
}
}
@@ -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\"}"));

}
}

0 comments on commit 1f5a83c

Please sign in to comment.