diff options
author | Antoine Vigneau <antoine.vigneau@sonarsource.com> | 2023-12-11 14:43:09 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-22 20:03:02 +0000 |
commit | 0b634fb2bf2c75f30193e406e16e1ebcba266f8f (patch) | |
tree | ac0d5360f640e253c5d2bb0985eea6404b6685de /server/sonar-auth-gitlab | |
parent | f1dfcc61c643d3c13be6b9743722ed36784569ab (diff) | |
download | sonarqube-0b634fb2bf2c75f30193e406e16e1ebcba266f8f.tar.gz sonarqube-0b634fb2bf2c75f30193e406e16e1ebcba266f8f.zip |
SONAR-21119 Support Gitlab API url from config
Diffstat (limited to 'server/sonar-auth-gitlab')
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<GsonGroup> 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"); |