Destination: Consider ref visibility when scheduling replication

Bug: Issue 4398
Change-Id: I11856eabf61e734691e1b00f1c3083c017f96a01
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
index e267da3..3b098cc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
@@ -231,6 +231,27 @@
         cfg, "remote", rc.getName(), name, defValue, unit);
   }
 
+  private boolean isVisible(final Project.NameKey project, final String ref,
+      ReplicationState... states) {
+    try {
+      return threadScoper.scope(new Callable<Boolean>() {
+        @Override
+        public Boolean call() throws NoSuchProjectException {
+          ProjectControl projectControl = controlFor(project);
+          return projectControl.isVisible()
+              && (PushOne.ALL_REFS.equals(ref)
+                  || projectControl.controlForRef(ref).isVisible());
+        }
+      }).call();
+    } catch (NoSuchProjectException err) {
+      stateLog.error(String.format("source project %s not available", project),
+          err, states);
+    } catch (Exception e) {
+      throw Throwables.propagate(e);
+    }
+    return false;
+  }
+
   private boolean isVisible(final Project.NameKey project,
       ReplicationState... states) {
     try {
@@ -252,7 +273,7 @@
   void schedule(final Project.NameKey project, final String ref,
       final URIish uri, ReplicationState state) {
     repLog.info("scheduling replication {}:{} => {}", project, ref, uri);
-    if (!isVisible(project, state)) {
+    if (!isVisible(project, ref, state)) {
       return;
     }