]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14871 Extract global and project Gitlab validation to separate classes
authorJacek <jacek.poreda@sonarsource.com>
Mon, 7 Jun 2021 11:57:26 +0000 (13:57 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 10 Jun 2021 20:03:27 +0000 (20:03 +0000)
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidator.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ValidateAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidatorTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java

index 3ece1b02483815366b0f1a7545e0a66bc5a3f06d..788caa61d2ce8c428d548030c202ffd50619f226 100644 (file)
@@ -82,7 +82,7 @@ public class AlmSettingsTesting {
       .setMonorepo(false);
   }
 
-  static ProjectAlmSettingDto newGitlabProjectAlmSettingDto(AlmSettingDto gitlabAlmSetting, ProjectDto project) {
+  public static ProjectAlmSettingDto newGitlabProjectAlmSettingDto(AlmSettingDto gitlabAlmSetting, ProjectDto project) {
     return new ProjectAlmSettingDto()
       .setAlmSettingUuid(gitlabAlmSetting.getUuid())
       .setProjectUuid(project.getUuid())
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidator.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidator.java
new file mode 100644 (file)
index 0000000..40c4195
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.almintegration.validator;
+
+import org.sonar.alm.client.gitlab.GitlabHttpClient;
+import org.sonar.api.server.ServerSide;
+import org.sonar.db.alm.setting.AlmSettingDto;
+
+@ServerSide
+public class GitlabGlobalSettingsValidator {
+
+  private final GitlabHttpClient gitlabHttpClient;
+
+  public GitlabGlobalSettingsValidator(GitlabHttpClient gitlabHttpClient) {
+    this.gitlabHttpClient = gitlabHttpClient;
+  }
+
+  public void validate(AlmSettingDto almSettingDto) {
+    String gitlabUrl = almSettingDto.getUrl();
+    String accessToken = almSettingDto.getPersonalAccessToken();
+
+    if (gitlabUrl == null || accessToken == null) {
+      throw new IllegalArgumentException("Your Gitlab global configuration is incomplete.");
+    }
+
+    gitlabHttpClient.checkUrl(gitlabUrl);
+    gitlabHttpClient.checkToken(gitlabUrl, accessToken);
+    gitlabHttpClient.checkReadPermission(gitlabUrl, accessToken);
+    gitlabHttpClient.checkWritePermission(gitlabUrl, accessToken);
+  }
+
+}
index 8bc96cd04392cf859b4134760266e709cbe62747..912f1a5f90be10f78670acd7b3c4808bfcc833f0 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.almintegration.ws;
 
 import org.sonar.core.platform.Module;
 import org.sonar.server.almintegration.validator.GithubGlobalSettingsValidator;
+import org.sonar.server.almintegration.validator.GitlabGlobalSettingsValidator;
 import org.sonar.server.almintegration.ws.azure.ImportAzureProjectAction;
 import org.sonar.server.almintegration.ws.azure.ListAzureProjectsAction;
 import org.sonar.server.almintegration.ws.azure.SearchAzureReposAction;
@@ -54,6 +55,7 @@ public class AlmIntegrationsWSModule extends Module {
       ListGithubRepositoriesAction.class,
       ImportGitLabProjectAction.class,
       SearchGitlabReposAction.class,
+      GitlabGlobalSettingsValidator.class,
       ImportAzureProjectAction.class,
       ListAzureProjectsAction.class,
       SearchAzureReposAction.class,
index 1fb38f73ceee49f2f11141d2050503eccb685f19..61aa3c2cc14df75c702caf7f1084ed38ac9c3112 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.server.almsettings.ws;
 import org.sonar.alm.client.azure.AzureDevOpsHttpClient;
 import org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient;
 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
-import org.sonar.alm.client.gitlab.GitlabHttpClient;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -30,6 +29,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.server.almintegration.validator.GithubGlobalSettingsValidator;
+import org.sonar.server.almintegration.validator.GitlabGlobalSettingsValidator;
 import org.sonar.server.user.UserSession;
 
 public class ValidateAction implements AlmSettingsWsAction {
@@ -40,21 +40,21 @@ public class ValidateAction implements AlmSettingsWsAction {
   private final UserSession userSession;
   private final AlmSettingsSupport almSettingsSupport;
   private final AzureDevOpsHttpClient azureDevOpsHttpClient;
-  private final GitlabHttpClient gitlabHttpClient;
+  private final GitlabGlobalSettingsValidator gitlabSettingsValidator;
   private final GithubGlobalSettingsValidator githubGlobalSettingsValidator;
   private final BitbucketServerRestClient bitbucketServerRestClient;
   private final BitbucketCloudRestClient bitbucketCloudRestClient;
 
   public ValidateAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport,
     AzureDevOpsHttpClient azureDevOpsHttpClient,
-    GithubGlobalSettingsValidator githubGlobalSettingsValidator, GitlabHttpClient gitlabHttpClient,
+    GithubGlobalSettingsValidator githubGlobalSettingsValidator, GitlabGlobalSettingsValidator gitlabSettingsValidator,
     BitbucketServerRestClient bitbucketServerRestClient, BitbucketCloudRestClient bitbucketCloudRestClient) {
     this.dbClient = dbClient;
     this.userSession = userSession;
     this.almSettingsSupport = almSettingsSupport;
     this.azureDevOpsHttpClient = azureDevOpsHttpClient;
     this.githubGlobalSettingsValidator = githubGlobalSettingsValidator;
-    this.gitlabHttpClient = gitlabHttpClient;
+    this.gitlabSettingsValidator = gitlabSettingsValidator;
     this.bitbucketServerRestClient = bitbucketServerRestClient;
     this.bitbucketCloudRestClient = bitbucketCloudRestClient;
   }
@@ -87,7 +87,7 @@ public class ValidateAction implements AlmSettingsWsAction {
       AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
       switch (almSettingDto.getAlm()) {
         case GITLAB:
-          validateGitlab(almSettingDto);
+          gitlabSettingsValidator.validate(almSettingDto);
           break;
         case GITHUB:
           githubGlobalSettingsValidator.validate(almSettingDto);
@@ -113,13 +113,6 @@ public class ValidateAction implements AlmSettingsWsAction {
     }
   }
 
-  private void validateGitlab(AlmSettingDto almSettingDto) {
-    gitlabHttpClient.checkUrl(almSettingDto.getUrl());
-    gitlabHttpClient.checkToken(almSettingDto.getUrl(), almSettingDto.getPersonalAccessToken());
-    gitlabHttpClient.checkReadPermission(almSettingDto.getUrl(), almSettingDto.getPersonalAccessToken());
-    gitlabHttpClient.checkWritePermission(almSettingDto.getUrl(), almSettingDto.getPersonalAccessToken());
-  }
-
   private void validateBitbucketServer(AlmSettingDto almSettingDto) {
     bitbucketServerRestClient.validateUrl(almSettingDto.getUrl());
     bitbucketServerRestClient.validateToken(almSettingDto.getUrl(), almSettingDto.getPersonalAccessToken());
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidatorTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidatorTest.java
new file mode 100644 (file)
index 0000000..1e16fef
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.almintegration.validator;
+
+import org.junit.Test;
+import org.sonar.alm.client.gitlab.GitlabHttpClient;
+import org.sonar.db.alm.setting.AlmSettingDto;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.mock;
+
+public class GitlabGlobalSettingsValidatorTest {
+
+  private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class);
+
+  private final GitlabGlobalSettingsValidator underTest = new GitlabGlobalSettingsValidator(gitlabHttpClient);
+
+  @Test
+  public void validate_success() {
+    AlmSettingDto almSettingDto = new AlmSettingDto()
+      .setUrl("https://gitlab.com/api")
+      .setPersonalAccessToken("personal-access-token");
+
+    underTest.validate(almSettingDto);
+  }
+
+  @Test
+  public void validate_fail_url_not_set() {
+    AlmSettingDto almSettingDto = new AlmSettingDto()
+      .setUrl(null)
+      .setPersonalAccessToken("personal-access-token");
+
+    assertThatThrownBy(() -> underTest.validate(almSettingDto))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("Your Gitlab global configuration is incomplete.");
+  }
+
+  @Test
+  public void validate_fail_pat_not_set() {
+    AlmSettingDto almSettingDto = new AlmSettingDto()
+      .setUrl("https://gitlab.com/api")
+      .setPersonalAccessToken(null);
+
+    assertThatThrownBy(() -> underTest.validate(almSettingDto))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("Your Gitlab global configuration is incomplete.");
+  }
+
+}
index c576b29d7724559cf575e4e962ba31d382cfb1e5..90c0d8f02d1313db7db4cac83db83d8f3ea4c0ed 100644 (file)
@@ -25,13 +25,13 @@ import org.mockito.ArgumentCaptor;
 import org.sonar.alm.client.azure.AzureDevOpsHttpClient;
 import org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient;
 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
-import org.sonar.alm.client.gitlab.GitlabHttpClient;
 import org.sonar.api.resources.ResourceTypes;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbTester;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.almintegration.validator.GithubGlobalSettingsValidator;
+import org.sonar.server.almintegration.validator.GitlabGlobalSettingsValidator;
 import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
@@ -43,6 +43,7 @@ import org.sonar.server.ws.WsActionTester;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -58,12 +59,12 @@ public class ValidateActionTest {
   private final ComponentFinder componentFinder = new ComponentFinder(db.getDbClient(), mock(ResourceTypes.class));
   private final AlmSettingsSupport almSettingsSupport = new AlmSettingsSupport(db.getDbClient(), userSession, componentFinder, multipleAlmFeatureProvider);
   private final AzureDevOpsHttpClient azureDevOpsHttpClient = mock(AzureDevOpsHttpClient.class);
-  private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class);
+  private final GitlabGlobalSettingsValidator gitlabSettingsValidator = mock(GitlabGlobalSettingsValidator.class);
   private final GithubGlobalSettingsValidator githubGlobalSettingsValidator = mock(GithubGlobalSettingsValidator.class);
   private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
   private final BitbucketCloudRestClient bitbucketCloudRestClient = mock(BitbucketCloudRestClient.class);
   private final WsActionTester ws = new WsActionTester(
-    new ValidateAction(db.getDbClient(), userSession, almSettingsSupport, azureDevOpsHttpClient, githubGlobalSettingsValidator, gitlabHttpClient,
+    new ValidateAction(db.getDbClient(), userSession, almSettingsSupport, azureDevOpsHttpClient, githubGlobalSettingsValidator, gitlabSettingsValidator,
       bitbucketServerRestClient, bitbucketCloudRestClient));
 
   @Test
@@ -96,10 +97,7 @@ public class ValidateActionTest {
       .setParam("key", almSetting.getKey())
       .execute();
 
-    verify(gitlabHttpClient).checkUrl(almSetting.getUrl());
-    verify(gitlabHttpClient).checkToken(almSetting.getUrl(), almSetting.getPersonalAccessToken());
-    verify(gitlabHttpClient).checkReadPermission(almSetting.getUrl(), almSetting.getPersonalAccessToken());
-    verify(gitlabHttpClient).checkWritePermission(almSetting.getUrl(), almSetting.getPersonalAccessToken());
+    verify(gitlabSettingsValidator).validate(any(AlmSettingDto.class));
   }
 
   @Test