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.sonar.alm.client.github.GithubApplicationClient;
25 import org.sonar.alm.client.github.GithubGlobalSettingsValidator;
26 import org.sonar.alm.client.github.config.GithubAppConfiguration;
27 import org.sonar.api.server.ServerSide;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.alm.setting.ALM;
30 import org.sonar.db.alm.setting.AlmSettingDao;
31 import org.sonar.db.alm.setting.AlmSettingDto;
34 public class GitHubDevOpsPlatformService implements DevOpsPlatformService {
36 public static final String DEVOPS_PLATFORM_URL = "devOpsPlatformUrl";
37 public static final String DEVOPS_PLATFORM_PROJECT_IDENTIFIER = "devOpsPlatformProjectIdentifier";
39 private final AlmSettingDao almSettingDao;
40 private final GithubGlobalSettingsValidator githubGlobalSettingsValidator;
41 private final GithubApplicationClient githubApplicationClient;
43 public GitHubDevOpsPlatformService(AlmSettingDao almSettingDao, GithubGlobalSettingsValidator githubGlobalSettingsValidator,
44 GithubApplicationClient githubApplicationClient) {
45 this.almSettingDao = almSettingDao;
46 this.githubGlobalSettingsValidator = githubGlobalSettingsValidator;
47 this.githubApplicationClient = githubApplicationClient;
51 public ALM getDevOpsPlatform() {
56 public Optional<DevOpsProjectDescriptor> getDevOpsProjectDescriptor(Map<String, String> characteristics) {
57 String githubApiUrl = characteristics.get(DEVOPS_PLATFORM_URL);
58 String githubRepository = characteristics.get(DEVOPS_PLATFORM_PROJECT_IDENTIFIER);
59 if (githubApiUrl != null && githubRepository != null) {
60 return Optional.of(new DevOpsProjectDescriptor(ALM.GITHUB, githubApiUrl, githubRepository));
62 return Optional.empty();
66 public Optional<AlmSettingDto> getValidAlmSettingDto(DbSession dbSession, DevOpsProjectDescriptor devOpsProjectDescriptor) {
67 return almSettingDao.selectByAlm(dbSession, getDevOpsPlatform()).stream()
68 .filter(almSettingDto -> devOpsProjectDescriptor.url().equals(almSettingDto.getUrl()))
69 .filter(almSettingDto -> hasAccessToRepo(almSettingDto, devOpsProjectDescriptor.projectIdentifier()))
73 private boolean hasAccessToRepo(AlmSettingDto almSettingDto, String repo) {
74 GithubAppConfiguration githubAppConfiguration = githubGlobalSettingsValidator.validate(almSettingDto);
75 return githubApplicationClient.getInstallationId(githubAppConfiguration, repo).isPresent();