Skip to content
Permalink
Browse files
Fix JENKINS-14508
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 c9e56e459d6fce1dddad937d10d06b9103b6a97d
Showing with 104 additions and 57 deletions.
  1. +1 −1 src/main/java/hudson/plugins/emailext/AttachmentUtils.java
  2. +10 −8 src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java
  3. +10 −8 src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java
  4. +5 −3 src/main/java/hudson/plugins/emailext/plugins/ContentBuilder.java
  5. +1 −1 src/main/java/hudson/plugins/emailext/{ → plugins}/EmailToken.java
  6. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildIdContent.java
  7. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildLogContent.java
  8. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildLogExcerptContent.java
  9. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildLogMultilineRegexContent.java
  10. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildLogRegexContent.java
  11. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildNumberContent.java
  12. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildStatusContent.java
  13. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/BuildURLContent.java
  14. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/CauseContent.java
  15. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/ChangesSinceLastBuildContent.java
  16. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/ChangesSinceLastSuccessfulBuildContent.java
  17. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/ChangesSinceLastUnstableBuildContent.java
  18. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/FailedTestsContent.java
  19. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/JellyScriptContent.java
  20. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/JenkinsURLContent.java
  21. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/JobDescriptionContent.java
  22. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/ProjectNameContent.java
  23. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/ProjectURLContent.java
  24. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/SVNRevisionContent.java
  25. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/ScriptContent.java
  26. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/TestCountsContent.java
  27. +1 −1 src/main/java/hudson/plugins/emailext/plugins/content/WorkspaceFileContent.java
  28. +5 −1 src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.groovy
  29. +3 −0 src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/global.groovy
  30. +8 −1 src/main/resources/lib/email-ext/tokenhelp.jelly
  31. +15 −0 src/main/webapp/help/globalConfig/defaultPresendScript.html
  32. +24 −12 src/test/java/hudson/plugins/emailext/plugins/ContentBuilderTest.java
@@ -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 &&
@@ -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;
@@ -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;
@@ -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);
@@ -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;
}

@@ -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.
@@ -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
*/
@@ -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 {
@@ -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;

@@ -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;
@@ -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\\}";
@@ -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());
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;

@@ -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;
@@ -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;
@@ -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;
@@ -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;

@@ -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;
@@ -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
@@ -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
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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")
}
}
}
}
@@ -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() {

1 comment on commit c9e56e4

@buildhive
Copy link

@buildhive buildhive commented on c9e56e4 Apr 17, 2013

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.