Merge "Allow adding ITS comments upon adding changes in gerrit"
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 0d1e5c1..9079808 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
@@ -41,20 +41,32 @@
 
   @Override
   public void doFilter(PatchSetCreatedEvent patchsetCreated) throws IOException {
-    if (!(gerritConfig.getBoolean(its.name(), null, "commentOnPatchSetCreated",
-        true))) {
-      return;
-    }
+    boolean addPatchSetComment = gerritConfig.getBoolean(its.name(), null,
+        "commentOnPatchSetCreated", true);
 
-    String gitComment =
-        getComment(patchsetCreated.change.project,
-            patchsetCreated.patchSet.revision);
-    String[] issues = getIssueIds(gitComment);
+    boolean addChangeComment = "1".equals(patchsetCreated.patchSet.number) &&
+        gerritConfig.getBoolean(its.name(), null, "commentOnChangeCreated",
+            false);
 
-    for (String issue : issues) {
-      its.addRelatedLink(issue, new URL(patchsetCreated.change.url),
-          "Gerrit Patch Set " + patchsetCreated.change.id + "/"
-              + patchsetCreated.patchSet.number);
+    if (addPatchSetComment || addChangeComment) {
+      String gitComment =
+          getComment(patchsetCreated.change.project,
+              patchsetCreated.patchSet.revision);
+
+      String[] issues = getIssueIds(gitComment);
+
+      for (String issue : issues) {
+        if (addChangeComment) {
+          its.addRelatedLink(issue, new URL(patchsetCreated.change.url),
+              "Gerrit Change " + patchsetCreated.change.id);
+        }
+
+        if (addPatchSetComment) {
+          its.addRelatedLink(issue, new URL(patchsetCreated.change.url),
+              "Gerrit Patch-Set " + patchsetCreated.change.id + "/"
+                  + patchsetCreated.patchSet.number);
+        }
+      }
     }
   }
 }
diff --git a/hooks-its/src/main/resources/Documentation/config.md b/hooks-its/src/main/resources/Documentation/config.md
index 9167fe3..adaaf47 100644
--- a/hooks-its/src/main/resources/Documentation/config.md
+++ b/hooks-its/src/main/resources/Documentation/config.md
@@ -27,6 +27,11 @@
 +
 Default is `true`.
 
+[[itsName.commentOnChangeCreated]]itsName.commentOnChangeCreated::
++
+If true, creating a change adds an ITS comment to the change's associated issue.
++
+Default is `false`.
 
 [[itsName.commentOnChangeMerged]]itsName.commentOnChangeMerged::
 +