Skip to content

Commit

Permalink
Fix JENKINS-20117
Browse files Browse the repository at this point in the history
Switched to using
Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor)
instead of
build.getProject().getPublishersList().get(ExtendedEmailPublisher.class)
to get access to the descriptor.
  • Loading branch information
slide committed Oct 23, 2013
1 parent 227d9b0 commit 6fc8eae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
Expand Up @@ -5,6 +5,7 @@
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.tasks.Mailer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.jelly.JellyContext;
Expand All @@ -20,6 +21,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

Expand All @@ -39,20 +41,6 @@ public class JellyScriptContent extends DataBoundTokenMacro {
public boolean acceptsMacroName(String macroName) {
return macroName.equals(MACRO_NAME);
}
/*
public String getHelpText() {
return "Custom message content generated from a Jelly script template. "
+ "There are two templates provided: \"" + DEFAULT_HTML_TEMPLATE_NAME + "\" "
+ "and \"" + DEFAULT_TEXT_TEMPLATE_NAME + "\". Custom Jelly templates should be placed in "
+ "$JENKINS_HOME/" + EMAIL_TEMPLATES_DIRECTORY + ". When using custom templates, "
+ "the template filename without \".jelly\" should be used for "
+ "the \"" + TEMPLATE_NAME_ARG + "\" argument.\n"
+ "<ul>\n"
+ "<li><i>" + TEMPLATE_NAME_ARG + "</i> - the template name.<br>\n"
+ "Defaults to \"" + DEFAULT_TEMPLATE_NAME + "\".\n"
+ "</ul>\n";
}
*/

@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
Expand Down Expand Up @@ -125,18 +113,18 @@ private String convert(AbstractBuild<?, ?> build, JellyContext context, Script s

private JellyContext createContext(Object it, AbstractBuild<?, ?> build) {
JellyContext context = new JellyContext();
final ExtendedEmailPublisher publisher = build.getProject().getPublishersList().get(ExtendedEmailPublisher.class);
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);
context.setVariable("it", it);
context.setVariable("build", build);
context.setVariable("project", build.getParent());
context.setVariable("rooturl", publisher.getDescriptor().getHudsonUrl());
context.setVariable("rooturl", descriptor.getHudsonUrl());
return context;
}

private String getCharset(AbstractBuild<?, ?> build) {
String charset = Mailer.descriptor().getCharset();
ExtendedEmailPublisher publisher = build.getProject().getPublishersList().get(ExtendedEmailPublisher.class);
String overrideCharset = publisher.getDescriptor().getCharset();
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);
String overrideCharset = descriptor.getCharset();
if (overrideCharset != null) {
charset = overrideCharset;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.plugins.emailext.ScriptSandbox;
import hudson.plugins.emailext.plugins.EmailToken;

Expand Down Expand Up @@ -131,15 +132,15 @@ private String renderTemplate(AbstractBuild<?, ?> build, TaskListener listener,
String result;

Map<String, Object> binding = new HashMap<String, Object>();
ExtendedEmailPublisher publisher = build.getProject().getPublishersList().get(ExtendedEmailPublisher.class);
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);
binding.put("build", build);
binding.put("listener", listener);
binding.put("it", new ScriptContentBuildWrapper(build));
binding.put("rooturl", publisher.getDescriptor().getHudsonUrl());
binding.put("rooturl", descriptor.getHudsonUrl());
binding.put("project", build.getParent());

// we add the binding to the SimpleTemplateEngine instead of the shell
GroovyShell shell = createEngine(publisher, Collections.EMPTY_MAP);
GroovyShell shell = createEngine(descriptor, Collections.EMPTY_MAP);
SimpleTemplateEngine engine = new SimpleTemplateEngine(shell);
try {
result = engine.createTemplate(new InputStreamReader(templateStream)).make(binding).toString();
Expand All @@ -164,14 +165,14 @@ private String executeScript(AbstractBuild<?, ?> build, TaskListener listener, I
throws IOException {
String result = "";
Map binding = new HashMap<String, Object>();
ExtendedEmailPublisher publisher = build.getProject().getPublishersList().get(ExtendedEmailPublisher.class);
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);

binding.put("build", build);
binding.put("it", new ScriptContentBuildWrapper(build));
binding.put("project", build.getParent());
binding.put("rooturl", publisher.getDescriptor().getHudsonUrl());
binding.put("rooturl", descriptor.getHudsonUrl());

GroovyShell shell = createEngine(publisher, binding);
GroovyShell shell = createEngine(descriptor, binding);
Object res = shell.evaluate(new InputStreamReader(scriptStream));
if (res != null) {
result = res.toString();
Expand All @@ -187,7 +188,7 @@ private String executeScript(AbstractBuild<?, ?> build, TaskListener listener, I
* @throws FileNotFoundException
* @throws IOException
*/
private GroovyShell createEngine(ExtendedEmailPublisher publisher, Map<String, Object> variables)
private GroovyShell createEngine(ExtendedEmailPublisherDescriptor descriptor, Map<String, Object> variables)
throws FileNotFoundException, IOException {

ClassLoader cl = Jenkins.getInstance().getPluginManager().uberClassLoader;
Expand All @@ -200,7 +201,7 @@ private GroovyShell createEngine(ExtendedEmailPublisher publisher, Map<String, O
"hudson",
"hudson.model"));

if (publisher.getDescriptor().isSecurityEnabled()) {
if (descriptor.isSecurityEnabled()) {
cc.addCompilationCustomizers(new SandboxTransformer());
sandbox = new ScriptSandbox();
}
Expand Down

0 comments on commit 6fc8eae

Please sign in to comment.