Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-25083] use MailAddressResolver for retrieving user email instead of UserProperty #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,10 +90,8 @@ private void setUserGroups(String userId, Map<String, String> variables) {

private void setUserEmail(String userId, Map<String, String> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> outputVars = new HashMap<>();
UserIdCause cause = new UserIdCause(TEST_USER);
UserIdCauseDeterminant determinant = new UserIdCauseDeterminant();
determinant.setJenkinsUserBuildVars(cause, outputVars);

assertThat(outputVars.get(BuildUserVariable.EMAIL), is(equalTo("[email protected]")));
}

@TestExtension
public static class TestMailAddressResolver extends MailAddressResolver {
@Override
public String findMailAddressFor(User user) {
return "[email protected]";
}
}
}