Skip to content

Commit

Permalink
Merge pull request #8 from ikedam/feature/JENKINS-20226_DataBoundCons…
Browse files Browse the repository at this point in the history
…tructor

[FIXED JENKINS-20226] Supports @DataBoundConstructor.
  • Loading branch information
jglick committed Oct 29, 2013
2 parents e779665 + 93ad8a7 commit 9043e7a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
34 changes: 30 additions & 4 deletions src/main/java/hudson/tasks/Mailer.java
Expand Up @@ -40,12 +40,14 @@
import hudson.tasks.i18n.Messages;
import hudson.util.FormValidation;
import hudson.util.Secret;
import hudson.util.XStream2;

import jenkins.model.JenkinsLocationConfiguration;
import org.apache.commons.lang.StringUtils;
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 +103,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 @@ -185,6 +211,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
* @deprecated as of 1.4
* Maintained in {@link JenkinsLocationConfiguration} but left here
* for compatibility just in case, so as not to lose this information.
* This is loaded to {@link JenkinsLocationConfiguration} via the XML file
* marshaled with {@link XStream2}.
*/
private String hudsonUrl;

Expand Down Expand Up @@ -432,10 +460,8 @@ public void setSmtpAuth(String userName, String password) {
}

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

if(hudsonUrl==null) {
// if Hudson URL is not configured yet, infer some default
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>
43 changes: 33 additions & 10 deletions src/test/java/hudson/tasks/MailerTest.java
Expand Up @@ -34,6 +34,8 @@
import javax.mail.Address;
import javax.mail.internet.InternetAddress;

import jenkins.model.JenkinsLocationConfiguration;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;

Expand All @@ -53,8 +55,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 +68,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 Expand Up @@ -117,4 +112,32 @@ public void testGlobalConfigRoundtrip() throws Exception {
assertNull("expected null, got: " + d.getSmtpAuthUserName(), d.getSmtpAuthUserName());
assertNull("expected null, got: " + d.getSmtpAuthPassword(), d.getSmtpAuthPassword());
}

/**
* Simulates {@link JenkinsLocationConfiguration} is not configured.
*/
private static class CleanJenkinsLocationConfiguration extends JenkinsLocationConfiguration {
@Override
public synchronized void load() {
getConfigFile().delete();
super.load();
}
};

/**
* Test {@link JenkinsLocationConfiguration} can load hudsonUrl.
*/
public void testHudsonUrlCompatibility() throws Exception {
// not configured.
assertNull(new CleanJenkinsLocationConfiguration().getUrl());

Mailer m = new Mailer("", true, false);
FreeStyleProject p = createFreeStyleProject();
p.getPublishersList().add(m);
WebClient wc = new WebClient();
submit(wc.getPage(p,"configure").getFormByName("config"));

// configured via the marshaled XML file of Mailer
assertEquals(wc.getContextPath(), new CleanJenkinsLocationConfiguration().getUrl());
}
}

5 comments on commit 9043e7a

@oleg-nenashev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that new release is required

@jglick
Copy link
Member Author

@jglick jglick commented on 9043e7a Oct 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-nenashev yes. Do you want to do some manual sanity checking and cut the release?

@oleg-nenashev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check the master version of plugin tomorrow.
Release should be driven by @slide (according to POM, he is a current maintainer)

@slide
Copy link
Member

@slide slide commented on 9043e7a Oct 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the original work to break Mailer out of core, but I am not the maintainer.

@oleg-nenashev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,
Then I'll test the changes on my installations and release a new version (w/o merging of any new requests including #7 )

Please sign in to comment.