From 0b634fb2bf2c75f30193e406e16e1ebcba266f8f Mon Sep 17 00:00:00 2001 From: Antoine Vigneau Date: Mon, 11 Dec 2023 14:43:09 +0100 Subject: [PATCH] SONAR-21119 Support Gitlab API url from config --- .../java/org/sonar/auth/gitlab/GitLabRestClient.java | 12 +++++------- .../java/org/sonar/auth/gitlab/GitLabSettings.java | 6 +++++- .../org/sonar/auth/gitlab/GitLabSettingsTest.java | 11 +++++++---- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java index a5a6ed1d489..4d0c2646066 100644 --- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java +++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java @@ -28,16 +28,14 @@ import org.sonar.auth.OAuthRestClient; public class GitLabRestClient { - private static final String API_SUFFIX = "/api/v4"; + private final GitLabSettings gitLabSettings; - private final GitLabSettings settings; - - public GitLabRestClient(GitLabSettings settings) { - this.settings = settings; + public GitLabRestClient(GitLabSettings gitLabSettings) { + this.gitLabSettings = gitLabSettings; } GsonUser getUser(OAuth20Service scribe, OAuth2AccessToken accessToken) { - try (Response response = OAuthRestClient.executeRequest(settings.url() + API_SUFFIX + "/user", scribe, accessToken)) { + try (Response response = OAuthRestClient.executeRequest(gitLabSettings.apiUrl() + "/user", scribe, accessToken)) { String responseBody = response.getBody(); return GsonUser.parse(responseBody); } catch (IOException e) { @@ -46,6 +44,6 @@ public class GitLabRestClient { } List getGroups(OAuth20Service scribe, OAuth2AccessToken accessToken) { - return OAuthRestClient.executePaginatedRequest(settings.url() + API_SUFFIX + "/groups?min_access_level=10", scribe, accessToken, GsonGroup::parse); + return OAuthRestClient.executePaginatedRequest(gitLabSettings.apiUrl() + "/groups?min_access_level=10", scribe, accessToken, GsonGroup::parse); } } diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabSettings.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabSettings.java index bf5810fe516..d81e75e3f82 100644 --- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabSettings.java +++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabSettings.java @@ -63,6 +63,10 @@ public class GitLabSettings implements DevOpsPlatformSettings { return url; } + public String apiUrl() { + return url() + "/api/v4"; + } + public String applicationId() { return configuration.get(GITLAB_AUTH_APPLICATION_ID).orElse(null); } @@ -119,7 +123,7 @@ public class GitLabSettings implements DevOpsPlatformSettings { .build(), PropertyDefinition.builder(GITLAB_AUTH_URL) .name("GitLab URL") - .description("URL to access GitLab.") + .description("Base URL to access GitLab. https://gitlab.com for Gitlab SaaS") .category(CATEGORY) .subCategory(SUBCATEGORY) .defaultValue("https://gitlab.com") diff --git a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabSettingsTest.java b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabSettingsTest.java index e910eab0712..ca98c7e8072 100644 --- a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabSettingsTest.java +++ b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabSettingsTest.java @@ -50,12 +50,15 @@ public class GitLabSettingsTest { @Test public void test_settings() { assertThat(config.url()).isEqualTo("https://gitlab.com"); + assertThat(config.apiUrl()).isEqualTo("https://gitlab.com/api/v4"); - settings.setProperty(GITLAB_AUTH_URL, "https://gitlab.com/api/"); - assertThat(config.url()).isEqualTo("https://gitlab.com/api"); + settings.setProperty(GITLAB_AUTH_URL, "https://onpremise.gitlab.com/"); + assertThat(config.url()).isEqualTo("https://onpremise.gitlab.com"); + assertThat(config.apiUrl()).isEqualTo("https://onpremise.gitlab.com/api/v4"); - settings.setProperty(GITLAB_AUTH_URL, "https://gitlab.com/api"); - assertThat(config.url()).isEqualTo("https://gitlab.com/api"); + settings.setProperty(GITLAB_AUTH_URL, "https://onpremise.gitlab.com"); + assertThat(config.url()).isEqualTo("https://onpremise.gitlab.com"); + assertThat(config.apiUrl()).isEqualTo("https://onpremise.gitlab.com/api/v4"); assertThat(config.isEnabled()).isFalse(); settings.setProperty(GITLAB_AUTH_ENABLED, "true"); -- 2.39.5