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.bitbucketserver;
22 import java.util.Optional;
23 import org.jetbrains.annotations.Nullable;
24 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
25 import org.sonar.alm.client.bitbucketserver.Branch;
26 import org.sonar.alm.client.bitbucketserver.BranchesList;
27 import org.sonar.alm.client.bitbucketserver.Repository;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.alm.pat.AlmPatDto;
31 import org.sonar.db.alm.setting.AlmSettingDto;
32 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
33 import org.sonar.db.project.CreationMethod;
34 import org.sonar.db.project.ProjectDto;
35 import org.sonar.server.common.almintegration.ProjectKeyGenerator;
36 import org.sonar.server.common.almsettings.DevOpsProjectCreator;
37 import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
38 import org.sonar.server.common.project.ProjectCreator;
39 import org.sonar.server.component.ComponentCreationData;
40 import org.sonar.server.user.UserSession;
42 import static java.lang.String.format;
43 import static java.util.Objects.requireNonNull;
45 public class BitbucketServerProjectCreator implements DevOpsProjectCreator {
47 private final AlmSettingDto almSettingDto;
48 private final BitbucketServerRestClient bitbucketServerRestClient;
49 private final DbClient dbClient;
50 private final DevOpsProjectDescriptor devOpsProjectDescriptor;
51 private final UserSession userSession;
52 private final ProjectCreator projectCreator;
53 private final ProjectKeyGenerator projectKeyGenerator;
55 public BitbucketServerProjectCreator(AlmSettingDto almSettingDto, BitbucketServerRestClient bitbucketServerRestClient,
56 DbClient dbClient, DevOpsProjectDescriptor devOpsProjectDescriptor, UserSession userSession, ProjectCreator projectCreator,
57 ProjectKeyGenerator projectKeyGenerator) {
58 this.almSettingDto = almSettingDto;
59 this.bitbucketServerRestClient = bitbucketServerRestClient;
60 this.dbClient = dbClient;
61 this.devOpsProjectDescriptor = devOpsProjectDescriptor;
62 this.userSession = userSession;
63 this.projectCreator = projectCreator;
64 this.projectKeyGenerator = projectKeyGenerator;
68 public boolean isScanAllowedUsingPermissionsFromDevopsPlatform() {
69 throw new UnsupportedOperationException("Not Implemented");
73 public ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, CreationMethod creationMethod, Boolean monorepo, @Nullable String projectKey,
74 @Nullable String projectName) {
76 String pat = findPersonalAccessTokenOrThrow(dbSession);
77 String url = requireNonNull(almSettingDto.getUrl(), "DevOps Platform url cannot be null");
78 String bitbucketRepo = devOpsProjectDescriptor.repositoryIdentifier();
79 String bitbucketProject = getBitbucketProjectOrThrow();
81 Repository repository = bitbucketServerRestClient.getRepo(url, pat, bitbucketProject, bitbucketRepo);
82 String defaultBranchName = getDefaultBranchName(url, pat, bitbucketProject, bitbucketRepo);
84 ComponentCreationData componentCreationData = projectCreator.createProject(
86 getProjectKey(projectKey, repository),
87 getProjectName(projectName, repository),
90 ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
91 createProjectAlmSettingDto(dbSession, repository.getSlug(), projectDto, almSettingDto, monorepo);
93 return componentCreationData;
96 private String findPersonalAccessTokenOrThrow(DbSession dbSession) {
97 String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
98 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
100 return almPatDto.map(AlmPatDto::getPersonalAccessToken)
101 .orElseThrow(() -> new IllegalArgumentException(format("personal access token for '%s' is missing", almSettingDto.getKey())));
104 private String getBitbucketProjectOrThrow() {
105 if (devOpsProjectDescriptor.projectIdentifier() == null) {
106 throw new IllegalArgumentException(String.format("The BitBucket project, in which the repository %s is located, is mandatory",
107 devOpsProjectDescriptor.repositoryIdentifier()));
109 return devOpsProjectDescriptor.projectIdentifier();
112 private String getDefaultBranchName(String url, String pat, String project, String repo) {
113 BranchesList branches = bitbucketServerRestClient.getBranches(url, pat, project, repo);
114 Optional<Branch> defaultBranch = branches.findDefaultBranch();
115 return defaultBranch.map(Branch::getName).orElse(null);
118 private String getProjectKey(@Nullable String projectKey, Repository repo) {
119 return Optional.ofNullable(projectKey).orElseGet(() -> projectKeyGenerator.generateUniqueProjectKey(repo.getProject().getKey(), repo.getSlug()));
122 private static String getProjectName(@Nullable String projectName, Repository repository) {
123 return Optional.ofNullable(projectName).orElse(repository.getName());
126 private void createProjectAlmSettingDto(DbSession dbSession, String repoSlug, ProjectDto projectDto, AlmSettingDto almSettingDto,
127 Boolean isMonorepo) {
128 ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
129 .setAlmSettingUuid(almSettingDto.getUuid())
130 .setAlmRepo(repoSlug)
132 .setProjectUuid(projectDto.getUuid())
133 .setSummaryCommentEnabled(true)
134 .setMonorepo(isMonorepo);
136 dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), projectDto.getName(), projectDto.getKey());