Merge branch 'stable-3.1' into stable-3.2

* stable-3.1:
  Support comment visibility
  Fix issues in actions documentation

Change-Id: I6ccb4fe4512803db7c935e481b0a83d5c6a7a6bd
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
index e76e66c..5544df2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
@@ -99,7 +99,10 @@
       log.debug("Trying to add comment for issue {}", issueKey);
       apiBuilder
           .getIssue(server)
-          .doPost(issueKey + "/comment", gson.toJson(new JiraComment(comment)), HTTP_CREATED);
+          .doPost(
+              issueKey + "/comment",
+              gson.toJson(new JiraComment(comment, server.getVisibility())),
+              HTTP_CREATED);
       log.debug("Comment added to issue {}", issueKey);
     } else {
       log.error("Issue {} does not exist or no access permission", issueKey);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraConfig.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraConfig.java
index 7c53b7f..e2bec95 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraConfig.java
@@ -33,6 +33,7 @@
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraURL;
+import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVisibilityType;
 import java.io.IOException;
 import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.lib.Config;
@@ -47,12 +48,16 @@
   static final String PROJECT_CONFIG_URL_KEY = "instanceUrl";
   static final String PROJECT_CONFIG_USERNAME_KEY = "username";
   static final String PROJECT_CONFIG_PASSWORD_KEY = "password";
+  static final String PROJECT_CONFIG_COMMENT_VISIBILITY_TYPE = "visibilityType";
+  static final String PROJECT_CONFIG_COMMENT_VISIBILITY_VALUE = "visibilityValue";
 
   private static final Logger log = LoggerFactory.getLogger(JiraConfig.class);
   private static final String COMMENTLINK = "commentlink";
   private static final String GERRIT_CONFIG_URL = "url";
   private static final String GERRIT_CONFIG_USERNAME = "username";
   private static final String GERRIT_CONFIG_PASSWORD = "password";
+  private static final String GERRIT_CONFIG_COMMENT_VISIBILITY_TYPE = "visibilityType";
+  private static final String GERRIT_CONFIG_COMMENT_VISIBILITY_VALUE = "visibilityValue";
 
   private final String pluginName;
   private final PluginConfigFactory cfgFactory;
@@ -87,6 +92,10 @@
         .url(gerritConfig.getString(pluginName, null, GERRIT_CONFIG_URL))
         .username(gerritConfig.getString(pluginName, null, GERRIT_CONFIG_USERNAME))
         .password(gerritConfig.getString(pluginName, null, GERRIT_CONFIG_PASSWORD))
+        .visibility(
+            gerritConfig.getEnum(
+                pluginName, null, GERRIT_CONFIG_COMMENT_VISIBILITY_TYPE, JiraVisibilityType.NOTSET),
+            gerritConfig.getString(pluginName, null, GERRIT_CONFIG_COMMENT_VISIBILITY_VALUE))
         .build();
   }
 
@@ -104,6 +113,12 @@
         .url(pluginConfig.getString(PROJECT_CONFIG_URL_KEY, null))
         .username(pluginConfig.getString(PROJECT_CONFIG_USERNAME_KEY, null))
         .password(pluginConfig.getString(PROJECT_CONFIG_PASSWORD_KEY, null))
+        .visibility(
+            pluginConfig.getEnum(
+                JiraVisibilityType.values(),
+                PROJECT_CONFIG_COMMENT_VISIBILITY_TYPE,
+                JiraVisibilityType.NOTSET),
+            pluginConfig.getString(PROJECT_CONFIG_COMMENT_VISIBILITY_VALUE, null))
         .build();
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfo.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfo.java
index b384189..4ffb72d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfo.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfo.java
@@ -16,9 +16,12 @@
 package com.googlesource.gerrit.plugins.its.jira;
 
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraURL;
+import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVisibility;
+import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVisibilityType;
 import java.net.MalformedURLException;
 
 public class JiraItsServerInfo {
+
   public static class Builder {
     private JiraItsServerInfo instance = new JiraItsServerInfo();
 
@@ -43,6 +46,15 @@
       return this;
     }
 
+    public Builder visibility(JiraVisibilityType type, String value) {
+      try {
+        instance.visibility = new JiraVisibility(type, value);
+      } catch (IllegalArgumentException e) {
+        instance.visibility = null;
+      }
+      return this;
+    }
+
     public JiraItsServerInfo build() {
       return instance;
     }
@@ -51,6 +63,7 @@
   private JiraURL url;
   private String username;
   private String password;
+  private JiraVisibility visibility;
 
   public static Builder builder() {
     return new JiraItsServerInfo.Builder();
@@ -68,6 +81,10 @@
     return password;
   }
 
+  public JiraVisibility getVisibility() {
+    return visibility;
+  }
+
   public boolean isValid() {
     return url != null && username != null && password != null;
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraComment.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraComment.java
index eb2755d..d629018 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraComment.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraComment.java
@@ -17,9 +17,11 @@
 public class JiraComment {
 
   private final String body;
+  private final JiraVisibility visibility;
 
-  public JiraComment(String body) {
+  public JiraComment(String body, JiraVisibility visibility) {
     this.body = body;
+    this.visibility = visibility;
   }
 
   public String getBody() {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVisibility.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVisibility.java
new file mode 100644
index 0000000..6037dbb
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVisibility.java
@@ -0,0 +1,46 @@
+// Copyright (C) 2020 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.its.jira.restapi;
+
+import com.googlesource.gerrit.plugins.its.jira.JiraItsServerInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class JiraVisibility {
+  private static final Logger log = LoggerFactory.getLogger(JiraItsServerInfo.class);
+
+  private final JiraVisibilityType type;
+  private final String value;
+
+  public JiraVisibility(JiraVisibilityType type, String value) {
+    if (type != JiraVisibilityType.NOTSET && value != null) {
+      this.type = type;
+      this.value = value;
+    } else {
+      if (type != JiraVisibilityType.NOTSET || value != null) {
+        log.error("visibilityType and visibilityValue must be set together");
+      }
+      throw new IllegalArgumentException();
+    }
+  }
+
+  public JiraVisibilityType getType() {
+    return type;
+  }
+
+  public String getValue() {
+    return value;
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVisibilityType.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVisibilityType.java
new file mode 100644
index 0000000..d9c7a29
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVisibilityType.java
@@ -0,0 +1,36 @@
+// Copyright (C) 2020 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.its.jira.restapi;
+
+import com.google.gson.annotations.SerializedName;
+
+public enum JiraVisibilityType {
+  NOTSET(null),
+
+  @SerializedName("role")
+  ROLE("role"),
+  @SerializedName("group")
+  GROUP("group");
+
+  private String type;
+
+  JiraVisibilityType(String type) {
+    this.type = type;
+  }
+
+  public String toString() {
+    return this.type;
+  }
+}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 62a30df..9e35208 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -149,20 +149,20 @@
 
     [rule "open"]
         event-type = patchset-created
-        action = add-velocity-comment inline Change ${its.formatLink($changeUrl)} is created.
+        action = add-standard-comment
         action = In Progress
     [rule "resolve"]
         event-type = comment-added
-        approval-Code-Review = 2
-        action = add-velocity-comment inline Change ${its.formatLink($changeUrl)} is verified.
+        approvalCodeReview = 2
+        action = add-standard-comment
         action = In Review
     [rule "merged"]
         event-type = change-merged
-        action = add-velocity-comment inline Change ${its.formatLink($changeUrl)} is merged.
+        action = add-standard-comment
         action = Done
     [rule "abandoned"]
-        event-type = change-abandoned'
-        action = add-velocity-comment inline Change ${its.formatLink($changeUrl)} is abandoned.
+        event-type = change-abandoned
+        action = add-standard-comment
         action = To Do
 
 The first rule triggers an action which adds a comment and a hyperlink to the change created
@@ -171,14 +171,6 @@
 in Jira to `In Progress`. The title of the action `In Progress` should match the workflow actions
 used by the JIRA server as different versions of JIRA can have different workflow actions.
 
-**Note:** Velocity comments were deprecated in Gerrit 2.14 and will be removed in Gerrit 2.16/3.0;
-the `actions-@Plugin@.config` needs to be changed accordingly. For example, to use Soy comments
-instead of velocity comments:
-
-    [rule "open"]
-        event-type = patchset-created
-        action = add-soy-comment Change ${its.formatLink($changeUrl)} is created.
-        action = In Progress
 
 Multiple Jira servers integration
 ---------------------------------
@@ -257,4 +249,27 @@
 
 ```
   action = mark-property-as-released-version ref
-```
\ No newline at end of file
+```
+
+### add-comment
+
+The `add-comment`, `add-standard-comment` or `add-soy-comment` actions add comment for
+certain events. By default, these comments are visible to all on JIRA instance.
+
+It is possible to get better control over comments visibility by adding configuration
+entries in *gerrit.config* or *project.config* file in the *refs/meta/config* branch.
+
+A typical visibility configuration will look like:
+
+```
+  [plugin "its-jira"]
+    visibilityType = role
+    visibilityValue = Dev
+```
+
+This will publish comments visible only by users with *role* set as *Dev* in JIRA.
+
+`visibilityType` and `visibilityValue` must be set together.
+
+`visibilityType` could be set to **role** or **group**, and `visibilityValue` refers
+to JIRA *role* or *group*.
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java
index 08e8e29..e304b28 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java
@@ -15,12 +15,15 @@
 package com.googlesource.gerrit.plugins.its.jira;
 
 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
 import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
 import static com.github.tomakehurst.wiremock.client.WireMock.noContent;
 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
 import static com.google.gerrit.server.project.ProjectCache.illegalState;
 import static java.lang.String.format;
 import static java.net.HttpURLConnection.HTTP_CREATED;
@@ -69,7 +72,7 @@
 
   private Path its_dir;
 
-  @Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
+  @Rule public WireMockRule wireMockRule = new WireMockRule(options().port(PORT));
 
   @Override
   public void beforeTest(Description description) throws Exception {
@@ -212,6 +215,30 @@
     verifyIssueCall();
   }
 
+  @Test
+  @GerritConfig(name = COMMENT_SECTION + ".match", value = "([A-Z]+-[0-9]+)")
+  @GerritConfig(
+      name = COMMENT_SECTION + ".html",
+      value = "<a href=\"" + URL + "/browse/$1\">$1</a>")
+  @GerritConfig(name = COMMENT_SECTION + ".association", value = "SUGGESTED")
+  @GerritConfig(name = PLUGIN_NAME + ".url", value = URL)
+  @GerritConfig(name = PLUGIN_NAME + ".username", value = "user")
+  @GerritConfig(name = PLUGIN_NAME + ".password", value = "pass")
+  @GerritConfig(name = PLUGIN_NAME + ".visibilityType", value = "group")
+  @GerritConfig(name = PLUGIN_NAME + ".visibilityValue", value = "AllDev")
+  public void testIssueWithVisibility() throws Exception {
+    createItsRulesConfigWithComment();
+    mockServerCall();
+    mockCommentCall();
+    wireMockRule.givenThat(
+        WireMock.get(urlEqualTo(BASE_PREFIX + ISSUE_CLASS_PREFIX + JIRA_ISSUE)).willReturn(ok()));
+
+    createChangeWithIssue();
+
+    verifyIssueCall();
+    verifyCommentCallWithVisibility();
+  }
+
   private void mockServerCall() {
     wireMockRule.resetRequests();
     wireMockRule.givenThat(
@@ -257,6 +284,14 @@
             urlEqualTo(BASE_PREFIX + ISSUE_CLASS_PREFIX + JIRA_ISSUE + COMMENT_CLASS_PREFIX)));
   }
 
+  private void verifyCommentCallWithVisibility() {
+    wireMockRule.verify(
+        postRequestedFor(
+                urlEqualTo(BASE_PREFIX + ISSUE_CLASS_PREFIX + JIRA_ISSUE + COMMENT_CLASS_PREFIX))
+            .withRequestBody(matchingJsonPath("$.visibility.type", equalTo("group")))
+            .withRequestBody(matchingJsonPath("$.visibility.value", equalTo("AllDev"))));
+  }
+
   private void createChangeWithIssue() throws Exception {
     pushFactory
         .create(admin.newIdent(), testRepo, JIRA_ISSUE, "a.txt", "test")