diff options
author | Pierre <pierre.guillot@sonarsource.com> | 2021-03-10 17:55:16 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-03-16 20:08:15 +0000 |
commit | f1f37ab00a6a0b92eaaa450c587a613226fb96b1 (patch) | |
tree | 69052bf6bf17e1099cc7743ce54657b8562711fe /server/sonar-alm-client | |
parent | e34f7a9bb6f1799e42c33c8f34c5e1e823e96884 (diff) | |
download | sonarqube-f1f37ab00a6a0b92eaaa450c587a613226fb96b1.tar.gz sonarqube-f1f37ab00a6a0b92eaaa450c587a613226fb96b1.zip |
SONAR-14581 print stacktrace during integration error with gitlab
Diffstat (limited to 'server/sonar-alm-client')
2 files changed, 89 insertions, 0 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java index a2d6b982f50..61dc5a7c0bc 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabHttpClient.java @@ -86,10 +86,15 @@ public class GitlabHttpClient { } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Could not parse GitLab answer to verify read permission. Got a non-json payload as result."); } catch (IOException e) { + logException(url, e); throw new IllegalArgumentException(errorMessage); } } + private static void logException(String url, IOException e) { + LOG.info(String.format("Gitlab API call to [%s] failed with error message : [%s]", url, e.getMessage()), e); + } + public void checkToken(String gitlabUrl, String personalAccessToken) { String url = String.format("%s/user", gitlabUrl); @@ -108,6 +113,7 @@ public class GitlabHttpClient { } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Could not parse GitLab answer to verify token. Got a non-json payload as result."); } catch (IOException e) { + logException(url, e); throw new IllegalArgumentException(errorMessage); } } @@ -131,6 +137,7 @@ public class GitlabHttpClient { } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Could not parse GitLab answer to verify write permission. Got a non-json payload as result."); } catch (IOException e) { + logException(url, e); throw new IllegalArgumentException(errorMessage); } @@ -219,6 +226,7 @@ public class GitlabHttpClient { } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Could not parse GitLab answer to retrieve a project. Got a non-json payload as result."); } catch (IOException e) { + logException(url, e); throw new IllegalStateException(e.getMessage(), e); } } @@ -240,6 +248,7 @@ public class GitlabHttpClient { } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Could not parse GitLab answer to retrieve project branches. Got a non-json payload as result."); } catch (IOException e) { + logException(url, e); throw new IllegalStateException(e.getMessage(), e); } } @@ -266,6 +275,7 @@ public class GitlabHttpClient { } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Could not parse GitLab answer to search projects. Got a non-json payload as result."); } catch (IOException e) { + logException(url, e); throw new IllegalStateException(e.getMessage(), e); } } diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java index 38745899f06..f5ba152946d 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabHttpClientTest.java @@ -26,9 +26,12 @@ import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; 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.alm.client.TimeoutConfiguration; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -36,6 +39,10 @@ import static org.assertj.core.api.Assertions.tuple; public class GitlabHttpClientTest { + + @Rule + public LogTester logTester = new LogTester(); + private final MockWebServer server = new MockWebServer(); private GitlabHttpClient underTest; private String gitlabUrl; @@ -304,4 +311,76 @@ public class GitlabHttpClientTest { .isInstanceOf(IllegalArgumentException.class) .hasMessage("Could not get projects from GitLab instance"); } + + @Test + public void fail_check_read_permission_with_unexpected_io_exception_with_detailed_log() throws IOException { + server.shutdown(); + + assertThatThrownBy(() -> underTest.checkReadPermission(gitlabUrl, "token")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Could not validate GitLab read permission. Got an unexpected answer."); + assertThat(logTester.logs(LoggerLevel.INFO).get(0)) + .contains("Gitlab API call to [http://localhost:"+server.getPort()+"/projects] " + + "failed with error message : [Failed to connect to localhost"); + } + + @Test + public void fail_check_token_with_unexpected_io_exception_with_detailed_log() throws IOException { + server.shutdown(); + + assertThatThrownBy(() -> underTest.checkToken(gitlabUrl, "token")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Could not validate GitLab token. Got an unexpected answer."); + assertThat(logTester.logs(LoggerLevel.INFO).get(0)) + .contains("Gitlab API call to [http://localhost:"+server.getPort()+"/user] " + + "failed with error message : [Failed to connect to localhost"); + } + + @Test + public void fail_check_write_permission_with_unexpected_io_exception_with_detailed_log() throws IOException { + server.shutdown(); + + assertThatThrownBy(() -> underTest.checkWritePermission(gitlabUrl, "token")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Could not validate GitLab write permission. Got an unexpected answer."); + assertThat(logTester.logs(LoggerLevel.INFO).get(0)) + .contains("Gitlab API call to [http://localhost:"+server.getPort()+"/markdown] " + + "failed with error message : [Failed to connect to localhost"); + } + + @Test + public void fail_get_project_with_unexpected_io_exception_with_detailed_log() throws IOException { + server.shutdown(); + + assertThatThrownBy(() -> underTest.getProject(gitlabUrl, "token", 0L)) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Failed to connect to localhost"); + assertThat(logTester.logs(LoggerLevel.INFO).get(0)) + .contains("Gitlab API call to [http://localhost:"+server.getPort()+"/projects/0] " + + "failed with error message : [Failed to connect to localhost"); + } + + @Test + public void fail_get_branches_with_unexpected_io_exception_with_detailed_log() throws IOException { + server.shutdown(); + + assertThatThrownBy(() -> underTest.getBranches(gitlabUrl, "token", 0L)) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Failed to connect to localhost"); + assertThat(logTester.logs(LoggerLevel.INFO).get(0)) + .contains("Gitlab API call to [http://localhost:"+server.getPort()+"/projects/0/repository/branches] " + + "failed with error message : [Failed to connect to localhost"); + } + + @Test + public void fail_search_projects_with_unexpected_io_exception_with_detailed_log() throws IOException { + server.shutdown(); + + assertThatThrownBy(() -> underTest.searchProjects(gitlabUrl, "token", null, 1, 1)) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Failed to connect to localhost"); + assertThat(logTester.logs(LoggerLevel.INFO).get(0)) + .contains("Gitlab API call to [http://localhost:"+server.getPort()+"/projects?archived=false&simple=true&membership=true&order_by=name&sort=asc&search=&page=1&per_page=1] " + + "failed with error message : [Failed to connect to localhost"); + } } |