]> source.dussan.org Git - sonarqube.git/blob
feaab35332b7ccf742ff9bd3932f6b69bbed2de0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.common.project;
21
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;
38
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;
43
44 @ServerSide
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;
51
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;
59   }
60
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
67       DevOpsProjectCreator projectCreator = devOpsProjectCreatorFactory.getDevOpsProjectCreator(almSetting, projectDescriptor)
68         .orElseThrow(() -> new IllegalArgumentException(format("Platform %s not supported", almSetting.getAlm().name())));
69
70       CreationMethod creationMethod = getCreationMethod(ALM_IMPORT, userSession.isAuthenticatedBrowserSession());
71       ComponentCreationData componentCreationData = projectCreator.createProjectAndBindToDevOpsPlatform(
72         dbSession,
73         creationMethod,
74         request.monorepo(),
75         request.projectKey(),
76         request.projectName());
77
78       ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
79       BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
80
81       if (request.newCodeDefinitionType() != null) {
82         newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
83           mainBranchDto.getKey(), request.newCodeDefinitionType(), request.newCodeDefinitionValue());
84       }
85       componentUpdater.commitAndIndex(dbSession, componentCreationData);
86       ProjectAlmSettingDto projectAlmSettingDto = dbClient.projectAlmSettingDao().selectByProject(dbSession, projectDto)
87         .orElseThrow(() -> new IllegalStateException("Project ALM setting was not created"));
88       dbSession.commit();
89       return new ImportedProject(projectDto, projectAlmSettingDto);
90     }
91   }
92
93 }