Add enabledExperiments to soy template

This will add ENABLED_EXPERIMENTS to window,
it should contain a list of enabled experiments for the given user.

Provides the ability to pass in experiments from url with
`?experiment=a&experiment=b`.

Change-Id: Ia79157e6968946e6bd91cfe8e1ab23cba140d11d
This commit is contained in:
Tao Zhou
2020-03-30 14:04:49 +02:00
parent de3a454d31
commit 7103caa7b1
4 changed files with 54 additions and 5 deletions

View File

@@ -38,6 +38,7 @@ import com.google.template.soy.data.SanitizedContent;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
@@ -48,6 +49,9 @@ import java.util.regex.Pattern;
@UsedAt(Project.GOOGLE)
public class IndexHtmlUtil {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final Gson gson = OutputFormat.JSON_COMPACT.newGson();
public static final String changeCanonicalUrl = ".*/c/(?<project>.+)/\\+/(?<changeNum>\\d+)";
public static final String basePatchNumUrlPart = "(/(-?\\d+|edit)(\\.\\.(\\d+|edit))?)";
public static final Pattern changeUrlPattern =
@@ -107,8 +111,8 @@ public class IndexHtmlUtil {
Function<String, SanitizedContent> urlInScriptTagOrdainer,
String requestedURL)
throws URISyntaxException, RestApiException {
return ImmutableMap.<String, Object>builder()
.putAll(
ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
data.putAll(
staticTemplateData(
canonicalURL,
cdnPath,
@@ -116,15 +120,19 @@ public class IndexHtmlUtil {
urlParameterMap,
urlInScriptTagOrdainer,
requestedURL))
.putAll(dynamicTemplateData(gerritApi))
.build();
.putAll(dynamicTemplateData(gerritApi));
Set<String> enabledExperiments = experimentData(urlParameterMap);
if (!enabledExperiments.isEmpty()) {
data.put("enabledExperiments", serializeObject(gson, enabledExperiments));
}
return data.build();
}
/** Returns dynamic parameters of {@code index.html}. */
public static ImmutableMap<String, Object> dynamicTemplateData(GerritApi gerritApi)
throws RestApiException {
ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
Gson gson = OutputFormat.JSON_COMPACT.newGson();
Map<String, SanitizedContent> initialData = new HashMap<>();
Server serverApi = gerritApi.config().server();
initialData.put("\"/config/server/info\"", serializeObject(gson, serverApi.getInfo()));
@@ -210,6 +218,24 @@ public class IndexHtmlUtil {
return data.build();
}
/** Returns experimentData to be used in {@code index.html}. */
static Set<String> experimentData(Map<String, String[]> urlParameterMap)
throws URISyntaxException {
Set<String> enabledExperiments = new HashSet<>();
// Allow enable experiments with url
// ?experiment=a&experiment=b should result in:
// "experiment" => [a,b]
if (urlParameterMap.containsKey("experiment")) {
String[] experiments = urlParameterMap.get("experiment");
for (String exp : experiments) {
enabledExperiments.add(exp);
}
}
return enabledExperiments;
}
private static String computeCanonicalPath(@Nullable String canonicalURL)
throws URISyntaxException {
if (Strings.isNullOrEmpty(canonicalURL)) {

View File

@@ -6,6 +6,7 @@ junit_tests(
deps = [
"//java/com/google/gerrit/extensions:api",
"//java/com/google/gerrit/httpd",
"//java/com/google/gerrit/json",
"//java/com/google/gerrit/server",
"//java/com/google/gerrit/testing:gerrit-test-util",
"//javatests/com/google/gerrit/util/http/testutil",

View File

@@ -18,12 +18,15 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.httpd.raw.IndexHtmlUtil.changeUrlPattern;
import static com.google.gerrit.httpd.raw.IndexHtmlUtil.computeChangeRequestsPath;
import static com.google.gerrit.httpd.raw.IndexHtmlUtil.diffUrlPattern;
import static com.google.gerrit.httpd.raw.IndexHtmlUtil.experimentData;
import static com.google.gerrit.httpd.raw.IndexHtmlUtil.staticTemplateData;
import com.google.template.soy.data.SanitizedContent;
import com.google.template.soy.data.UnsafeSanitizedContentOrdainer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class IndexHtmlUtilTest {
@@ -137,4 +140,17 @@ public class IndexHtmlUtilTest {
return UnsafeSanitizedContentOrdainer.ordainAsSafe(
s, SanitizedContent.ContentKind.TRUSTED_RESOURCE_URI);
}
@Test
public void useExperiments() throws Exception {
Map<String, String[]> urlParms = new HashMap<>();
String[] experiments = new String[] {"foo", "bar", "foo"};
Set<String> expected = new HashSet<>();
for (String exp : experiments) {
expected.add(exp);
}
urlParms.put("experiment", experiments);
Set<String> data = experimentData(urlParms);
assertThat(data).isEqualTo(expected);
}
}

View File

@@ -20,6 +20,7 @@
{@param canonicalPath: ?}
{@param staticResourcePath: ?}
{@param gerritInitialData: /** {string} map of REST endpoint to response for startup. */ ?}
{@param? enabledExperiments: /** A list of enabled experiments for current user. */ ?}
{@param? assetsPath: ?} /** {string} URL to static assets root, if served from CDN. */
{@param? assetsBundle: ?} /** {string} Assets bundle .html file, served from $assetsPath. */
{@param? faviconPath: ?}
@@ -71,6 +72,11 @@
// '/accounts/self/detail' => { 'username' : 'gerrit-user' }
window.INITIAL_DATA = JSON.parse({$gerritInitialData});
{/if}
{if $enabledExperiments}
// ENABLED_EXPERIMENTS is a list of string that contains all enabled experiments
// for the given user.
window.ENABLED_EXPERIMENTS = JSON.parse($enabledExperiments);
{/if}
</script>{\n}
{if $faviconPath}