From 964ce2999b7bef53d78c42210b8284b369241603 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 1 Oct 2015 13:17:04 +0200 Subject: [PATCH] Fix call to /batch/project WS and handling of errors --- .../DefaultProjectRepositoriesLoader.java | 22 +++++++++++++++++-- .../DefaultProjectRepositoriesLoaderTest.java | 14 ++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java index fd0b0e0e4ec..6f4f10c74fa 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java @@ -19,13 +19,18 @@ */ 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); diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java index 00a2e569a96..25a5fd2f171 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java @@ -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 -- 2.39.5