Merge "Add enabledExperiments to soy template"
diff --git a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
index b6375c8..96e859a 100644
--- a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
+++ b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
@@ -38,6 +38,7 @@
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 @@
@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 @@
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 @@
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 @@
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)) {
diff --git a/javatests/com/google/gerrit/httpd/BUILD b/javatests/com/google/gerrit/httpd/BUILD
index d751890..b65f9f4 100644
--- a/javatests/com/google/gerrit/httpd/BUILD
+++ b/javatests/com/google/gerrit/httpd/BUILD
@@ -6,6 +6,7 @@
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",
diff --git a/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java b/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java
index cad4796..da995e7 100644
--- a/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java
+++ b/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java
@@ -18,12 +18,15 @@
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 @@
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);
+ }
}
diff --git a/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy b/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy
index 212f504..935ea3a 100644
--- a/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy
+++ b/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy
@@ -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}