Merge branch 'stable-2.13' into stable-2.14

* stable-2.13:
  Make uploadpack rate limit exceeded message configurable
  Make configuration examples consistent
  Fix code block in documentation

Change-Id: I41c4c03973f08a1a479fa673005446e49263cf53
diff --git a/.buckconfig b/.buckconfig
deleted file mode 100644
index 8301d1b..0000000
--- a/.buckconfig
+++ /dev/null
@@ -1,14 +0,0 @@
-[alias]
-  quota = //:quota
-  plugin = //:quota
-
-[java]
-  src_roots = java, resources
-
-[project]
-  ignore = .git
-
-[cache]
-  mode = dir
-  dir = buck-out/cache
-
diff --git a/.gitignore b/.gitignore
index 869f732..72f041f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,11 +1,6 @@
-/.buckversion
-/.buckd
-/buck-out
-/bucklets
-/target
 /.classpath
+/.primary_build_tool
 /.project
-/.settings/org.maven.ide.eclipse.prefs
-/.settings/org.eclipse.m2e.core.prefs
-.buckversion
-.watchmanconfig
+/.settings/
+/bazel-*
+/eclipse-out/
diff --git a/BUCK b/BUCK
deleted file mode 100644
index 4d527fb..0000000
--- a/BUCK
+++ /dev/null
@@ -1,30 +0,0 @@
-include_defs('//bucklets/gerrit_plugin.bucklet')
-
-gerrit_plugin(
-  name = 'quota',
-  srcs = glob(['src/main/java/**/*.java']),
-  resources = glob(['src/main/resources/**/*']),
-  manifest_entries = [
-    'Gerrit-PluginName: quota',
-    'Gerrit-Module: com.googlesource.gerrit.plugins.quota.Module',
-  ],
-  provided_deps = [
-    '//lib/commons:lang',
-  ]
-)
-
-TEST_DEPS = GERRIT_PLUGIN_API + GERRIT_TESTS + [
-  ':quota__plugin'
-]
-
-java_library(
-  name = 'classpath',
-  deps = TEST_DEPS,
-)
-
-java_test(
-  name = 'quota_tests',
-  srcs = glob(['src/test/java/**/*.java']),
-  labels = ['quota'],
-  deps = TEST_DEPS,
-)
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..c8a682f
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,35 @@
+load("//tools/bzl:junit.bzl", "junit_tests")
+load(
+    "//tools/bzl:plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_TEST_DEPS",
+)
+
+gerrit_plugin(
+    name = "quota",
+    srcs = glob(["src/main/java/**/*.java"]),
+    manifest_entries = [
+        "Gerrit-PluginName: quota",
+        "Gerrit-Module: com.googlesource.gerrit.plugins.quota.Module",
+    ],
+    resources = glob(["src/main/resources/**/*"]),
+)
+
+junit_tests(
+    name = "quota_tests",
+    srcs = glob(["src/test/java/**/*.java"]),
+    tags = ["quota"],
+    deps = [
+        ":quota__plugin_test_deps",
+    ],
+)
+
+java_library(
+    name = "quota__plugin_test_deps",
+    testonly = 1,
+    visibility = ["//visibility:public"],
+    exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+        ":quota__plugin",
+    ],
+)
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..86afccc
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,26 @@
+workspace(name = "quota")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "e8160a65a591e602a2fb48953b269dd8d42a7c37",
+    #local_path = "/home/<user>/projects/bazlets",
+)
+
+#Snapshot Plugin API
+#load(
+#    "@com_googlesource_gerrit_bazlets//:gerrit_api_maven_local.bzl",
+#    "gerrit_api_maven_local",
+#)
+
+# Load snapshot Plugin API
+#gerrit_api_maven_local()
+
+# Release Plugin API
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl",
+    "gerrit_api",
+)
+
+# Load release Plugin API
+gerrit_api()
diff --git a/bazlets.bzl b/bazlets.bzl
new file mode 100644
index 0000000..e14e488
--- /dev/null
+++ b/bazlets.bzl
@@ -0,0 +1,17 @@
+NAME = "com_googlesource_gerrit_bazlets"
+
+def load_bazlets(
+    commit,
+    local_path = None
+  ):
+  if not local_path:
+      native.git_repository(
+          name = NAME,
+          remote = "https://gerrit.googlesource.com/bazlets",
+          commit = commit,
+      )
+  else:
+      native.local_repository(
+          name = NAME,
+          path = local_path,
+      )
diff --git a/lib/commons/BUCK b/lib/commons/BUCK
deleted file mode 100644
index be9dfe5..0000000
--- a/lib/commons/BUCK
+++ /dev/null
@@ -1,11 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-maven_jar(
-  name = 'lang',
-  id = 'commons-lang:commons-lang:2.5',
-  sha1 = 'b0236b252e86419eef20c31a44579d2aee2f0a69',
-  exclude = ['META-INF/LICENSE.txt', 'META-INF/NOTICE.txt'],
-  attach_source = False,
-  license = 'Apache2.0',
-)
-
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK
deleted file mode 100644
index a28cf4e..0000000
--- a/lib/gerrit/BUCK
+++ /dev/null
@@ -1,22 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-VER = '2.13.9'
-REPO = MAVEN_CENTRAL
-
-maven_jar(
-  name = 'plugin-api',
-  id = 'com.google.gerrit:gerrit-plugin-api:' + VER,
-  sha1 = 'a9b00025900f68c1f86036ea07591a2876b10c37',
-  attach_source = False,
-  repository = REPO,
-  license = 'Apache2.0',
-)
-
-maven_jar(
-  name = 'acceptance-framework',
-  id = 'com.google.gerrit:gerrit-acceptance-framework:' + VER,
-  sha1 ='53a6e2baca19303bbf3967b861cf9814323c31f6',
-  license = 'Apache2.0',
-  attach_source = False,
-  repository = REPO,
-)
diff --git a/pom.xml b/pom.xml
deleted file mode 100644
index 0c61ba6..0000000
--- a/pom.xml
+++ /dev/null
@@ -1,109 +0,0 @@
-<!--
-Copyright (C) 2014 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.
--->
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-
-  <groupId>com.googlesource.gerrit.plugins.quota</groupId>
-  <artifactId>quota</artifactId>
-  <packaging>jar</packaging>
-  <version>2.13-SNAPSHOT</version>
-  <name>quota</name>
-
-  <properties>
-    <Gerrit-ApiType>plugin</Gerrit-ApiType>
-    <Gerrit-ApiVersion>${project.version}</Gerrit-ApiVersion>
-  </properties>
-
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-jar-plugin</artifactId>
-        <version>2.4</version>
-        <configuration>
-          <archive>
-            <manifestEntries>
-              <Gerrit-PluginName>quota</Gerrit-PluginName>
-              <Gerrit-Module>com.googlesource.gerrit.plugins.quota.Module</Gerrit-Module>
-
-              <Implementation-Vendor>Gerrit Code Review</Implementation-Vendor>
-              <Implementation-URL>http://code.google.com/p/gerrit/</Implementation-URL>
-
-              <Implementation-Title>${Gerrit-ApiType} ${project.artifactId}</Implementation-Title>
-              <Implementation-Version>${project.version}</Implementation-Version>
-
-              <Gerrit-ApiType>${Gerrit-ApiType}</Gerrit-ApiType>
-              <Gerrit-ApiVersion>${Gerrit-ApiVersion}</Gerrit-ApiVersion>
-            </manifestEntries>
-          </archive>
-        </configuration>
-      </plugin>
-
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-compiler-plugin</artifactId>
-        <version>2.3.2</version>
-        <configuration>
-          <source>1.7</source>
-          <target>1.7</target>
-          <encoding>UTF-8</encoding>
-        </configuration>
-      </plugin>
-    </plugins>
-  </build>
-
-  <dependencies>
-    <dependency>
-      <groupId>com.google.gerrit</groupId>
-      <artifactId>gerrit-${Gerrit-ApiType}-api</artifactId>
-      <version>${Gerrit-ApiVersion}</version>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
-      <groupId>junit</groupId>
-      <artifactId>junit</artifactId>
-      <version>4.11</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.easymock</groupId>
-      <artifactId>easymock</artifactId>
-      <version>3.4</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>log4j</groupId>
-      <artifactId>log4j</artifactId>
-      <version>1.2.17</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-log4j12</artifactId>
-      <version>1.7.7</version>
-      <scope>test</scope>
-    </dependency>
-  </dependencies>
-
-  <repositories>
-    <repository>
-      <id>gerrit-api-repository</id>
-      <url>https://gerrit-api.storage.googleapis.com/snapshot/</url>
-    </repository>
-  </repositories>
-</project>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsConfig.java b/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsConfig.java
index c29ae7f..7567855 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsConfig.java
@@ -14,13 +14,13 @@
 
 package com.googlesource.gerrit.plugins.quota;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ArrayTable;
 import com.google.common.collect.Table;
 import java.text.MessageFormat;
 import java.util.Arrays;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
@@ -183,8 +183,8 @@
    */
   Optional<Map<String, RateLimit>> getRatelimits(Type type) {
     if (rateLimits != null) {
-      return Optional.fromNullable(rateLimits.row(type));
+      return Optional.ofNullable(rateLimits.row(type));
     }
-    return Optional.absent();
+    return Optional.empty();
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsFinder.java b/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsFinder.java
index 2cf7d6f..751735c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsFinder.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/AccountLimitsFinder.java
@@ -15,7 +15,6 @@
 
 import static com.googlesource.gerrit.plugins.quota.AccountLimitsConfig.KEY;
 
-import com.google.common.base.Optional;
 import com.google.gerrit.common.data.GroupDescription;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.account.GroupMembership;
@@ -31,6 +30,7 @@
 import org.slf4j.LoggerFactory;
 
 import java.util.Map;
+import java.util.Optional;
 
 public class AccountLimitsFinder {
   private static final Logger log =
@@ -64,11 +64,11 @@
           log.error("Ignoring limits for unknown group ''{}'' in quota.config",
               groupName);
         } else if (memberShip.contains(d.getGroupUUID())) {
-          return Optional.fromNullable(limits.get().get(groupName));
+          return Optional.ofNullable(limits.get().get(groupName));
         }
       }
     }
-    return Optional.absent();
+    return Optional.empty();
   }
 
   /**
@@ -78,9 +78,9 @@
    */
   public Optional<RateLimit> getRateLimit(Type type, String groupName) {
     if (getRatelimits(type).isPresent()) {
-      return Optional.fromNullable(getRatelimits(type).get().get(groupName));
+      return Optional.ofNullable(getRatelimits(type).get().get(groupName));
     }
-    return Optional.absent();
+    return Optional.empty();
   }
 
   /**
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java b/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java
index 240f42e..935498a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/Module.java
@@ -19,7 +19,6 @@
 import static com.google.gerrit.server.project.ProjectResource.PROJECT_KIND;
 import static com.googlesource.gerrit.plugins.quota.QuotaResource.QUOTA_KIND;
 
-import com.google.common.base.Optional;
 import com.google.common.cache.CacheLoader;
 import com.google.common.util.concurrent.RateLimiter;
 import com.google.gerrit.extensions.events.GarbageCollectorListener;
@@ -40,6 +39,7 @@
 import com.google.inject.Scopes;
 import com.google.inject.internal.UniqueAnnotations;
 import com.googlesource.gerrit.plugins.quota.AccountLimitsConfig.RateLimit;
+import java.util.Optional;
 import org.eclipse.jgit.transport.PostReceiveHook;
 
 class Module extends CacheModule {
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index e22de10..711aee0 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -1,102 +1,81 @@
-Build
-=====
+# Build
 
-This plugin can be built with Buck or Maven.
+This plugin can be built with Bazel, and two build modes are supported:
 
-Buck
-----
+* Standalone
+* In Gerrit tree
 
-Two build modes are supported: Standalone and in Gerrit tree.
-The standalone build mode is recommended, as this mode doesn't require
-the Gerrit tree to exist locally.
+Standalone build mode is recommended, as this mode doesn't require local Gerrit
+tree to exist.
 
-
-### Build standalone
-
-Clone bucklets library:
-
-```
-  git clone https://gerrit.googlesource.com/bucklets
-
-```
-and link it to quota plugin directory:
-
-```
-  cd quota && ln -s ../bucklets .
-```
-
-Add link to the .buckversion file:
-
-```
-  cd quota && ln -s bucklets/buckversion .buckversion
-```
-
-Add link to the .watchmanconfig file:
-```
-  cd server-config && ln -s bucklets/watchmanconfig .watchmanconfig
-```
+## Build standalone
 
 To build the plugin, issue the following command:
 
-
 ```
-  buck build plugin
+  bazel build @PLUGIN@
 ```
 
 The output is created in
 
 ```
-  buck-out/gen/quota.jar
+  bazel-genfiles/@PLUGIN@.jar
 ```
 
-Test are executed with
+To package the plugin sources run:
 
 ```
-  buck test
+  bazel build lib@PLUGIN@__plugin-src.jar
+```
+
+The output is created in:
+
+```
+  bazel-bin/lib@PLUGIN@__plugin-src.jar
+```
+
+To execute the tests run:
+
+```
+  bazel test //...
 ```
 
 This project can be imported into the Eclipse IDE:
 
 ```
-  ./bucklets/tools/eclipse.py
+  ./tools/eclipse/project.sh
 ```
 
-### Build in Gerrit tree
+## Build in Gerrit tree
 
-Clone or link this plugin to the plugins directory of Gerrit's source
-tree, and issue the command:
+Clone or link this plugin to the plugins directory of Gerrit's
+source tree. From Gerrit source tree issue the command:
 
 ```
-  buck build plugins/quota
+  bazel build plugins/@PLUGIN@
 ```
 
 The output is created in
 
 ```
-  buck-out/gen/plugins/quota/quota.jar
+  bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar
 ```
 
 This project can be imported into the Eclipse IDE:
+Add the plugin name to the `CUSTOM_PLUGINS` in `tools/bzl/plugins.bzl`, and
+execute:
 
 ```
   ./tools/eclipse/project.py
 ```
 
-Test are executed with
+To execute the tests run:
 
 ```
-  buck test --include quota-plugin
+  bazel test plugins/quota:quota_tests
 ```
 
-Maven
------
 
-Note that the Maven build is provided for compatibility reasons, but
-it is considered to be deprecated and will be removed in a future
-version of this plugin.
+[Back to @PLUGIN@ documentation index][index]
 
-To build with Maven, run
-
-```
-mvn clean package
-```
+[index]: index.html
diff --git a/src/test/java/com/googlesource/gerrit/plugins/quota/PublisherExceptionTest.java b/src/test/java/com/googlesource/gerrit/plugins/quota/PublisherExceptionTest.java
index 291b4dd..28d2ef7 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/quota/PublisherExceptionTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/quota/PublisherExceptionTest.java
@@ -26,7 +26,6 @@
 import org.apache.log4j.Logger;
 import org.apache.log4j.spi.LoggingEvent;
 import org.easymock.Capture;
-import org.easymock.EasyMock;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -57,7 +56,7 @@
 
   @Before
   public void setupLogging() {
-    captor = EasyMock.newCapture();
+    captor = new Capture<>();
     appender = createMock(Appender.class);
     appender.doAppend(capture(captor));
     expectLastCall().anyTimes();
diff --git a/tools/bazel.rc b/tools/bazel.rc
new file mode 100644
index 0000000..4ed16cf
--- /dev/null
+++ b/tools/bazel.rc
@@ -0,0 +1,2 @@
+build --workspace_status_command=./tools/workspace-status.sh
+test --build_tests_only
diff --git a/tools/bzl/BUILD b/tools/bzl/BUILD
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/bzl/BUILD
diff --git a/tools/bzl/classpath.bzl b/tools/bzl/classpath.bzl
new file mode 100644
index 0000000..dfcbe9c
--- /dev/null
+++ b/tools/bzl/classpath.bzl
@@ -0,0 +1,2 @@
+load("@com_googlesource_gerrit_bazlets//tools:classpath.bzl",
+     "classpath_collector")
diff --git a/tools/bzl/junit.bzl b/tools/bzl/junit.bzl
new file mode 100644
index 0000000..3af7e58
--- /dev/null
+++ b/tools/bzl/junit.bzl
@@ -0,0 +1,4 @@
+load(
+    "@com_googlesource_gerrit_bazlets//tools:junit.bzl",
+    "junit_tests",
+)
diff --git a/tools/bzl/maven_jar.bzl b/tools/bzl/maven_jar.bzl
new file mode 100644
index 0000000..2eabedb
--- /dev/null
+++ b/tools/bzl/maven_jar.bzl
@@ -0,0 +1 @@
+load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", "maven_jar")
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..a2e438f
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,6 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_TEST_DEPS",
+)
diff --git a/tools/eclipse/BUILD b/tools/eclipse/BUILD
new file mode 100644
index 0000000..b1fced5
--- /dev/null
+++ b/tools/eclipse/BUILD
@@ -0,0 +1,9 @@
+load("//tools/bzl:classpath.bzl", "classpath_collector")
+
+classpath_collector(
+    name = "main_classpath_collect",
+    testonly = 1,
+    deps = [
+        "//:quota__plugin_test_deps",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..7f0e082
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# Copyright (C) 2017 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.
+`bazel query @com_googlesource_gerrit_bazlets//tools/eclipse:project --output location | sed s/BUILD:.*//`project.py -n quota -r .
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..5dcf283
--- /dev/null
+++ b/tools/workspace-status.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+# This script will be run by bazel when the build process starts to
+# generate key-value information that represents the status of the
+# workspace. The output should be like
+#
+# KEY1 VALUE1
+# KEY2 VALUE2
+#
+# If the script exits with non-zero code, it's considered as a failure
+# and the output will be discarded.
+
+function rev() {
+  cd $1 && git describe --always --match "v[0-9].*" --dirty
+}
+
+echo STABLE_BUILD_QUOTA_LABEL "$(rev .)"