From 2a6ffc93a81e59567f935c724ca9ea339a25ed80 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Thu, 19 Oct 2023 15:20:40 +0200 Subject: [PATCH] SONAR-20699 Don't fail project creation when GitHub configuration is not found. --- .../ws/GithubProjectCreatorFactory.java | 13 +++--------- .../ws/GithubProjectCreatorFactoryTest.java | 21 +++++++------------ 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java index e6387b65e9c..d8a5adc3f82 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactory.java @@ -26,11 +26,11 @@ import org.slf4j.LoggerFactory; import org.sonar.alm.client.github.AppInstallationToken; import org.sonar.alm.client.github.GithubApplicationClient; import org.sonar.alm.client.github.GithubGlobalSettingsValidator; +import org.sonar.alm.client.github.GithubPermissionConverter; import org.sonar.alm.client.github.config.GithubAppConfiguration; import org.sonar.alm.client.github.security.AccessToken; import org.sonar.api.server.ServerSide; import org.sonar.auth.github.GitHubSettings; -import org.sonar.alm.client.github.GithubPermissionConverter; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.ALM; @@ -93,19 +93,12 @@ public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory } DevOpsProjectDescriptor devOpsProjectDescriptor = new DevOpsProjectDescriptor(ALM.GITHUB, githubApiUrl, githubRepository); - Optional githubProjectCreator = dbClient.almSettingDao().selectByAlm(dbSession, ALM.GITHUB).stream() + return dbClient.almSettingDao().selectByAlm(dbSession, ALM.GITHUB).stream() .filter(almSettingDto -> devOpsProjectDescriptor.url().equals(almSettingDto.getUrl())) .map(almSettingDto -> findInstallationIdAndCreateDevOpsProjectCreator(dbSession, devOpsProjectDescriptor, almSettingDto)) .flatMap(Optional::stream) .findFirst(); - if (githubProjectCreator.isPresent()) { - return githubProjectCreator; - } - - throw new IllegalStateException(format("The project %s could not be created. It was auto-detected as a %s project " - + "and no valid DevOps platform configuration were found to access %s. Please check with a SonarQube administrator.", - devOpsProjectDescriptor.projectIdentifier(), devOpsProjectDescriptor.alm(), devOpsProjectDescriptor.url())); } private Optional findInstallationIdAndCreateDevOpsProjectCreator(DbSession dbSession, DevOpsProjectDescriptor devOpsProjectDescriptor, @@ -144,7 +137,7 @@ public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory GithubAppConfiguration githubAppConfiguration = new GithubAppConfiguration(Long.parseLong(gitHubSettings.appId()), gitHubSettings.privateKey(), gitHubSettings.apiURL()); long installationId = findInstallationIdToAccessRepo(githubAppConfiguration, devOpsProjectDescriptor.projectIdentifier()) .orElseThrow(() -> new IllegalStateException(format("GitHub auto-provisioning is activated. However the repo %s is not in the scope of the authentication application. " - + "The permissions can't be checked, and the project can not be created.", + + "The permissions can't be checked, and the project can not be created.", devOpsProjectDescriptor.projectIdentifier()))); return Optional.of(generateAppInstallationToken(githubAppConfiguration, installationId)); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java index 498ece5fbf5..0afb9ab71c6 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/GithubProjectCreatorFactoryTest.java @@ -31,8 +31,8 @@ import org.mockito.junit.MockitoJUnitRunner; import org.sonar.alm.client.github.AppInstallationToken; import org.sonar.alm.client.github.GithubApplicationClient; import org.sonar.alm.client.github.GithubGlobalSettingsValidator; -import org.sonar.auth.github.GitHubSettings; import org.sonar.alm.client.github.GithubPermissionConverter; +import org.sonar.auth.github.GitHubSettings; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.alm.setting.ALM; @@ -58,10 +58,6 @@ import static org.sonar.core.ce.CeTaskCharacteristics.DEVOPS_PLATFORM_URL; @RunWith(MockitoJUnitRunner.class) public class GithubProjectCreatorFactoryTest { - private static final String EXCEPTION_MESSAGE = - "The project orgname/projectName could not be created. It was auto-detected as a GITHUB project and no valid DevOps platform configuration were found to access https://api.toto.com." - + " Please check with a SonarQube administrator."; - private static final String PROJECT_NAME = "projectName"; private static final String ORGANIZATION_NAME = "orgname"; private static final String GITHUB_REPO_FULL_NAME = ORGANIZATION_NAME + "/" + PROJECT_NAME; @@ -70,8 +66,7 @@ public class GithubProjectCreatorFactoryTest { private static final DevOpsProjectDescriptor GITHUB_PROJECT_DESCRIPTOR = new DevOpsProjectDescriptor(ALM.GITHUB, GITHUB_API_URL, GITHUB_REPO_FULL_NAME); private static final Map VALID_GITHUB_PROJECT_COORDINATES = Map.of( DEVOPS_PLATFORM_URL, GITHUB_PROJECT_DESCRIPTOR.url(), - DEVOPS_PLATFORM_PROJECT_IDENTIFIER, GITHUB_PROJECT_DESCRIPTOR.projectIdentifier() - ); + DEVOPS_PLATFORM_PROJECT_IDENTIFIER, GITHUB_PROJECT_DESCRIPTOR.projectIdentifier()); private static final long APP_INSTALLATION_ID = 534534534543L; @Mock @@ -116,9 +111,9 @@ public class GithubProjectCreatorFactoryTest { } @Test - public void getDevOpsProjectCreator_whenValidCharacteristicsButNoAlmSettingDao_shouldThrow() { - assertThatIllegalStateException().isThrownBy(() -> githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, VALID_GITHUB_PROJECT_COORDINATES)) - .withMessage(EXCEPTION_MESSAGE); + public void getDevOpsProjectCreator_whenValidCharacteristicsButNoAlmSettingDao_shouldReturnEmpty() { + Optional devOpsProjectCreator = githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, VALID_GITHUB_PROJECT_COORDINATES); + assertThat(devOpsProjectCreator).isEmpty(); } @Test @@ -132,12 +127,12 @@ public class GithubProjectCreatorFactoryTest { } @Test - public void getDevOpsProjectCreator_whenAppHasNoAccessToRepo_shouldThrow() { + public void getDevOpsProjectCreator_whenAppHasNoAccessToRepo_shouldReturnEmpty() { mockAlmSettingDto(true); when(githubApplicationClient.getInstallationId(any(), eq(GITHUB_REPO_FULL_NAME))).thenReturn(Optional.empty()); - assertThatIllegalStateException().isThrownBy(() -> githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, VALID_GITHUB_PROJECT_COORDINATES)) - .withMessage(EXCEPTION_MESSAGE); + Optional devOpsProjectCreator = githubProjectCreatorFactory.getDevOpsProjectCreator(dbSession, VALID_GITHUB_PROJECT_COORDINATES); + assertThat(devOpsProjectCreator).isEmpty(); } @Test -- 2.39.5