Skip to content

Commit

Permalink
[FIXED JENKINS-20226] Supports @DataBoundConstructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam committed Oct 28, 2013
1 parent e779665 commit 557b3ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
49 changes: 25 additions & 24 deletions src/main/java/hudson/tasks/Mailer.java
Expand Up @@ -46,6 +46,7 @@
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
Expand Down Expand Up @@ -101,11 +102,35 @@ public class Mailer extends Notifier {
*/
public boolean dontNotifyEveryUnstableBuild;

public boolean isNotifyEveryUnstableBuild() {
return !dontNotifyEveryUnstableBuild;
}

/**
* If true, individuals will receive e-mails regarding who broke the build.
*/
public boolean sendToIndividuals;

/**
* Default Constructor.
*
* This is left for backward compatibility.
*/
@Deprecated
public Mailer() {}

/**
* @param recipients
* @param notifyEveryUnstableBuild inverted for historical reasons.
* @param sendToIndividuals
*/
@DataBoundConstructor
public Mailer(String recipients, boolean notifyEveryUnstableBuild, boolean sendToIndividuals) {
this.recipients = recipients;
this.dontNotifyEveryUnstableBuild = !notifyEveryUnstableBuild;
this.sendToIndividuals = sendToIndividuals;
}

@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
if(debug)
Expand Down Expand Up @@ -179,15 +204,6 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
*/
private String defaultSuffix;

/**
* Hudson's own URL, to put into the e-mail.
*
* @deprecated as of 1.4
* Maintained in {@link JenkinsLocationConfiguration} but left here
* for compatibility just in case, so as not to lose this information.
*/
private String hudsonUrl;

/**
* If non-null, use SMTP-AUTH with these information.
*/
Expand Down Expand Up @@ -431,21 +447,6 @@ public void setSmtpAuth(String userName, String password) {
this.smtpAuthPassword = Secret.fromString(password);
}

@Override
public Publisher newInstance(StaplerRequest req, JSONObject formData) {
Mailer m = new Mailer();
req.bindParameters(m,"mailer_");
m.dontNotifyEveryUnstableBuild = req.getParameter("mailer_notifyEveryUnstableBuild")==null;

if(hudsonUrl==null) {
// if Hudson URL is not configured yet, infer some default
hudsonUrl = Functions.inferHudsonURL(req);
save();
}

return m;
}

public FormValidation doAddressCheck(@QueryParameter String value) {
try {
new InternetAddress(value);
Expand Down
14 changes: 6 additions & 8 deletions src/main/resources/hudson/tasks/Mailer/config.jelly
Expand Up @@ -24,15 +24,13 @@ THE SOFTWARE.

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%Recipients}" description="${%description}">
<f:textbox name="mailer_recipients" value="${instance.recipients}"/>
<f:entry field="recipients" title="${%Recipients}" description="${%description}">
<f:textbox />
</f:entry>
<f:entry title="">
<f:checkbox name="mailer_notifyEveryUnstableBuild" checked="${h.defaultToTrue(!instance.dontNotifyEveryUnstableBuild)}"
title="${%Send e-mail for every unstable build}"/>
<f:entry field="notifyEveryUnstableBuild" title="">
<f:checkbox title="${%Send e-mail for every unstable build}" default="true" />
</f:entry>
<f:entry title="" help="/help/tasks/mailer/sendToindividuals.html">
<f:checkbox name="mailer_sendToIndividuals" checked="${instance.sendToIndividuals}"
title="${%Send separate e-mails to individuals who broke the build}"/>
<f:entry field="sendToIndividuals" title="" help="/help/tasks/mailer/sendToindividuals.html">
<f:checkbox title="${%Send separate e-mails to individuals who broke the build}" />
</f:entry>
</j:jelly>
13 changes: 3 additions & 10 deletions src/test/java/hudson/tasks/MailerTest.java
Expand Up @@ -53,8 +53,7 @@ public void testSenderAddress() throws Exception {
// create a project to simulate a build failure
FreeStyleProject project = createFreeStyleProject();
project.getBuildersList().add(new FailureBuilder());
Mailer m = new Mailer();
m.recipients = recipient;
Mailer m = new Mailer(recipient, false, false);
project.getPublishersList().add(m);

project.scheduleBuild2(0).get();
Expand All @@ -67,16 +66,10 @@ public void testSenderAddress() throws Exception {

@Email("http://www.nabble.com/email-recipients-disappear-from-freestyle-job-config-on-save-to25479293.html")
public void testConfigRoundtrip() throws Exception {
Mailer m = new Mailer();
m.recipients = "kk@kohsuke.org";
m.dontNotifyEveryUnstableBuild = true;
m.sendToIndividuals = true;
Mailer m = new Mailer("kk@kohsuke.org", false, true);
verifyRoundtrip(m);

m = new Mailer();
m.recipients = "";
m.dontNotifyEveryUnstableBuild = false;
m.sendToIndividuals = false;
m = new Mailer("", true, false);
verifyRoundtrip(m);
}

Expand Down

0 comments on commit 557b3ec

Please sign in to comment.