Skip to content

Commit

Permalink
[JENKINS-19433] - MailAddressResolver::resolve(User) uses a Mailer.Us…
Browse files Browse the repository at this point in the history
…erProperty as a data source with a highest priority.

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Mar 4, 2014
1 parent fe2d408 commit 2b59658
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/main/java/hudson/tasks/MailAddressResolver.java
Expand Up @@ -38,6 +38,7 @@
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import hudson.tasks.Mailer;

/**
* Infers e-mail addresses for the user when none is specified.
Expand Down Expand Up @@ -101,12 +102,20 @@ public abstract class MailAddressResolver implements ExtensionPoint {

/**
* Try to resolve email address using resolvers.
* If a user specifies a Mail {@link UserProperty}, then it will be used with
* the highest priority.
* @return User address or null if resolution failed
*/
public static String resolve(User u) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Resolving e-mail address for \""+u+"\" ID="+u.getId());
}

// Use User Property with a highest priority
String userPropertyAddress = extractAddressFromUserProperty(u);
if (userPropertyAddress != null) {
return userPropertyAddress;
}

for (MailAddressResolver r : all()) {
String email = r.findMailAddressFor(u);
Expand All @@ -131,12 +140,9 @@ public static String resolve(User u) {
*/
public static String resolveFast(User u) {

// Try user properties
Mailer.UserProperty emailProperty = u.getProperty(Mailer.UserProperty.class);
if (emailProperty != null) {
String explicitAddress = emailProperty.getExplicitlyConfiguredAddress();
if (explicitAddress != null) // A final check to prevent concurrency issues
return explicitAddress;
String userPropertyAddress = extractAddressFromUserProperty(u);
if (userPropertyAddress != null) {
return userPropertyAddress;
}

String extractedAddress = extractAddressFromId(u.getFullName());
Expand All @@ -160,6 +166,22 @@ public static String resolveFast(User u) {

return null;
}

/**
* Try to resolve user email using his {@link UserProperty}.
* @param u A user, for whom the email should be resolved
* @return User address or null if the resolution fails
*/
private static String extractAddressFromUserProperty (User u) {
Mailer.UserProperty emailProperty = u.getProperty(Mailer.UserProperty.class);
if (emailProperty != null) {
String explicitAddress = emailProperty.getExplicitlyConfiguredAddress();
if (explicitAddress != null) // A final check to prevent concurrency issues
return explicitAddress;
}

return null; // Return nothing by default
}

/**
* Tries to extract an email address from the user id, or returns null
Expand Down

0 comments on commit 2b59658

Please sign in to comment.