Merge branch 'stable-2.15'

* stable-2.15:
  Add Eclipse generated files to .gitignore
  AutomaticMerger: Format with google-java-format
  AtomicityHelper: Remove usage of deprecated SubmitInput.waitForMerge
  Stop using deprecated Files.toString
  Add missing test deps for Eclipse project
  Set executable flag on tools/eclipse/project.sh
  Add standalone build with 2.14.17 API
  Fix partial merge of cross-repo

Also update WORKSPACE to build with 2.16-rc3 API.

Change-Id: I2b965d677539da7dc9a7c5bdeda37baa35366e7e
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..4ed16cf
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1,2 @@
+build --workspace_status_command=./tools/workspace-status.sh
+test --build_tests_only
diff --git a/.gitignore b/.gitignore
index 32858aa..629ad35 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,11 @@
 
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 hs_err_pid*
+
+# Bazel generated files
+/bazel-*
+
+# Eclipse generated files
+.classpath
+.project
+/eclipse-out
diff --git a/BUILD b/BUILD
index c3f7e9a..cd6ea18 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,4 @@
-load("//tools/bzl:plugin.bzl", "gerrit_plugin", "PLUGIN_DEPS", "PLUGIN_TEST_DEPS")
+load("//tools/bzl:plugin.bzl", "PLUGIN_DEPS", "PLUGIN_TEST_DEPS", "gerrit_plugin")
 load("//tools/bzl:junit.bzl", "junit_tests")
 
 gerrit_plugin(
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..b7f2d49
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,26 @@
+workspace(name = "autosubmitter")
+
+load("//:bazlets.bzl", "load_bazlets")
+
+load_bazlets(
+    commit = "d044cd60d37e302d9ac324f7a2c4e48b4011d4e6",
+    #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..f089af4
--- /dev/null
+++ b/bazlets.bzl
@@ -0,0 +1,18 @@
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
+
+NAME = "com_googlesource_gerrit_bazlets"
+
+def load_bazlets(
+        commit,
+        local_path = None):
+    if not local_path:
+        git_repository(
+            name = NAME,
+            remote = "https://gerrit.googlesource.com/bazlets",
+            commit = commit,
+        )
+    else:
+        native.local_repository(
+            name = NAME,
+            path = local_path,
+        )
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
index 9c41867..d61a77b 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
@@ -154,8 +154,7 @@
       if (atomicityHelper.isAtomicReview(change)) {
         attemptToMergeAtomic(change);
       } else {
-        log.info("Submitting non-atomic change {}...", change.number);
-        atomicityHelper.mergeReview(change.project, change.number);
+        attemptToMergeNonAtomic(change);
       }
     }
   }
@@ -224,6 +223,22 @@
     }
   }
 
+  private void attemptToMergeNonAtomic(Change change) throws Exception {
+    // There may be a parent commit that it not merged while having all approvals
+    // because it is part of a cross-repo. We take care to not let Gerrit merge it
+    // by merging only the commits whose parents are already merged.
+    boolean dependsOnNonMergedCommit =
+        atomicityHelper.hasDependentReview(change.project, change.number);
+    if (dependsOnNonMergedCommit) {
+      log.info(
+          "Change {} is not mergeable because it depends on a non merged commit.", change.number);
+      return;
+    }
+
+    log.info("Submitting non-atomic change {}...", change.number);
+    atomicityHelper.mergeReview(change.project, change.number);
+  }
+
   private void processNewAtomicPatchSet(Change change) {
     try {
       checkReviewExists(change.number);
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..d60b6e3
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,82 @@
+# Build
+
+This plugin can be built with Bazel, and two build modes are supported:
+
+* Standalone
+* In Gerrit tree
+
+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
+```
+
+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@
+```
+
+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
+
+```
+  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:
+
+```
+  ./tools/eclipse/project.py
+```
+
+How to build the Gerrit Plugin API is described in the [Gerrit
+documentation](../../../Documentation/dev-bazel.html#_extension_and_plugin_api_jar_files).
+
+[Back to @PLUGIN@ documentation index][index]
+
+[index]: index.html
+[bazelissue]: https://github.com/bazelbuild/bazel/issues/2797
diff --git a/test.rb b/test.rb
index 3df4ebe..859d375 100755
--- a/test.rb
+++ b/test.rb
@@ -107,6 +107,20 @@
     check_status(commit2, 'MERGED')
   end
 
+  def test_commit_above_non_mergeable_crossrepo
+    commit1a = create_review(PROJECT1, "review1a on #{PROJECT1}", "crossrepo/topic1")
+    commit1b = create_review(PROJECT1, "review1b on #{PROJECT1}")
+    commit2 = create_review(PROJECT2, "review2 on #{PROJECT2}", "crossrepo/topic1")
+    approve_review(commit1a)
+    check_status(commit1a, 'NEW')
+    check_status(commit1b, 'NEW')
+    check_status(commit2, 'NEW')
+
+    approve_review(commit1b)
+
+    check_status(commit1b, 'NEW')
+  end
+
   def test_refupdatedevent_merge_upper_commit
     commit1a = create_review(PROJECT1, "review1a on #{PROJECT1}")
     commit1b = create_review(PROJECT1, "review1b on #{PROJECT1}")
diff --git a/tools/bzl/BUILD b/tools/bzl/BUILD
new file mode 100644
index 0000000..f40498e
--- /dev/null
+++ b/tools/bzl/BUILD
@@ -0,0 +1 @@
+# Empty BUILD file, needed by Bazel.
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/plugin.bzl b/tools/bzl/plugin.bzl
new file mode 100644
index 0000000..0b25d23
--- /dev/null
+++ b/tools/bzl/plugin.bzl
@@ -0,0 +1,6 @@
+load(
+    "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
+    "PLUGIN_DEPS",
+    "PLUGIN_TEST_DEPS",
+    "gerrit_plugin",
+)
diff --git a/tools/eclipse/BUILD b/tools/eclipse/BUILD
new file mode 100644
index 0000000..790d222
--- /dev/null
+++ b/tools/eclipse/BUILD
@@ -0,0 +1,9 @@
+load("//tools/bzl:plugin.bzl", "PLUGIN_DEPS", "PLUGIN_TEST_DEPS")
+load("//tools/bzl:classpath.bzl", "classpath_collector")
+
+classpath_collector(
+    name = "main_classpath_collect",
+    deps = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+        "//:autosubmitter__plugin",
+    ],
+)
diff --git a/tools/eclipse/project.sh b/tools/eclipse/project.sh
new file mode 100755
index 0000000..f62d372
--- /dev/null
+++ b/tools/eclipse/project.sh
@@ -0,0 +1,15 @@
+#!/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/eclipse:project --output location | sed s/BUILD:.*//`project.py -n autosubmitter -r .
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..948d260
--- /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_AUTOSUBMITTER_LABEL $(rev .)