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.almsettings.azuredevops;
22 import java.util.Optional;
23 import org.jetbrains.annotations.Nullable;
24 import org.sonar.alm.client.azure.AzureDevOpsHttpClient;
25 import org.sonar.alm.client.azure.AzureDevopsServerException;
26 import org.sonar.alm.client.azure.GsonAzureRepo;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.alm.pat.AlmPatDto;
30 import org.sonar.db.alm.setting.AlmSettingDto;
31 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
32 import org.sonar.db.project.CreationMethod;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.server.common.almintegration.ProjectKeyGenerator;
35 import org.sonar.server.common.almsettings.DevOpsProjectCreator;
36 import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
37 import org.sonar.server.common.project.ProjectCreator;
38 import org.sonar.server.component.ComponentCreationData;
39 import org.sonar.server.user.UserSession;
41 import static com.google.common.base.Preconditions.checkArgument;
42 import static java.lang.String.format;
43 import static java.util.Objects.requireNonNull;
45 public class AzureDevOpsProjectCreator implements DevOpsProjectCreator {
47 private final DbClient dbClient;
48 private final AlmSettingDto almSettingDto;
49 private final DevOpsProjectDescriptor devOpsProjectDescriptor;
50 private final UserSession userSession;
51 private final AzureDevOpsHttpClient azureDevOpsHttpClient;
52 private final ProjectCreator projectCreator;
53 private final ProjectKeyGenerator projectKeyGenerator;
55 public AzureDevOpsProjectCreator(DbClient dbClient, AlmSettingDto almSettingDto, DevOpsProjectDescriptor devOpsProjectDescriptor, UserSession userSession,
56 AzureDevOpsHttpClient azureDevOpsHttpClient, ProjectCreator projectCreator, ProjectKeyGenerator projectKeyGenerator) {
57 this.dbClient = dbClient;
58 this.almSettingDto = almSettingDto;
59 this.devOpsProjectDescriptor = devOpsProjectDescriptor;
60 this.userSession = userSession;
61 this.azureDevOpsHttpClient = azureDevOpsHttpClient;
62 this.projectCreator = projectCreator;
63 this.projectKeyGenerator = projectKeyGenerator;
67 public boolean isScanAllowedUsingPermissionsFromDevopsPlatform() {
68 throw new UnsupportedOperationException("Not Implemented");
72 public ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, CreationMethod creationMethod, Boolean monorepo, @Nullable String projectKey,
73 @Nullable String projectName) {
74 String pat = findPersonalAccessTokenOrThrow(dbSession, almSettingDto);
75 String url = requireNonNull(almSettingDto.getUrl(), "DevOps Platform url cannot be null");
76 checkArgument(devOpsProjectDescriptor.projectIdentifier() != null, "DevOps Project Identifier cannot be null for Azure DevOps");
77 GsonAzureRepo repo = fetchAzureDevOpsProject(url, pat, devOpsProjectDescriptor.projectIdentifier(), devOpsProjectDescriptor.repositoryIdentifier());
79 ComponentCreationData componentCreationData = projectCreator.createProject(
81 getProjectKey(projectKey, repo),
82 getProjectName(projectName, repo),
83 repo.getDefaultBranchName(),
85 ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
86 createProjectAlmSettingDto(dbSession, repo, projectDto, almSettingDto, monorepo);
87 return componentCreationData;
90 private String findPersonalAccessTokenOrThrow(DbSession dbSession, AlmSettingDto almSettingDto) {
91 String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
92 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
93 return almPatDto.map(AlmPatDto::getPersonalAccessToken)
94 .orElseThrow(() -> new IllegalArgumentException(String.format("personal access token for '%s' is missing", almSettingDto.getKey())));
97 private GsonAzureRepo fetchAzureDevOpsProject(String azureDevOpsUrl, String pat, String projectIdentifier, String repositoryIdentifier) {
99 return azureDevOpsHttpClient.getRepo(azureDevOpsUrl, pat, projectIdentifier, repositoryIdentifier);
100 } catch (AzureDevopsServerException e) {
101 throw new IllegalStateException(format("Failed to fetch AzureDevOps repository '%s' from project '%s' from '%s'", repositoryIdentifier, projectIdentifier, azureDevOpsUrl),
106 private String getProjectKey(@Nullable String projectKey, GsonAzureRepo repository) {
107 return Optional.ofNullable(projectKey).orElseGet(() -> projectKeyGenerator.generateUniqueProjectKey(repository.getProject().getName(), repository.getName()));
110 private static String getProjectName(@Nullable String projectName, GsonAzureRepo repository) {
111 return Optional.ofNullable(projectName).orElse(repository.getName());
114 private void createProjectAlmSettingDto(DbSession dbSession, GsonAzureRepo repository, ProjectDto projectDto, AlmSettingDto almSettingDto, Boolean monorepo) {
115 ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
116 .setAlmSettingUuid(almSettingDto.getUuid())
117 .setAlmRepo(repository.getName())
118 .setAlmSlug(repository.getProject().getName())
119 .setProjectUuid(projectDto.getUuid())
120 .setSummaryCommentEnabled(true)
121 .setMonorepo(monorepo);
122 dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), projectDto.getName(), projectDto.getKey());