Merge "Filter out RO GitHub repositories for replication"
diff --git a/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthProtocol.java b/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthProtocol.java
index 8cf64ab..70eb514 100644
--- a/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthProtocol.java
+++ b/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthProtocol.java
@@ -37,22 +37,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Charsets;
-import com.google.common.base.Strings;
-import com.google.common.io.CharStreams;
-import com.google.gson.Gson;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
 import com.google.gerrit.extensions.auth.oauth.OAuthToken;
 import com.google.gerrit.extensions.auth.oauth.OAuthVerifier;
 
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubOAuthServiceProvider.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubOAuthServiceProvider.java
index 575ea11..0a617fc 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubOAuthServiceProvider.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubOAuthServiceProvider.java
@@ -22,7 +22,6 @@
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.Sets;
-import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.extensions.auth.oauth.OAuthServiceProvider;
 import com.google.gerrit.extensions.auth.oauth.OAuthToken;
 import com.google.gerrit.extensions.auth.oauth.OAuthUserInfo;
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java
index 625f664..97bc6be 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java
@@ -38,9 +38,6 @@
 import com.googlesource.gerrit.plugins.github.velocity.VelocityViewServlet;
 import com.googlesource.gerrit.plugins.github.wizard.VelocityControllerServlet;
 
-import org.apache.http.client.HttpClient;
-import org.apache.velocity.runtime.RuntimeInstance;
-
 public class GuiceHttpModule extends ServletModule {
 
   @Override
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceModule.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceModule.java
index 2b37e5e..6eed4ea 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceModule.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceModule.java
@@ -14,13 +14,14 @@
 
 package com.googlesource.gerrit.plugins.github;
 
+import com.google.gerrit.common.EventListener;
 import com.google.gerrit.extensions.registration.DynamicSet;
 import com.google.gerrit.extensions.webui.TopMenu;
 import com.google.gerrit.server.account.GroupBackend;
 import com.google.inject.AbstractModule;
+import com.google.inject.Scopes;
 import com.google.inject.TypeLiteral;
 import com.google.inject.assistedinject.FactoryModuleBuilder;
-
 import com.googlesource.gerrit.plugins.github.group.GitHubGroupBackend;
 import com.googlesource.gerrit.plugins.github.group.GitHubGroupMembership;
 import com.googlesource.gerrit.plugins.github.group.GitHubGroupsCache;
@@ -28,6 +29,9 @@
 import com.googlesource.gerrit.plugins.github.oauth.GitHubLogin;
 import com.googlesource.gerrit.plugins.github.oauth.IdentifiedUserGitHubLoginProvider;
 import com.googlesource.gerrit.plugins.github.oauth.UserScopedProvider;
+import com.googlesource.gerrit.plugins.github.replication.ReplicationErrorListener;
+import com.googlesource.gerrit.plugins.github.replication.ReplicationStatusFlatFile;
+import com.googlesource.gerrit.plugins.github.replication.ReplicationStatusStore;
 
 public class GuiceModule extends AbstractModule {
   @Override
@@ -39,10 +43,15 @@
 
     DynamicSet.bind(binder(), TopMenu.class).to(GitHubTopMenu.class);
     DynamicSet.bind(binder(), GroupBackend.class).to(GitHubGroupBackend.class);
+    DynamicSet.bind(binder(), EventListener.class).to(ReplicationErrorListener.class);
+
 
     install(new FactoryModuleBuilder()
         .build(GitHubOrganisationGroup.Factory.class));
     install(new FactoryModuleBuilder()
         .build(GitHubGroupMembership.Factory.class));
+
+    bind(ReplicationStatusStore.class).to(ReplicationStatusFlatFile.class)
+        .in(Scopes.SINGLETON);
   }
 }
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 169f92a..d6a7213 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
@@ -114,8 +114,7 @@
         projectControlFactory.controlFor(project.getNameKey()).controlForRef(
             destinationBranch);
 
-    try {
-      RevWalk revWalk = new RevWalk(git);
+    try (RevWalk revWalk = new RevWalk(git)) {
       try {
         Ref destRef = git.getRef(destinationBranch);
         if (destRef == null) {
@@ -181,7 +180,6 @@
                   doValidation));
         }
       } finally {
-        revWalk.close();
         if (newChange == null) {
           db.rollback();
         }
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationErrorListener.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationErrorListener.java
new file mode 100644
index 0000000..3eea388
--- /dev/null
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationErrorListener.java
@@ -0,0 +1,69 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.github.replication;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gerrit.common.EventListener;
+import com.google.gerrit.reviewdb.client.Project.NameKey;
+import com.google.gerrit.server.OutputFormat;
+import com.google.gerrit.server.events.Event;
+import com.google.gerrit.server.events.RefEvent;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+@Singleton
+public class ReplicationErrorListener implements EventListener {
+  private static final String REF_REPLICATED_EVENT = "ref-replicated";
+  private static Logger log = LoggerFactory
+      .getLogger(ReplicationErrorListener.class);
+
+  private final ReplicationStatusStore statusStore;
+  private final Gson gson;
+
+  @Inject
+  public ReplicationErrorListener(ReplicationStatusStore statusStore) {
+    this.statusStore = statusStore;
+    this.gson = OutputFormat.JSON_COMPACT.newGson();
+  }
+
+  @Override
+  public void onEvent(Event event) {
+    if (RefEvent.class.isAssignableFrom(event.getClass())
+        && event.getType().equals(REF_REPLICATED_EVENT)) {
+      RefEvent refEvent = (RefEvent) event;
+      NameKey projectNameKey = refEvent.getProjectNameKey();
+      JsonObject eventJson = (JsonObject) gson.toJsonTree(event);
+      String projectKey =
+          projectNameKey.get() + "/" + eventJson.get("ref").getAsString();
+
+      try {
+        if (eventJson.get("status").getAsString().equals("succeeded")) {
+          statusStore.remove(projectKey);
+        } else {
+          statusStore.set(projectKey, eventJson);
+        }
+      } catch (IOException e) {
+        log.error("Unable to update replication status for event " + eventJson
+            + " on project " + projectKey, e);
+      }
+    }
+  }
+}
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationStatusFlatFile.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationStatusFlatFile.java
new file mode 100644
index 0000000..ebba163
--- /dev/null
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationStatusFlatFile.java
@@ -0,0 +1,58 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.github.replication;
+
+import static java.nio.file.StandardOpenOption.CREATE;
+import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
+import static java.nio.file.StandardOpenOption.WRITE;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.gson.JsonObject;
+import com.google.inject.Inject;
+
+public class ReplicationStatusFlatFile implements ReplicationStatusStore {
+  private final Path pluginData;
+
+  @Inject
+  public ReplicationStatusFlatFile(@PluginData Path pluginData) {
+    this.pluginData = pluginData;
+  }
+
+  @Override
+  public void set(String key, JsonObject event) throws IOException {
+    Path replicationStatusPath = getReplicationStatusPath(key);
+    Files.write(replicationStatusPath, event.toString().getBytes(),
+        TRUNCATE_EXISTING, CREATE, WRITE);
+  }
+
+  @Override
+  public void remove(String key) throws IOException {
+      Path replicationStatusPath = getReplicationStatusPath(key);
+      if (Files.exists(replicationStatusPath)) {
+        Files.delete(replicationStatusPath);
+      }
+  }
+
+  private Path getReplicationStatusPath(String key) throws IOException {
+    String sanitizedKey = key.replace(".", "_").replace(" ","_");
+    Path projectPath = pluginData.resolve(sanitizedKey + ".replication-error.json");
+    Files.createDirectories(projectPath.getParent());
+    return projectPath;
+  }
+}
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationStatusStore.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationStatusStore.java
new file mode 100644
index 0000000..2eb8c24
--- /dev/null
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/ReplicationStatusStore.java
@@ -0,0 +1,26 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.github.replication;
+
+import java.io.IOException;
+
+import com.google.gson.JsonObject;
+
+public interface ReplicationStatusStore {
+
+  public void set(String key, JsonObject event) throws IOException;
+
+  public void remove(String key) throws IOException;
+}
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 2d34231..8449edb 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
@@ -210,10 +210,10 @@
           .listCommits()) {
         ObjectId pullRequestHeadObjectId =
             ObjectId.fromString(pullRequestCommit.getSha());
-
+  
         try {
           gitWalk.parseCommit(pullRequestHeadObjectId);
-
+  
           ResultSet<PatchSet> patchSets;
           try {
             patchSets =