Remove multiple implementations of getSshHost() in email classes

The getSshHost() method was implemented in both ReplacePatchSetSender
and NewChangeSender.

Remove these and implement it in the NotificationEmail base class
instead.

Change-Id: I55e36021915d147e01c676e7837557f5283812f6
This commit is contained in:
Yang Zhenhui
2012-12-21 15:05:36 +08:00
committed by David Pursehouse
parent 618f878dc0
commit 3a495e6c46
5 changed files with 34 additions and 40 deletions

View File

@@ -29,9 +29,10 @@ public class AddReviewerSender extends NewChangeSender {
@Inject
public AddReviewerSender(EmailArguments ea,
@AnonymousCowardName String anonymousCowardName, SshInfo sshInfo,
@AnonymousCowardName String anonymousCowardName, SshInfo si,
@Assisted Change c) {
super(ea, anonymousCowardName, sshInfo, c);
super(ea, anonymousCowardName, c);
setSshInfo(si);
}
@Override

View File

@@ -40,9 +40,10 @@ public class CreateChangeSender extends NewChangeSender {
@Inject
public CreateChangeSender(EmailArguments ea,
@AnonymousCowardName String anonymousCowardName, SshInfo sshInfo,
@AnonymousCowardName String anonymousCowardName, SshInfo si,
@Assisted Change c) {
super(ea, anonymousCowardName, sshInfo, c);
super(ea, anonymousCowardName, c);
setSshInfo(si);
}
@Override

View File

@@ -17,9 +17,6 @@ package com.google.gerrit.server.mail;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.ssh.SshInfo;
import com.jcraft.jsch.HostKey;
import java.util.ArrayList;
import java.util.Collection;
@@ -29,14 +26,12 @@ import java.util.Set;
/** Sends an email alerting a user to a new change for them to review. */
public abstract class NewChangeSender extends ChangeEmail {
private final SshInfo sshInfo;
private final Set<Account.Id> reviewers = new HashSet<Account.Id>();
private final Set<Account.Id> extraCC = new HashSet<Account.Id>();
protected NewChangeSender(EmailArguments ea, String anonymousCowardName,
SshInfo sshInfo, Change c) {
Change c) {
super(ea, anonymousCowardName, c, "newchange");
this.sshInfo = sshInfo;
}
public void addReviewers(final Collection<Account.Id> cc) {
@@ -73,17 +68,4 @@ public abstract class NewChangeSender extends ChangeEmail {
}
return names;
}
public String getSshHost() {
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
return null;
}
final String host = hostKeys.get(0).getHost();
if (host.startsWith("*:")) {
return getGerritHost() + host.substring(1);
}
return host;
}
}

View File

@@ -19,11 +19,16 @@ import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
import com.google.gerrit.server.mail.ProjectWatch.Watchers;
import com.google.gerrit.server.ssh.SshInfo;
import com.google.gwtorm.server.OrmException;
import com.jcraft.jsch.HostKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Common class for notifications that are related to a project and branch
*/
@@ -33,6 +38,7 @@ public abstract class NotificationEmail extends OutgoingEmail {
protected Project.NameKey project;
protected Branch.NameKey branch;
private SshInfo sshInfo;
protected NotificationEmail(EmailArguments ea, String anonymousCowardName,
String mc, Project.NameKey project, Branch.NameKey branch) {
@@ -70,6 +76,26 @@ public abstract class NotificationEmail extends OutgoingEmail {
}
}
protected void setSshInfo(SshInfo si) {
this.sshInfo = si;
}
public String getSshHost() {
if (sshInfo == null) {
return null;
}
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
return null;
}
final String host = hostKeys.get(0).getHost();
if (host.startsWith("*:")) {
return getGerritHost() + host.substring(1);
}
return host;
}
@Override
protected void setupVelocityContext() {
super.setupVelocityContext();

View File

@@ -23,8 +23,6 @@ import com.google.gerrit.server.ssh.SshInfo;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.jcraft.jsch.HostKey;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -39,14 +37,13 @@ public class ReplacePatchSetSender extends ReplyToChangeSender {
private final Set<Account.Id> reviewers = new HashSet<Account.Id>();
private final Set<Account.Id> extraCC = new HashSet<Account.Id>();
private final SshInfo sshInfo;
@Inject
public ReplacePatchSetSender(EmailArguments ea,
@AnonymousCowardName String anonymousCowardName, SshInfo si,
@Assisted Change c) {
super(ea, anonymousCowardName, c, "newpatchset");
sshInfo = si;
setSshInfo(si);
}
public void addReviewers(final Collection<Account.Id> cc) {
@@ -88,17 +85,4 @@ public class ReplacePatchSetSender extends ReplyToChangeSender {
}
return names;
}
public String getSshHost() {
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
return null;
}
final String host = hostKeys.get(0).getHost();
if (host.startsWith("*:")) {
return getGerritHost() + host.substring(1);
}
return host;
}
}