From 33fbef2770c6983bee8ad7a6edd8eee7d9b91c68 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Mon, 10 Mar 2008 22:34:16 +0000 Subject: [PATCH] incorporating the logic to infer e-mail address from SCM. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@7522 71c3de6d-444a-0410-be80-ed276b4c234a --- .../hudson/tasks/MailAddressResolver.java | 146 ++++++++++++++++++ core/src/main/java/hudson/tasks/Mailer.java | 3 +- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/hudson/tasks/MailAddressResolver.java diff --git a/core/src/main/java/hudson/tasks/MailAddressResolver.java b/core/src/main/java/hudson/tasks/MailAddressResolver.java new file mode 100644 index 0000000000..f4653d06ff --- /dev/null +++ b/core/src/main/java/hudson/tasks/MailAddressResolver.java @@ -0,0 +1,146 @@ +package hudson.tasks; + +import hudson.ExtensionPoint; +import hudson.Plugin; +import hudson.scm.SCM; +import hudson.scm.CVSSCM; +import hudson.scm.SubversionSCM; +import hudson.model.User; +import hudson.model.AbstractProject; + +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.regex.Pattern; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * Infers e-mail addresses for the user when none is specified. + * + *

+ * This is an extension point of Hudson. Plugins tha contribute new implementation + * of this class must register it to {@link #LIST}. The typical way to do this is: + * + *

+ * class PluginImpl extends {@link Plugin} {
+ *   public void start() {
+ *     ...
+ *     MailAddressResolver.LIST.add(new MyMailAddressResolver());
+ *   }
+ * }
+ * 
+ * + * @author Kohsuke Kawaguchi + * @sine 1.192 + */ +public abstract class MailAddressResolver implements ExtensionPoint { + /** + * Infers e-mail address of the given user. + * + *

+ * This method is called when a {@link User} without explicitly configured e-mail + * address is used, as an attempt to infer e-mail address. + * + *

+ * The normal strategy is to look at {@link User#getProjects() the projects that the user + * is participating}, then use the repository information to infer the e-mail address. + * + *

+ * When multiple resolvers are installed, they are consulted in order and + * the search will be over when an address is inferred by someone. + * + *

+ * Since {@link MailAddressResolver} is singleton, this method can be invoked concurrently + * from multiple threads. + * + * @return + * null if the inference failed. + */ + public abstract String findMailAddressFor(User u); + + public static String resolve(User u) { + for (MailAddressResolver r : LIST) { + String email = r.findMailAddressFor(u); + if(email!=null) return email; + } + return null; + } + + + /** + * All registered {@link MailAddressResolver} implementations. + */ + public static final List LIST = new CopyOnWriteArrayList(); + + static { + // register built-in instances + LIST.add(new DefaultAddressResolver()); + } + + /** + * {@link MailAddressResolver} implemenations that cover well-known major public sites. + * + *

+ * Since this has low UI visibility, we are open to having a large number of rules here. + * If you'd like to add one, please contribute more rules. + */ + public static class DefaultAddressResolver extends MailAddressResolver { + public String findMailAddressFor(User u) { + for (AbstractProject p : u.getProjects()) { + SCM scm = p.getScm(); + if (scm instanceof CVSSCM) { + CVSSCM cvsscm = (CVSSCM) scm; + + String s = findMailAddressFor(u,cvsscm.getCvsRoot()); + if(s!=null) return s; + } + if (scm instanceof SubversionSCM) { + SubversionSCM svn = (SubversionSCM) scm; + for (SubversionSCM.ModuleLocation loc : svn.getLocations()) { + String s = findMailAddressFor(u,loc.remote); + if(s!=null) return s; + } + } + } + + // didn't hit any known rules + return null; + } + + /** + * + * @param scm + * String that represents SCM connectivity. + */ + protected String findMailAddressFor(User u, String scm) { + for (Map.Entry e : RULE_TABLE.entrySet()) + if(e.getKey().matcher(scm).matches()) + return u.getId()+e.getValue(); + return null; + } + + private static final Map RULE_TABLE = new HashMap(); + + static { + {// java.net + Pattern svnurl = Pattern.compile("https://[^.]+.dev.java.net/svn/([^/]+)(/.*)?"); + + String username = "([A-Za-z0-9_\\-])+"; + String host = "(.*.dev.java.net|kohsuke.sfbay.*)"; + Pattern cvsUrl = Pattern.compile(":pserver:"+username+"@"+host+":/cvs"); + + RULE_TABLE.put(svnurl,"@dev.java.net"); + RULE_TABLE.put(cvsUrl,"@dev.java.net"); + } + + {// source forge + Pattern cvsUrl = Pattern.compile(":(pserver|ext):([^@]+)@([^.]+).cvs.(sourceforge|sf).net:.+"); + RULE_TABLE.put(cvsUrl,"@users.sourceforge.net"); + // SVN URL? + } + + // TODO: read some file under $HUDSON_HOME? + } + + } +} diff --git a/core/src/main/java/hudson/tasks/Mailer.java b/core/src/main/java/hudson/tasks/Mailer.java index e57b47f5cf..3421efd271 100644 --- a/core/src/main/java/hudson/tasks/Mailer.java +++ b/core/src/main/java/hudson/tasks/Mailer.java @@ -386,7 +386,8 @@ public class Mailer extends Publisher { } public UserProperty newInstance(User user) { - return new UserProperty(null); + + return new UserProperty(MailAddressResolver.resolve(user)); } public UserProperty newInstance(StaplerRequest req) throws FormException {