Update ScanTask to use Projects API

Instead of using Gerrit's internals, the Projects API gives enough
visibility into branches that we can use it directly.  Unfortunately,
this change is not compatible with Gerrit 2.11.

Change-Id: I5fce4cef976135972c4f4f722fd3bacbe03707ea
diff --git a/src/main/java/com/googlesource/gerrit/plugins/repositoryuse/ScanTaskImpl.java b/src/main/java/com/googlesource/gerrit/plugins/repositoryuse/ScanTaskImpl.java
index 642c9a0..9904996 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/repositoryuse/ScanTaskImpl.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/repositoryuse/ScanTaskImpl.java
@@ -14,19 +14,18 @@
 
 package com.googlesource.gerrit.plugins.repositoryuse;
 
-import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.extensions.api.projects.BranchInfo;
+import com.google.gerrit.extensions.api.projects.Projects;
+import com.google.gerrit.extensions.restapi.RestApiException;
 import com.google.inject.assistedinject.Assisted;
 import com.google.inject.assistedinject.AssistedInject;
 
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.Repository;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.IOException;
-import java.util.Map;
+import java.util.List;
 
 public class ScanTaskImpl implements ScanTask {
   private static final Logger log =
@@ -35,30 +34,30 @@
   private String project;
   private String branch;
   private RefUpdateHandlerFactory refUpdateHandlerFactory;
-  private GitRepositoryManager repoManager;
+  private Projects projects;
 
   @AssistedInject
   public ScanTaskImpl(@Assisted String project,
       RefUpdateHandlerFactory refUpdateHandlerFactory,
-      GitRepositoryManager repoManager) {
-    init(project, null, refUpdateHandlerFactory, repoManager);
+      Projects projects) {
+    init(project, null, refUpdateHandlerFactory, projects);
   }
 
   @AssistedInject
   public ScanTaskImpl(@Assisted("project") String project,
       @Assisted("branch") String branch,
       RefUpdateHandlerFactory refUpdateHandlerFactory,
-      GitRepositoryManager repoManager) {
-    init(project, branch, refUpdateHandlerFactory, repoManager);
+      Projects projects) {
+    init(project, branch, refUpdateHandlerFactory, projects);
   }
 
   private void init(String project, String branch,
       RefUpdateHandlerFactory refUpdateHandlerFactory,
-      GitRepositoryManager repoManager) {
+      Projects projects) {
     this.project = project;
     this.branch = branch;
     this.refUpdateHandlerFactory = refUpdateHandlerFactory;
-    this.repoManager = repoManager;
+    this.projects = projects;
   }
 
   @Override
@@ -72,21 +71,20 @@
 
   @Override
   public void run() {
-    Map<String, org.eclipse.jgit.lib.Ref> branches = null;
-    Project.NameKey nameKey = new Project.NameKey(project);
+    List<BranchInfo> branches = null;
     try {
-      try (Repository repo = repoManager.openRepository(nameKey)) {
-        branches = repo.getRefDatabase().getRefs(Constants.R_HEADS);
-      }
-    } catch (IOException e) {
+      branches = projects.name(project).branches().get();
+    } catch (RestApiException e) {
       log.error(e.getMessage(), e);
     }
-    if (branch == null) {
-      for (String currentBranch : branches.keySet()) {
-        // Create with a "new" base commit to rescan entire branch
-        RefUpdate rescan = new RefUpdate(project,
-            branches.get(currentBranch).getName(), ObjectId.zeroId().getName(),
-            branches.get(currentBranch).getObjectId().name());
+    if (branch != null && !branch.startsWith(Constants.R_HEADS)) {
+      branch = Constants.R_HEADS + branch;
+    }
+    for (BranchInfo currentBranch : branches) {
+      // Create with a "new" base commit to rescan entire branch
+      if (branch == null || branch == currentBranch.ref) {
+        RefUpdate rescan = new RefUpdate(project, currentBranch.ref,
+            ObjectId.zeroId().getName(), currentBranch.revision);
         try {
           refUpdateHandlerFactory.create(rescan).run();
         } catch (Exception e) {
@@ -94,24 +92,6 @@
               branch, e.getMessage()), e);
         }
       }
-    } else {
-      if (branch.startsWith(Constants.R_HEADS)) {
-        branch = branch.substring(Constants.R_HEADS.length());
-      }
-
-      if (branches.containsKey(branch)) {
-        RefUpdate rescan = new RefUpdate(project,
-            branches.get(branch).getName(), ObjectId.zeroId().getName(),
-            branches.get(branch).getObjectId().name());
-        try {
-          refUpdateHandlerFactory.create(rescan).run();
-        } catch (Exception e) {
-          log.error(String.format("Error updating %s branch %s: %s", project,
-              branch, e.getMessage()), e);
-        }
-      } else {
-        log.warn(String.format("Branch %s does not exist; skipping", branch));
-      }
     }
   }