Merge branch 'stable-3.0' into stable-3.1

* stable-3.0:
  Move plugin configuration to a separate file
  Upgrade bazlets to latest stable-3.0 to build with 3.0.12 API
  Upgrade bazlets to latest stable-2.16 to build with 2.16.22 API

Change-Id: Ie6d5079a3530a2adbe276f9d7399b0e8315cd882
diff --git a/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java b/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java
index c1a9b4a..2d89752 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfig.java
@@ -14,14 +14,15 @@
 
 package com.ericsson.gerrit.plugins.eventslog;
 
+import com.google.common.base.MoreObjects;
 import com.google.gerrit.extensions.annotations.PluginName;
-import com.google.gerrit.server.config.PluginConfig;
 import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.gerrit.server.config.SitePaths;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import org.eclipse.jgit.lib.Config;
 
 /** Holder of all things related to events-log plugin configuration. */
 @Singleton
@@ -64,22 +65,27 @@
 
   @Inject
   EventsLogConfig(PluginConfigFactory cfgFactory, SitePaths site, @PluginName String pluginName) {
-    PluginConfig cfg = cfgFactory.getFromGerritConfig(pluginName, true);
-    copyLocal = cfg.getBoolean(CONFIG_COPY_LOCAL, DEFAULT_COPY_LOCAL);
-    maxAge = cfg.getInt(CONFIG_MAX_AGE, DEFAULT_MAX_AGE);
-    maxTries = cfg.getInt(CONFIG_MAX_TRIES, DEFAULT_MAX_TRIES);
-    returnLimit = cfg.getInt(CONFIG_RETURN_LIMIT, DEFAULT_RETURN_LIMIT);
-    waitTime = cfg.getInt(CONFIG_WAIT_TIME, DEFAULT_WAIT_TIME);
-    connectTime = cfg.getInt(CONFIG_CONN_TIME, DEFAULT_CONN_TIME);
-    storeUrl = cfg.getString(CONFIG_URL, H2_DB_PREFIX + site.data_dir.resolve("db").normalize());
+    Config cfg = cfgFactory.getGlobalPluginConfig(pluginName);
+    copyLocal = cfg.getBoolean(pluginName, CONFIG_COPY_LOCAL, DEFAULT_COPY_LOCAL);
+    maxAge = cfg.getInt(pluginName, CONFIG_MAX_AGE, DEFAULT_MAX_AGE);
+    maxTries = cfg.getInt(pluginName, CONFIG_MAX_TRIES, DEFAULT_MAX_TRIES);
+    returnLimit = cfg.getInt(pluginName, CONFIG_RETURN_LIMIT, DEFAULT_RETURN_LIMIT);
+    waitTime = cfg.getInt(pluginName, CONFIG_WAIT_TIME, DEFAULT_WAIT_TIME);
+    connectTime = cfg.getInt(pluginName, CONFIG_CONN_TIME, DEFAULT_CONN_TIME);
+    storeUrl =
+        MoreObjects.firstNonNull(
+            cfg.getString(pluginName, null, CONFIG_URL),
+            H2_DB_PREFIX + site.data_dir.resolve("db").normalize());
     localStorePath =
         Paths.get(
-            cfg.getString(
-                CONFIG_LOCAL_PATH, site.site_path.resolve("events-db").normalize().toString()));
-    urlOptions = cfg.getStringList(CONFIG_URL_OPTIONS);
-    storeUsername = cfg.getString(CONFIG_USERNAME);
-    storePassword = cfg.getString(CONFIG_PASSWORD);
-    maxConnections = Math.max(cfg.getInt(CONFIG_MAX_CONNECTIONS, DEFAULT_MAX_CONNECTIONS), 1);
+            MoreObjects.firstNonNull(
+                cfg.getString(pluginName, null, CONFIG_LOCAL_PATH),
+                site.resolve("events-db").normalize().toString()));
+    urlOptions = cfg.getStringList(pluginName, null, CONFIG_URL_OPTIONS);
+    storeUsername = cfg.getString(pluginName, null, CONFIG_USERNAME);
+    storePassword = cfg.getString(pluginName, null, CONFIG_PASSWORD);
+    maxConnections =
+        Math.max(cfg.getInt(pluginName, CONFIG_MAX_CONNECTIONS, DEFAULT_MAX_CONNECTIONS), 1);
   }
 
   public int getMaxAge() {
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 1c74a20..95e060c 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -1,11 +1,11 @@
 @PLUGIN@ Configuration
 ===================
 
-File 'gerrit.config'
+File '@PLUGIN@.config'
 --------------------
 
 ```
-  [plugin "@PLUGIN@"]
+  [@PLUGIN@]
     maxAge = 20
     returnLimit = 10000
     storeUrl = jdbc:h2:<gerrit_site>/data/db
@@ -14,16 +14,16 @@
     copyLocal = true
 ```
 
-plugin.@PLUGIN@.maxAge
+@PLUGIN@.maxAge
 :    Specify the maximum allowed age in days of the entries in the database.
      Any entries that are older than this value will be removed every day at
      23:00 hours. When not specified, the default value is 30 days.
 
-plugin.@PLUGIN@.returnLimit
+@PLUGIN@.returnLimit
 :    Specify the max amount of events that will be returned for each query.
      When not specified, the default value is 5000.
 
-plugin.@PLUGIN@.storeUrl
+@PLUGIN@.storeUrl
 :    Specify the path to the directory in which to keep the database. When not
      specified, the default path is jdbc:h2:\<gerrit_site>/data/db.
      Supported database engines:
@@ -31,19 +31,19 @@
 * postgresql
 * mysql
 
-plugin.@PLUGIN@.localStorePath
+@PLUGIN@.localStorePath
 :    Specify the path to the directory in which to keep the back up database.
      When not specified, the default path is \<gerrit_site>/events-db/.
 
-plugin.@PLUGIN@.storeUsername
+@PLUGIN@.storeUsername
 :    Username to connect to the database, not defined by default. This value can
      also be defined in secure.config.
 
-plugin.@PLUGIN@.storePassword
+@PLUGIN@.storePassword
 :    Password to connect to the database, not defined by default. This value can
      also be defined in secure.config.
 
-plugin.@PLUGIN@.urlOptions
+@PLUGIN@.urlOptions
 :    Options to append to the database url. Each option should be specified in a
      separate line using the option=value format. For example:
 * `urlOptions = loglevel=INFO`
@@ -53,28 +53,28 @@
 
 * `urlOptions = allowMultiQueries=true`
 
-plugin.@PLUGIN@.maxTries
+@PLUGIN@.maxTries
 :    Maximum number of times the plugin should attempt to store the event if a
      loss in database connection occurs. Setting this value to 0 will disable
      retries. When not specified, the default value is 3. After this number of
      failed tries, events shall be stored in the back up database until connection
      can be established.
 
-plugin.@PLUGIN@.retryTimeout
+@PLUGIN@.retryTimeout
 :    Amount of time in milliseconds for which the plugin should wait in between
      event storage retries. When not specified, the default value is set to 1000ms.
 
-plugin.@PLUGIN@.connectTimeout
+@PLUGIN@.connectTimeout
 :    Interval of time in milliseconds for which the plugin should try to reconnect
      to the database. When not specified, the default value is set to 1000ms.
 
-plugin.@PLUGIN@.copyLocal
+@PLUGIN@.copyLocal
 :    To keep a copy of the backup database once main database connection is
      restored, set to true. The file will be copied to the same location as the
      backup database with a timestamp appended. Note that the copied file will
      not be deleted and must be removed manually. When not specified, the default
      value is set to false.
 
-plugin.@PLUGIN@.maxConnections
+@PLUGIN@.maxConnections
 :    Maximum number of instances in the connection pool to the database. Includes
      active and idle connections. By default 8.
diff --git a/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java b/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java
index 702328b..0448611 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/eventslog/EventsLogConfigTest.java
@@ -54,7 +54,6 @@
 @RunWith(MockitoJUnitRunner.class)
 public class EventsLogConfigTest {
   private static final String LOCAL_STORE_PATH = "~/gerrit/events-db/";
-  private static final String PLUGIN = "plugin";
   private static final String PLUGIN_NAME = "eventsLog";
   private static final int CUSTOM_MAX_CONNECTIONS = 32;
   private static final List<String> urlOptions = ImmutableList.of("DB_CLOSE_DELAY=10");
@@ -73,8 +72,8 @@
 
   @Test
   public void shouldReturnDefaultsWhenMissingConfig() {
-    PluginConfig pluginConfig = new PluginConfig(PLUGIN_NAME, new Config());
-    when(cfgFactoryMock.getFromGerritConfig(PLUGIN_NAME, true)).thenReturn(pluginConfig);
+    Config pluginConfig = new Config();
+    when(cfgFactoryMock.getGlobalPluginConfig(PLUGIN_NAME)).thenReturn(pluginConfig);
     EventsLogConfig eventsLogConfig = new EventsLogConfig(cfgFactoryMock, site, PLUGIN_NAME);
     assertThat(eventsLogConfig.getCopyLocal()).isFalse();
     assertThat(eventsLogConfig.getMaxAge()).isEqualTo(DEFAULT_MAX_AGE);
@@ -94,8 +93,7 @@
 
   @Test
   public void shouldReturnConfigValues() {
-    PluginConfig pluginConfig = new PluginConfig(PLUGIN_NAME, customConfig());
-    when(cfgFactoryMock.getFromGerritConfig(PLUGIN_NAME, true)).thenReturn(pluginConfig);
+    when(cfgFactoryMock.getGlobalPluginConfig(PLUGIN_NAME)).thenReturn(customConfig());
     EventsLogConfig eventsLogConfig = new EventsLogConfig(cfgFactoryMock, site, PLUGIN_NAME);
     assertThat(eventsLogConfig.getCopyLocal()).isTrue();
     assertThat(eventsLogConfig.getMaxAge()).isEqualTo(20);
@@ -113,18 +111,18 @@
 
   private Config customConfig() {
     Config config = new Config();
-    config.setBoolean(PLUGIN, PLUGIN_NAME, CONFIG_COPY_LOCAL, true);
-    config.setInt(PLUGIN, PLUGIN_NAME, CONFIG_MAX_AGE, 20);
-    config.setInt(PLUGIN, PLUGIN_NAME, CONFIG_MAX_TRIES, 5);
-    config.setInt(PLUGIN, PLUGIN_NAME, CONFIG_RETURN_LIMIT, 10000);
-    config.setInt(PLUGIN, PLUGIN_NAME, CONFIG_CONN_TIME, 5000);
-    config.setInt(PLUGIN, PLUGIN_NAME, CONFIG_WAIT_TIME, 5000);
-    config.setString(PLUGIN, PLUGIN_NAME, CONFIG_URL, "jdbc:h2:~/gerrit/db");
-    config.setString(PLUGIN, PLUGIN_NAME, CONFIG_LOCAL_PATH, LOCAL_STORE_PATH);
-    config.setStringList(PLUGIN, PLUGIN_NAME, CONFIG_URL_OPTIONS, urlOptions);
-    config.setString(PLUGIN, PLUGIN_NAME, CONFIG_USERNAME, "testUsername");
-    config.setString(PLUGIN, PLUGIN_NAME, CONFIG_PASSWORD, "testPassword");
-    config.setInt(PLUGIN, PLUGIN_NAME, CONFIG_MAX_CONNECTIONS, CUSTOM_MAX_CONNECTIONS);
+    config.setBoolean(PLUGIN_NAME, null, CONFIG_COPY_LOCAL, true);
+    config.setInt(PLUGIN_NAME, null, CONFIG_MAX_AGE, 20);
+    config.setInt(PLUGIN_NAME, null, CONFIG_MAX_TRIES, 5);
+    config.setInt(PLUGIN_NAME, null, CONFIG_RETURN_LIMIT, 10000);
+    config.setInt(PLUGIN_NAME, null, CONFIG_CONN_TIME, 5000);
+    config.setInt(PLUGIN_NAME, null, CONFIG_WAIT_TIME, 5000);
+    config.setString(PLUGIN_NAME, null, CONFIG_URL, "jdbc:h2:~/gerrit/db");
+    config.setString(PLUGIN_NAME, null, CONFIG_LOCAL_PATH, LOCAL_STORE_PATH);
+    config.setStringList(PLUGIN_NAME, null, CONFIG_URL_OPTIONS, urlOptions);
+    config.setString(PLUGIN_NAME, null, CONFIG_USERNAME, "testUsername");
+    config.setString(PLUGIN_NAME, null, CONFIG_PASSWORD, "testPassword");
+    config.setInt(PLUGIN_NAME, null, CONFIG_MAX_CONNECTIONS, CUSTOM_MAX_CONNECTIONS);
     return config;
   }
 }