Skip to content

Commit

Permalink
Fix JENKINS-14508
Browse files Browse the repository at this point in the history
Added default presend script and DEFAULT_PRESEND_SCRIPT token to be used in the
project settings. Also added ability to use content tokens in pre-send script area
so that the pre-send script could be loaded from the templates area if desired.
  • Loading branch information
slide committed Apr 17, 2013
1 parent 0ee306b commit c9e56e4
Show file tree
Hide file tree
Showing 32 changed files with 104 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/emailext/AttachmentUtils.java
Expand Up @@ -86,7 +86,7 @@ private List<MimeBodyPart> getAttachments(final ExtendedEmailPublisher publisher
} else if(!StringUtils.isBlank(attachmentsPattern)) {
attachments = new ArrayList<MimeBodyPart>();

FilePath[] files = ws.list(new ContentBuilder().transformText(attachmentsPattern, publisher, null, build, listener));
FilePath[] files = ws.list(new ContentBuilder().transformText(attachmentsPattern, publisher, build, listener));

for(FilePath file : files) {
if(maxAttachmentSize > 0 &&
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java
Expand Up @@ -31,6 +31,7 @@

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.model.TaskListener;

import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.control.CompilerConfiguration;
Expand Down Expand Up @@ -381,10 +382,11 @@ private boolean sendMail(EmailType mailType, AbstractBuild<?, ?> build, BuildLis
return false;
}

private boolean executePresendScript(AbstractBuild<?, ?> build, BuildListener listener, MimeMessage msg, EmailTrigger trigger, Map<String, EmailTrigger> triggered)
throws RuntimeException {
private boolean executePresendScript(AbstractBuild<?, ?> build, BuildListener listener, MimeMessage msg, EmailTrigger trigger, Map<String, EmailTrigger> triggered)
throws RuntimeException {
boolean cancel = false;
if(StringUtils.isNotBlank(presendScript)) {
presendScript = new ContentBuilder().transformText(presendScript, this, build, listener);
if (StringUtils.isNotBlank(presendScript)) {
listener.getLogger().println("Executing pre-send script");
ClassLoader cl = Jenkins.getInstance().getPluginManager().uberClassLoader;
ScriptSandbox sandbox = null;
Expand Down Expand Up @@ -472,7 +474,7 @@ private MimeMessage createMail(EmailType type, AbstractBuild<?, ?> build, BuildL

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(getContent(type, build, msg, listener, charset));
multipart.addBodyPart(getContent(type, build, listener, charset));

AttachmentUtils attachments = new AttachmentUtils(attachmentsPattern);
attachments.attach(multipart, this, build, listener);
Expand Down Expand Up @@ -669,13 +671,13 @@ private User getByLegacyUserCause(AbstractBuild<?, ?> build) {

private void setSubject(final EmailType type, final AbstractBuild<?, ?> build, MimeMessage msg, BuildListener listener, String charset)
throws MessagingException {
String subject = new ContentBuilder().transformText(type.getSubject(), this, type, build, listener);
String subject = new ContentBuilder().transformText(type.getSubject(), this, build, listener);
msg.setSubject(subject, charset);
}

private String getRecipientList(final EmailType type, final AbstractBuild<?, ?> build, String recipients, BuildListener listener, String charset)
throws MessagingException {
final String recipientsTransformed = StringUtils.isBlank(recipients) ? "" : new ContentBuilder().transformText(recipients, this, type, build, listener);
final String recipientsTransformed = StringUtils.isBlank(recipients) ? "" : new ContentBuilder().transformText(recipients, this, build, listener);
return recipientsTransformed;
}

Expand All @@ -685,9 +687,9 @@ public boolean isExecuteOnMatrixNodes() {
|| MatrixTriggerMode.ONLY_CONFIGURATIONS == mtm;
}

private MimeBodyPart getContent(final EmailType type, final AbstractBuild<?, ?> build, MimeMessage msg, BuildListener listener, String charset)
private MimeBodyPart getContent(final EmailType type, final AbstractBuild<?, ?> build, BuildListener listener, String charset)
throws MessagingException {
final String text = new ContentBuilder().transformText(type.getBody(), this, type, build, listener);
final String text = new ContentBuilder().transformText(type.getBody(), this, build, listener);

String messageContentType = contentType;
// contentType is null if the project was not reconfigured after upgrading.
Expand Down
Expand Up @@ -88,6 +88,11 @@ public class ExtendedEmailPublisherDescriptor extends BuildStepDescriptor<Publis
*/
private String defaultBody;

/**
* This is the global default pre-send script.
*/
private String defaultPresendScript = "";

/**
* This is the global emergency email address
*/
Expand Down Expand Up @@ -276,15 +281,10 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

public boolean isTokenMacroAvailable() {
boolean result = false;
Plugin tokenMacroPlugin = Jenkins.getInstance().getPlugin("token-macro");
if(tokenMacroPlugin != null) {
result = !tokenMacroPlugin.getWrapper().getVersionNumber().isOlderThan(new VersionNumber("1.5.1"));
}
return result;
public String getDefaultPresendScript() {
return defaultPresendScript;
}

@Override
public Publisher newInstance(StaplerRequest req, JSONObject formData)
throws hudson.model.Descriptor.FormException {
Expand Down Expand Up @@ -389,6 +389,8 @@ public boolean configure(StaplerRequest req, JSONObject formData)
emergencyReroute = nullify(req.getParameter("ext_mailer_emergency_reroute"));
defaultReplyTo = nullify(req.getParameter("ext_mailer_default_replyto")) != null ?
req.getParameter("ext_mailer_default_replyto") : "";
defaultPresendScript = nullify(req.getParameter("ext_mailer_default_presend_script")) != null ?
req.getParameter("ext_mailer_default_presend_script") : "";

debugMode = req.getParameter("ext_mailer_debug_mode") != null;

Expand Down
Expand Up @@ -2,7 +2,6 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.EmailType;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.tasks.Publisher;
Expand All @@ -29,6 +28,7 @@ public class ContentBuilder {
private static final String DEFAULT_SUBJECT = "\\$DEFAULT_SUBJECT|\\$\\{DEFAULT_SUBJECT\\}";
private static final String DEFAULT_RECIPIENTS = "\\$DEFAULT_RECIPIENTS|\\$\\{DEFAULT_RECIPIENTS\\}";
private static final String DEFAULT_REPLYTO = "\\$DEFAULT_REPLYTO|\\$\\{DEFAULT_REPLYTO\\}";
private static final String DEFAULT_PRESEND_SCRIPT = "\\$DEFAULT_PRESEND_SCRIPT|\\$\\{DEFAULT_PRESEND_SCRIPT\\}";
private static final String PROJECT_DEFAULT_BODY = "\\$PROJECT_DEFAULT_CONTENT|\\$\\{PROJECT_DEFAULT_CONTENT\\}";
private static final String PROJECT_DEFAULT_SUBJECT = "\\$PROJECT_DEFAULT_SUBJECT|\\$\\{PROJECT_DEFAULT_SUBJECT\\}";
private static final String PROJECT_DEFAULT_REPLYTO = "\\$PROJECT_DEFAULT_REPLYTO|\\$\\{PROJECT_DEFAULT_REPLYTO\\}";
Expand All @@ -37,22 +37,24 @@ private String noNull(String string) {
return string == null ? "" : string;
}

public String transformText(String origText, ExtendedEmailPublisher publisher, EmailType type, AbstractBuild<?, ?> build, TaskListener listener) {
public String transformText(String origText, ExtendedEmailPublisher publisher, AbstractBuild<?, ?> build, TaskListener listener) {
String defaultContent = Matcher.quoteReplacement(noNull(publisher.defaultContent));
String defaultSubject = Matcher.quoteReplacement(noNull(publisher.defaultSubject));
String defaultReplyTo = Matcher.quoteReplacement(noNull(publisher.replyTo));
String defaultBody = Matcher.quoteReplacement(noNull(ExtendedEmailPublisher.DESCRIPTOR.getDefaultBody()));
String defaultExtSubject = Matcher.quoteReplacement(noNull(ExtendedEmailPublisher.DESCRIPTOR.getDefaultSubject()));
String defaultRecipients = Matcher.quoteReplacement(noNull(ExtendedEmailPublisher.DESCRIPTOR.getDefaultRecipients()));
String defaultExtReplyTo = Matcher.quoteReplacement(noNull(ExtendedEmailPublisher.DESCRIPTOR.getDefaultReplyTo()));
String defaultPresendScript = Matcher.quoteReplacement(noNull(ExtendedEmailPublisher.DESCRIPTOR.getDefaultPresendScript()));
String newText = origText.replaceAll(
PROJECT_DEFAULT_BODY, defaultContent).replaceAll(
PROJECT_DEFAULT_SUBJECT, defaultSubject).replaceAll(
PROJECT_DEFAULT_REPLYTO, defaultReplyTo).replaceAll(
DEFAULT_BODY, defaultBody).replaceAll(
DEFAULT_SUBJECT, defaultExtSubject).replaceAll(
DEFAULT_RECIPIENTS, defaultRecipients).replaceAll(
DEFAULT_REPLYTO, defaultExtReplyTo);
DEFAULT_REPLYTO, defaultExtReplyTo).replaceAll(
DEFAULT_PRESEND_SCRIPT, defaultPresendScript);

try {
newText = TokenMacro.expandAll(build, listener, newText, false, getPrivateMacros());
Expand Down
Expand Up @@ -2,7 +2,7 @@
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hudson.plugins.emailext;
package hudson.plugins.emailext.plugins;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import org.apache.commons.lang.StringEscapeUtils;

import java.io.IOException;
Expand Down
Expand Up @@ -27,7 +27,7 @@
import hudson.console.ConsoleNote;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down
Expand Up @@ -24,7 +24,7 @@
import hudson.console.ConsoleNote;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import org.apache.commons.lang.StringEscapeUtils;

import java.io.BufferedReader;
Expand Down
Expand Up @@ -26,7 +26,7 @@
import hudson.console.ConsoleNote;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;

import org.apache.commons.lang.StringEscapeUtils;

Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;

import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
Expand Down
Expand Up @@ -4,7 +4,7 @@
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;

import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
Expand Down
Expand Up @@ -4,7 +4,7 @@
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;

Expand Down
Expand Up @@ -4,7 +4,7 @@
import hudson.model.AbstractProject;
import hudson.model.AbstractBuild.DependencyChange;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.Util;
import hudson.scm.ChangeLogSet;
import java.io.IOException;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Result;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;

@EmailToken
public class ChangesSinceLastSuccessfulBuildContent
Expand Down
Expand Up @@ -3,7 +3,7 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Result;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;

@EmailToken
public class ChangesSinceLastUnstableBuildContent
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.AbstractTestResultAction;
import java.io.IOException;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.tasks.Mailer;
import org.apache.commons.io.IOUtils;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import java.io.IOException;
import jenkins.model.Jenkins;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;

import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;

import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
Expand Down
Expand Up @@ -4,7 +4,7 @@
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;

import java.io.IOException;
import java.util.Map;
Expand Down
Expand Up @@ -9,7 +9,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.ScriptSandbox;
import hudson.plugins.emailext.plugins.ContentBuilder;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.tasks.test.AbstractTestResultAction;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.EmailToken;
import hudson.plugins.emailext.plugins.EmailToken;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
Expand Down
Expand Up @@ -110,7 +110,11 @@ f.advanced() {
tr() {
td(colspan: "4") {
f.entry(title: _("Pre-send Script"), help: "/plugin/email-ext/help/projectConfig/presendScript.html") {
f.textarea(id: "project_presend_script", name: "project_presend_script", value: instance?.presendScript, class: "setting-input")
if(instance?.configured) {
f.textarea(id: "project_presend_script", name: "project_presend_script", value: instance.presendScript, class: "setting-input")
} else {
f.textarea(id: "project_presend_script", name: "project_presend_script", value: "\$DEFAULT_PRESEND_SCRIPT", class: "setting-input")
}
}
}
}
Expand Down
Expand Up @@ -79,6 +79,9 @@ f.section(title: _("Extended E-mail Notification")) {
f.entry(help: "/plugin/email-ext/help/globalConfig/defaultBody.html", title: _("Default Content")) {
f.textarea(class: "setting-input", value: descriptor.defaultBody, name: "ext_mailer_default_body")
}
f.entry(help: "/plugin/email-ext/help/globalConfig/defaultPresendScript.html", title: _("Default Pre-send Script")) {
f.textarea(class: "setting-input", value: descriptor.defaultPresendScript, name: "ext_mailer_default_presend_script")
}
f.optionalBlock(help: "/plugin/email-ext/help/globalConfig/debugMode.html", checked: descriptor.isDebugMode(), name: "ext_mailer_debug_mode", title: _("Enable Debug Mode"))
f.optionalBlock(help: "/plugin/email-ext/help/globalConfig/security.html", checked: descriptor.isSecurityEnabled(), name: "ext_mailer_security_enabled", title: _("Enable Security"))
tr() {
Expand Down

1 comment on commit c9e56e4

@buildhive
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jenkins » email-ext-plugin #134 UNSTABLE
Looks like this commit caused a build failure
(what's this?)

Please sign in to comment.