aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-alm-client/src
diff options
context:
space:
mode:
authorBelen Pruvost <belen.pruvost@sonarsource.com>2021-04-30 11:43:52 +0200
committersonartech <sonartech@sonarsource.com>2021-04-30 20:03:24 +0000
commit4fa8b1e2b4964a8c65737b8400d648f272edff98 (patch)
treed134171b6853bb14b03606a084e8d1ae29f45b91 /server/sonar-alm-client/src
parentc6466f36f25fa6f42207361b37cd9b3698d9811a (diff)
downloadsonarqube-4fa8b1e2b4964a8c65737b8400d648f272edff98.tar.gz
sonarqube-4fa8b1e2b4964a8c65737b8400d648f272edff98.zip
SONAR-14746 improve BitbucketServer client log
Diffstat (limited to 'server/sonar-alm-client/src')
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java26
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java47
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> 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() {
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");
+ }
+
}