Skip to content

Commit

Permalink
Fix JENKINS-20265
Browse files Browse the repository at this point in the history
Added new content TRIGGER_NAME that will be replaced with the name of the
trigger that caused the email.
  • Loading branch information
slide committed Nov 7, 2013
1 parent a42c315 commit e2b1e77
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
@@ -1,12 +1,14 @@
package hudson.plugins.emailext.plugins.content;

import com.google.common.collect.ListMultimap;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import java.util.Map;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;

public class TriggerNameContent extends DataBoundTokenMacro {
public class TriggerNameContent extends TokenMacro {
private static final String MACRO_NAME = "TRIGGER_NAME";
private final String name;

Expand All @@ -20,8 +22,7 @@ public boolean acceptsMacroName(String macroName) {
}

@Override
public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
public String evaluate(AbstractBuild<?, ?> ab, TaskListener tl, String string, Map<String, String> map, ListMultimap<String, String> lm) throws MacroEvaluationException, IOException, InterruptedException {
return name;
}
}
@@ -0,0 +1,82 @@
package hudson.plugins.emailext.plugins.content;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.plugins.emailext.EmailType;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.trigger.PreBuildTrigger;
import hudson.plugins.emailext.plugins.trigger.SuccessTrigger;
import javax.mail.Message;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.matchers.JUnitMatchers.hasItems;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.mock_javamail.Mailbox;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author acearl
*/
public class TriggerNameContentTest {
private ExtendedEmailPublisher publisher;
private FreeStyleProject project;

@Rule
public JenkinsRule j = new JenkinsRule() {
@Override
protected void before() throws Throwable {
super.before();
publisher = new ExtendedEmailPublisher();
publisher.defaultSubject = "%DEFAULT_SUBJECT";
publisher.defaultContent = "%DEFAULT_CONTENT";
publisher.attachmentsPattern = "";
publisher.recipientList = "%DEFAULT_RECIPIENTS";
publisher.presendScript = "";

project = createFreeStyleProject();
project.getPublishersList().add(publisher);
}

@Override
protected void after() {
super.after();
Mailbox.clearAll();
}
};


@Test
public void testTriggerName() throws Exception {
SuccessTrigger trigger = new SuccessTrigger(true, true, true, false, "$DEFAULT_RECIPIENTS",
"$DEFAULT_REPLYTO", "$DEFAULT_SUBJECT", "${TRIGGER_NAME}", "", 0, "project");
addEmailType(trigger);
publisher.getConfiguredTriggers().add(trigger);

FreeStyleBuild build = project.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);

assertThat("Email should have been triggered, so we should see it in the logs.", build.getLog(100),
hasItems("Email was triggered for: " + SuccessTrigger.TRIGGER_NAME));
assertEquals(1, Mailbox.get("mickey@disney.com").size());
Message message = Mailbox.get("mickey@disney.com").get(0);
assertEquals("Success", message.getSubject());
}

private void addEmailType(EmailTrigger trigger) {
trigger.setEmail(new EmailType() {
{
setRecipientList("mickey@disney.com");
setSubject("${TRIGGER_NAME}");
}
});
}
}

0 comments on commit e2b1e77

Please sign in to comment.