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;
25 import javax.annotation.Priority;
26 import org.apache.commons.lang.NotImplementedException;
27 import org.sonar.alm.client.github.security.AccessToken;
28 import org.sonar.api.server.ServerSide;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.alm.setting.ALM;
31 import org.sonar.db.alm.setting.AlmSettingDto;
32 import org.sonar.server.component.ComponentCreationData;
36 public class DelegatingDevOpsPlatformService implements DevOpsPlatformService {
38 private final Set<DevOpsPlatformService> delegates;
40 public DelegatingDevOpsPlatformService(Set<DevOpsPlatformService> delegates) {
41 this.delegates = delegates;
45 public ALM getDevOpsPlatform() {
46 throw new NotImplementedException();
50 public Optional<DevOpsProjectDescriptor> getDevOpsProjectDescriptor(Map<String, String> characteristics) {
51 return delegates.stream()
52 .flatMap(delegate -> delegate.getDevOpsProjectDescriptor(characteristics).stream())
57 public Optional<AlmSettingDto> getValidAlmSettingDto(DbSession dbSession, DevOpsProjectDescriptor devOpsProjectDescriptor) {
58 return findDelegate(devOpsProjectDescriptor.alm())
59 .flatMap(delegate -> delegate.getValidAlmSettingDto(dbSession, devOpsProjectDescriptor));
63 public boolean isScanAllowedUsingPermissionsFromDevopsPlatform(AlmSettingDto almSettingDto, DevOpsProjectDescriptor devOpsProjectDescriptor) {
64 return findDelegate(devOpsProjectDescriptor.alm())
65 .map(delegate -> delegate.isScanAllowedUsingPermissionsFromDevopsPlatform(almSettingDto, devOpsProjectDescriptor))
66 .orElseThrow(() -> new IllegalStateException("No delegate found to handle projects on " + devOpsProjectDescriptor.alm()));
70 public ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, String projectKey, AlmSettingDto almSettingDto,
71 DevOpsProjectDescriptor devOpsProjectDescriptor) {
72 return findDelegate(almSettingDto.getAlm())
73 .map(delegate -> delegate.createProjectAndBindToDevOpsPlatform(dbSession, projectKey, almSettingDto, devOpsProjectDescriptor))
74 .orElseThrow(() -> new IllegalStateException("Impossible to bind project to ALM platform " + almSettingDto.getAlm()));
78 public ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, AlmSettingDto almSettingDto, AccessToken accessToken,
79 DevOpsProjectDescriptor devOpsProjectDescriptor) {
80 return findDelegate(almSettingDto.getAlm())
81 .map(delegate -> delegate.createProjectAndBindToDevOpsPlatform(dbSession, almSettingDto, accessToken, devOpsProjectDescriptor))
82 .orElseThrow(() -> new IllegalStateException("Impossible to bind project to ALM platform " + almSettingDto.getAlm()));
85 private Optional<DevOpsPlatformService> findDelegate(ALM alm) {
86 return delegates.stream()
87 .filter(delegate -> delegate.getDevOpsPlatform().equals(alm))