.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())
--- /dev/null
+/*
+ * 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);
+ }
+
+}
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;
ListGithubRepositoriesAction.class,
ImportGitLabProjectAction.class,
SearchGitlabReposAction.class,
+ GitlabGlobalSettingsValidator.class,
ImportAzureProjectAction.class,
ListAzureProjectsAction.class,
SearchAzureReposAction.class,
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;
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 {
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;
}
AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
switch (almSettingDto.getAlm()) {
case GITLAB:
- validateGitlab(almSettingDto);
+ gitlabSettingsValidator.validate(almSettingDto);
break;
case GITHUB:
githubGlobalSettingsValidator.validate(almSettingDto);
}
}
- 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());
--- /dev/null
+/*
+ * 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.");
+ }
+
+}
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;
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;
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
.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