Skip to content
Permalink
Browse files

Fix JENKINS-13848 and JENKINS-13672

  • Loading branch information
slide committed Dec 8, 2012
1 parent d6e323f commit 4c8c49515e780d5abe2a39208d407ba49110d73e
@@ -11,6 +11,7 @@

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -20,6 +21,7 @@

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.MessagingException;
import javax.mail.Multipart;
@@ -71,7 +73,7 @@ public String getName() {
}
}

private List<MimeBodyPart> getAttachments(ExtendedEmailPublisher publisher, final AbstractBuild<?, ?> build, final BuildListener listener)
private List<MimeBodyPart> getAttachments(final AbstractBuild<?, ?> build, final BuildListener listener)
throws MessagingException, InterruptedException, IOException {
List<MimeBodyPart> attachments = null;
FilePath ws = build.getWorkspace();
@@ -83,7 +85,7 @@ public String getName() {
} 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(attachmentsPattern);

for(FilePath file : files) {
if(maxAttachmentSize > 0 &&
@@ -100,7 +102,7 @@ public String getName() {
try {
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(file.getName());
attachments.add(attachmentPart);
attachments.add(attachmentPart);
totalAttachmentSize += file.length();
} catch(MessagingException e) {
listener.getLogger().println("Error adding `" +
@@ -112,12 +114,12 @@ public String getName() {
return attachments;
}

public void attach(Multipart multipart, ExtendedEmailPublisher publisher, AbstractBuild<?,?> build, BuildListener listener) {
public void attach(Multipart multipart, AbstractBuild<?,?> build, BuildListener listener) {
try {
List<MimeBodyPart> attachments = getAttachments(publisher, build, listener);
List<MimeBodyPart> attachments = getAttachments(build, listener);
if(attachments != null) {
for(MimeBodyPart attachment : attachments) {
multipart.addBodyPart(attachment);
for(MimeBodyPart attachment : attachments) {
multipart.addBodyPart(attachment);
}
}
} catch (IOException e) {
@@ -128,4 +130,29 @@ public void attach(Multipart multipart, ExtendedEmailPublisher publisher, Abstra
listener.error("Interrupted in processing attachments: " + e.getMessage());
}
}

/**
* Attaches the build log to the multipart item.
*/
public static void attachBuildLog(Multipart multipart, AbstractBuild<?, ?> build, BuildListener listener) {
try {
File logFile = build.getLogFile();
long maxAttachmentSize =
ExtendedEmailPublisher.DESCRIPTOR.getMaxAttachmentSize();

if(maxAttachmentSize > 0 && logFile.length() >= maxAttachmentSize) {
listener.getLogger().println("Skipping build log attachment - "
+ " too large for maximum attachments size");
return;
}

FileDataSource fileSource = new FileDataSource(logFile);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(fileSource));
attachment.setFileName("build.log");
multipart.addBodyPart(attachment);
} catch (MessagingException e) {
listener.error("Error attaching build log to message: " + e.getMessage());
}
}
}
@@ -45,6 +45,16 @@
*/
private boolean sendToRecipientList;

/**
* Pattern for attachments to be sent as part of this email type.
*/
private String attachmentsPattern;

/**
* True to attach the build log to the email
*/
private boolean attachBuildLog;

public EmailType() {
subject = "";
body = "";
@@ -53,6 +63,8 @@ public EmailType() {
includeCulprits = false;
sendToRecipientList = false;
sendToRequester = false;
attachmentsPattern = "";
attachBuildLog = false;
}

public String getSubject() {
@@ -117,6 +129,22 @@ public void setRecipientList(String recipientList) {
this.recipientList = recipientList.trim();
}

public String getAttachmentsPattern() {
return attachmentsPattern != null ? attachmentsPattern.trim() : attachmentsPattern;
}

public void setAttachmentsPattern(String attachmentsPattern) {
this.attachmentsPattern = attachmentsPattern;
}

public boolean getAttachBuildLog() {
return attachBuildLog;
}

public void setAttachBuildLog(boolean attachBuildLog) {
this.attachBuildLog = attachBuildLog;
}

public Object readResolve() {
if(this.recipientList != null) {
// get rid of PROJECT_DEFAULT_RECIPIENTS stuff
@@ -155,6 +155,11 @@ public static EmailTriggerDescriptor getEmailTriggerType(String mailerId) {
*/
public String presendScript;

/**
* True to attach the log from the build to the email.
*/
public boolean attachBuildLog;

private MatrixTriggerMode matrixTriggerMode;

/**
@@ -375,14 +380,36 @@ private MimeMessage createMail(EmailType type, AbstractBuild<?, ?> build, BuildL
}

// Set the contents of the email
msg.addHeader("X-Jenkins-Job", build.getProject().getDisplayName());
msg.addHeader("X-Jenkins-Result", build.getResult().toString());
msg.setSentDate(new Date());
setSubject(type, build, msg, listener, charset);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(getContent(type, build, msg, listener, charset));
MimeBodyPart msgBody = getContent(type, build, msg, listener, charset);
AttachmentUtils attachments = new AttachmentUtils(attachmentsPattern);
attachments.attach(multipart, this, build, listener);
msg.setContent(multipart);
attachments.attach(multipart, build, listener);

// add attachments from the email type if they are setup
if(StringUtils.isNotBlank(type.getAttachmentsPattern())) {
AttachmentUtils typeAttachments = new AttachmentUtils(type.getAttachmentsPattern());
typeAttachments.attach(multipart, build, listener);
}

if(attachBuildLog || type.getAttachBuildLog()) {
debug(listener.getLogger(), "Request made to attach build log");
AttachmentUtils.attachBuildLog(multipart, build, listener);
}

if(multipart.getCount() > 0) {
debug(listener.getLogger(), "There are attachments, so setting up as a multipart message");
multipart.addBodyPart(msgBody);
msg.setContent(multipart);
} else {
debug(listener.getLogger(), "No attachments, setting content directly (non-MIME message)");
msg.setContent(msgBody.getContent(), msgBody.getContentType());
}

EnvVars env = null;
try {
env = build.getEnvironment(listener);
@@ -274,6 +274,7 @@ public Publisher newInstance(StaplerRequest req, JSONObject formData)
m.defaultContent = formData.getString("project_default_content");
m.attachmentsPattern = formData.getString("project_attachments");
m.presendScript = formData.getString("project_presend_script");
m.attachBuildLog = "true".equalsIgnoreCase(formData.optString("project_attach_buildlog"));
m.configuredTriggers = new ArrayList<EmailTrigger>();

// Create a new email trigger for each one that is configured
@@ -300,6 +301,8 @@ private EmailType createMailType(JSONObject formData, String mailType) {
m.setSendToDevelopers(formData.optBoolean(prefix + "sendToDevelopers"));
m.setSendToRequester(formData.optBoolean(prefix + "sendToRequester"));
m.setIncludeCulprits(formData.optBoolean(prefix + "includeCulprits"));
m.setAttachmentsPattern(formData.getString(prefix + "attachmentsPattern"));
m.setAttachBuildLog(formData.optBoolean(prefix + "attachBuildLog"));
return m;
}

@@ -98,8 +98,11 @@
</j:otherwise>
</j:choose>
</f:entry>



<f:entry title="${%Attach Build Log}"
help="/plugin/email-ext/help/projectConfig/attachBuildLog.html">
<f:checkbox name="project_attach_buildlog" checked="${instance.attachBuildLog}" />
</f:entry>

<!-- This is the help section. It displays a bunch of dynamic help for all content tokens. -->
<tr>
@@ -126,7 +129,6 @@
<td></td>
</tr>


<j:if test="${descriptor.isMatrixProject(it)}">
<f:entry title="${%Trigger for matrix projects}" field="matrixTriggerMode">
<f:enum>${it.description}</f:enum>
@@ -199,11 +201,11 @@
mailTypeObj="${trigger.email}"
sendToList="${trigger.defaultSendToList}"
sendToDevs="${trigger.defaultSendToDevs}"
sendToRequester="${trigger.defaultSendToRequester}"
sendToRequester="${trigger.defaultSendToRequester}"
includeCulps="${trigger.defaultIncludeCulps}"
trigger="${trigger}"
configured="${true}"
secId="${secId}">
secId="${secId}" >
</m:mailtype>
</j:forEach>

@@ -259,7 +261,7 @@
mailTypeObj="${trigger.email}"
sendToList="${trigger.defaultSendToList}"
sendToDevs="${trigger.defaultSendToDevs}"
sendToRequester="${trigger.defaultSendToRequester}"
sendToRequester="${trigger.defaultSendToRequester}"
includeCulps="${trigger.defaultIncludeCulps}"
trigger="${trigger}"
configured="${false}"
@@ -134,6 +134,18 @@
name="mailer_${mailType}_body"
value="${mailTypeObj.body}"/>
</f:entry>
<f:entry
title="${%Attachments}"
help="/plugin/email-ext/help/projectConfig/attachments.html"
description="${%description('http://ant.apache.org/manual/Types/fileset.html')}">
<input class="setting-input"
name="mailer_${mailType}_attachmentsPattern" type="text"
value="${mailTypeObj.attachmentsPattern}" />
</f:entry>
<f:entry title="${%Attach Build Log}"
help="/plugin/email-ext/help/projectConfig/attachBuildLog.html">
<f:checkbox name="mailer_${mailType}_attachmentBuildLog" checked="${mailTypeObj.attachBuildLog}" />
</f:entry>
</table>
</td>
</tr>
@@ -0,0 +1,3 @@
description=Can use wildcards like ''module/dist/**/*.zip''. \
See the <a href="{0}">@includes of Ant fileset</a> for the exact format. \
The base directory is <a href="ws/">the workspace</a>.
@@ -0,0 +1,3 @@
<div>
Check this to enable attaching the log from the build to the email.
</div>

0 comments on commit 4c8c495

Please sign in to comment.
You can’t perform that action at this time.