Skip to content
Permalink
Browse files

Created P4UserProperty to store Email address.

P4UserProperty extends UserProperty to store the Perforce User’s email.
Then retrieves it with P4AddressResolver by extending
MailAddressResolver.

JENKINS-28421
  • Loading branch information...
p4paul committed Aug 20, 2015
1 parent e117cd2 commit c1f15ea19c720a3d4840aafa478b177c0d0f8f97
@@ -2,7 +2,6 @@

import hudson.model.User;
import hudson.scm.ChangeLogSet;
import hudson.tasks.Mailer.UserProperty;

import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -14,6 +13,7 @@
import java.util.logging.Logger;

import org.jenkinsci.plugins.p4.client.ConnectionHelper;
import org.jenkinsci.plugins.p4.email.P4UserProperty;
import org.kohsuke.stapler.export.Exported;

import com.perforce.p4java.core.ChangelistStatus;
@@ -66,14 +66,10 @@ public void setChange(ConnectionHelper p4, int changeId) throws Exception {
author = User.get(user);

// set email property on user
UserProperty prop = author.getProperty(UserProperty.class);
if (prop == null || prop.getAddress() == null
|| prop.getAddress().isEmpty()) {
String email = p4.getEmail(user);
UserProperty userProp = new UserProperty(email);
author.addProperty(userProp);
logger.fine("Setting email for user: " + user + ":" + email);
}
String email = p4.getEmail(user);
P4UserProperty p4prop = new P4UserProperty(email);
author.addProperty(p4prop);
logger.fine("Setting email for user: " + user + ":" + email);

// set date of change
date = changelist.getDate();
@@ -0,0 +1,24 @@
package org.jenkinsci.plugins.p4.email;

import hudson.model.User;
import hudson.tasks.MailAddressResolver;

import java.util.logging.Logger;

public class P4AddressResolver extends MailAddressResolver {

private static Logger logger = Logger.getLogger(P4AddressResolver.class
.getName());

@Override
public String findMailAddressFor(User user) {
P4UserProperty prop = user.getProperty(P4UserProperty.class);
if (prop != null) {
String id = user.getId();
String email = prop.getEmail();
logger.info("MailAddressResolver: " + id + ":" + email);
return email;
}
return null;
}
}
@@ -0,0 +1,48 @@
package org.jenkinsci.plugins.p4.email;

import hudson.Extension;
import hudson.Util;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import hudson.model.User;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;

public class P4UserProperty extends UserProperty {

private final String email;

@DataBoundConstructor
public P4UserProperty(String email) {
this.email = Util.fixEmptyAndTrim(email);
}

@Extension
public static class DescriptorImpl extends UserPropertyDescriptor {

@Override
public UserProperty newInstance(User user) {
return new P4UserProperty(null);
}

@Override
public String getDisplayName() {
return "Perforce User Property";
}

@Override
public UserProperty
newInstance(StaplerRequest req, JSONObject formData)
throws FormException {
return new P4UserProperty(formData.optString("email"));
}
}

@Exported
public String getEmail() {
return email;
}
}
@@ -0,0 +1,5 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="Perforce Email" description="ReadOnly: Defined in the Perforce User spec.">
<f:readOnlyTextbox field="email"/>
</f:entry>
</j:jelly>

0 comments on commit c1f15ea

Please sign in to comment.
You can’t perform that action at this time.