]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21119 Support Gitlab API url from config
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Mon, 11 Dec 2023 13:43:09 +0000 (14:43 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 22 Dec 2023 20:03:02 +0000 (20:03 +0000)
server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabSettings.java
server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabSettingsTest.java

index a5a6ed1d4896516d0876b297b5a70257280ff6e1..4d0c2646066c8f14b8563b429fa76411e4c5298b 100644 (file)
@@ -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);
   }
 }
index bf5810fe516fecd204dd1d551b9cd459cef27e9e..d81e75e3f826f9977ea85b924765d1726e75d76b 100644 (file)
@@ -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")
index e910eab07125eabfab5ca31ecf8cb9df0a95b2fb..ca98c7e80721f3e925b257e0ed507ad77c2d241e 100644 (file)
@@ -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");