3 * Copyright (C) 2009-2024 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.common.project;
22 import java.util.Optional;
23 import org.sonar.api.server.ServerSide;
24 import org.sonar.db.DbClient;
25 import org.sonar.db.DbSession;
26 import org.sonar.db.alm.setting.AlmSettingDto;
27 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
28 import org.sonar.db.component.BranchDto;
29 import org.sonar.db.project.CreationMethod;
30 import org.sonar.db.project.ProjectDto;
31 import org.sonar.server.common.almsettings.DevOpsProjectCreator;
32 import org.sonar.server.common.almsettings.DevOpsProjectCreatorFactory;
33 import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
34 import org.sonar.server.common.component.ComponentUpdater;
35 import org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver;
36 import org.sonar.server.component.ComponentCreationData;
37 import org.sonar.server.user.UserSession;
39 import static java.lang.String.format;
40 import static org.sonar.db.project.CreationMethod.getCreationMethod;
41 import static org.sonar.db.project.CreationMethod.Category.ALM_IMPORT;
42 import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.checkNewCodeDefinitionParam;
45 public class ImportProjectService {
46 private final DbClient dbClient;
47 private final DevOpsProjectCreatorFactory devOpsProjectCreatorFactory;
48 private final UserSession userSession;
49 private final ComponentUpdater componentUpdater;
50 private final NewCodeDefinitionResolver newCodeDefinitionResolver;
52 public ImportProjectService(DbClient dbClient, DevOpsProjectCreatorFactory devOpsProjectCreatorFactory, UserSession userSession, ComponentUpdater componentUpdater,
53 NewCodeDefinitionResolver newCodeDefinitionResolver) {
54 this.dbClient = dbClient;
55 this.devOpsProjectCreatorFactory = devOpsProjectCreatorFactory;
56 this.userSession = userSession;
57 this.componentUpdater = componentUpdater;
58 this.newCodeDefinitionResolver = newCodeDefinitionResolver;
61 public ImportedProject importProject(ImportProjectRequest request) {
62 try (DbSession dbSession = dbClient.openSession(false)) {
63 checkNewCodeDefinitionParam(request.newCodeDefinitionType(), request.newCodeDefinitionValue());
64 AlmSettingDto almSetting = dbClient.almSettingDao().selectByUuid(dbSession, request.almSettingId()).orElseThrow(() -> new IllegalArgumentException("ALM setting not found"));
65 DevOpsProjectDescriptor projectDescriptor = new DevOpsProjectDescriptor(almSetting.getAlm(), almSetting.getUrl(), request.repositoryIdentifier(),
66 request.projectIdentifier());
68 DevOpsProjectCreator projectCreator = devOpsProjectCreatorFactory.getDevOpsProjectCreator(almSetting, projectDescriptor)
69 .orElseThrow(() -> new IllegalArgumentException(format("Platform %s not supported", almSetting.getAlm().name())));
71 CreationMethod creationMethod = getCreationMethod(ALM_IMPORT, userSession.isAuthenticatedBrowserSession());
72 ComponentCreationData componentCreationData = projectCreator.createProjectAndBindToDevOpsPlatform(
77 request.projectName());
79 ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
80 BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
82 if (request.newCodeDefinitionType() != null) {
83 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
84 mainBranchDto.getKey(), request.newCodeDefinitionType(), request.newCodeDefinitionValue());
86 componentUpdater.commitAndIndex(dbSession, componentCreationData);
87 ProjectAlmSettingDto projectAlmSettingDto = dbClient.projectAlmSettingDao().selectByProject(dbSession, projectDto)
88 .orElseThrow(() -> new IllegalStateException("Project ALM setting was not created"));
90 return new ImportedProject(projectDto, projectAlmSettingDto);