Merge branch 'stable-2.14'

* stable-2.14:
  Add configuration to disable granting project ownership
  Make project creator an owner if not already by inheritance
  Fix code block in documentation
  Allow project creation to delegating users
  Add eclipse-out to .gitignore
  Remove Buck build
  Add standalone bazel build
  Add missing "Implementation-Vendor" in manifest with bazel build
  Format BUILD file with buildifier

Change-Id: I40bbb19373f4252527a953b7c0f6034af79cf0c6
diff --git a/.buckconfig b/.buckconfig
deleted file mode 100644
index 0a84b64..0000000
--- a/.buckconfig
+++ /dev/null
@@ -1,15 +0,0 @@
-[alias]
-  project-group-structure = //:project-group-structure
-  plugin = //:project-group-structure
-  src = //:project-group-structure-sources
-
-[java]
-  src_roots = java, resources
-
-[project]
-  ignore = .git
-
-[cache]
-  mode = dir
-  dir = buck-out/cache
-
diff --git a/.gitignore b/.gitignore
index d781c5f..72f041f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,9 +1,6 @@
-/.buckd
-/.buckversion
 /.classpath
+/.primary_build_tool
 /.project
 /.settings/
-/.watchmanconfig
-/buck-out
-/bucklets
+/bazel-*
 /eclipse-out/
diff --git a/BUCK b/BUCK
deleted file mode 100644
index 84af6a0..0000000
--- a/BUCK
+++ /dev/null
@@ -1,68 +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_TESTS + GERRIT_PLUGIN_API + [
-  ':project-group-structure__plugin',
-  # bazlets include those 3 bouncycastle jars in plugin API so this is temporary
-  # until this plugin is built with bazel.
-  # see https://gerrit-review.googlesource.com/#/c/102670/ for more info.
-  ':bouncycastle_bcprov',
-  ':bouncycastle_bcpg',
-  ':bouncycastle_bcpkix',
-]
-
-gerrit_plugin(
-  name = 'project-group-structure',
-  srcs = SOURCES,
-  resources = RESOURCES,
-  manifest_entries = [
-    'Gerrit-PluginName: project-group-structure',
-    'Gerrit-ApiType: plugin',
-    'Gerrit-Module: com.ericsson.gerrit.plugins.projectgroupstructure.Module',
-    'Implementation-Title: project-group-structure plugin',
-    'Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/project-group-structure',
-    'Implementation-Vendor: Ericsson',
-  ],
-)
-
-java_test(
-  name = 'project-group-structure-tests',
-  srcs = glob(['src/test/java/**/*.java']),
-  labels = ['project-group-structure'],
-  deps = TEST_DEPS,
-)
-
-java_sources(
-  name = 'project-group-structure-sources',
-  srcs = SOURCES + RESOURCES,
-)
-
-# this is required for bucklets/tools/eclipse/project.py to work
-java_library(
-  name = 'classpath',
-  deps = TEST_DEPS,
-)
-
-BC_VERS = '1.56'
-
-maven_jar(
-  name = 'bouncycastle_bcprov',
-  id = 'org.bouncycastle:bcprov-jdk15on:' + BC_VERS,
-  sha1 = 'a153c6f9744a3e9dd6feab5e210e1c9861362ec7',
-)
-
-maven_jar(
-  name = 'bouncycastle_bcpg',
-  id = 'org.bouncycastle:bcpg-jdk15on:' + BC_VERS,
-  sha1 = '9c3f2e7072c8cc1152079b5c25291a9f462631f1',
-)
-
-maven_jar(
-  name = 'bouncycastle_bcpkix',
-  id = 'org.bouncycastle:bcpkix-jdk15on:' + BC_VERS,
-  sha1 = '4648af70268b6fdb24674fb1fd7c1fcc73db1231',
-)
diff --git a/BUILD b/BUILD
index cfd259d..41af36e 100644
--- a/BUILD
+++ b/BUILD
@@ -9,20 +9,30 @@
 gerrit_plugin(
     name = "project-group-structure",
     srcs = glob(["src/main/java/**/*.java"]),
-    resources = glob(["src/main/resources/**/*"]),
     manifest_entries = [
         "Gerrit-PluginName: project-group-structure",
         "Gerrit-Module: com.ericsson.gerrit.plugins.projectgroupstructure.Module",
         "Implementation-Title: project-group-structure plugin",
         "Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/project-group-structure",
+        "Implementation-Vendor: Ericsson",
     ],
+    resources = glob(["src/main/resources/**/*"]),
 )
 
 junit_tests(
     name = "project_group_structure_tests",
     srcs = glob(["src/test/java/**/*.java"]),
     tags = ["project-group-structure"],
-    deps = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+    deps = [
+        ":project-group-structure__plugin_test_deps",
+    ],
+)
+
+java_library(
+    name = "project-group-structure__plugin_test_deps",
+    testonly = 1,
+    visibility = ["//visibility:public"],
+    exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
         ":project-group-structure__plugin",
     ],
 )
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..5679ceb
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,25 @@
+workspace(name = "project_group_structure")
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "87908ae95402aa78dcb29075a7822509c9d04af6",
+    #    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/gerrit/BUCK b/lib/gerrit/BUCK
deleted file mode 100644
index f1f5562..0000000
--- a/lib/gerrit/BUCK
+++ /dev/null
@@ -1,19 +0,0 @@
-include_defs('//bucklets/maven_jar.bucklet')
-
-VER = '2.15-SNAPSHOT'
-REPO = MAVEN_LOCAL
-
-maven_jar(
-  name = 'plugin-api',
-  id = 'com.google.gerrit:gerrit-plugin-api:' + VER,
-  license = 'Apache2.0',
-  attach_source = False,
-  repository = REPO,
-)
-maven_jar(
-  name = 'acceptance-framework',
-  id = 'com.google.gerrit:gerrit-acceptance-framework:' + VER,
-  license = 'Apache2.0',
-  attach_source = False,
-  repository = REPO,
-)
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index 935077b..630c6fb 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -1,17 +1,69 @@
-Build
-=====
+# Build
 
-This @PLUGIN@ plugin is built with Bazel.
+This plugin can be built with Bazel, and two build modes are supported:
 
-Clone (or link) this plugin to the `plugins` directory of Gerrit's source tree.
+* Standalone
+* In Gerrit tree
 
-Then issue
+Standalone build mode is recommended, as this mode doesn't require local Gerrit
+tree to exist. Moreover, there are some limitations and additional manual steps
+required when building in Gerrit tree mode (see corresponding sections).
+
+## Build standalone
+
+To build the plugin, issue the following command:
+
+```
+  bazel build @PLUGIN@
+```
+
+The output is created in
+
+```
+  bazel-genfiles/@PLUGIN@.jar
+```
+
+To package the plugin sources run:
+
+```
+  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:
+
+```
+  ./tools/eclipse/project.sh
+```
+
+## Build in Gerrit tree
+
+Clone or link this plugin to the plugins directory of Gerrit's
+source tree. From Gerrit source tree issue the command:
 
 ```
   bazel build plugins/@PLUGIN@
 ```
 
-in the root of Gerrit's source tree to build
+Note that due to a [known issue in Bazel][bazelissue], if the plugin
+has previously been built in standalone mode, it is necessary to clean
+the workspace before building in-tree:
+
+```
+  cd plugins/@PLUGIN@
+  bazel clean --expunge
+```
 
 The output is created in
 
@@ -19,9 +71,9 @@
   bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar
 ```
 
-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:
+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
@@ -30,5 +82,11 @@
 To execute the tests run:
 
 ```
-  bazel test plugins/@PLUGIN@:project_group_structure_tests
+  bazel test --test_tag_filters=@PLUGIN@ //...
 ```
+
+
+[Back to @PLUGIN@ documentation index][index]
+
+[index]: index.html
+[bazelissue]: https://github.com/bazelbuild/bazel/issues/2797
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/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..1691f5d
--- /dev/null
+++ b/tools/eclipse/BUILD
@@ -0,0 +1,17 @@
+load("//tools/bzl:classpath.bzl", "classpath_collector")
+
+java_library(
+    name = "classpath",
+    testonly = 1,
+    runtime_deps = [
+        "//:project-group-structure__plugin_test_deps",
+    ],
+)
+
+classpath_collector(
+    name = "main_classpath_collect",
+    testonly = 1,
+    deps = [
+        "//:project-group-structure__plugin_test_deps",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..be0fd7a
--- /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 project-group-structure -r .
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..5320776
--- /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_PROJECT-GROUP-STRUCTURE_LABEL $(rev .)