Merge branch 'stable-3.1' into stable-3.2

* stable-3.1:
  Always replicate changes regardless of the permissions

Change-Id: I76212d958d876b55eb4067a2287c1af7ab5b1073
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
index 0d4c57d..639bc80 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
@@ -29,6 +29,7 @@
 import com.google.gerrit.entities.BranchNameKey;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.entities.RefNames;
+import com.google.gerrit.exceptions.StorageException;
 import com.google.gerrit.extensions.config.FactoryModule;
 import com.google.gerrit.extensions.registration.DynamicItem;
 import com.google.gerrit.extensions.restapi.AuthException;
@@ -289,10 +290,10 @@
               new Callable<Boolean>() {
                 @Override
                 public Boolean call() throws NoSuchProjectException, PermissionBackendException {
-                  ProjectState projectState;
+                  Optional<ProjectState> projectState;
                   try {
-                    projectState = projectCache.checkedGet(project);
-                  } catch (IOException e) {
+                    projectState = projectCache.get(project);
+                  } catch (StorageException e) {
                     repLog.warn(
                         "NOT scheduling replication {}:{} because could not open source project",
                         project,
@@ -300,25 +301,21 @@
                         e);
                     return false;
                   }
-                  if (projectState == null) {
+                  if (!projectState.isPresent()) {
                     repLog.warn(
                         "NOT scheduling replication {}:{} because project does not exist",
                         project,
                         ref);
                     throw new NoSuchProjectException(project);
                   }
-                  if (!projectState.statePermitsRead()) {
+                  if (!projectState.get().statePermitsRead()) {
                     repLog.warn(
                         "NOT scheduling replication {}:{} because project is not readable",
                         project,
                         ref);
                     return false;
                   }
-                  if (!shouldReplicate(projectState, userProvider.get())) {
-                    repLog.warn(
-                        "NOT scheduling replication {}:{} because project is supposed to be replicated",
-                        project,
-                        ref);
+                  if (!shouldReplicate(projectState.get(), userProvider.get())) {
                     return false;
                   }
                   if (FetchOne.ALL_REFS.equals(ref)) {
@@ -360,16 +357,16 @@
               new Callable<Boolean>() {
                 @Override
                 public Boolean call() throws NoSuchProjectException, PermissionBackendException {
-                  ProjectState projectState;
+                  Optional<ProjectState> projectState;
                   try {
-                    projectState = projectCache.checkedGet(project);
-                  } catch (IOException e) {
+                    projectState = projectCache.get(project);
+                  } catch (StorageException e) {
                     return false;
                   }
-                  if (projectState == null) {
+                  if (!projectState.isPresent()) {
                     throw new NoSuchProjectException(project);
                   }
-                  return shouldReplicate(projectState, userProvider.get());
+                  return shouldReplicate(projectState.get(), userProvider.get());
                 }
               })
           .call();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java
index 6970299..c44bcbc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java
@@ -84,7 +84,7 @@
         | ExecutionException
         | IllegalStateException
         | TimeoutException e) {
-      throw new RestApiException(e.getMessage(), e);
+      throw RestApiException.wrap(e.getMessage(), e);
     } catch (RemoteConfigurationMissingException e) {
       throw new UnprocessableEntityException(e.getMessage());
     }