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.bitbucketcloud;
22 import java.util.Optional;
23 import org.jetbrains.annotations.Nullable;
24 import org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient;
25 import org.sonar.alm.client.bitbucket.bitbucketcloud.Repository;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.alm.pat.AlmPatDto;
29 import org.sonar.db.alm.setting.AlmSettingDto;
30 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
31 import org.sonar.db.project.CreationMethod;
32 import org.sonar.db.project.ProjectDto;
33 import org.sonar.server.common.almintegration.ProjectKeyGenerator;
34 import org.sonar.server.common.almsettings.DevOpsProjectCreator;
35 import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
36 import org.sonar.server.common.project.ProjectCreator;
37 import org.sonar.server.component.ComponentCreationData;
38 import org.sonar.server.user.UserSession;
40 import static java.lang.String.format;
41 import static java.util.Objects.requireNonNull;
42 import static java.util.Optional.ofNullable;
44 public class BitbucketCloudProjectCreator implements DevOpsProjectCreator {
46 private final DbClient dbClient;
47 private final AlmSettingDto almSettingDto;
48 private final DevOpsProjectDescriptor devOpsProjectDescriptor;
49 private final UserSession userSession;
50 private final BitbucketCloudRestClient bitbucketCloudRestClient;
51 private final ProjectCreator projectCreator;
52 private final ProjectKeyGenerator projectKeyGenerator;
55 public BitbucketCloudProjectCreator(DbClient dbClient, AlmSettingDto almSettingDto, DevOpsProjectDescriptor devOpsProjectDescriptor, UserSession userSession,
56 BitbucketCloudRestClient bitbucketCloudRestClient, ProjectCreator projectCreator, ProjectKeyGenerator projectKeyGenerator) {
57 this.dbClient = dbClient;
58 this.almSettingDto = almSettingDto;
59 this.devOpsProjectDescriptor = devOpsProjectDescriptor;
60 this.userSession = userSession;
61 this.bitbucketCloudRestClient = bitbucketCloudRestClient;
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) {
75 String pat = findPersonalAccessTokenOrThrow(dbSession, almSettingDto);
76 String workspace = ofNullable(almSettingDto.getAppId())
77 .orElseThrow(() -> new IllegalArgumentException(String.format("workspace for alm setting %s is missing", almSettingDto.getKey())));
79 Repository repo = bitbucketCloudRestClient.getRepo(pat, workspace, devOpsProjectDescriptor.repositoryIdentifier());
81 ComponentCreationData componentCreationData = projectCreator.createProject(
83 getProjectKey(workspace, projectKey, repo),
84 getProjectName(projectName, repo),
85 repo.getMainBranch().getName(),
87 ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
89 createProjectAlmSettingDto(dbSession, repo.getSlug(), projectDto, almSettingDto, monorepo);
90 return componentCreationData;
93 private String findPersonalAccessTokenOrThrow(DbSession dbSession, AlmSettingDto almSettingDto) {
94 String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
95 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
96 return almPatDto.map(AlmPatDto::getPersonalAccessToken)
97 .orElseThrow(() -> new IllegalArgumentException(format("personal access token for '%s' is missing", almSettingDto.getKey())));
100 private String getProjectKey(String workspace, @Nullable String projectKey, Repository repository) {
101 return Optional.ofNullable(projectKey).orElseGet(() -> projectKeyGenerator.generateUniqueProjectKey(workspace, repository.getSlug()));
104 private static String getProjectName(@Nullable String projectName, Repository repository) {
105 return Optional.ofNullable(projectName).orElse(repository.getName());
108 private void createProjectAlmSettingDto(DbSession dbSession, String repoSlug, ProjectDto projectDto, AlmSettingDto almSettingDto, Boolean monorepo) {
109 ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
110 .setAlmSettingUuid(almSettingDto.getUuid())
111 .setAlmRepo(repoSlug)
113 .setProjectUuid(projectDto.getUuid())
114 .setSummaryCommentEnabled(true)
115 .setMonorepo(monorepo);
116 dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), projectDto.getName(), projectDto.getKey());