Convert to auto-closeable RevWalk

Change-Id: I7ff7780df86a13c2d9396dcb93c256daffc848a3
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
index 88a32fd..169f92a 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
@@ -181,7 +181,7 @@
                   doValidation));
         }
       } finally {
-        revWalk.release();
+        revWalk.close();
         if (newChange == null) {
           db.rollback();
         }
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
index b13cc13..9834c2c 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
@@ -173,34 +173,35 @@
     ObjectId baseObjectId = ObjectId.fromString(pr.getBase().getSha());
     ObjectId prHeadObjectId = ObjectId.fromString(pr.getHead().getSha());
 
-    RevWalk walk = new RevWalk(gitRepo);
-    walk.markUninteresting(walk.lookupCommit(baseObjectId));
-    walk.markStart(walk.lookupCommit(prHeadObjectId));
-    walk.sort(RevSort.REVERSE);
+    try (RevWalk walk = new RevWalk(gitRepo)) {
+      walk.markUninteresting(walk.lookupCommit(baseObjectId));
+      walk.markStart(walk.lookupCommit(prHeadObjectId));
+      walk.sort(RevSort.REVERSE);
 
-    int patchNr = 1;
-    for (GHPullRequestCommitDetail ghCommitDetail : pr.listCommits()) {
-      status.update(Code.SYNC, "Patch #" + patchNr, "Patch#" + patchNr
-          + ": Inserting PullRequest into Gerrit");
-      RevCommit revCommit =
-          walk.parseCommit(ObjectId.fromString(ghCommitDetail.getSha()));
+      int patchNr = 1;
+      for (GHPullRequestCommitDetail ghCommitDetail : pr.listCommits()) {
+        status.update(Code.SYNC, "Patch #" + patchNr, "Patch#" + patchNr
+            + ": Inserting PullRequest into Gerrit");
+        RevCommit revCommit =
+            walk.parseCommit(ObjectId.fromString(ghCommitDetail.getSha()));
 
-      GHUser prUser = pr.getUser();
-      GitUser commitAuthor = ghCommitDetail.getCommit().getAuthor();
-      GitHubUser gitHubUser = GitHubUser.from(prUser, commitAuthor);
+        GHUser prUser = pr.getUser();
+        GitUser commitAuthor = ghCommitDetail.getCommit().getAuthor();
+        GitHubUser gitHubUser = GitHubUser.from(prUser, commitAuthor);
 
-      Account.Id pullRequestOwner = getOrRegisterAccount(db, gitHubUser);
-      Id changeId =
-          createChange.addCommitToChange(db, project, gitRepo,
-              destinationBranch, pullRequestOwner, revCommit,
-              getChangeMessage(pr),
-              String.format(TOPIC_FORMAT, pr.getNumber()), false);
-      if (changeId != null) {
-        prChanges.add(changeId);
+        Account.Id pullRequestOwner = getOrRegisterAccount(db, gitHubUser);
+        Id changeId =
+            createChange.addCommitToChange(db, project, gitRepo,
+                destinationBranch, pullRequestOwner, revCommit,
+                getChangeMessage(pr),
+                String.format(TOPIC_FORMAT, pr.getNumber()), false);
+        if (changeId != null) {
+          prChanges.add(changeId);
+        }
       }
-    }
 
-    return prChanges;
+      return prChanges;
+    }
   }
 
   private com.google.gerrit.reviewdb.client.Account.Id getOrRegisterAccount(
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
index c661242..2d34231 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
@@ -205,30 +205,31 @@
       Repository gitRepo, GHPullRequest ghPullRequest)
       throws IncorrectObjectTypeException, IOException {
     boolean pullRequestToImport = false;
-    RevWalk gitWalk = new RevWalk(gitRepo);
-    for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
-        .listCommits()) {
-      ObjectId pullRequestHeadObjectId =
-          ObjectId.fromString(pullRequestCommit.getSha());
+    try (RevWalk gitWalk = new RevWalk(gitRepo)) {
+      for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
+          .listCommits()) {
+        ObjectId pullRequestHeadObjectId =
+            ObjectId.fromString(pullRequestCommit.getSha());
 
-      try {
-        gitWalk.parseCommit(pullRequestHeadObjectId);
-
-        ResultSet<PatchSet> patchSets;
         try {
-          patchSets =
-              db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
-        } catch (OrmException e) {
-          LOG.error("Error whilst fetching patch-sets from DB associated to commit "
-              + pullRequestCommit.getSha());
-          return false;
+          gitWalk.parseCommit(pullRequestHeadObjectId);
+
+          ResultSet<PatchSet> patchSets;
+          try {
+            patchSets =
+                db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
+          } catch (OrmException e) {
+            LOG.error("Error whilst fetching patch-sets from DB associated to commit "
+                + pullRequestCommit.getSha());
+            return false;
+          }
+          pullRequestToImport = !patchSets.iterator().hasNext();
+          patchSets.close();
+        } catch (MissingObjectException e) {
+          pullRequestToImport = true;
         }
-        pullRequestToImport = !patchSets.iterator().hasNext();
-        patchSets.close();
-      } catch (MissingObjectException e) {
-        pullRequestToImport = true;
       }
+      return pullRequestToImport;
     }
-    return pullRequestToImport;
   }
 }