Merge branch 'stable-2.16' into stable-3.0
* stable-2.16:
Bump required Bazel version to 2.1.0
Change-Id: I0799fdd4f9c2b293bb7e52c74b1071e21c295a83
diff --git a/BUILD b/BUILD
index 68ad690..f099db7 100644
--- a/BUILD
+++ b/BUILD
@@ -38,8 +38,5 @@
visibility = ["//visibility:public"],
exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
":slack-integration__plugin",
- "@mockito//jar",
- "@powermock_module_junit4//jar",
- "@powermock_api_mockito//jar",
],
)
diff --git a/README.md b/README.md
index 2007a3d..e9ecfed 100644
--- a/README.md
+++ b/README.md
@@ -107,6 +107,10 @@
ignore-private-patch-set - boolean (true/false)
Whether any Slack notifications regarding a private change shouldn't
be published (defaults to true).
+ ignore-comment-author - Pattern
+ A "dotall" enabled regular expression pattern that, when matches
+ against the comment author username, will prevent the publishing
+ of comment added event messages (defaults to an empty string).
publish-on-patch-set-created - boolean (true/false)
Whether a Slack notification should be published when a new patch set
is created.
diff --git a/WORKSPACE b/WORKSPACE
index c423ff1..8162181 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -3,7 +3,7 @@
load("//:bazlets.bzl", "load_bazlets")
load_bazlets(
- commit = "1b6fc9b39001806b37a4eedbb0a4816daf1a227d",
+ commit = "96f691ebbf4ef1c46b798e871ed5acd9d844651c",
#local_path = "/home/<user>/projects/bazlets"
)
@@ -24,7 +24,3 @@
# Load release Plugin API
gerrit_api()
-
-load("//:external_plugin_deps.bzl", "external_plugin_deps")
-
-external_plugin_deps()
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl
deleted file mode 100644
index 5e4c4c7..0000000
--- a/external_plugin_deps.bzl
+++ /dev/null
@@ -1,22 +0,0 @@
-load("//tools/bzl:maven_jar.bzl", "maven_jar")
-
-def external_plugin_deps():
- POWER_MOCK_VERSION = "1.6.2"
-
- maven_jar(
- name = "powermock_module_junit4",
- artifact = "org.powermock:powermock-module-junit4:" + POWER_MOCK_VERSION,
- sha1 = "dff58978da716e000463bc1b08013d6a7cf3d696",
- )
-
- maven_jar(
- name = "powermock_api_mockito",
- artifact = "org.powermock:powermock-api-mockito:" + POWER_MOCK_VERSION,
- sha1 = "c213230ae20a7b422f3d622a261d0e3427d2464c",
- )
-
- maven_jar(
- name = "mockito",
- artifact = "org.mockito:mockito-all:1.10.19",
- sha1 = "539df70269cc254a58cccc5d8e43286b4a73bf30",
- )
diff --git a/src/main/java/com/cisco/gerrit/plugins/slack/config/ProjectConfig.java b/src/main/java/com/cisco/gerrit/plugins/slack/config/ProjectConfig.java
index 088cc4f..050f704 100644
--- a/src/main/java/com/cisco/gerrit/plugins/slack/config/ProjectConfig.java
+++ b/src/main/java/com/cisco/gerrit/plugins/slack/config/ProjectConfig.java
@@ -43,6 +43,7 @@
private boolean ignoreUnchangedPatchSet;
private boolean ignoreWorkInProgressPatchSet;
private boolean ignorePrivatePatchSet;
+ private String ignoreCommentAuthor;
private boolean publishOnPatchSetCreated;
private boolean publishOnChangeMerged;
private boolean publishOnCommentAdded;
@@ -108,6 +109,11 @@
.getFromProjectConfigWithInheritance(projectNameKey, CONFIG_NAME)
.getBoolean("ignore-private-patch-set", true);
+ ignoreCommentAuthor =
+ configFactory
+ .getFromProjectConfigWithInheritance(projectNameKey, CONFIG_NAME)
+ .getString("ignore-comment-author", "");
+
publishOnPatchSetCreated =
configFactory
.getFromProjectConfigWithInheritance(projectNameKey, CONFIG_NAME)
@@ -184,6 +190,10 @@
return ignorePrivatePatchSet;
}
+ public String getIgnoreCommentAuthor() {
+ return ignoreCommentAuthor;
+ }
+
public boolean shouldPublishOnPatchSetCreated() {
return publishOnPatchSetCreated;
}
diff --git a/src/main/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGenerator.java b/src/main/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGenerator.java
index 2e2a1eb..6f8775a 100644
--- a/src/main/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGenerator.java
+++ b/src/main/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGenerator.java
@@ -22,6 +22,8 @@
import com.cisco.gerrit.plugins.slack.config.ProjectConfig;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.events.CommentAddedEvent;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,7 +74,23 @@
LOGGER.warn("Error checking private and work-in-progress status", e);
}
- return true;
+ boolean result;
+ result = true;
+
+ try {
+ Pattern pattern;
+ pattern = Pattern.compile(config.getIgnoreCommentAuthor(), Pattern.DOTALL);
+
+ Matcher matcher;
+ matcher = pattern.matcher(event.author.get().username);
+
+ // If the ignore pattern matches, publishing should not happen
+ result = !matcher.matches();
+ } catch (Exception e) {
+ LOGGER.warn("The specified ignore-comment-author pattern was invalid", e);
+ }
+
+ return result;
}
@Override
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/client/WebhookClientIntegrationTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/client/WebhookClientIntegrationTest.java
index b5e8011..f8c01e4 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/client/WebhookClientIntegrationTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/client/WebhookClientIntegrationTest.java
@@ -29,28 +29,14 @@
import com.google.gerrit.server.config.PluginConfigFactory;
import java.io.InputStream;
import java.util.Properties;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class WebhookClientIntegrationTest {
private static final String PROJECT_NAME = "test-project";
- private Project.NameKey mockNameKey = mock(Project.NameKey.class);
private PluginConfigFactory mockConfigFactory = mock(PluginConfigFactory.class);
private PluginConfig mockPluginConfig = mock(PluginConfig.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig() throws Exception {
Project.NameKey projectNameKey;
projectNameKey = Project.NameKey.parse(PROJECT_NAME);
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/config/ProjectConfigTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/config/ProjectConfigTest.java
index efd5c6f..ccab779 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/config/ProjectConfigTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/config/ProjectConfigTest.java
@@ -29,19 +29,11 @@
import com.google.gerrit.server.config.PluginConfigFactory;
import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/** Tests for the PluginConfig class. */
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class ProjectConfigTest {
private static final String PROJECT_NAME = "test-project";
- private Project.NameKey mockNameKey = mock(Project.NameKey.class);
-
private PluginConfigFactory mockConfigFactory = mock(PluginConfigFactory.class);
private PluginConfig mockPluginConfig = mock(PluginConfig.class);
@@ -50,9 +42,6 @@
@Before
public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
-
Project.NameKey projectNameKey;
projectNameKey = Project.NameKey.parse(PROJECT_NAME);
@@ -69,6 +58,7 @@
when(mockPluginConfig.getString("channel", "general")).thenReturn("test-channel");
when(mockPluginConfig.getString("username", "gerrit")).thenReturn("test-user");
when(mockPluginConfig.getString("ignore", "")).thenReturn("^WIP.*");
+ when(mockPluginConfig.getString("ignore-comment-author", "")).thenReturn("^jenkins.*");
when(mockPluginConfig.getBoolean("publish-on-patch-set-created", true)).thenReturn(true);
when(mockPluginConfig.getBoolean("publish-on-change-merged", true)).thenReturn(true);
when(mockPluginConfig.getBoolean("publish-on-comment-added", true)).thenReturn(true);
@@ -98,6 +88,11 @@
}
@Test
+ public void testGetIgnoreCommentAuthor() throws Exception {
+ assertThat(config.getIgnoreCommentAuthor(), is(equalTo("^jenkins.*")));
+ }
+
+ @Test
public void testShouldPublishOnPatchSetCreated() throws Exception {
assertThat(config.shouldPublishOnPatchSetCreated(), is(equalTo(true)));
}
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/message/ChangeMergedMessageGeneratorTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/message/ChangeMergedMessageGeneratorTest.java
index 303b7f3..269c502 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/message/ChangeMergedMessageGeneratorTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/message/ChangeMergedMessageGeneratorTest.java
@@ -31,24 +31,15 @@
import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.events.ChangeMergedEvent;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/**
* Tests for the ChangeMergedMessageGeneratorTest class. The expected behavior is that the
* ChangeMergedMessageGenerator should publish regardless of a configured ignore pattern.
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class ChangeMergedMessageGeneratorTest {
private static final String PROJECT_NAME = "test-project";
- private Project.NameKey mockNameKey = mock(Project.NameKey.class);
-
private PluginConfigFactory mockConfigFactory = mock(PluginConfigFactory.class);
private PluginConfig mockPluginConfig = mock(PluginConfig.class);
@@ -57,12 +48,6 @@
private AccountAttribute mockAccount = mock(AccountAttribute.class);
private ChangeAttribute mockChange = mock(ChangeAttribute.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig(boolean publishOnChangeMerged) throws Exception {
Project.NameKey projectNameKey;
projectNameKey = Project.NameKey.parse(PROJECT_NAME);
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGeneratorTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGeneratorTest.java
index 159993a..f2b7e5f 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGeneratorTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/message/CommentAddedMessageGeneratorTest.java
@@ -31,24 +31,15 @@
import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.events.CommentAddedEvent;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/**
* Tests for the CommentAddedMessageGeneratorTest class. The expected behavior is that the
* CommentAddedMessageGeneratorTest should publish regardless of a configured ignore pattern.
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class CommentAddedMessageGeneratorTest {
private static final String PROJECT_NAME = "test-project";
- private Project.NameKey mockNameKey = mock(Project.NameKey.class);
-
private PluginConfigFactory mockConfigFactory = mock(PluginConfigFactory.class);
private PluginConfig mockPluginConfig = mock(PluginConfig.class);
@@ -58,12 +49,6 @@
private ChangeAttribute mockChange = mock(ChangeAttribute.class);
private AccountAttribute mockOwner = mock(AccountAttribute.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig(
boolean publishOnCommentAdded,
boolean ignoreWorkInProgressPatchSet,
@@ -85,6 +70,7 @@
when(mockPluginConfig.getString("channel", "general")).thenReturn("testchannel");
when(mockPluginConfig.getString("username", "gerrit")).thenReturn("testuser");
when(mockPluginConfig.getString("ignore", "")).thenReturn("^WIP.*");
+ when(mockPluginConfig.getString("ignore-comment-author", "")).thenReturn("^jenkins.*");
when(mockPluginConfig.getBoolean("publish-on-comment-added", true))
.thenReturn(publishOnCommentAdded);
when(mockPluginConfig.getBoolean("ignore-wip-patch-set", true))
@@ -238,6 +224,21 @@
}
@Test
+ public void doesNotPublishWhenIgnoreAuthorMatches() throws Exception {
+ // Setup mocks
+ ProjectConfig config = getConfig();
+ mockEvent.change = Suppliers.ofInstance(mockChange);
+ mockEvent.author = Suppliers.ofInstance(mockAccount);
+ mockAccount.username = "jenkins";
+
+ // Test
+ MessageGenerator messageGenerator;
+ messageGenerator = MessageGeneratorFactory.newInstance(mockEvent, config);
+
+ assertThat(messageGenerator.shouldPublish(), is(false));
+ }
+
+ @Test
public void generatesExpectedMessage() throws Exception {
// Setup mocks
ProjectConfig config = getConfig();
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/message/PatchSetCreatedMessageGeneratorTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/message/PatchSetCreatedMessageGeneratorTest.java
index 64cbc70..ed47473 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/message/PatchSetCreatedMessageGeneratorTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/message/PatchSetCreatedMessageGeneratorTest.java
@@ -33,16 +33,9 @@
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.data.PatchSetAttribute;
import com.google.gerrit.server.events.PatchSetCreatedEvent;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/** Tests for the PatchSetCreatedMessageGeneratorTest class. */
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class PatchSetCreatedMessageGeneratorTest {
private static final String PROJECT_NAME = "test-project";
@@ -57,12 +50,6 @@
private ChangeAttribute mockChange = mock(ChangeAttribute.class);
private PatchSetAttribute mockPatchSet = mock(PatchSetAttribute.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig(
String ignore,
boolean publishOnPatchSetCreated,
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/message/PrivateStateChangedGeneratorTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/message/PrivateStateChangedGeneratorTest.java
index c9684cb..f2695cf 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/message/PrivateStateChangedGeneratorTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/message/PrivateStateChangedGeneratorTest.java
@@ -31,24 +31,15 @@
import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.events.PrivateStateChangedEvent;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/**
* Tests for the PrivateStateChangedGenerator class. The expected behavior is that the
* PrivateStateChangedGenerator should publish when the state changes.
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class PrivateStateChangedGeneratorTest {
private static final String PROJECT_NAME = "test-project";
- private Project.NameKey mockNameKey = mock(Project.NameKey.class);
-
private PluginConfigFactory mockConfigFactory = mock(PluginConfigFactory.class);
private PluginConfig mockPluginConfig = mock(PluginConfig.class);
@@ -57,12 +48,6 @@
private AccountAttribute mockAccount = mock(AccountAttribute.class);
private ChangeAttribute mockChange = mock(ChangeAttribute.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig(boolean publishOnPrivateToPublic) throws Exception {
Project.NameKey projectNameKey;
projectNameKey = Project.NameKey.parse(PROJECT_NAME);
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/message/ReviewerAddedMessageGeneratorTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/message/ReviewerAddedMessageGeneratorTest.java
index c41c92e..ebd6816 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/message/ReviewerAddedMessageGeneratorTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/message/ReviewerAddedMessageGeneratorTest.java
@@ -31,25 +31,16 @@
import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.events.ReviewerAddedEvent;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/**
* Tests for the ReviewerAddedMessageGeneratorTest class. The expected behavior is that the
* ReviewerAddedMessageGeneratorTest should publish if both the plugin and publish-on-reviewer-added
* are enabled.
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class ReviewerAddedMessageGeneratorTest {
private static final String PROJECT_NAME = "test-project";
- private Project.NameKey mockNameKey = mock(Project.NameKey.class);
-
private PluginConfigFactory mockConfigFactory = mock(PluginConfigFactory.class);
private PluginConfig mockPluginConfig = mock(PluginConfig.class);
@@ -58,12 +49,6 @@
private AccountAttribute mockAccount = mock(AccountAttribute.class);
private ChangeAttribute mockChange = mock(ChangeAttribute.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig(
boolean publishOnReviewerAdded,
boolean ignoreWorkInProgressPatchSet,
diff --git a/src/test/java/com/cisco/gerrit/plugins/slack/message/WorkInProgressStateChangedGeneratorTest.java b/src/test/java/com/cisco/gerrit/plugins/slack/message/WorkInProgressStateChangedGeneratorTest.java
index 33accac..afa7211 100644
--- a/src/test/java/com/cisco/gerrit/plugins/slack/message/WorkInProgressStateChangedGeneratorTest.java
+++ b/src/test/java/com/cisco/gerrit/plugins/slack/message/WorkInProgressStateChangedGeneratorTest.java
@@ -31,19 +31,12 @@
import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.data.ChangeAttribute;
import com.google.gerrit.server.events.WorkInProgressStateChangedEvent;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
/**
* Tests for the WorkInProgressStateChangedGenerator class. The expected behavior is that the
* WorkInProgressStateChangedGenerator should publish when the state changes.
*/
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({Project.NameKey.class})
public class WorkInProgressStateChangedGeneratorTest {
private static final String PROJECT_NAME = "test-project";
@@ -57,12 +50,6 @@
private AccountAttribute mockAccount = mock(AccountAttribute.class);
private ChangeAttribute mockChange = mock(ChangeAttribute.class);
- @Before
- public void setup() throws Exception {
- PowerMockito.mockStatic(Project.NameKey.class);
- when(Project.NameKey.parse(PROJECT_NAME)).thenReturn(mockNameKey);
- }
-
private ProjectConfig getConfig(boolean publishOnWipReady) throws Exception {
Project.NameKey projectNameKey;
projectNameKey = Project.NameKey.parse(PROJECT_NAME);