From 4fa8b1e2b4964a8c65737b8400d648f272edff98 Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Fri, 30 Apr 2021 11:43:52 +0200 Subject: [PATCH] SONAR-14746 improve BitbucketServer client log --- .../BitbucketServerRestClient.java | 26 ++++++---- .../BitbucketServerRestClientTest.java | 47 +++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java index 344c1f1d917..ceb1d7d50b3 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java @@ -44,6 +44,7 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonarqube.ws.client.OkHttpClientBuilder; +import static com.google.common.base.Strings.isNullOrEmpty; import static java.lang.String.format; import static java.util.Locale.ENGLISH; import static org.sonar.api.internal.apachecommons.lang.StringUtils.removeEnd; @@ -102,7 +103,7 @@ public class BitbucketServerRestClient { return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), ProjectList.class)); } - public BranchesList getBranches(String serverUrl, String token, String projectSlug, String repositorySlug){ + public BranchesList getBranches(String serverUrl, String token, String projectSlug, String repositorySlug) { HttpUrl url = buildUrl(serverUrl, format("/rest/api/1.0/projects/%s/repos/%s/branches", projectSlug, repositorySlug)); return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), BranchesList.class)); } @@ -119,13 +120,17 @@ public class BitbucketServerRestClient { return doCall(request, handler); } - protected static Request prepareRequestWithBearerToken(String token, String method, HttpUrl url, @Nullable RequestBody body) { - return new Request.Builder() + protected static Request prepareRequestWithBearerToken(@Nullable String token, String method, HttpUrl url, @Nullable RequestBody body) { + Request.Builder builder = new Request.Builder() .method(method, body) .url(url) - .addHeader("Authorization", "Bearer " + token) - .addHeader("x-atlassian-token", "no-check") - .build(); + .addHeader("x-atlassian-token", "no-check"); + + if (!isNullOrEmpty(token)) { + builder.addHeader("Authorization", "Bearer " + token); + } + + return builder.build(); } protected G doCall(Request request, Function handler) { @@ -133,8 +138,10 @@ public class BitbucketServerRestClient { handleError(response); return handler.apply(response); } catch (JsonSyntaxException e) { + LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ": " + e.getMessage(), e); throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ", got an unexpected response", e); } catch (IOException e) { + LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ": " + e.getMessage(), e); throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER, e); } } @@ -157,16 +164,17 @@ public class BitbucketServerRestClient { } protected static String getErrorMessage(ResponseBody body) throws IOException { + String bodyString = body.string(); if (equals(MediaType.parse("application/json;charset=utf-8"), body.contentType())) { try { - return Stream.of(buildGson().fromJson(body.charStream(), Errors.class).errorData) + return Stream.of(buildGson().fromJson(bodyString, Errors.class).errorData) .map(e -> e.exceptionName + " " + e.message) .collect(Collectors.joining("\n")); } catch (JsonParseException e) { - return body.string(); + return bodyString; } } - return body.string(); + return bodyString; } protected static Gson buildGson() { diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java index ccfddb717d5..1862bdac3a3 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java @@ -24,8 +24,10 @@ import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.sonar.alm.client.ConstantTimeoutConfiguration; +import org.sonar.api.utils.log.LogTester; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -33,6 +35,10 @@ import static org.assertj.core.api.Assertions.tuple; public class BitbucketServerRestClientTest { private final MockWebServer server = new MockWebServer(); + + @Rule + public LogTester logTester = new LogTester(); + private BitbucketServerRestClient underTest; @Before @@ -321,4 +327,45 @@ public class BitbucketServerRestClientTest { .hasMessage("Invalid personal access token"); } + @Test + public void fail_validate_on_io_exception() throws IOException { + server.shutdown(); + + String serverUrl = server.url("/").toString(); + assertThatThrownBy(() -> underTest.validateUrl(serverUrl)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unable to contact Bitbucket server"); + + assertThat(String.join(", ", logTester.logs())).contains("Unable to contact Bitbucket server: Failed to connect"); + } + + @Test + public void fail_validate_url_on_non_json_result_log_correctly_the_response(){ + server.enqueue(new MockResponse() + .setHeader("Content-Type", "application/json;charset=UTF-8") + .setResponseCode(500) + .setBody("not json")); + + String serverUrl = server.url("/").toString(); + assertThatThrownBy(() -> underTest.validateReadPermission(serverUrl, "token")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unable to contact Bitbucket server"); + + assertThat(String.join(", ", logTester.logs())).contains("Unable to contact Bitbucket server: 500 not json"); + } + + @Test + public void fail_validate_url_on_text_result_log_the_returned_payload(){ + server.enqueue(new MockResponse() + .setResponseCode(500) + .setBody("this is a text payload")); + + String serverUrl = server.url("/").toString(); + assertThatThrownBy(() -> underTest.validateReadPermission(serverUrl, "token")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unable to contact Bitbucket server"); + + assertThat(String.join(", ", logTester.logs())).contains("Unable to contact Bitbucket server: 500 this is a text payload"); + } + } -- 2.39.5