Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Partial fix for JENKINS-20319: Configuration slicing resets send sepa…
…rate emails... for Email notification

The configurationslicing plugin now only disables core mailers, if
- the recipient list is set to empty, and the notify individuals... checkbox is unchecked.
- the recipient list is explicitly set to "(Disabled)" (case-insensitive) via the configuration slicer.

Email notifications using the email-ext plugin still suffer from this error.
  • Loading branch information
unknown authored and unknown committed Feb 12, 2015
1 parent cf50253 commit 49f3ca7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
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;
}
}

}

}
7 changes: 7 additions & 0 deletions src/main/java/configurationslicing/email/ExtEmailSlicer.java
Expand Up @@ -105,6 +105,13 @@ public boolean removeMailer(AbstractProject project) throws IOException {
return false;
}
}

/**
* not yet implemented for ExtendedEmailPublisher
*/
public boolean sendToIndividuals(AbstractProject project) {
return false;
}
}

}
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);
}
62 changes: 61 additions & 1 deletion src/test/java/configurationslicing/EmailSlicerTest.java
@@ -1,20 +1,49 @@
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.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 testSendToIndividualsWithCoreMailer() 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"));
}

public void testNormalize() {
doTestNormalize("email@gov.com", "email@gov.com");
Expand Down Expand Up @@ -82,5 +111,36 @@ 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 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;
}
}

0 comments on commit 49f3ca7

Please sign in to comment.