Add action to add a plain comment to an issue
Change-Id: I06b2b1bba40d5099f44531eae8fa8ed3d55e4c32
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/ItsHookModule.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/ItsHookModule.java
index 16acf53..c2a6183 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/ItsHookModule.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/ItsHookModule.java
@@ -29,6 +29,7 @@
import com.googlesource.gerrit.plugins.hooks.workflow.ActionController;
import com.googlesource.gerrit.plugins.hooks.workflow.Property;
import com.googlesource.gerrit.plugins.hooks.workflow.Rule;
+import com.googlesource.gerrit.plugins.hooks.workflow.action.AddComment;
import com.googlesource.gerrit.plugins.hooks.workflow.action.AddStandardComment;
import com.googlesource.gerrit.plugins.hooks.workflow.action.AddVelocityComment;
@@ -59,6 +60,7 @@
factory(Property.Factory.class);
factory(Condition.Factory.class);
factory(Rule.Factory.class);
+ factory(AddComment.Factory.class);
factory(AddStandardComment.Factory.class);
factory(AddVelocityComment.Factory.class);
}
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutor.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutor.java
index 64fd014..ce098e2 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutor.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutor.java
@@ -23,6 +23,7 @@
import com.google.inject.Inject;
import com.googlesource.gerrit.plugins.hooks.its.ItsFacade;
import com.googlesource.gerrit.plugins.hooks.workflow.action.Action;
+import com.googlesource.gerrit.plugins.hooks.workflow.action.AddComment;
import com.googlesource.gerrit.plugins.hooks.workflow.action.AddStandardComment;
import com.googlesource.gerrit.plugins.hooks.workflow.action.AddVelocityComment;
@@ -34,14 +35,16 @@
ActionExecutor.class);
private final ItsFacade its;
+ private final AddComment.Factory addCommentFactory;
private final AddStandardComment.Factory addStandardCommentFactory;
private final AddVelocityComment.Factory addVelocityCommentFactory;
@Inject
- public ActionExecutor(ItsFacade its,
+ public ActionExecutor(ItsFacade its, AddComment.Factory addCommentFactory,
AddStandardComment.Factory addStandardCommentFactory,
AddVelocityComment.Factory addVelocityCommentFactory) {
this.its = its;
+ this.addCommentFactory = addCommentFactory;
this.addStandardCommentFactory = addStandardCommentFactory;
this.addVelocityCommentFactory = addVelocityCommentFactory;
}
@@ -51,8 +54,10 @@
try {
String name = actionRequest.getName();
Action action = null;
- if ("add-standard-comment".equals(name)) {
- action = addStandardCommentFactory.create();
+ if ("add-comment".equals(name)) {
+ action = addCommentFactory.create();
+ } else if ("add-standard-comment".equals(name)) {
+ action = addStandardCommentFactory.create();
} else if ("add-velocity-comment".equals(name)) {
action = addVelocityCommentFactory.create();
}
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/action/AddComment.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/action/AddComment.java
new file mode 100644
index 0000000..b83d205
--- /dev/null
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/action/AddComment.java
@@ -0,0 +1,54 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.hooks.workflow.action;
+
+import java.io.IOException;
+import java.util.Set;
+
+import org.apache.commons.lang.StringUtils;
+
+import com.google.common.base.Strings;
+import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.hooks.its.ItsFacade;
+import com.googlesource.gerrit.plugins.hooks.workflow.ActionRequest;
+import com.googlesource.gerrit.plugins.hooks.workflow.Property;
+
+/**
+ * Adds a fixed comment to an issue.
+ *
+ * The action requests parameters get concatenated and get added to the issue.
+ */
+public class AddComment implements Action {
+ public interface Factory {
+ AddComment create();
+ }
+
+ private final ItsFacade its;
+
+ @Inject
+ public AddComment(ItsFacade its) {
+ this.its = its;
+ }
+
+ @Override
+ public void execute(String issue, ActionRequest actionRequest,
+ Set<Property> properties) throws IOException {
+ String[] parameters = actionRequest.getParameters();
+ String comment = StringUtils.join(parameters, " ");
+ if (!Strings.isNullOrEmpty(comment)) {
+ its.addComment(issue, comment);
+ }
+ }
+}
diff --git a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutorTest.java b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutorTest.java
index 9974967..7cc2597 100644
--- a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutorTest.java
+++ b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/ActionExecutorTest.java
@@ -26,6 +26,7 @@
import com.google.inject.Injector;
import com.googlesource.gerrit.plugins.hooks.its.ItsFacade;
import com.googlesource.gerrit.plugins.hooks.testutil.LoggingMockingTestCase;
+import com.googlesource.gerrit.plugins.hooks.workflow.action.AddComment;
import com.googlesource.gerrit.plugins.hooks.workflow.action.AddStandardComment;
import com.googlesource.gerrit.plugins.hooks.workflow.action.AddVelocityComment;
@@ -33,6 +34,7 @@
private Injector injector;
private ItsFacade its;
+ private AddComment.Factory addCommentFactory;
private AddStandardComment.Factory addStandardCommentFactory;
private AddVelocityComment.Factory addVelocityCommentFactory;
@@ -121,6 +123,23 @@
assertLogThrowableMessageContains("injected exception 3");
}
+ public void testAddCommentDelegation() throws IOException {
+ ActionRequest actionRequest = createMock(ActionRequest.class);
+ expect(actionRequest.getName()).andReturn("add-comment");
+
+ Set<Property> properties = Collections.emptySet();
+
+ AddComment addComment = createMock(AddComment.class);
+ expect(addCommentFactory.create()).andReturn(addComment);
+
+ addComment.execute("4711", actionRequest, properties);
+
+ replayMocks();
+
+ ActionExecutor actionExecutor = createActionExecutor();
+ actionExecutor.execute("4711", actionRequest, properties);
+ }
+
public void testAddStandardCommentDelegation() throws IOException {
ActionRequest actionRequest = createMock(ActionRequest.class);
expect(actionRequest.getName()).andReturn("add-standard-comment");
@@ -172,6 +191,9 @@
its = createMock(ItsFacade.class);
bind(ItsFacade.class).toInstance(its);
+ addCommentFactory = createMock(AddComment.Factory.class);
+ bind(AddComment.Factory.class).toInstance(addCommentFactory);
+
addStandardCommentFactory = createMock(AddStandardComment.Factory.class);
bind(AddStandardComment.Factory.class).toInstance(
addStandardCommentFactory);
diff --git a/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/action/AddCommentTest.java b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/action/AddCommentTest.java
new file mode 100644
index 0000000..d833c55
--- /dev/null
+++ b/hooks-its/src/test/java/com/googlesource/gerrit/plugins/hooks/workflow/action/AddCommentTest.java
@@ -0,0 +1,74 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.plugins.hooks.workflow.action;
+
+import static org.easymock.EasyMock.expect;
+
+import java.io.IOException;
+import java.util.HashSet;
+
+import com.google.gerrit.server.config.FactoryModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.googlesource.gerrit.plugins.hooks.its.ItsFacade;
+import com.googlesource.gerrit.plugins.hooks.testutil.LoggingMockingTestCase;
+import com.googlesource.gerrit.plugins.hooks.workflow.ActionRequest;
+import com.googlesource.gerrit.plugins.hooks.workflow.Property;
+
+public class AddCommentTest extends LoggingMockingTestCase {
+ private Injector injector;
+
+ private ItsFacade its;
+
+ public void testEmpty() throws IOException {
+ ActionRequest actionRequest = createMock(ActionRequest.class);
+ expect(actionRequest.getParameters()).andReturn(
+ new String[] {});
+
+ replayMocks();
+
+ AddComment addComment = createAddComment();
+ addComment.execute("4711", actionRequest, new HashSet<Property>());
+ }
+
+ public void testPlain() throws IOException {
+ ActionRequest actionRequest = createMock(ActionRequest.class);
+ expect(actionRequest.getParameters()).andReturn(
+ new String[] {"Some", "test", "comment"});
+
+ its.addComment("4711", "Some test comment");
+
+ replayMocks();
+
+ AddComment addComment = createAddComment();
+ addComment.execute("4711", actionRequest, new HashSet<Property>());
+ }
+
+ private AddComment createAddComment() {
+ return injector.getInstance(AddComment.class);
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ injector = Guice.createInjector(new TestModule());
+ }
+
+ private class TestModule extends FactoryModule {
+ @Override
+ protected void configure() {
+ its = createMock(ItsFacade.class);
+ bind(ItsFacade.class).toInstance(its);
+ }
+ }
+}
\ No newline at end of file