Index: src/main/java/hudson/plugins/emailext/plugins/ContentBuilder.java =================================================================== --- src/main/java/hudson/plugins/emailext/plugins/ContentBuilder.java (revision 22240) +++ src/main/java/hudson/plugins/emailext/plugins/ContentBuilder.java (working copy) @@ -1,5 +1,7 @@ package hudson.plugins.emailext.plugins; +import groovy.text.SimpleTemplateEngine; +import groovy.text.Template; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.plugins.emailext.EmailExtException; @@ -66,9 +68,9 @@ .replaceAll(DEFAULT_BODY, Matcher.quoteReplacement(ExtendedEmailPublisher.DESCRIPTOR.getDefaultBody())) .replaceAll(DEFAULT_SUBJECT, Matcher.quoteReplacement(ExtendedEmailPublisher.DESCRIPTOR.getDefaultSubject())); - newText = replaceTokensWithContent(newText, publisher, type, build); - return newText; - } + return type.isScript() ? transformUsingScript(newText, publisher, type, build) + : replaceTokensWithContent(newText, publisher, type, build); + } private static

, B extends AbstractBuild> String replaceTokensWithContent(String origText, ExtendedEmailPublisher publisher, EmailType type, AbstractBuild build) { @@ -192,5 +194,35 @@ } } + + private

, B extends AbstractBuild> + String transformUsingScript(String origText, ExtendedEmailPublisher publisher, EmailType type, B build) { + SimpleTemplateEngine engine = new SimpleTemplateEngine(); + Template template = null; + try { + template = engine.createTemplate(origText); + Map binding = new HashMap(); + Map args = new HashMap(); + //Setting the AbstractBuild as a bind variable + binding.put("build", build); + + //Set all the EmailContent as bind variables to be directly used in the script + for(Map.Entry contentEntry : EMAIL_CONTENT_TYPE_MAP.entrySet()){ + EmailContent content = contentEntry.getValue(); + String contentText = content.getContent(build, publisher, type, args); + if(content.hasNestedContent()){ + contentText = replaceTokensWithContent(contentText, publisher, type, build); + } + binding.put(contentEntry.getKey(), contentText); + } + + return template.make(binding).toString(); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Error creating GroovyTemplate from text ["+origText+"]",e); + //Throwing the exception as Runtime as any exception here would mostly (?) be due to incorrect script + //and not an expected reason + throw new RuntimeException("Error using the template",e); + } + } } Index: src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java =================================================================== --- src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java (revision 22240) +++ src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java (working copy) @@ -123,6 +123,8 @@ */ public String defaultContent; + public boolean defaultContentIsScript; + /** * Get the list of configured email triggers for this project. */ @@ -527,6 +529,7 @@ m.contentType = req.getParameter("project_content_type"); m.defaultSubject = req.getParameter("project_default_subject"); m.defaultContent = req.getParameter("project_default_content"); + m.defaultContentIsScript = req.getParameter("project_default_content_is_script")!=null; m.configuredTriggers = new ArrayList(); // Create a new email trigger for each one that is configured @@ -556,6 +559,7 @@ m.setSendToRecipientList(req.getParameter(prefix + "sendToRecipientList")!=null); m.setSendToDevelopers(req.getParameter(prefix + "sendToDevelopers")!=null); m.setIncludeCulprits(req.getParameter(prefix + "includeCulprits")!=null); + m.setScript(req.getParameter(prefix + "script")!=null); return m; } Index: src/main/java/hudson/plugins/emailext/EmailType.java =================================================================== --- src/main/java/hudson/plugins/emailext/EmailType.java (revision 22240) +++ src/main/java/hudson/plugins/emailext/EmailType.java (working copy) @@ -40,6 +40,11 @@ */ private boolean sendToRecipientList; + /** + * Specifies whether the mail's content should be interpreted as Groovy script + */ + private boolean script; + public EmailType(){ subject = ""; body = ""; @@ -47,6 +52,7 @@ sendToDevelopers = false; includeCulprits = false; sendToRecipientList = false; + script = false; } public String getSubject() { @@ -104,4 +110,11 @@ this.recipientList = recipientList; } + public boolean isScript() { + return script; + } + + public void setScript(boolean script) { + this.script = script; + } } Index: src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.jelly =================================================================== --- src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.jelly (revision 22240) +++ src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.jelly (working copy) @@ -57,6 +57,19 @@ + + + + + + + + + + + Index: src/main/resources/hudson/plugins/emailext/tags/mailtype.jelly =================================================================== --- src/main/resources/hudson/plugins/emailext/tags/mailtype.jelly (revision 22240) +++ src/main/resources/hudson/plugins/emailext/tags/mailtype.jelly (working copy) @@ -122,6 +122,11 @@ name="mailer.${mailType}.body" value="${mailTypeObj.body}"/> + + + @@ -130,4 +135,4 @@ - \ No newline at end of file + Index: src/main/webapp/help/projectConfig/mailType/script.html =================================================================== --- src/main/webapp/help/projectConfig/mailType/script.html (revision 0) +++ src/main/webapp/help/projectConfig/mailType/script.html (revision 0) @@ -0,0 +1,6 @@ +

+ Specifies whether the mail's content should be interpreted as a Groovy + SimpleTemplate script. + If so, the script can access the AbstractBuild object using the + build bind variable. +
Index: pom.xml =================================================================== --- pom.xml (revision 22240) +++ pom.xml (working copy) @@ -27,5 +27,11 @@ 2.4 provided + + + org.codehaus.groovy + groovy-all + 1.5.6 +