Skip to content
Permalink
Browse files

JENKINS-42140 Allow using 'emailext' step in pipeline without 'node'/…

…workspace context
  • Loading branch information...
davidvanlaatum committed Feb 18, 2017
1 parent 422c46c commit 5c81b10bf56d5a399507d95be581eef7668a31ce
50 pom.xml
@@ -49,31 +49,49 @@
<role>maintainer</role>
</roles>
</developer>
<developer>
<id>slide</id>
</developers>

<contributors>
<contributor>
<!--<id>slide</id>-->
<name>Alex Earl</name>
<email>slide.o.mix@gmail.com</email>
</developer>
<developer>
<id>ashlux</id>
<roles>
<role>Past Maintainer</role>
</roles>
</contributor>
<contributor>
<!--<id>ashlux</id>-->
<name>Ash Lux</name>
<email>ashlux@gmail.com</email>
</developer>
<developer>
<id>kdsweeney</id>
<roles>
<role>Past Maintainer</role>
</roles>
</contributor>
<contributor>
<!--<id>kdsweeney</id>-->
<name>Kyle Sweeney</name>
</developer>
<developer>
<id>krwalker</id>
<roles>
<role>Past Maintainer</role>
</roles>
</contributor>
<contributor>
<!--<id>krwalker</id>-->
<name>K. R. Walker</name>
<email>krwalker@stellarscience.com</email>
</developer>
<developer>
<id>ssogabe</id>
<roles>
<role>Past Maintainer</role>
</roles>
</contributor>
<contributor>
<!--<id>ssogabe</id>-->
<name>Seiji Sogabe</name>
<email>s.sogabe@gmail.com</email>
</developer>
</developers>
<roles>
<role>Past Maintainer</role>
</roles>
</contributor>
</contributors>

<dependencies>
<!-- libraries -->
@@ -130,35 +130,37 @@ public String getName() {
FilePath ws = context.getWorkspace();
long totalAttachmentSize = 0;
long maxAttachmentSize = context.getPublisher().getDescriptor().getMaxAttachmentSize();
if (ws == null) {
context.getListener().error("Error: No workspace found!");
} else if (!StringUtils.isBlank(attachmentsPattern)) {
attachments = new ArrayList<>();

FilePath[] files = ws.list(ContentBuilder.transformText(attachmentsPattern, context, null));

for (FilePath file : files) {
if (maxAttachmentSize > 0
&& totalAttachmentSize + file.length() >= maxAttachmentSize) {
context.getListener().getLogger().println("Skipping `" + file.getName()
+ "' (" + file.length()
+ " bytes) - too large for maximum attachments size");
continue;
}

MimeBodyPart attachmentPart = new MimeBodyPart();
FilePathDataSource fileDataSource = new FilePathDataSource(file);

try {
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(MimeUtility.encodeText(file.getName()));
attachmentPart.setContentID(String.format("<%s>", file.getName()));
attachments.add(attachmentPart);
totalAttachmentSize += file.length();
} catch (MessagingException e) {
context.getListener().getLogger().println("Error adding `"
+ file.getName() + "' as attachment - "
+ e.getMessage());
if (!StringUtils.isBlank(attachmentsPattern)) {
if (ws == null) {
context.getListener().error("Error: No workspace found!");
} else {
attachments = new ArrayList<>();

FilePath[] files = ws.list(ContentBuilder.transformText(attachmentsPattern, context, null));

for (FilePath file : files) {
if (maxAttachmentSize > 0
&& totalAttachmentSize + file.length() >= maxAttachmentSize) {
context.getListener().getLogger().println("Skipping `" + file.getName()
+ "' (" + file.length()
+ " bytes) - too large for maximum attachments size");
continue;
}

MimeBodyPart attachmentPart = new MimeBodyPart();
FilePathDataSource fileDataSource = new FilePathDataSource(file);

try {
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(MimeUtility.encodeText(file.getName()));
attachmentPart.setContentID(String.format("<%s>", file.getName()));
attachments.add(attachmentPart);
totalAttachmentSize += file.length();
} catch (MessagingException e) {
context.getListener().getLogger().println("Error adding `"
+ file.getName() + "' as attachment - "
+ e.getMessage());
}
}
}
}
@@ -136,15 +136,9 @@ public void setRecipientProviders(List<RecipientProvider> recipientProviders) {
@StepContextParameter
private transient TaskListener listener;

@StepContextParameter
private transient Launcher launcher;

@StepContextParameter
private transient Run<?,?> run;

@StepContextParameter
private transient FilePath workspace;

@Override
protected Void run() throws Exception {
ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();
@@ -181,7 +175,8 @@ protected Void run() throws Exception {
publisher.contentType = step.mimeType;
}

final ExtendedEmailPublisherContext ctx = new ExtendedEmailPublisherContext(publisher, run, workspace, launcher, listener);
final ExtendedEmailPublisherContext ctx = new ExtendedEmailPublisherContext(publisher, run,
getContext().get(FilePath.class), getContext().get(Launcher.class), listener);
final Multimap<String, EmailTrigger> triggered = ArrayListMultimap.create();
triggered.put(AlwaysTrigger.TRIGGER_NAME, publisher.configuredTriggers.get(0));
ctx.setTrigger(publisher.configuredTriggers.get(0));
@@ -747,9 +747,14 @@ private Multipart addContent(ExtendedEmailPublisherContext context, String chars
extension = ".txt";
}

FilePath savedOutput = new FilePath(context.getWorkspace(),
String.format("%s-%s%s", context.getTrigger().getDescriptor().getDisplayName(), context.getRun().getId(), extension));
savedOutput.write(text, charset);
FilePath workspace = context.getWorkspace();
if (workspace != null) {
FilePath savedOutput = new FilePath(workspace,
String.format("%s-%s%s", context.getTrigger().getDescriptor().getDisplayName(), context.getRun().getId(), extension));
savedOutput.write(text, charset);
} else {
context.getListener().getLogger().println("No workspace to save the email to");
}
}
} catch (IOException | InterruptedException e) {
context.getListener().getLogger().println("Error trying to save email output to file. " + e.getMessage());

0 comments on commit 5c81b10

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