Merge "Revert "Expose instanceName in the subject template""

This commit is contained in:
xchangcheng 2018-04-30 15:07:07 +00:00 committed by Gerrit Code Review
commit 9813123504
4 changed files with 31 additions and 6 deletions

View File

@ -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::
+

View File

@ -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);
}
}

View File

@ -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");

View File

@ -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}