Allow to turn off adding ITS comments based on the type of comment

Added the following configuration options:
* commentOnChangeAbandoned
* commentOnChangeMerged
* commentOnChangeRestored
* commentOnCommentAdded
* commentOnPatchSetCreated
* commentOnRefUpdatedGitWeb

Change-Id: Id4b88ce7047d7390950d8a9918a2d9cc12c4d8a1
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddComment.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddComment.java
index 5d178a9..c138d18 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddComment.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddComment.java
@@ -16,8 +16,11 @@
 
 import java.io.IOException;
 
+import org.eclipse.jgit.lib.Config;
+
 import com.google.common.base.Strings;
 import com.google.gerrit.server.config.AnonymousCowardName;
+import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.events.AccountAttribute;
 import com.google.gerrit.server.events.ApprovalAttribute;
 import com.google.gerrit.server.events.ChangeAbandonedEvent;
@@ -37,26 +40,48 @@
   @Inject @AnonymousCowardName
   private String anonymousCowardName;
 
+  @Inject
+  @GerritServerConfig
+  private Config gerritConfig;
+
   @Override
   public void doFilter(CommentAddedEvent hook) throws IOException {
+    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnCommentAdded",
+        true))) {
+      return;
+    }
+
     String comment = getComment(hook);
     addComment(hook.change, comment);
   }
 
   @Override
   public void doFilter(ChangeMergedEvent hook) throws IOException {
+    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnChangeMerged",
+        true))) {
+      return;
+    }
+
     String comment = getComment(hook);
     addComment(hook.change, comment);
   }
 
   @Override
   public void doFilter(ChangeAbandonedEvent hook) throws IOException {
+    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnChangeAbandoned",
+        true))) {
+      return;
+    }
     String comment = getComment(hook);
     addComment(hook.change, comment);
   }
 
   @Override
   public void doFilter(ChangeRestoredEvent hook) throws IOException {
+    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnChangeRestored",
+        true))) {
+      return;
+    }
     String comment = getComment(hook);
     addComment(hook.change, comment);
   }
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToChangeId.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToChangeId.java
index fe83bc2..ec46d48 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToChangeId.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToChangeId.java
@@ -17,9 +17,11 @@
 import java.io.IOException;
 import java.net.URL;
 
+import org.eclipse.jgit.lib.Config;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.events.PatchSetCreatedEvent;
 import com.google.inject.Inject;
 import com.googlesource.gerrit.plugins.hooks.its.ItsFacade;
@@ -33,8 +35,16 @@
   @Inject
   private ItsFacade its;
 
+  @Inject
+  @GerritServerConfig
+  private Config gerritConfig;
+
   @Override
   public void doFilter(PatchSetCreatedEvent patchsetCreated) throws IOException {
+    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnPatchSetCreated",
+        true))) {
+      return;
+    }
 
     String gitComment =
         getComment(patchsetCreated.change.project,
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToGitWeb.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToGitWeb.java
index 02d40e2..0db89d9 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToGitWeb.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilterAddRelatedLinkToGitWeb.java
@@ -52,6 +52,10 @@
 
   @Override
   public void doFilter(RefUpdatedEvent hook) throws IOException {
+    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnRefUpdatedGitWeb",
+        true))) {
+      return;
+    }
 
     String gitComment = getComment(hook.refUpdate.project,  hook.refUpdate.newRev);
     log.debug("Git commit " + hook.refUpdate.newRev + ": " + gitComment);
diff --git a/hooks-its/src/main/resources/Documentation/config.md b/hooks-its/src/main/resources/Documentation/config.md
new file mode 100644
index 0000000..9167fe3
--- /dev/null
+++ b/hooks-its/src/main/resources/Documentation/config.md
@@ -0,0 +1,63 @@
+hooks-its
+=========
+
+link:https://gerrit-review.googlesource.com/#/admin/projects/plugins/hooks-its['hooks-its']
+is by itself not a real plugin, but the common parent project for issue tracking
+system (ITS) plugins for gerrit, such as
+link:https://gerrit-review.googlesource.com/#/admin/projects/plugins/hooks-bugzilla['hooks-bugzilla'],
+or
+link:https://gerrit-review.googlesource.com/#/admin/projects/plugins/hooks-jira['hooks-jira'].
+
+
+
+Common configuration
+--------------------
+
+The base functionality for 'hooks-its' based plugins can be configured via the
+plugin's section (e.g.: `bugzilla` for 'hooks-bugzilla') within
+`etc/gerrit.config`. In the following description, we use `itsName` as
+placeholder for the plugin's name.  Be sure to replace it with the plugin's real
+name in the documentation of a 'hooks-its' based plugin (e.g.: Use `bugzilla`
+instead of `itsName` for 'hooks-bugzilla').
+
+[[itsName.commentOnChangeAbandoned]]itsName.commentOnChangeAbandoned::
++
+If true, abandoning a change adds an ITS comment to the change's associated
+issue.
++
+Default is `true`.
+
+
+[[itsName.commentOnChangeMerged]]itsName.commentOnChangeMerged::
++
+If true, merging a change's patch set adds an ITS comment to the change's
+associated issue.
++
+Default is `true`.
+
+[[itsName.commentOnChangeRestored]]itsName.commentOnChangeRestored::
++
+If true, restoring an abandoned change adds an ITS comment to the change's
+associated issue.
++
+Default is `true`.
+
+[[itsName.commentOnCommentAdded]]itsName.commentOnCommentAdded::
++
+If true, adding a comment and/or review to a change in gerrit adds an ITS
+comment to the change's associated issue.
++
+Default is `true`.
+
+[[itsName.commentOnPatchSetCreated]]itsName.commentOnPatchSetCreated::
++
+If true, creating a patch set for a change adds an ITS comment to the change's
+associated issue.
++
+Default is `true`.
+
+[[itsName.commentOnRefUpdatedGitWeb]]itsName.commentOnRefUpdatedGitWeb::
++
+If true, updating a ref adds a GitWeb link to the associated issue.
++
+Default is `true`.