Merge branch 'stable-3.2'
* stable-3.2:
Ignore events coming from another Gerrit server
Update bazlets to v3.2.2
Change-Id: Ie905ff8c26a22ab4d3ba25eaa61b9842e39d1795
diff --git a/WORKSPACE b/WORKSPACE
index 8308b76..b83937e 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -3,7 +3,7 @@
load("//:bazlets.bzl", "load_bazlets")
load_bazlets(
- commit = "d826d85285bb22d3fe817fe165a7e1d3472f65fa",
+ commit = "4a27255dff75eadc98b86391806be13e030e6ff3",
# local_path = "/home/<user>/projects/bazlets",
)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java
index af5e9a9..3429ba4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java
@@ -17,11 +17,13 @@
import static java.util.stream.Collectors.toList;
import com.google.common.flogger.FluentLogger;
+import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.AccessSection;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.Project.NameKey;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
+import com.google.gerrit.server.config.GerritInstanceId;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.PluginConfigFactory;
@@ -55,6 +57,7 @@
private final ProjectCache projectCache;
private final PluginConfigFactory pluginCfgFactory;
private final Config gerritConfig;
+ private String instanceId;
private static final ThreadLocal<Project.NameKey> currentProjectName =
ThreadLocal.withInitial(() -> null);
@@ -68,16 +71,25 @@
@PluginName String pluginName,
ProjectCache projectCache,
PluginConfigFactory pluginCfgFactory,
- @GerritServerConfig Config gerritConfig) {
+ @GerritServerConfig Config gerritConfig,
+ @Nullable @GerritInstanceId String instanceId) {
this.pluginName = pluginName;
this.projectCache = projectCache;
this.pluginCfgFactory = pluginCfgFactory;
this.gerritConfig = gerritConfig;
+ this.instanceId = instanceId;
}
// Plugin enablement --------------------------------------------------------
public boolean isEnabled(RefEvent event) {
+ if ((instanceId == null && event.instanceId != null)
+ || (instanceId != null && !instanceId.equals(event.instanceId))) {
+ logger.atFine().log(
+ "Event %s is coming from a remote Gerrit instance-id (%s)", event, event.instanceId);
+ return false;
+ }
+
if (event instanceof PatchSetCreatedEvent
|| event instanceof CommentAddedEvent
|| event instanceof ChangeMergedEvent
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfigTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfigTest.java
index 34068bb..56f8ba4 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfigTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfigTest.java
@@ -14,6 +14,7 @@
package com.googlesource.gerrit.plugins.its.base.its;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
@@ -24,6 +25,7 @@
import com.google.gerrit.entities.Project;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.config.FactoryModule;
+import com.google.gerrit.server.config.GerritInstanceId;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.PluginConfigFactory;
@@ -39,8 +41,13 @@
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Guice;
import com.google.inject.Injector;
+import com.google.inject.util.Providers;
import com.googlesource.gerrit.plugins.its.base.testutil.LoggingMockingTestCase;
import com.googlesource.gerrit.plugins.its.base.validation.ItsAssociationPolicy;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import org.eclipse.jgit.lib.Config;
@@ -52,6 +59,17 @@
private PluginConfigFactory pluginConfigFactory;
private Config serverConfig;
+ @Retention(RUNTIME)
+ @Target(ElementType.METHOD)
+ static @interface GerritInstanceIdTest {}
+
+ static final String LOCAL_INSTANCE_ID = "local-gerrit-instance-id";
+ static final String FOREIGN_INSTANCE_ID = "foreign-gerrit-instance-id";
+
+ public void enableItsConfig() {
+ setupIsEnabled("true", null, null, new String[] {});
+ }
+
public void setupIsEnabled(
String enabled, String itsProject, String parentEnabled, String[] branches) {
ProjectState projectState = mock(ProjectState.class);
@@ -253,6 +271,28 @@
assertTrue(itsConfig.isEnabled(projectNK, "refs/heads/testBranch"));
}
+ @GerritInstanceIdTest
+ public void testIsNotEnabledEventFromForeignInstanceId() {
+ enableItsConfig();
+
+ assertFalse(createItsConfig().isEnabled(newEventFromGerritInstanceId(FOREIGN_INSTANCE_ID)));
+ assertLogMessageContains("is coming from a remote Gerrit instance-id");
+ }
+
+ @GerritInstanceIdTest
+ public void testIsEnabledEventFromLocalInstanceId() {
+ enableItsConfig();
+
+ assertTrue(createItsConfig().isEnabled(newEventFromGerritInstanceId(LOCAL_INSTANCE_ID)));
+ }
+
+ public void testIsNotEnabledEventWithRemoteInstanceIdButNotDefinedLocally() {
+ enableItsConfig();
+
+ assertFalse(createItsConfig().isEnabled(newEventFromGerritInstanceId(FOREIGN_INSTANCE_ID)));
+ assertLogMessageContains("is coming from a remote Gerrit instance-id");
+ }
+
public void testIsEnabledEventNoBranches() {
String[] branches = {};
setupIsEnabled("true", null, null, branches);
@@ -709,6 +749,12 @@
.getEnum("plugin", "ItsTestName", "association", ItsAssociationPolicy.MANDATORY);
}
+ private PatchSetCreatedEvent newEventFromGerritInstanceId(String instanceId) {
+ PatchSetCreatedEvent event = new PatchSetCreatedEvent(testChange("testProject", "testBranch"));
+ event.instanceId = instanceId;
+ return event;
+ }
+
private ItsConfig createItsConfig() {
return injector.getInstance(ItsConfig.class);
}
@@ -720,10 +766,20 @@
@Override
public void setUp() throws Exception {
super.setUp();
- injector = Guice.createInjector(new TestModule());
+
+ Method testMethod = ItsConfigTest.class.getMethod(getName());
+ String gerritInstanceIdTest =
+ testMethod.getAnnotation(GerritInstanceIdTest.class) == null ? null : LOCAL_INSTANCE_ID;
+ injector = Guice.createInjector(new TestModule(gerritInstanceIdTest));
}
private class TestModule extends FactoryModule {
+ private final String gerritInstanceId;
+
+ TestModule(String gerritInstanceId) {
+ this.gerritInstanceId = gerritInstanceId;
+ }
+
@Override
protected void configure() {
projectCache = mock(ProjectCache.class);
@@ -736,6 +792,10 @@
serverConfig = mock(Config.class);
bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(serverConfig);
+
+ bind(String.class)
+ .annotatedWith(GerritInstanceId.class)
+ .toProvider(Providers.of(gerritInstanceId));
}
}
}