Merge "Extract fetching commit messages into injectable class"
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/CommitMessageFetcher.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/CommitMessageFetcher.java
new file mode 100644
index 0000000..a04b175
--- /dev/null
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/util/CommitMessageFetcher.java
@@ -0,0 +1,50 @@
+package com.googlesource.gerrit.plugins.hooks.util;
+
+import java.io.IOException;
+
+import com.google.gerrit.reviewdb.client.Project.NameKey;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.inject.Inject;
+
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CommitMessageFetcher {
+  private static final Logger log = LoggerFactory.getLogger(
+      CommitMessageFetcher.class);
+
+  private final GitRepositoryManager repoManager;
+
+  @Inject
+  CommitMessageFetcher(GitRepositoryManager repoManager) {
+    this.repoManager = repoManager;
+  }
+
+  public String fetch(String projectName, String commitId) throws IOException {
+    final Repository repo =
+        repoManager.openRepository(new NameKey(projectName));
+    try {
+      RevWalk revWalk = new RevWalk(repo);
+      RevCommit commit = revWalk.parseCommit(ObjectId.fromString(commitId));
+
+      return commit.getFullMessage();
+    } finally {
+      repo.close();
+    }
+  }
+
+  public String fetchGuarded(String projectName, String commitId) {
+    String ret = "";
+    try {
+      ret = fetch(projectName, commitId);
+    } catch (IOException e) {
+      log.error("Could not fetch commit message for commit " + commitId +
+          " of project " + projectName, e);
+    }
+    return ret;
+  }
+}
diff --git a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilter.java b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilter.java
index 4d2ed8d..0bb1046 100644
--- a/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilter.java
+++ b/hooks-its/src/main/java/com/googlesource/gerrit/plugins/hooks/workflow/GerritHookFilter.java
@@ -16,15 +16,10 @@
 
 import java.io.IOException;
 
-import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.gerrit.common.ChangeListener;
-import com.google.gerrit.reviewdb.client.Project.NameKey;
 import com.google.gerrit.server.events.ChangeAbandonedEvent;
 import com.google.gerrit.server.events.ChangeEvent;
 import com.google.gerrit.server.events.ChangeMergedEvent;
@@ -35,6 +30,7 @@
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.hooks.util.CommitMessageFetcher;
 
 public class GerritHookFilter implements ChangeListener {
   private static final Logger log = LoggerFactory.getLogger(GerritHookFilter.class);
@@ -42,19 +38,12 @@
   @Inject
   private GitRepositoryManager repoManager;
 
+  @Inject
+  private CommitMessageFetcher commitMessageFetcher;
+
   public String getComment(String projectName, String commitId)
       throws IOException {
-
-    final Repository repo =
-        repoManager.openRepository(new NameKey(projectName));
-    try {
-      RevWalk revWalk = new RevWalk(repo);
-      RevCommit commit = revWalk.parseCommit(ObjectId.fromString(commitId));
-
-      return commit.getFullMessage();
-    } finally {
-      repo.close();
-    }
+    return commitMessageFetcher.fetch(projectName, commitId);
   }
 
   public void doFilter(PatchSetCreatedEvent hook) throws IOException,