Merge branch 'stable-2.11'

* stable-2.11:
  Use try-with-resources for AutoCloseable types
  [fix] only 500 changes are imported.

Change-Id: Icb5999918d8d570cfad2a446b4f0ccb4ee8e341a
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/CompleteProjectImport.java b/src/main/java/com/googlesource/gerrit/plugins/importer/CompleteProjectImport.java
index 1f116ee..c108e08 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/CompleteProjectImport.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/CompleteProjectImport.java
@@ -92,8 +92,7 @@
 
   private void deleteImportRefs(Project.NameKey project)
       throws RepositoryNotFoundException, IOException {
-    Repository repo = repoManager.openRepository(project);
-    try {
+    try (Repository repo = repoManager.openRepository(project)) {
       Map<String, Ref> refs = repo.getRefDatabase().getRefs(
           ConfigureRepositoryStep.R_IMPORTS);
       for (Ref ref : refs.values()) {
@@ -111,8 +110,6 @@
                 "Failed to delete %s, RefUpdate.Result = %s", ref, result));
         }
       }
-    } finally {
-      repo.close();
     }
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/GerritApi.java b/src/main/java/com/googlesource/gerrit/plugins/importer/GerritApi.java
index c5c7e5b..7d47c79 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/GerritApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/GerritApi.java
@@ -51,7 +51,7 @@
   public ProjectInfo getProject(String projectName) throws BadRequestException,
       IOException;
 
-  public List<ChangeInfo> queryChanges(String projectName)
+  public List<ChangeInfo> queryChanges(String projectName, int start, int limit)
       throws BadRequestException, IOException;
 
   public GroupInfo getGroup(String groupName) throws BadRequestException,
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java
index bc8061e..b8e1cf2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java
@@ -230,8 +230,7 @@
       checkProjectInSource(input, pm);
       setParentProjectName(input, pm);
       checkPreconditions(pm);
-      Repository repo = openRepoStep.open(targetProject, resume, pm);
-      try {
+      try (Repository repo = openRepoStep.open(targetProject, resume, pm)) {
         ImportJson.persist(lockFile, importJson.format(input, info), pm);
         configRepoStep.configure(repo, srcProject, input.from, pm);
         gitFetchStep.fetch(input.user, input.pass, repo, pm);
@@ -242,8 +241,6 @@
           importGroupsStepFactory.create(input.from, input.user, input.pass,
               targetProject, pm).importGroups();
         }
-      } finally {
-        repo.close();
       }
       importLog.onImport((IdentifiedUser) currentUser.get(), srcProject,
           targetProject, input.from);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/LocalApi.java b/src/main/java/com/googlesource/gerrit/plugins/importer/LocalApi.java
index 8234ef7..c9ec59f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/LocalApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/LocalApi.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.importer;
 
 import com.google.common.collect.Iterables;
+import com.google.gerrit.extensions.api.changes.Changes.QueryRequest;
 import com.google.gerrit.extensions.client.ListChangesOption;
 import com.google.gerrit.extensions.common.ChangeInfo;
 import com.google.gerrit.extensions.common.CommentInfo;
@@ -63,19 +64,26 @@
   }
 
   @Override
-  public List<ChangeInfo> queryChanges(String projectName) throws IOException,
+  public List<ChangeInfo> queryChanges(String projectName, int start,
+      int limit) throws IOException,
       BadRequestException {
     try {
-      return gApi.changes()
-          .query("project:" + projectName)
+
+      QueryRequest query = gApi.changes()
+          .query("project:" + projectName);
+      query
+          .withStart(start)
           .withOptions(
               ListChangesOption.DETAILED_LABELS,
               ListChangesOption.DETAILED_ACCOUNTS,
               ListChangesOption.MESSAGES,
               ListChangesOption.CURRENT_REVISION,
               ListChangesOption.ALL_REVISIONS,
-              ListChangesOption.ALL_COMMITS)
-          .get();
+              ListChangesOption.ALL_COMMITS);
+      if(limit > 0) {
+        query.withLimit(limit);
+      }
+      return query.get();
     } catch (RestApiException e) {
       throw new BadRequestException(e.getMessage());
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java b/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java
index 0473471..5ee6f06 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/RemoteApi.java
@@ -57,10 +57,11 @@
   }
 
   @Override
-  public List<ChangeInfo> queryChanges(String projectName) throws IOException,
-      BadRequestException {
+  public List<ChangeInfo> queryChanges(String projectName,
+      int start, int limit) throws IOException, BadRequestException {
     String endPoint =
-        "/changes/?q=project:" + projectName +
+        "/changes/?S=" +
+        start + ((limit > 0) ? "&n=" + limit : "") + "&q=project:" + projectName +
         "&O=" + Integer.toHexString(ListChangesOption.toBits(
             EnumSet.of(
                 ListChangesOption.DETAILED_LABELS,
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayChangesStep.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayChangesStep.java
index 781be9f..5cc44f3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayChangesStep.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ReplayChangesStep.java
@@ -16,6 +16,7 @@
 
 import com.google.common.collect.Iterators;
 import com.google.gerrit.common.Nullable;
+import com.google.gerrit.common.data.GlobalCapability;
 import com.google.gerrit.common.errors.NoSuchAccountException;
 import com.google.gerrit.extensions.client.ChangeStatus;
 import com.google.gerrit.extensions.common.ChangeInfo;
@@ -129,19 +130,32 @@
   void replay() throws IOException, OrmException,
       NoSuchAccountException, NoSuchChangeException, RestApiException,
       ValidationException {
-    List<ChangeInfo> changes = api.queryChanges(srcProject.get());
-
-    pm.beginTask("Replay Changes", changes.size());
-    try (RevWalk rw = new RevWalk(repo)) {
-      for (ChangeInfo c : changes) {
-        try {
-          replayChange(rw, c);
-        } catch (Exception e) {
-          log.error(String.format("Failed to replay change %s.",
-              Url.decode(c.id)), e);
-          throw e;
+    int start = 0;
+    int limit = GlobalCapability.DEFAULT_MAX_QUERY_LIMIT;
+    pm.beginTask("Replay Changes", ProgressMonitor.UNKNOWN);
+    for(;;) {
+      List<ChangeInfo> changes = api.queryChanges(srcProject.get(),
+          start, limit);
+      if(changes.isEmpty()) {
+        break;
+      }
+      start += changes.size();
+      try (RevWalk rw = new RevWalk(repo)) {
+        ChangeInfo last = null;
+        for (ChangeInfo c : changes) {
+          try {
+            replayChange(rw, c);
+          } catch (Exception e) {
+            log.error(String.format("Failed to replay change %s.",
+                Url.decode(c.id)), e);
+            throw e;
+          }
+          last = c;
+          pm.update(1);
         }
-        pm.update(1);
+        if(!Boolean.TRUE.equals(last._moreChanges)) {
+          break;
+        }
       }
     }
     pm.endTask();