Skip to content

Commit

Permalink
Fix JENKINS-21861
Browse files Browse the repository at this point in the history
Add check for MatrixBuild
  • Loading branch information
Alex Earl committed Jan 16, 2015
1 parent 9961b79 commit 8a47a78
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 82 deletions.
39 changes: 28 additions & 11 deletions src/main/java/hudson/plugins/emailext/AttachmentUtils.java
Expand Up @@ -2,8 +2,11 @@


import hudson.FilePath; import hudson.FilePath;
import hudson.Launcher; import hudson.Launcher;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.AbstractBuild; import hudson.model.AbstractBuild;
import hudson.model.BuildListener; import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.plugins.emailext.plugins.ContentBuilder; import hudson.plugins.emailext.plugins.ContentBuilder;
import hudson.plugins.emailext.plugins.ZipDataSource; import hudson.plugins.emailext.plugins.ZipDataSource;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -80,22 +83,22 @@ private static class LogFileDataSource implements DataSource {


private static final String DATA_SOURCE_NAME = "build.log"; private static final String DATA_SOURCE_NAME = "build.log";


private final AbstractBuild<?,?> build; private final Run<?,?> run;
private final boolean compress; private final boolean compress;


public LogFileDataSource(AbstractBuild<?,?> build, boolean compress) { public LogFileDataSource(Run<?,?> run, boolean compress) {
this.build = build; this.run = run;
this.compress = compress; this.compress = compress;
} }


public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
InputStream res; InputStream res;
long logFileLength = build.getLogText().length(); long logFileLength = run.getLogText().length();
long pos = 0; long pos = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream(); ByteArrayOutputStream bao = new ByteArrayOutputStream();


while(pos < logFileLength) { while(pos < logFileLength) {
pos = build.getLogText().writeLogTo(pos, bao); pos = run.getLogText().writeLogTo(pos, bao);
} }


res = new ByteArrayInputStream(bao.toByteArray()); res = new ByteArrayInputStream(bao.toByteArray());
Expand All @@ -112,7 +115,7 @@ public OutputStream getOutputStream() throws IOException {


public String getContentType() { public String getContentType() {
return MimetypesFileTypeMap.getDefaultFileTypeMap() return MimetypesFileTypeMap.getDefaultFileTypeMap()
.getContentType(build.getLogFile()); .getContentType(run.getLogFile());
} }


public String getName() { public String getName() {
Expand Down Expand Up @@ -190,9 +193,9 @@ public void attach(Multipart multipart, ExtendedEmailPublisherContext context) {
} }
} }


public static void attachBuildLog(ExtendedEmailPublisherContext context, Multipart multipart, boolean compress) { private static void attachSingleLog(ExtendedEmailPublisherContext context, Run<?,?> run, Multipart multipart, boolean compress) {
try { try {
File logFile = context.getBuild().getLogFile(); File logFile = run.getLogFile();
long maxAttachmentSize = context.getPublisher().getDescriptor().getMaxAttachmentSize(); long maxAttachmentSize = context.getPublisher().getDescriptor().getMaxAttachmentSize();


if (maxAttachmentSize > 0 && logFile.length() >= maxAttachmentSize) { if (maxAttachmentSize > 0 && logFile.length() >= maxAttachmentSize) {
Expand All @@ -207,14 +210,28 @@ public static void attachBuildLog(ExtendedEmailPublisherContext context, Multipa
context.getListener().getLogger().println("Request made to compress build log"); context.getListener().getLogger().println("Request made to compress build log");
} }


fileSource = new LogFileDataSource(context.getBuild(), compress); fileSource = new LogFileDataSource(run, compress);
attachment.setFileName("build." + (compress ? "zip" : "log")); if(run instanceof MatrixRun)
attachment.setFileName("build" + "-" + ((MatrixRun)run).getParent().getCombination().toString('-', '-') + "." + (compress ? "zip" : "log"));
else
attachment.setFileName("build." + (compress ? "zip" : "log"));
attachment.setDataHandler(new DataHandler(fileSource)); attachment.setDataHandler(new DataHandler(fileSource));
multipart.addBodyPart(attachment); multipart.addBodyPart(attachment);
} catch (MessagingException e) { } catch(MessagingException e) {
context.getListener().error("Error attaching build log to message: " + e.getMessage()); context.getListener().error("Error attaching build log to message: " + e.getMessage());
} }
} }

public static void attachBuildLog(ExtendedEmailPublisherContext context, Multipart multipart, boolean compress) {
if(context.getBuild() instanceof MatrixBuild) {
MatrixBuild build = (MatrixBuild)context.getBuild();
for(MatrixRun run : build.getExactRuns()) {
attachSingleLog(context, run, multipart, compress);
}
} else {
attachSingleLog(context, context.getBuild(), multipart, compress);
}
}


@Deprecated @Deprecated
public static void attachBuildLog(ExtendedEmailPublisher publisher, Multipart multipart, AbstractBuild<?, ?> build, BuildListener listener, boolean compress) { public static void attachBuildLog(ExtendedEmailPublisher publisher, Multipart multipart, AbstractBuild<?, ?> build, BuildListener listener, boolean compress) {
Expand Down
Expand Up @@ -4,17 +4,22 @@
import hudson.matrix.AxisList; import hudson.matrix.AxisList;
import hudson.matrix.MatrixBuild; import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject; import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.labels.LabelAtom; import hudson.model.labels.LabelAtom;
import hudson.plugins.emailext.plugins.EmailTrigger; import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.RecipientProvider; import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.trigger.AlwaysTrigger;
import hudson.plugins.emailext.plugins.trigger.PreBuildTrigger; import hudson.plugins.emailext.plugins.trigger.PreBuildTrigger;
import hudson.plugins.emailext.plugins.recipients.ListRecipientProvider;
import hudson.slaves.DumbSlave; import hudson.slaves.DumbSlave;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
Expand All @@ -37,6 +42,7 @@ public void before() throws Throwable {
publisher = new ExtendedEmailPublisher(); publisher = new ExtendedEmailPublisher();
publisher.defaultSubject = "%DEFAULT_SUBJECT"; publisher.defaultSubject = "%DEFAULT_SUBJECT";
publisher.defaultContent = "%DEFAULT_CONTENT"; publisher.defaultContent = "%DEFAULT_CONTENT";
publisher.attachBuildLog = false;


project = createMatrixProject(); project = createMatrixProject();
project.getPublishersList().add( publisher ); project.getPublishersList().add( publisher );
Expand All @@ -63,8 +69,7 @@ public void testPreBuildMatrixBuildSendParentOnly() throws Exception {
addEmailType( trigger ); addEmailType( trigger );
publisher.getConfiguredTriggers().add( trigger ); publisher.getConfiguredTriggers().add( trigger );
MatrixBuild build = project.scheduleBuild2(0).get(); MatrixBuild build = project.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build); j.assertBuildStatusSuccess(build);



assertThat( "Email should have been triggered, so we should see it in the logs.", build.getLog( 100 ), assertThat( "Email should have been triggered, so we should see it in the logs.", build.getLog( 100 ),
hasItems( "Email was triggered for: " + PreBuildTrigger.TRIGGER_NAME ) ); hasItems( "Email was triggered for: " + PreBuildTrigger.TRIGGER_NAME ) );
Expand All @@ -88,7 +93,7 @@ public void testPreBuildMatrixBuildSendSlavesOnly() throws Exception{
} }


@Test @Test
public void testPreBuildMatrixBuildSendSlavesAndParent() throws Exception{ public void testPreBuildMatrixBuildSendSlavesAndParent() throws Exception {
addSlaveToProject(0,1); addSlaveToProject(0,1);
List<RecipientProvider> recProviders = Collections.emptyList(); List<RecipientProvider> recProviders = Collections.emptyList();
publisher.setMatrixTriggerMode(MatrixTriggerMode.BOTH); publisher.setMatrixTriggerMode(MatrixTriggerMode.BOTH);
Expand All @@ -102,6 +107,42 @@ public void testPreBuildMatrixBuildSendSlavesAndParent() throws Exception{
j.assertBuildStatusSuccess(build); j.assertBuildStatusSuccess(build);
assertEquals( 3, Mailbox.get( "solganik@gmail.com" ).size() ); assertEquals( 3, Mailbox.get( "solganik@gmail.com" ).size() );
} }

@Test
public void testAttachBuildLogForAllAxes() throws Exception {
publisher.setMatrixTriggerMode(MatrixTriggerMode.ONLY_PARENT);
publisher.attachBuildLog = true;
addSlaveToProject(0,1,2);
List<RecipientProvider> recProviders = Collections.emptyList();
AlwaysTrigger trigger = new AlwaysTrigger(recProviders, "$DEFAULT_RECIPIENTS",
"$DEFAULT_REPLYTO", "$DEFAULT_SUBJECT", "$DEFAULT_CONTENT", "", 0, "project");
addEmailType( trigger );
publisher.getConfiguredTriggers().add( trigger );
MatrixBuild build = project.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);

assertThat( "Email should have been triggered, so we should see it in the logs.", build.getLog( 100 ),
hasItems( "Email was triggered for: " + AlwaysTrigger.TRIGGER_NAME ) );

assertEquals( 1, Mailbox.get( "solganik@gmail.com" ).size() );

Message msg = Mailbox.get("solganik@gmail.com").get(0);

assertTrue("Message should be multipart", msg instanceof MimeMessage);
assertTrue("Content should be a MimeMultipart", msg.getContent() instanceof MimeMultipart);

MimeMultipart part = (MimeMultipart)msg.getContent();

assertEquals("Should have four body items (message + attachment)", 4, part.getCount());

int i = 1;
for(MatrixRun r : build.getExactRuns()) {
String fileName = "build" + "-" + r.getParent().getCombination().toString('-', '-') + ".log";
BodyPart attach = part.getBodyPart(i);
assertTrue("There should be a log named \"" + fileName + "\" attached", fileName.equalsIgnoreCase(attach.getFileName()));
i++;
}
}


private void addEmailType( EmailTrigger trigger ) { private void addEmailType( EmailTrigger trigger ) {
trigger.setEmail( new EmailType() trigger.setEmail( new EmailType()
Expand Down

0 comments on commit 8a47a78

Please sign in to comment.