diff --git a/src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminant.java b/src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminant.java index d0afc97..cc17471 100644 --- a/src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminant.java +++ b/src/main/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminant.java @@ -4,7 +4,7 @@ import hudson.model.User; import hudson.security.ACL; import hudson.security.SecurityRealm; -import hudson.tasks.Mailer.UserProperty; +import hudson.tasks.MailAddressResolver; import jenkins.model.Jenkins; import org.apache.commons.lang.StringUtils; import org.jenkinsci.plugins.builduser.utils.BuildUserVariable; @@ -90,10 +90,8 @@ private void setUserGroups(String userId, Map variables) { private void setUserEmail(String userId, Map variables) { Optional.ofNullable(User.getById(userId, false)) - .map(user -> user.getProperty(UserProperty.class)) - .map(UserProperty::getAddress) - .map(StringUtils::trimToEmpty) - .ifPresent(address -> variables.put(BuildUserVariable.EMAIL, address)); + .map(MailAddressResolver::resolve) + .ifPresent(email -> variables.put(BuildUserVariable.EMAIL, email)); } /** diff --git a/src/test/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminantEmailTest.java b/src/test/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminantEmailTest.java new file mode 100644 index 0000000..2bd9970 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/builduser/varsetter/impl/UserIdCauseDeterminantEmailTest.java @@ -0,0 +1,51 @@ +package org.jenkinsci.plugins.builduser.varsetter.impl; + +import hudson.model.Cause.UserIdCause; +import hudson.model.User; +import hudson.tasks.MailAddressResolver; +import hudson.tasks.Mailer.UserProperty; +import org.jenkinsci.plugins.builduser.utils.BuildUserVariable; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.TestExtension; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class UserIdCauseDeterminantEmailTest { + public static final String TEST_USER = "test_user"; + + @Rule + public JenkinsRule r = new JenkinsRule(); + + @Test + public void testSetJenkinsUserBuildVarsEmailWithResolver() throws IOException { + User user = User.getById(TEST_USER, true); + assert user != null; + user.addProperty(new UserProperty(null)); + + JenkinsRule.DummySecurityRealm realm = r.createDummySecurityRealm(); + r.jenkins.setSecurityRealm(realm); + + Map outputVars = new HashMap<>(); + UserIdCause cause = new UserIdCause(TEST_USER); + UserIdCauseDeterminant determinant = new UserIdCauseDeterminant(); + determinant.setJenkinsUserBuildVars(cause, outputVars); + + assertThat(outputVars.get(BuildUserVariable.EMAIL), is(equalTo("resolveduser@example.com"))); + } + + @TestExtension + public static class TestMailAddressResolver extends MailAddressResolver { + @Override + public String findMailAddressFor(User user) { + return "resolveduser@example.com"; + } + } +}