From bfd75384c2ea532c637a28e8714cbc20bd12c86c Mon Sep 17 00:00:00 2001 From: xchangcheng Date: Mon, 30 Apr 2018 14:04:55 +0000 Subject: [PATCH] Revert "Expose instanceName in the subject template" This reverts commit eb4d2d8da7bfc29511154fffe19f89f769a6a03c. Reason for revert: we observe com.google.template.soy.tofu.SoyTofuException: In 'print' tag, expression "$instanceAndProjectName" evaluates to undefined. at com.google.gerrit.server.mail.template.ChangeSubject(ChangeSubject.soy:9) in production Change-Id: I1701b5bcb4720086783536c5c987866bf73e54f4 --- Documentation/config-mail.txt | 5 ++--- .../server/mail/send/NotificationEmail.java | 15 ++++++++++++++- .../server/mail/send/NotificationEmailTest.java | 13 +++++++++++++ .../google/gerrit/server/mail/ChangeSubject.soy | 4 ++-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Documentation/config-mail.txt b/Documentation/config-mail.txt index 03f8d70be8..40a6284722 100644 --- a/Documentation/config-mail.txt +++ b/Documentation/config-mail.txt @@ -225,10 +225,9 @@ $shortProjectName:: + The project name with the path abbreviated. -$instanceName:: +$instanceAndProjectName:: + -The Gerrit instance name, as defined in the -link:config-gerrit.html#gerrit.instanceName[configuration]. +The Gerrit instance name, followed by the short project name $addInstanceNameInSubject:: + diff --git a/java/com/google/gerrit/server/mail/send/NotificationEmail.java b/java/com/google/gerrit/server/mail/send/NotificationEmail.java index 1b2c205e23..2a24f38f2f 100644 --- a/java/com/google/gerrit/server/mail/send/NotificationEmail.java +++ b/java/com/google/gerrit/server/mail/send/NotificationEmail.java @@ -111,8 +111,11 @@ public abstract class NotificationEmail extends OutgoingEmail { // shortProjectName is the project name with the path abbreviated. soyContext.put("shortProjectName", getShortProjectName(projectName)); + // instanceAndProjectName is the instance's name followed by the abbreviated project path + soyContext.put( + "instanceAndProjectName", + getInstanceAndProjectName(args.instanceNameProvider.get(), projectName)); soyContext.put("addInstanceNameInSubject", args.addInstanceNameInSubject); - soyContext.put("instanceName", args.instanceNameProvider.get()); soyContextEmailData.put("sshHost", getSshHost()); @@ -133,4 +136,14 @@ public abstract class NotificationEmail extends OutgoingEmail { return "..." + projectName.substring(lastIndexSlash + 1); } + + @VisibleForTesting + protected static String getInstanceAndProjectName(String instanceName, String projectName) { + if (instanceName == null || instanceName.isEmpty()) { + return getShortProjectName(projectName); + } + // Extract the project name (everything after the last slash) and prepends it with gerrit's + // instance name + return instanceName + "/" + projectName.substring(projectName.lastIndexOf('/') + 1); + } } diff --git a/javatests/com/google/gerrit/server/mail/send/NotificationEmailTest.java b/javatests/com/google/gerrit/server/mail/send/NotificationEmailTest.java index f078f90046..885f7cdca0 100644 --- a/javatests/com/google/gerrit/server/mail/send/NotificationEmailTest.java +++ b/javatests/com/google/gerrit/server/mail/send/NotificationEmailTest.java @@ -19,6 +19,19 @@ import static com.google.common.truth.Truth.assertThat; import org.junit.Test; public class NotificationEmailTest { + + @Test + public void getInstanceAndProjectName_returnsTheRightValue() { + String instanceAndProjectName = NotificationEmail.getInstanceAndProjectName("test", "/my/api"); + assertThat(instanceAndProjectName).isEqualTo("test/api"); + } + + @Test + public void getInstanceAndProjectName_handlesNull() { + String instanceAndProjectName = NotificationEmail.getInstanceAndProjectName(null, "/my/api"); + assertThat(instanceAndProjectName).isEqualTo("...api"); + } + @Test public void getShortProjectName() { assertThat(NotificationEmail.getShortProjectName("/api")).isEqualTo("api"); diff --git a/resources/com/google/gerrit/server/mail/ChangeSubject.soy b/resources/com/google/gerrit/server/mail/ChangeSubject.soy index b0224deb9b..48ec9a268a 100644 --- a/resources/com/google/gerrit/server/mail/ChangeSubject.soy +++ b/resources/com/google/gerrit/server/mail/ChangeSubject.soy @@ -22,13 +22,13 @@ * @param branch * @param change * @param shortProjectName - * @param instanceName + * @param instanceAndProjectName * @param addInstanceNameInSubject boolean */ {template .ChangeSubject kind="text"} {if not $addInstanceNameInSubject} Change in {$shortProjectName}[{$branch.shortName}]: {$change.shortSubject} {else} - [{$instanceName}] Change in {$shortProjectName}[{$branch.shortName}]: {$change.shortSubject} + Change in {$instanceAndProjectName}[{$branch.shortName}]: {$change.shortSubject} {/if} {/template}