Merge branch 'stable-2.13' into stable-2.14

* stable-2.13:
  Change copyright to AOSP

Change-Id: Idb74d73ba469885dc3c90d1e21fc04f75ee83910
diff --git a/.gitignore b/.gitignore
index 39971b0..78d2b4f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,6 @@
-/.buckversion
-/.buckd
-/buck-out
-/bucklets
-/target
 /.classpath
 /.project
 /.settings
+/.primary_build_tool
+/bazel-*
+/eclipse-out
diff --git a/BUCK b/BUCK
deleted file mode 100644
index b43a5f7..0000000
--- a/BUCK
+++ /dev/null
@@ -1,67 +0,0 @@
-include_defs('//bucklets/gerrit_plugin.bucklet')
-include_defs('//bucklets/java_sources.bucklet')
-include_defs('//bucklets/maven_jar.bucklet')
-
-SOURCES = glob(['src/main/java/**/*.java'])
-RESOURCES = glob(['src/main/resources/**/*'])
-
-TEST_DEPS = GERRIT_PLUGIN_API + GERRIT_TESTS + [
-   ':go-import__plugin',
-   ':mockito',
-]
-
-gerrit_plugin(
-   name = 'go-import',
-   srcs = SOURCES,
-   resources = RESOURCES,
-   manifest_entries = [
-     'Gerrit-PluginName: go-import',
-     'Gerrit-ApiType: plugin',
-     'Gerrit-HttpModule: com.ericsson.gerrit.plugins.goimport.HttpModule',
-     'Implementation-Title: go-import plugin',
-     'Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/go-import',
-     'Implementation-Vendor: Ericsson',
-   ],
-)
-
-java_library(
-   name = 'classpath',
-   deps = TEST_DEPS,
-)
-
-java_test(
-   name = 'go-import_tests',
-   labels = ['go-import'],
-   srcs = glob(['src/test/java/**/*.java']),
-   deps = TEST_DEPS,
-)
-
-java_sources(
-   name = 'go-import-sources',
-   srcs = SOURCES + RESOURCES,
-)
-
-maven_jar(
-  name = 'mockito',
-  id = 'org.mockito:mockito-core:2.7.19',
-  sha1 = '9e0dbe97eca58ef4c26e3b9e1a12ee42e76a63a5',
-  license = 'DO_NOT_DISTRIBUTE',
-  deps = [
-    ':byte-buddy',
-    ':objenesis',
-  ],
-)
-maven_jar(
-  name = 'byte-buddy',
-  id = 'net.bytebuddy:byte-buddy:1.6.11',
-  sha1 = '8a8f9409e27f1d62c909c7eef2aa7b3a580b4901',
-  license = 'DO_NOT_DISTRIBUTE',
-  attach_source = False,
-)
-maven_jar(
-  name = 'objenesis',
-  id = 'org.objenesis:objenesis:2.5',
-  sha1 = '612ecb799912ccf77cba9b3ed8c813da086076e9',
-  license = 'DO_NOT_DISTRIBUTE',
-  attach_source = False,
-)
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..d07240a
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,40 @@
+load("//tools/bzl:junit.bzl", "junit_tests")
+load(
+    "//tools/bzl:plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+    "PLUGIN_TEST_DEPS",
+)
+
+gerrit_plugin(
+    name = "go-import",
+    srcs = glob(["src/main/java/**/*.java"]),
+    manifest_entries = [
+        "Gerrit-PluginName: go-import",
+        "Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/go-import",
+        "Implementation-Title: go-import plugin",
+        "Implementation-Vendor: Ericsson",
+        "Gerrit-HttpModule: com.ericsson.gerrit.plugins.goimport.HttpModule",
+    ],
+    resources = glob(["src/main/resources/**/*"]),
+)
+
+junit_tests(
+    name = "go_import_tests",
+    testonly = 1,
+    srcs = glob(["src/test/java/**/*.java"]),
+    tags = ["go-import"],
+    deps = [
+        ":go-import__plugin_test_deps",
+    ],
+)
+
+java_library(
+    name = "go-import__plugin_test_deps",
+    testonly = 1,
+    visibility = ["//visibility:public"],
+    exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+        ":go-import__plugin",
+        "@mockito//jar",
+    ],
+)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2ede74f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+This plugin allows you to perform `go get` requests against a Gerrit project.
+
+For more information, see: `src/main/resources/Documentation/about.md`
+
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..312aef0
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,30 @@
+workspace(name = "go_import")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "11ce7521051ca73598d099aa8a396c9ffe932a74",
+    #    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()
+
+load("//:external_plugin_deps.bzl", "external_plugin_deps")
+
+external_plugin_deps()
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/external_plugin_deps.bzl b/external_plugin_deps.bzl
new file mode 100644
index 0000000..135e5f6
--- /dev/null
+++ b/external_plugin_deps.bzl
@@ -0,0 +1,24 @@
+load("//tools/bzl:maven_jar.bzl", "maven_jar")
+
+def external_plugin_deps():
+  maven_jar(
+    name = "mockito",
+    artifact = "org.mockito:mockito-core:2.5.0",
+    sha1 = "be28d46a52c7f2563580adeca350145e9ce916f8",
+    deps = [
+      "@byte_buddy//jar",
+      "@objenesis//jar",
+    ],
+  )
+
+  maven_jar(
+    name = "byte_buddy",
+    artifact = "net.bytebuddy:byte-buddy:1.5.12",
+    sha1 = "b1ba1d15f102b36ed43b826488114678d6d413da",
+  )
+
+  maven_jar(
+    name = "objenesis",
+    artifact = "org.objenesis:objenesis:2.4",
+    sha1 = "2916b6c96b50c5b3ec4452ed99401db745aabb27",
+  )
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK
deleted file mode 100644
index 9899bf3..0000000
--- a/lib/gerrit/BUCK
+++ /dev/null
@@ -1,22 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-VER = '2.13.7'
-REPO = MAVEN_CENTRAL
-
-maven_jar(
-  name = 'plugin-api',
-  id = 'com.google.gerrit:gerrit-plugin-api:' + VER,
-  sha1 = '3eeb112375ba0eb75a81b8da62a657d7e6fb82c2',
-  attach_source = False,
-  repository = REPO,
-  license = 'Apache2.0',
-)
-
-maven_jar(
-   name = 'acceptance-framework',
-   id = 'com.google.gerrit:gerrit-acceptance-framework:' + VER,
-   sha1 = '150c7f7171f2ba91d65462d443aec75c0fd923db',
-   license = 'Apache2.0',
-   attach_source = False,
-   repository = REPO,
-)
diff --git a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
index 6a76aa6..6b9e2c0 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
@@ -93,15 +93,40 @@
       String goGet = req.getParameter("go-get");
       if ("1".equals(goGet)) {
         String projectName = getProjectName(servletPath);
+        // Because Gerrit allows for arbitrary-depth project names
+        // (that is, both "a" and "a/b/c" are both legal), we are going
+        // to find the most specific such project that matches the path.
+        //
+        // For example, assume that we have the following projects:
+        //    a
+        //    a/b
+        // 1. If the requested path is "a", then project "a" would be chosen.
+        // 2. If the requested path is "a/b", then project "a/b" would be chosen.
+        // 3. If the requested path is "a/c", then project "a" would be chosen.
+        // 4. If the requested path is "a/b/c/d", then project "a/b" would be chosen.
+        // 5. If the requested path is "x/y/z", then this will fail with a 404 error.
+        String[] pathParts = projectName.split("/");
 
         byte[] tosend = PAGE_404.getBytes();
         rsp.setStatus(404);
-        if (projectExists(projectName)) {
-          tosend = PAGE_200.replace("${content}", getContent(projectName))
-              .getBytes();
-          rsp.setStatus(200);
-        }
+        // Start with the longest-length project name first.
+        for( int length = pathParts.length; length > 0; length-- ) {
+          // Create a new project name of the specified length; each time that we
+          // go through this loop, the project name will become shorter and shorter.
+          projectName = "";
+          for( int i = 0; i < length; i++ ) {
+            if( i > 0 ) {
+              projectName += "/";
+            }
+            projectName += pathParts[i];
+          }
 
+          if (projectExists(projectName)) {
+            tosend = PAGE_200.replace("${content}", getContent(projectName)).getBytes();
+            rsp.setStatus(200);
+            break;
+          }
+        }
         CacheHeaders.setNotCacheable(rsp);
         rsp.setContentType("text/html");
         rsp.setCharacterEncoding(HtmlDomUtil.ENC.name());
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 41a5908..d2b86eb 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -1,6 +1,7 @@
 This plugin provides support of
 [remote import paths](https://golang.org/cmd/go/#hdr-Remote_import_paths) and
-[go-get command](https://golang.org/cmd/go/).
+[go-get command](https://golang.org/cmd/go/), which means that you can perform
+`go get` (and thus `dep`) requests against a Gerrit project.
 
 ### Usage
 
@@ -16,10 +17,33 @@
 import "example.org/foo"
 ```
 
-### Limitation
-Folder of the repository is not supported in the context of Gerrit up to
-ambiguity of repository name. E.g., _example.org/foo/bar_ has two potential meanings:
+### Packages
+Go packages may be imported by specifying the package's _folder_ after the repository name.
+For example, "import github.com/bob/my-project/package1" will download the repository from
+`github.com/bob/my-project` and then include the package `package1`.
 
-* Folder _bar_ in repository _foo_, or
-* repository _foo/bar_.
+For Gerrit, this is still possible, but it's a little bit more ambiguous.
+On GitHub, all projects have a depth of two: (1) group and (2) repository.
+On Gerrit, any project may have an arbitrary depth.
+Thus, the following project names are valid in Gerrit:
+
+1. `bob`
+1. `bob/my-project`
+1. `bob/my-project/some-other-project`
+1. `tom`
+1. `tom/my-project`
+
+When a user requests a package via `go get`, this plugin will attempt to match the _most specific_
+project and return that.
+
+Using our previous examples of existing projects, this plugin will return the following projects
+for the given requests:
+
+| Request                            | Project          |
+| ---------------------------------- | ---------------- |
+| `/bob`                             | `bob`            |
+| `/bob/package1`                    | `bob`            |
+| `/bob/my-project`                  | `bob/my-project` |
+| `/bob/my-project/package1`         | `bob/my-project` |
+| `/bob/my-project/package1/folder2` | `bob/my-project` |
 
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index ddbf282..f2bfa00 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -1,7 +1,7 @@
 Build
 =====
 
-This plugin is built with Buck.
+This plugin is built with Bazel.
 
 Two build modes are supported: Standalone and in Gerrit tree. Standalone
 build mode is recommended, as this mode doesn't require local Gerrit
@@ -10,63 +10,28 @@
 Build standalone
 ----------------
 
-Clone bucklets library:
-
-```
-  git clone https://gerrit.googlesource.com/bucklets
-```
-and link it to @PLUGIN@ directory:
-
-```
-  cd @PLUGIN@ && ln -s ../bucklets .
-```
-
-Add link to the .buckversion file:
-
-```
-  cd @PLUGIN@ && ln -s bucklets/buckversion .buckversion
-```
-
-Add link to the .watchmanconfig file:
-
-```
-  cd @PLUGIN@ && ln -s bucklets/watchmanconfig .watchmanconfig
-```
-
 To build the plugin, issue the following command:
 
 ```
-  buck build plugin
+  bazel build @PLUGIN@
 ```
 
-The output is created in:
+The output is created in
 
 ```
-  buck-out/gen/@PLUGIN@.jar
-```
-
-This project can be imported into the Eclipse IDE:
-
-```
-  ./bucklets/tools/eclipse.py
+  bazel-genfiles/@PLUGIN@.jar
 ```
 
 To execute the tests run:
 
 ```
-  buck test
+  bazel test //...
 ```
 
-To build plugin sources run:
+This project can be imported into the Eclipse IDE:
 
 ```
-  buck build src
-```
-
-The output is created in:
-
-```
-  buck-out/gen/@PLUGIN@-sources.jar
+  ./tools/eclipse/project.sh
 ```
 
 Build in Gerrit tree
@@ -76,7 +41,7 @@
 tree, and issue the command:
 
 ```
-  buck build plugins/@PLUGIN@
+  bazel build plugins/@PLUGIN@
 ```
 
 The output is created in:
@@ -85,7 +50,9 @@
   buck-out/gen/plugins/@PLUGIN@/@PLUGIN@.jar
 ```
 
-This project can be imported into the Eclipse IDE:
+This project can be imported into the Eclipse IDE.
+Add the plugin name to the `CUSTOM_PLUGINS` set in
+Gerrit core in`tools/bzl/plugins.bzl`, and execute:
 
 ```
   ./tools/eclipse/project.py
@@ -94,7 +61,7 @@
 To execute the tests run:
 
 ```
-  buck test --include @PLUGIN@
+  bazel test plugins/@PLUGIN@:go_import_tests
 ```
 
 How to build the Gerrit Plugin API is described in the [Gerrit
diff --git a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
index 627c820..b2a015c 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
@@ -120,7 +120,7 @@
   public void testDoFilterWithExistingProject() throws Exception {
     when(mockRequest.getServletPath()).thenReturn("/projectName");
     when(mockRequest.getParameter("go-get")).thenReturn("1");
-    when(mockProjectCache.get(any(Project.NameKey.class))).thenReturn(mockProjectState);
+    when(mockProjectCache.get(new Project.NameKey("projectName"))).thenReturn(mockProjectState);
     unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
     verify(mockOutputStream, times(1)).write(PAGE_200.getBytes());
     verify(mockChain, times(0)).doFilter(mockRequest, mockResponse);
@@ -129,6 +129,18 @@
   }
 
   @Test
+  public void testDoFilterWithExistingProjectAndPackage() throws Exception {
+    when(mockRequest.getServletPath()).thenReturn("/projectName/my/package");
+    when(mockRequest.getParameter("go-get")).thenReturn("1");
+    when(mockProjectCache.get(new Project.NameKey("projectName"))).thenReturn(mockProjectState);
+    unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
+    verify(mockOutputStream, times(1)).write(PAGE_200.getBytes());
+    verify(mockChain, times(0)).doFilter(mockRequest, mockResponse);
+    verify(mockProjectCache, times(3)).get(any(Project.NameKey.class));
+    verify(mockResponse, times(1)).setStatus(200);
+  }
+
+  @Test
   public void testDoFilterWithNonExistingProject() throws Exception {
     when(mockRequest.getServletPath()).thenReturn("/projectName");
     when(mockRequest.getParameter("go-get")).thenReturn("1");
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..d5764f7
--- /dev/null
+++ b/tools/bzl/classpath.bzl
@@ -0,0 +1,4 @@
+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..84b4e61
--- /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 = [
+        "//:go-import__plugin_test_deps",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..2027669
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,19 @@
+#!/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 go-import -r .
+
+
diff --git a/tools/sonar/sonar.sh b/tools/sonar/sonar.sh
new file mode 100644
index 0000000..8df06d3
--- /dev/null
+++ b/tools/sonar/sonar.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+# Copyright (C) 2018 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/sonar:sonar --output location | sed s/BUILD:.*//`sonar.py
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..db4244c
--- /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_GO-IMPORT_LABEL $(rev .)