Skip to content

Commit

Permalink
[JENKINS-25083] use MailAddressResolver for retrieving user email ins…
Browse files Browse the repository at this point in the history
…tead of UserProperty
  • Loading branch information
fabiodcasilva committed Nov 2, 2024
1 parent 2bf637d commit a19c343
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
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]";
}
}
}

0 comments on commit a19c343

Please sign in to comment.