]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14746 improve BitbucketServer client log
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Fri, 30 Apr 2021 09:43:52 +0000 (11:43 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 30 Apr 2021 20:03:24 +0000 (20:03 +0000)
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java
server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java

index 344c1f1d917a9845f91704e6961dc610be087e02..ceb1d7d50b3a672dbc63584459dab349f3e7ce63 100644 (file)
@@ -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> G doCall(Request request, Function<Response, G> 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() {
index ccfddb717d524f44edcff31de2cb2c1c34ee339c..1862bdac3a3e9edf979f449a08548e0d7b32d0a6 100644 (file)
@@ -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");
+  }
+
 }