Ship a list of default experiments from the backend and allow disabling

With this commit, we ship a list of default experiments from the backend
to the frontend and allow administrators to disable default experiments
using a config in gerrit.config.

Change-Id: I2b47042475f330b1816195b230c03a3594ebdaa7
This commit is contained in:
Patrick Hiesel
2020-12-07 15:17:24 +01:00
parent 5f000f8509
commit 17142dcc47
3 changed files with 34 additions and 3 deletions

View File

@@ -3383,6 +3383,17 @@ removed in the next stable version.
enabled = ExperimentKey
----
[[experiments.disabled]]experiments.disabled::
+
List of experiments that are currently disabled. The release notes contain currently
available experiments. This list disables experiments with the given key that are
either enabled by default or explicitly in the config.
----
[experiments]
disabled = ExperimentKey
----
[[ldap]]
=== Section ldap

View File

@@ -20,6 +20,7 @@ import static java.util.stream.Collectors.toSet;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.UsedAt;
@@ -48,6 +49,10 @@ import org.eclipse.jgit.lib.Config;
public class IndexHtmlUtil {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
static final ImmutableSet<String> DEFAULT_EXPERIMENTS =
ImmutableSet.of(
"UiFeature__patchset_comments", "UiFeature__patchset_choice_for_comment_links");
private static final Gson GSON = OutputFormat.JSON_COMPACT.newGson();
/**
* Returns both static and dynamic parameters of {@code index.html}. The result is to be used when
@@ -69,9 +74,13 @@ public class IndexHtmlUtil {
canonicalURL, cdnPath, faviconPath, urlParameterMap, urlInScriptTagOrdainer))
.putAll(dynamicTemplateData(gerritApi, requestedURL));
Set<String> enabledExperiments = new HashSet<>(experimentData(urlParameterMap));
Set<String> enabledExperiments = new HashSet<>();
Arrays.stream(gerritServerConfig.getStringList("experiments", null, "enabled"))
.forEach(enabledExperiments::add);
DEFAULT_EXPERIMENTS.forEach(enabledExperiments::add);
Arrays.stream(gerritServerConfig.getStringList("experiments", null, "disabled"))
.forEach(enabledExperiments::remove);
experimentData(urlParameterMap).forEach(enabledExperiments::add);
if (!enabledExperiments.isEmpty()) {
data.put("enabledExperiments", serializeObject(GSON, enabledExperiments).toString());
}

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.httpd.raw;
import static com.google.common.truth.Truth.assertThat;
import static java.util.stream.Collectors.joining;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -54,8 +55,12 @@ public class IndexServletTest {
String testCdnPath = "bar-cdn";
String testFaviconURL = "zaz-url";
String disabledDefault = IndexHtmlUtil.DEFAULT_EXPERIMENTS.asList().get(0);
org.eclipse.jgit.lib.Config serverConfig = new org.eclipse.jgit.lib.Config();
serverConfig.setStringList("experiments", null, "enabled", ImmutableList.of("NewFeature"));
serverConfig.setStringList(
"experiments", null, "enabled", ImmutableList.of("NewFeature", "DisabledFeature"));
serverConfig.setStringList(
"experiments", null, "disabled", ImmutableList.of("DisabledFeature", disabledDefault));
IndexServlet servlet =
new IndexServlet(testCanonicalUrl, testCdnPath, testFaviconURL, gerritApi, serverConfig);
@@ -80,8 +85,14 @@ public class IndexServletTest {
+ "\\x22\\/config\\/server\\/info\\x22: \\x7b\\x22default_theme\\x22:"
+ "\\x22my-default-theme\\x22\\x7d, \\x22\\/config\\/server\\/top-menus\\x22: "
+ "\\x5b\\x5d\\x7d');");
String enabledDefaults =
IndexHtmlUtil.DEFAULT_EXPERIMENTS.stream()
.filter(e -> !e.equals(disabledDefault))
.collect(joining("\\x22,"));
assertThat(output)
.contains(
"window.ENABLED_EXPERIMENTS = JSON.parse('\\x5b\\x22NewFeature\\x22\\x5d');</script>");
"window.ENABLED_EXPERIMENTS = JSON.parse('\\x5b\\x22NewFeature\\x22,\\x22"
+ enabledDefaults
+ "\\x22\\x5d');</script>");
}
}