]> source.dussan.org Git - sonarqube.git/commitdiff
Fix call to /batch/project WS and handling of errors
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 1 Oct 2015 11:17:04 +0000 (13:17 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 1 Oct 2015 11:27:26 +0000 (13:27 +0200)
sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java
sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java

index fd0b0e0e4ece9d7edb9dc0f803c73c0c71a02ec9..6f4f10c74faa8889da80381b036f0637dfe7286c 100644 (file)
  */
 package org.sonar.batch.repository;
 
+import org.sonar.api.utils.HttpDownloader.HttpException;
+
 import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.Table;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Date;
 import java.util.Map;
+
 import javax.annotation.Nullable;
+
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.mutable.MutableBoolean;
 import org.slf4j.Logger;
@@ -55,7 +60,11 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad
       }
       return processStream(result.get(), projectKey);
     } catch (IllegalStateException e) {
-      LOG.debug("Couldn't get project repositories - continuing without it", e);
+      if (shouldThrow(e)) {
+        throw e;
+      }
+
+      LOG.debug("Project repository not available - continuing without it", e);
       return new ProjectRepositories();
     }
   }
@@ -66,11 +75,20 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad
     builder.append(BATCH_PROJECT_URL)
       .append("?key=").append(BatchUtils.encodeForUrl(projectKey));
     if (issuesMode) {
-      builder.append("&issues=true");
+      builder.append("&issues_mode=true");
     }
     return builder.toString();
   }
 
+  private static boolean shouldThrow(Exception e) {
+    if (e.getCause() != null && e.getCause() instanceof HttpException) {
+      HttpException http = (HttpException) e.getCause();
+      return http.getResponseCode() != 404;
+    }
+
+    return false;
+  }
+
   private static ProjectRepositories processStream(InputStream is, String projectKey) {
     try {
       WsProjectResponse response = WsProjectResponse.parseFrom(is);
index 00a2e569a9623c764c6f3b0c88583bb8e7c27e03..25a5fd2f1716407c0af658140ef5de0af54b6292 100644 (file)
@@ -23,6 +23,9 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
+
+import org.sonar.api.utils.HttpDownloader.HttpException;
 import org.apache.commons.lang.mutable.MutableBoolean;
 import org.junit.Before;
 import org.junit.Rule;
@@ -31,7 +34,6 @@ import org.junit.rules.ExpectedException;
 import org.sonar.batch.cache.WSLoader;
 import org.sonar.batch.cache.WSLoaderResult;
 import org.sonarqube.ws.WsBatch.WsProjectResponse;
-
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
@@ -61,13 +63,21 @@ public class DefaultProjectRepositoriesLoaderTest {
     assertThat(proj.exists()).isEqualTo(false);
   }
 
+  @Test(expected = IllegalStateException.class)
+  public void failFastHttpError() {
+    HttpException http = new HttpException(URI.create("uri"), 403);
+    IllegalStateException e = new IllegalStateException("http error", http);
+    when(wsLoader.loadStream(anyString())).thenThrow(e);
+    loader.load(PROJECT_KEY, false, null);
+  }
+
   @Test
   public void passIssuesModeParameter() {
     loader.load(PROJECT_KEY, false, null);
     verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
 
     loader.load(PROJECT_KEY, true, null);
-    verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F&issues=true");
+    verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F&issues_mode=true");
   }
 
   @Test