aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJacek <jacek.poreda@sonarsource.com>2021-06-07 13:57:26 +0200
committersonartech <sonartech@sonarsource.com>2021-06-10 20:03:27 +0000
commit1caaacfcbf21b4d12b6ee428684331e464b9ac5f (patch)
tree8b98fb92ee83e2221b6334ffd29627b7f85d9d4d /server
parent53642efcbee232d2cd110f04e3ad91958f118d8f (diff)
downloadsonarqube-1caaacfcbf21b4d12b6ee428684331e464b9ac5f.tar.gz
sonarqube-1caaacfcbf21b4d12b6ee428684331e464b9ac5f.zip
SONAR-14871 Extract global and project Gitlab validation to separate classes
Diffstat (limited to 'server')
-rw-r--r--server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java2
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidator.java49
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java2
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ValidateAction.java17
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidatorTest.java66
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java12
6 files changed, 128 insertions, 20 deletions
diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
index 3ece1b02483..788caa61d2c 100644
--- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
+++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
@@ -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
index 00000000000..40c4195e81b
--- /dev/null
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidator.java
@@ -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);
+ }
+
+}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java
index 8bc96cd0439..912f1a5f90b 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java
@@ -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,
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ValidateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ValidateAction.java
index 1fb38f73cee..61aa3c2cc14 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ValidateAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/ValidateAction.java
@@ -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
index 00000000000..1e16fef29fa
--- /dev/null
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/validator/GitlabGlobalSettingsValidatorTest.java
@@ -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.");
+ }
+
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java
index c576b29d772..90c0d8f02d1 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java
@@ -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