Skip to content
Permalink
Browse files

Extended pipeline support (#135)

JENKINS-35365 EMail-Ext Extended Pipeline Support - Recipient Providers
  • Loading branch information
davidvanlaatum committed Jun 13, 2016
1 parent b7bd1b3 commit cedf5e8fac56aa3d6b9c4392ad181b9af8ca3406
Showing with 619 additions and 78 deletions.
  1. +34 −7 pom.xml
  2. +99 −0 src/main/java/hudson/plugins/emailext/EmailExtRecipientStep.java
  3. +27 −3 src/main/java/hudson/plugins/emailext/EmailExtStep.java
  4. +40 −0 src/main/java/hudson/plugins/emailext/plugins/RecipientProvider.java
  5. +5 −0 src/main/java/hudson/plugins/emailext/plugins/RecipientProviderDescriptor.java
  6. +32 −12 src/main/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProvider.java
  7. +19 −27 src/main/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProvider.java
  8. +6 −5 ...n/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProvider.java
  9. +6 −0 src/main/java/hudson/plugins/emailext/plugins/recipients/ListRecipientProvider.java
  10. +30 −6 src/main/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilities.java
  11. +27 −0 src/main/resources/hudson/plugins/emailext/EmailExtRecipientStep/config.jelly
  12. +4 −1 src/main/resources/hudson/plugins/emailext/EmailExtStep/config.jelly
  13. +1 −1 src/test/java/hudson/plugins/emailext/EmailTypeTest.java
  14. +54 −0 src/test/java/hudson/plugins/emailext/plugins/RecipientProviderTest.java
  15. +0 −5 src/test/java/hudson/plugins/emailext/plugins/content/ScriptContentTest.java
  16. +69 −0 src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java
  17. +61 −0 src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java
  18. +14 −3 src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java
  19. +82 −0 src/test/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilitiesTest.java
  20. +9 −8 src/test/java/hudson/plugins/emailext/plugins/recipients/TestUtilities.java
41 pom.xml
@@ -5,6 +5,7 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.3</version>
<relativePath/>
</parent>

<licenses>
@@ -19,14 +20,15 @@
<packaging>hpi</packaging>
<version>2.44-SNAPSHOT</version>
<name>Email Extension Plugin</name>
<description>EMail Extension Plugin</description>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin</url>
<issueManagement>
<system>JIRA</system>
<url>http://issues.jenkins-ci.org/secure/IssueNavigator.jspa?reset=true&amp;jqlQuery=project+%3D+JENKINS+AND+status+in+%28Open%2C+%22In+Progress%22%2C+Reopened%29+AND+component+%3D+%27email-ext-plugin%27</url>
</issueManagement>

<properties>
<powermock.version>1.4.12</powermock.version>
<powermock.version>1.6.4</powermock.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<workflow.version>1.10</workflow.version>
<jenkins.version>1.609.2</jenkins.version>
@@ -38,6 +40,7 @@
<id>davidvanlaatum</id>
<name>David van Laatum</name>
<timezone>9.5</timezone>
<email>david@vanlaatum.id.au</email>
<roles>
<role>developer</role>
<role>maintainer</role>
@@ -129,6 +132,12 @@
<artifactId>workflow-step-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
@@ -153,13 +162,25 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -187,6 +208,12 @@
<version>${jenkins.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>


<!-- static analysys -->
@@ -226,13 +253,13 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-XX:MaxPermSize=512m -Dfile.encoding=${project.build.sourceEncoding}</argLine>
<argLine>-XX:MaxPermSize=512m -Dfile.encoding=${project.build.sourceEncoding} ${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>clirr-maven-plugin</artifactId>
<version>2.6.1</version>
<version>2.7</version>
<executions>
<execution>
<goals>
@@ -249,7 +276,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.1</version>
<version>3.0.3</version>
<configuration>
<xmlOutput>true</xmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
@@ -272,7 +299,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>clirr-maven-plugin</artifactId>
<version>2.6.1</version>
<version>2.7</version>
<configuration>
<logResults>true</logResults>
<xmlOutputFile>${project.build.directory}/clirr-report.xml</xmlOutputFile>
@@ -0,0 +1,99 @@
package hudson.plugins.emailext;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.RecipientProviderDescriptor;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.inject.Inject;
import javax.mail.internet.InternetAddress;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class EmailExtRecipientStep extends AbstractStepImpl {
private List<RecipientProvider> recipientProviders;

@DataBoundConstructor
public EmailExtRecipientStep(List<RecipientProvider> recipientProviders) {
this.recipientProviders = recipientProviders;
}

public List<RecipientProvider> getRecipientProviders() {
return recipientProviders;
}

@SuppressWarnings("WeakerAccess")
public static class Executor extends AbstractSynchronousNonBlockingStepExecution<String> {
private static final long serialVersionUID = 1L;

@Inject
private transient EmailExtRecipientStep step;

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

@StepContextParameter
private transient TaskListener listener;

@StepContextParameter
private transient EnvVars env;

@Override
protected String run() throws Exception {
if (step.recipientProviders == null || step.recipientProviders.isEmpty()) {
throw new IllegalArgumentException("You must provide at least one recipient provider");
}
ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();
ExtendedEmailPublisherContext context = new ExtendedEmailPublisherContext(publisher, run, null, null, listener);
Set<InternetAddress> to = new HashSet<>();
RecipientProvider.checkAllSupport(step.recipientProviders, run.getParent().getClass());
for (RecipientProvider provider : step.recipientProviders) {
provider.addRecipients(context, env, to, to, to);
}

StringBuilder rt = new StringBuilder();

Iterator<InternetAddress> iterator = to.iterator();
while (iterator.hasNext()) {
rt.append(iterator.next().toString());
if (iterator.hasNext()) {
rt.append(" ");
}
}

return rt.toString();
}
}

@Extension(optional = true)
public static final class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(Executor.class);
}

@Override
public String getFunctionName() {
return "emailextrecipients";
}

@Override
public String getDisplayName() {
return "Extended Email Recipients";
}

@SuppressWarnings("unused")
public List<RecipientProviderDescriptor> getRecipientProvidersDescriptors() {
return RecipientProvider.allSupporting(WorkflowJob.class);
}
}
}
@@ -6,13 +6,16 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.RecipientProviderDescriptor;
import hudson.plugins.emailext.plugins.trigger.AlwaysTrigger;
import jenkins.model.Jenkins;
import hudson.Util;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
@@ -21,7 +24,7 @@
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.CheckForNull;
import javax.management.Descriptor;
import java.util.List;

/**
* Created by acearl on 9/14/2015.
@@ -49,6 +52,8 @@

private boolean compressLog;

private List<RecipientProvider> recipientProviders;

@DataBoundConstructor
public EmailExtStep(String subject, String body) {
this.subject = subject;
@@ -111,6 +116,15 @@ public void setCompressLog(boolean compressLog) {
this.compressLog = compressLog;
}

@DataBoundSetter
public void setRecipientProviders(List<RecipientProvider> recipientProviders) {
this.recipientProviders = recipientProviders;
}

public List<? extends RecipientProvider> getRecipientProviders() {
return recipientProviders;
}

public static class EmailExtStepExecution extends AbstractSynchronousNonBlockingStepExecution<Void> {

private static final long serialVersionUID = 1L;
@@ -136,7 +150,12 @@ protected Void run() throws Exception {
publisher.configuredTriggers.clear();

AlwaysTrigger.DescriptorImpl descriptor = Jenkins.getActiveInstance().getDescriptorByType(AlwaysTrigger.DescriptorImpl.class);
publisher.configuredTriggers.add(descriptor.createDefault());
EmailTrigger trigger = descriptor.createDefault();
if (step.recipientProviders != null) {
RecipientProvider.checkAllSupport(step.recipientProviders, run.getParent().getClass());
trigger.getEmail().addRecipientProviders(step.recipientProviders);
}
publisher.configuredTriggers.add(trigger);

publisher.defaultSubject = step.subject;
publisher.defaultContent = step.body;
@@ -188,5 +207,10 @@ public String getFunctionName() {
public String getDisplayName() {
return "Extended Email";
}

@SuppressWarnings("unused")
public List<RecipientProviderDescriptor> getRecipientProvidersDescriptors() {
return RecipientProvider.allSupporting(WorkflowJob.class);
}
}
}
@@ -2,24 +2,64 @@

import hudson.EnvVars;
import hudson.ExtensionPoint;
import hudson.model.Job;
import hudson.plugins.emailext.ExtendedEmailPublisherContext;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.mail.internet.InternetAddress;

import hudson.DescriptorExtensionList;
import hudson.model.AbstractDescribableImpl;
import jenkins.model.Jenkins;
import org.jsoup.helper.StringUtil;

/**
* Created by acearl on 12/24/13.
*/
public abstract class RecipientProvider extends AbstractDescribableImpl<RecipientProvider> implements ExtensionPoint {
private static final Logger LOG = Logger.getLogger(RecipientProvider.class.getName());

public static DescriptorExtensionList<RecipientProvider, RecipientProviderDescriptor> all() {
return Jenkins.getActiveInstance().getDescriptorList(RecipientProvider.class);
}

public static List<RecipientProviderDescriptor> allSupporting(Class<? extends Job> clazz) {
List<RecipientProviderDescriptor> rt = new ArrayList<>();
for (RecipientProviderDescriptor recipientProviderDescriptor : all()) {
try {
if (recipientProviderDescriptor.isApplicable(clazz)) {
rt.add(recipientProviderDescriptor);
}
} catch (Exception ex) {
LOG.log(Level.WARNING, MessageFormat.format("Exception checking if {0} supports {1}, skipping",
recipientProviderDescriptor.getDisplayName(), clazz.getName()), ex);
}
}
return rt;
}

public static void checkAllSupport(@Nonnull List<? extends RecipientProvider> providers, Class<? extends Job> clazz) {
Set<String> notSupported = new TreeSet<>();
for (RecipientProvider provider : providers) {
if (!provider.getDescriptor().isApplicable(clazz)) {
notSupported.add(provider.getClass().getName());
}
}

if (!notSupported.isEmpty()) {
throw new IllegalArgumentException(MessageFormat.
format("The following recipient providers do not support {0} {1}", clazz.getName(),
StringUtil.join(notSupported, ", ")));
}
}

@Override
public RecipientProviderDescriptor getDescriptor() {
return (RecipientProviderDescriptor) super.getDescriptor();
@@ -1,6 +1,7 @@
package hudson.plugins.emailext.plugins;

import hudson.model.Descriptor;
import hudson.model.Job;

/**
* @author acearl
@@ -9,4 +10,8 @@

protected RecipientProviderDescriptor() {
}

public boolean isApplicable(Class<? extends Job> jobType) {
return true;
}
}

0 comments on commit cedf5e8

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