Skip to content

Commit

Permalink
Merge pull request #14 from mkutter/master
Browse files Browse the repository at this point in the history
Fix for JENKINS-20319
  • Loading branch information
ninian committed Mar 2, 2015
2 parents cf50253 + bbdaff0 commit c0d1e56
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 12 deletions.
7 changes: 3 additions & 4 deletions pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.424.6</version>
<version>1.509.2</version>
</parent>
<artifactId>configurationslicing</artifactId>
<packaging>hpi</packaging>
Expand Down Expand Up @@ -39,10 +39,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>email-ext</artifactId>
<version>2.7</version>
<scope>compile</scope>
<version>2.29</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
Expand Up @@ -16,6 +16,7 @@
public abstract class AbstractEmailSliceSpec extends UnorderedStringSlicerSpec<AbstractProject<?, ?>> {

public static final String DISABLED = "(Disabled)";
private static final String EMPTY = "";

private String joinString;
private String name;
Expand All @@ -32,7 +33,12 @@ public List<String> getValues(AbstractProject<?, ?> project) {
String recipients = handler.getRecipients(project);
recipients = normalize(recipients, "\n");
if (recipients == null) {
recipients = DISABLED;
if (handler.sendToIndividuals(project)) {
recipients = EMPTY;
}
else {
recipients = DISABLED;
}
}
List<String> values = new ArrayList<String>();
values.add(recipients);
Expand All @@ -41,8 +47,8 @@ public List<String> getValues(AbstractProject<?, ?> project) {
public boolean setValues(AbstractProject<?, ?> project, List<String> set) {
String newEmail = join(set);

// if no email is present, we're going to assume that's the same as disabled
boolean disabled = (DISABLED.equals(newEmail) || newEmail == null);
// only regard explicit (disabled) [regardless of case]
boolean disabled = (newEmail == null) ? false : (DISABLED.toLowerCase().equals(newEmail.toLowerCase()));
boolean saved = false;
ProjectHandler handler = getProjectHandler(project);

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/configurationslicing/email/CoreEmailSlicer.java
Expand Up @@ -86,6 +86,15 @@ public boolean removeMailer(AbstractProject project) throws IOException {
return false;
}
}
public boolean sendToIndividuals(AbstractProject project) {
Mailer mailer = getMailer(project);
if (mailer != null) {
return mailer.sendToIndividuals;
}
else {
return false;
}
}
}
@SuppressWarnings("unchecked")
private static class MavenEmailProjectHandler implements ProjectHandler {
Expand Down Expand Up @@ -136,6 +145,16 @@ public boolean removeMailer(AbstractProject project) throws IOException {
return false;
}
}
public boolean sendToIndividuals(AbstractProject project) {
MavenMailer mailer = getMailer(project);
if (mailer != null) {
return mailer.sendToIndividuals;
}
else {
return false;
}
}

}

}
20 changes: 20 additions & 0 deletions src/main/java/configurationslicing/email/ExtEmailSlicer.java
Expand Up @@ -6,12 +6,15 @@
import hudson.model.Hudson;
import hudson.plugins.emailext.EmailType;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.EmailTriggerDescriptor;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.tasks.Publisher;
import hudson.util.DescribableList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -105,6 +108,23 @@ public boolean removeMailer(AbstractProject project) throws IOException {
return false;
}
}

/**
* not yet implemented for ExtendedEmailPublisher
*/
public boolean sendToIndividuals(AbstractProject project) {
boolean result = false;
ExtendedEmailPublisher mailer = getMailer(project);
if (mailer != null) {
for (EmailTrigger trigger : mailer.getConfiguredTriggers()) {
if (trigger.getEmail().getSendToDevelopers()) {
result = true;
break;
}
}
}
return result;
}
}

}
Expand Up @@ -11,5 +11,5 @@ public interface ProjectHandler {
boolean removeMailer(AbstractProject project) throws IOException;
boolean addMailer(AbstractProject project) throws IOException;
boolean setRecipients(AbstractProject project, String recipients) throws IOException;

boolean sendToIndividuals(AbstractProject project);
}
103 changes: 99 additions & 4 deletions src/test/java/configurationslicing/EmailSlicerTest.java
@@ -1,20 +1,55 @@
package configurationslicing;


import hudson.maven.MavenReporter;
import hudson.maven.MavenModuleSet;
import hudson.maven.reporters.MavenMailer;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.FreeStyleProject;
import hudson.plugins.emailext.EmailType;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.tasks.Publisher;
import hudson.tasks.Mailer;
import hudson.util.DescribableList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jvnet.hudson.test.HudsonTestCase;

import configurationslicing.email.AbstractEmailSliceSpec;
import configurationslicing.email.ExtEmailSlicer;
import configurationslicing.email.CoreEmailSlicer.CoreEmailSliceSpec;
import configurationslicing.email.ExtEmailSlicer;
import configurationslicing.email.ExtEmailSlicer.ExtEmailSliceSpec;

public class EmailSlicerTest extends HudsonTestCase {

/**
* Given a project with a core mailer with mailer.sendToIndividuals == true, setting recipients with a slicer must
* <ul>
* <li>return null when the recipient list is set to empty (null)</li>
* <li>return "(Disabled)" when the recipient list is set to (disabled) (in any case, even "(dISABLED)")</li>
* <li>return the set list of recipients (like "john@doe.com sue@gov.com") otherwise</li>
* @throws Exception
*/
public void testSendToIndividuals() throws Exception {
assertEquals("Setting empty recipients with sendToIndividuals enabled", null, setAndGetCoreValues(createMavenProjectWithSendToIndividualsAndEmptyRecipients(), null));
assertEquals("Setting >(disabled)< with sendToIndividuals enabled", "(Disabled)", setAndGetCoreValues(createMavenProjectWithSendToIndividualsAndEmptyRecipients(), "(disabled)"));
assertEquals("Setting >(DISABLED)< with sendToIndividuals enabled", "(Disabled)", setAndGetCoreValues(createMavenProjectWithSendToIndividualsAndEmptyRecipients(), "(DISABLED)"));
assertEquals("Setting recipients with sendToIndividuals enabled", "john@doe.com sue@gov.com", setAndGetCoreValues(createMavenProjectWithSendToIndividualsAndEmptyRecipients(), "john@doe.com sue@gov.com"));

assertEquals("Setting empty recipients with sendToIndividuals enabled", null, setAndGetCoreValues(createFreestyleProjectWithSendToIndividualsAndEmptyRecipients(), null));
assertEquals("Setting >(disabled)< with sendToIndividuals enabled", "(Disabled)", setAndGetCoreValues(createFreestyleProjectWithSendToIndividualsAndEmptyRecipients(), "(disabled)"));
assertEquals("Setting >(DISABLED)< with sendToIndividuals enabled", "(Disabled)", setAndGetCoreValues(createFreestyleProjectWithSendToIndividualsAndEmptyRecipients(), "(DISABLED)"));
assertEquals("Setting recipients with sendToIndividuals enabled", "john@doe.com sue@gov.com", setAndGetCoreValues(createFreestyleProjectWithSendToIndividualsAndEmptyRecipients(), "john@doe.com sue@gov.com"));

assertEquals("Setting empty recipients with ext mailer", null, setAndGetExtValues(createFreestyleProjectWithExtMailer(), null));
assertEquals("Setting >(disabled)< with ext mailer", "(Disabled)", setAndGetExtValues(createFreestyleProjectWithExtMailer(), "(disabled)"));
}

public void testNormalize() {
doTestNormalize("email@gov.com", "email@gov.com");
Expand All @@ -27,9 +62,7 @@ private void doTestNormalize(String email, String expect) {
assertEquals(expect, normalized);
}

public void testSetValues() throws Exception {
doTestSetValues("(Disabled)", "");
doTestSetValues("(Disabled)", " \b\t ");
public void testSetValues() throws Exception {
doTestSetValues("(Disabled)", "(Disabled)");
doTestSetValues("caps@gov email@gov.com", "email@gov.com, CAPS@gov");
}
Expand Down Expand Up @@ -82,5 +115,67 @@ public void testCommonValues() {
values = slice.getConfiguredValues();
assertEquals(4, values.size());
}

private String setAndGetCoreValues(AbstractProject<?,?> project, String valuesString) {
CoreEmailSliceSpec spec = new CoreEmailSliceSpec();

List<String> values = new ArrayList<String>();
values.add(valuesString);
spec.setValues(project, values);

List<String> gotList = spec.getValues(project);
String got = spec.join(gotList);
return got;
}

private String setAndGetExtValues(AbstractProject<?,?> project, String valuesString) {
ExtEmailSliceSpec spec = new ExtEmailSliceSpec();

List<String> values = new ArrayList<String>();
values.add(valuesString);
spec.setValues(project, values);

List<String> gotList = spec.getValues(project);
String got = spec.join(gotList);
return got;
}

private MavenModuleSet createMavenProjectWithSendToIndividualsAndEmptyRecipients()
throws IOException {
MavenModuleSet mavenProject = createMavenProject();
MavenMailer mailer = new MavenMailer();
mailer.sendToIndividuals = true;
DescribableList<MavenReporter,Descriptor<MavenReporter>> reporters = mavenProject.getReporters();
reporters.add(mailer);
return mavenProject;
}

private AbstractProject<?,?> createFreestyleProjectWithSendToIndividualsAndEmptyRecipients()
throws IOException {
FreeStyleProject project = createFreeStyleProject();
Mailer mailer = new Mailer();
mailer.sendToIndividuals = true;
DescribableList<Publisher,Descriptor<Publisher>> publishers = project.getPublishersList();
publishers.add(mailer);
return project;
}

private AbstractProject<?,?> createFreestyleProjectWithExtMailer() throws IOException {
FreeStyleProject project = createFreeStyleProject();
DescribableList<Publisher,Descriptor<Publisher>> publishers = project.getPublishersList();
ExtendedEmailPublisher publisher = new ExtendedEmailPublisher();
FailureTrigger trigger = new FailureTrigger();
EmailType email = new EmailType();
email.setSendToDevelopers(true);
email.setSendToRecipientList(true);
trigger.setEmail(email);
publisher.getConfiguredTriggers().add(trigger);

// there is no way to get this text from the plugin itself
publisher.defaultContent = "$DEFAULT_CONTENT";
publisher.defaultSubject = "$DEFAULT_SUBJECT";

publishers.add(publisher);
return project;
}
}

0 comments on commit c0d1e56

Please sign in to comment.