Simplify submission for single changes

When submitting a single change, do not use the Gerrit API and Lucene
associated searches for avoiding involved misbehavior due to async
index updates.

The logic is simple: just merge the change. No additional complexity and
API calls are needed.

Change-Id: Ie80cfa63547f62164d3bc1fd35d0fc20f9fdc680
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
index 00604e4..0d59513 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
@@ -133,10 +133,11 @@
    * @throws OrmException
    * @throws IOException
    */
-  public void mergeReview(ChangeInfo info) throws RestApiException, NoSuchChangeException, OrmException, IOException {
+  public void mergeReview(String project, int changeNumber)
+      throws RestApiException, NoSuchChangeException, OrmException, IOException {
     final SubmitInput input = new SubmitInput();
     input.waitForMerge = true;
-    final RevisionResource r = getRevisionResource(info.project, info._number);
+    final RevisionResource r = getRevisionResource(project, changeNumber);
     submitter.apply(r, input);
   }
 
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 fbed498..0a2549e 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
@@ -138,8 +138,15 @@
   private void autoSubmitIfMergeable(ChangeAttribute change)
       throws OrmException, RestApiException, NoSuchChangeException, IOException, UpdateException {
     if (atomicityHelper.isSubmittable(change.project, change.number)) {
-      log.info(String.format("Change %d is submittable. Will try to merge all related changes.", change.number));
-      attemptToMerge(change);
+      if (atomicityHelper.isAtomicReview(change)) {
+        log.info(
+            String.format(
+                "Change %d is submittable. Will try to merge all related changes.", change.number));
+        attemptToMergeAtomic(change);
+      } else {
+        log.info(String.format("Change %d is submittable. Submitting ...", change.number));
+        atomicityHelper.mergeReview(change.project, change.number);
+      }
     }
   }
 
@@ -167,15 +174,14 @@
     return false;
   }
 
-  private void attemptToMerge(ChangeAttribute change) throws RestApiException, OrmException, NoSuchChangeException, IOException, UpdateException {
+  private void attemptToMergeAtomic(ChangeAttribute change)
+      throws RestApiException, OrmException, NoSuchChangeException, IOException, UpdateException {
     final List<ChangeInfo> related = Lists.newArrayList();
-    if (atomicityHelper.isAtomicReview(change)) {
-      related.addAll(api.changes().query("status: open AND topic: " + change.topic)
-          .withOption(ListChangesOption.CURRENT_REVISION).get());
-    } else {
-      ChangeApi changeApi = api.changes().id(change.project, change.branch, change.id);
-      related.add(changeApi.get(EnumSet.of(ListChangesOption.CURRENT_REVISION)));
-    }
+    related.addAll(
+        api.changes()
+            .query("status: open AND topic: " + change.topic)
+            .withOption(ListChangesOption.CURRENT_REVISION)
+            .get());
     boolean submittable = true;
     boolean mergeable = true;
     for (final ChangeInfo info : related) {
@@ -191,10 +197,10 @@
       if (mergeable) {
         log.debug(String.format("Change %d is mergeable", change.number));
         for (final ChangeInfo info : related) {
-          atomicityHelper.mergeReview(info);
+          atomicityHelper.mergeReview(info.project, info._number);
         }
       } else {
-	  reviewUpdater.commentOnReview(change.project, change.number, AutomergeConfig.CANT_MERGE_COMMENT_FILE);
+        reviewUpdater.commentOnReview(change.project, change.number, AutomergeConfig.CANT_MERGE_COMMENT_FILE);
       }
     }
   }