3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.almsettings.ws;
23 import java.util.Optional;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.sonar.alm.client.github.AppInstallationToken;
27 import org.sonar.alm.client.github.GithubApplicationClient;
28 import org.sonar.alm.client.github.GithubGlobalSettingsValidator;
29 import org.sonar.alm.client.github.config.GithubAppConfiguration;
30 import org.sonar.alm.client.github.security.AccessToken;
31 import org.sonar.api.server.ServerSide;
32 import org.sonar.auth.github.GitHubSettings;
33 import org.sonar.auth.github.GithubPermissionConverter;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.alm.setting.ALM;
37 import org.sonar.db.alm.setting.AlmSettingDto;
38 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
39 import org.sonar.server.component.ComponentUpdater;
40 import org.sonar.server.project.ProjectDefaultVisibility;
41 import org.sonar.server.user.UserSession;
43 import static java.lang.String.format;
44 import static org.sonar.core.ce.CeTaskCharacteristics.DEVOPS_PLATFORM_PROJECT_IDENTIFIER;
45 import static org.sonar.core.ce.CeTaskCharacteristics.DEVOPS_PLATFORM_URL;
48 public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory {
49 private static final Logger LOG = LoggerFactory.getLogger(GithubProjectCreatorFactory.class);
51 private final DbClient dbClient;
52 private final GithubGlobalSettingsValidator githubGlobalSettingsValidator;
53 private final GithubApplicationClient githubApplicationClient;
54 private final ProjectDefaultVisibility projectDefaultVisibility;
55 private final ProjectKeyGenerator projectKeyGenerator;
56 private final UserSession userSession;
57 private final ComponentUpdater componentUpdater;
58 private final GitHubSettings gitHubSettings;
59 private final GithubPermissionConverter githubPermissionConverter;
61 public GithubProjectCreatorFactory(DbClient dbClient, GithubGlobalSettingsValidator githubGlobalSettingsValidator,
62 GithubApplicationClient githubApplicationClient, ProjectDefaultVisibility projectDefaultVisibility, ProjectKeyGenerator projectKeyGenerator, UserSession userSession,
63 ComponentUpdater componentUpdater, GitHubSettings gitHubSettings, GithubPermissionConverter githubPermissionConverter) {
64 this.dbClient = dbClient;
65 this.githubGlobalSettingsValidator = githubGlobalSettingsValidator;
66 this.githubApplicationClient = githubApplicationClient;
67 this.projectDefaultVisibility = projectDefaultVisibility;
68 this.projectKeyGenerator = projectKeyGenerator;
69 this.userSession = userSession;
70 this.componentUpdater = componentUpdater;
71 this.gitHubSettings = gitHubSettings;
72 this.githubPermissionConverter = githubPermissionConverter;
76 public Optional<DevOpsProjectCreator> getDevOpsProjectCreator(DbSession dbSession, Map<String, String> characteristics) {
77 String githubApiUrl = characteristics.get(DEVOPS_PLATFORM_URL);
78 String githubRepository = characteristics.get(DEVOPS_PLATFORM_PROJECT_IDENTIFIER);
79 if (githubApiUrl == null || githubRepository == null) {
80 return Optional.empty();
82 DevOpsProjectDescriptor devOpsProjectDescriptor = new DevOpsProjectDescriptor(ALM.GITHUB, githubApiUrl, githubRepository);
84 Optional<DevOpsProjectCreator> githubProjectCreator = dbClient.almSettingDao().selectByAlm(dbSession, ALM.GITHUB).stream()
85 .filter(almSettingDto -> devOpsProjectDescriptor.url().equals(almSettingDto.getUrl()))
86 .map(almSettingDto -> findInstallationIdAndCreateDevOpsProjectCreator(dbSession, devOpsProjectDescriptor, almSettingDto))
87 .flatMap(Optional::stream)
90 if (githubProjectCreator.isPresent()) {
91 return githubProjectCreator;
94 throw new IllegalStateException(format("The project %s could not be created. It was auto-detected as a %s project "
95 + "and no valid DevOps platform configuration were found to access %s. Please check with a SonarQube administrator.",
96 devOpsProjectDescriptor.projectIdentifier(), devOpsProjectDescriptor.alm(), devOpsProjectDescriptor.url()));
99 private Optional<DevOpsProjectCreator> findInstallationIdAndCreateDevOpsProjectCreator(DbSession dbSession, DevOpsProjectDescriptor devOpsProjectDescriptor,
100 AlmSettingDto almSettingDto) {
101 GithubAppConfiguration githubAppConfiguration = githubGlobalSettingsValidator.validate(almSettingDto);
102 return findInstallationIdToAccessRepo(githubAppConfiguration, devOpsProjectDescriptor.projectIdentifier())
103 .map(installationId -> generateAppInstallationToken(githubAppConfiguration, installationId))
104 .map(appInstallationToken -> createGithubProjectCreator(dbSession, devOpsProjectDescriptor, almSettingDto, appInstallationToken));
107 private Optional<Long> findInstallationIdToAccessRepo(GithubAppConfiguration githubAppConfiguration, String repositoryKey) {
108 return githubApplicationClient.getInstallationId(githubAppConfiguration, repositoryKey);
111 private AppInstallationToken generateAppInstallationToken(GithubAppConfiguration githubAppConfiguration, long installationId) {
112 return githubApplicationClient.createAppInstallationToken(githubAppConfiguration, installationId)
113 .orElseThrow(() -> new IllegalStateException(format("Error while generating token for GitHub Api Url %s (installation id: %s)",
114 githubAppConfiguration.getApiEndpoint(), installationId)));
117 private GithubProjectCreator createGithubProjectCreator(DbSession dbSession, DevOpsProjectDescriptor devOpsProjectDescriptor, AlmSettingDto almSettingDto,
118 AppInstallationToken appInstallationToken) {
119 LOG.info("DevOps configuration {} auto-detected for project {}", almSettingDto.getKey(), devOpsProjectDescriptor.projectIdentifier());
120 GithubProjectCreationParameters githubProjectCreationParameters = new GithubProjectCreationParameters(devOpsProjectDescriptor,
121 almSettingDto, appInstallationToken, projectDefaultVisibility.get(dbSession).isPrivate(), gitHubSettings.isProvisioningEnabled(), userSession);
122 return new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater,
123 githubProjectCreationParameters);
126 public Optional<DevOpsProjectCreator> getDevOpsProjectCreator(DbSession dbSession, AlmSettingDto almSettingDto, AccessToken accessToken,
127 DevOpsProjectDescriptor devOpsProjectDescriptor) {
128 GithubProjectCreationParameters githubProjectCreationParameters = new GithubProjectCreationParameters(devOpsProjectDescriptor,
129 almSettingDto, accessToken, projectDefaultVisibility.get(dbSession).isPrivate(), gitHubSettings.isProvisioningEnabled(), userSession);
131 new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, componentUpdater, githubProjectCreationParameters)