Merge branch 'stable-3.2'

Change-Id: I1ed519b543e478736a459df3a2e5950f04e4fc83
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 9c3f550..339d16c 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();
   }
 
@@ -124,7 +139,7 @@
         config.addCommentLinkSection(commentlinkSection);
         md.getCommitBuilder().setAuthor(serverUser);
         md.getCommitBuilder().setCommitter(serverUser);
-        projectCache.evict(config.getProject());
+        projectCache.evict(config.getProject().getNameKey());
         config.commit(md);
       }
     } catch (ConfigInvalidException | IOException e) {
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 59e00c9..a86b80c 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -156,20 +156,20 @@
 ```ini
     [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
 ```
 
@@ -270,3 +270,26 @@
 ```ini
   action = mark-property-as-released-version ref
 ```
+
+### 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 2a53aae..a656a06 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 java.lang.String.format;
 import static java.net.HttpURLConnection.HTTP_CREATED;
 import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
@@ -70,7 +73,7 @@
 
   private Path its_dir;
 
-  @Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
+  @Rule public WireMockRule wireMockRule = new WireMockRule(options().port(PORT));
   @Inject private ProjectOperations projectOperations;
 
   @Override
@@ -213,6 +216,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(
@@ -258,6 +285,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")