Add repository rule to provide gerrit api version

In I0a786a91147 Gerrit-ApiVersion attribute was added to the MANIFEST.MF
in plugin rule in tree build mode. This change extends standalone build
mode to do the same.

There is one complication, though, because standalone build mode can use
release plugin API or SNAPSHOT and the resulting api version should be
switched as well. Use repository rule, that generates the version.txt
file and depend on it in plugin rule.

Test Plan:

1. Switch reviewers plugin to using bazlets repository from this commit
2. Confirm that building reviewers plugin with release plugin API would
add the following line to the MANIFEST.MF file:

  Gerrit-ApiVersion: 2.16.21

3. Change to gerrit workspace and publish SNAPSHOT plugin artifacts to
   the local Maven repository
4. Confirm that building reviewers plugin with SNAPSHOT plugin API would
add the following line to the MANIFEST.MF file:

  Gerrit-ApiVersion: 2.16.22-SNAPSHOT

Change-Id: I6f17e6c6bbfd4d6fba7bcda8a3f5fc1403c4dc4e
diff --git a/gerrit_api.bzl b/gerrit_api.bzl
index 5a0d296..67bed73 100644
--- a/gerrit_api.bzl
+++ b/gerrit_api.bzl
@@ -1,4 +1,5 @@
 load("//:bouncycastle.bzl", "bouncycastle_repos")
+load("//:gerrit_api_version.bzl", "gerrit_api_version")
 load("//:rules_python.bzl", "rules_python_repos")
 load("//tools:maven_jar.bzl", "maven_jar")
 
@@ -9,6 +10,11 @@
 VER = "2.16.21"
 
 def gerrit_api():
+    gerrit_api_version(
+        name = "gerrit_api_version",
+        version = VER,
+    )
+
     bouncycastle_repos()
     rules_python_repos()
 
diff --git a/gerrit_api_maven_local.bzl b/gerrit_api_maven_local.bzl
index a052e85..1926649 100644
--- a/gerrit_api_maven_local.bzl
+++ b/gerrit_api_maven_local.bzl
@@ -1,4 +1,5 @@
 load("//:bouncycastle.bzl", "bouncycastle_repos")
+load("//:gerrit_api_version.bzl", "gerrit_api_version")
 load("//:rules_python.bzl", "rules_python_repos")
 load("//tools:maven_jar.bzl", "MAVEN_LOCAL", "maven_jar")
 
@@ -6,9 +7,14 @@
 gerrit_api is rule for fetching Gerrit plugin API using Bazel.
 """
 
-VER = "2.16.5-SNAPSHOT"
+VER = "2.16.22-SNAPSHOT"
 
 def gerrit_api_maven_local():
+    gerrit_api_version(
+        name = "gerrit_api_version",
+        version = VER,
+    )
+
     bouncycastle_repos()
     rules_python_repos()
 
diff --git a/gerrit_api_version.bzl b/gerrit_api_version.bzl
new file mode 100644
index 0000000..58fa01a
--- /dev/null
+++ b/gerrit_api_version.bzl
@@ -0,0 +1,27 @@
+# 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.
+
+def _gerrit_api_version_impl(ctx):
+    ctx.file("version.txt", "Gerrit-ApiVersion: " + ctx.attr.version, False)
+    ctx.file("BUILD.bazel", 'exports_files(["version.txt"])', False)
+
+gerrit_api_version = repository_rule(
+    implementation = _gerrit_api_version_impl,
+    local = True,
+    attrs = {
+        "version": attr.string(
+            mandatory = True,
+        ),
+    },
+)
diff --git a/gerrit_plugin.bzl b/gerrit_plugin.bzl
index b878179..e5b1f43 100644
--- a/gerrit_plugin.bzl
+++ b/gerrit_plugin.bzl
@@ -112,13 +112,17 @@
             "TZ=UTC",
             "export TZ",
             "GEN_VERSION=$$(cat $(location :%s__gen_stamp_info))" % name,
+            "API_VERSION=$$(cat $(location @gerrit_api_version//:version.txt))",
             "cd $$TMP",
             "unzip -q $$ROOT/$<",
-            "echo \"Implementation-Version: $$GEN_VERSION\n$$(cat META-INF/MANIFEST.MF)\" > META-INF/MANIFEST.MF",
+            "echo \"Implementation-Version: $$GEN_VERSION\n$$API_VERSION\n$$(cat META-INF/MANIFEST.MF)\" > META-INF/MANIFEST.MF",
             "find . -exec touch '{}' ';'",
             "zip -Xqr $$ROOT/$@ .",
         ]),
-        tools = [":%s__gen_stamp_info" % name],
+        tools = [
+            ":%s__gen_stamp_info" % name,
+            "@gerrit_api_version//:version.txt",
+        ],
         outs = ["%s%s.jar" % (name, target_suffix)],
         visibility = ["//visibility:public"],
     )