You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitbucketServerProjectCreator.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.almsettings.bitbucketserver;
  21. import java.util.Optional;
  22. import org.jetbrains.annotations.Nullable;
  23. import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
  24. import org.sonar.alm.client.bitbucketserver.Branch;
  25. import org.sonar.alm.client.bitbucketserver.BranchesList;
  26. import org.sonar.alm.client.bitbucketserver.Repository;
  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;
  40. import static java.lang.String.format;
  41. import static java.util.Objects.requireNonNull;
  42. public class BitbucketServerProjectCreator implements DevOpsProjectCreator {
  43. private final AlmSettingDto almSettingDto;
  44. private final BitbucketServerRestClient bitbucketServerRestClient;
  45. private final DbClient dbClient;
  46. private final DevOpsProjectDescriptor devOpsProjectDescriptor;
  47. private final UserSession userSession;
  48. private final ProjectCreator projectCreator;
  49. private final ProjectKeyGenerator projectKeyGenerator;
  50. public BitbucketServerProjectCreator(AlmSettingDto almSettingDto, BitbucketServerRestClient bitbucketServerRestClient,
  51. DbClient dbClient, DevOpsProjectDescriptor devOpsProjectDescriptor, UserSession userSession, ProjectCreator projectCreator,
  52. ProjectKeyGenerator projectKeyGenerator) {
  53. this.almSettingDto = almSettingDto;
  54. this.bitbucketServerRestClient = bitbucketServerRestClient;
  55. this.dbClient = dbClient;
  56. this.devOpsProjectDescriptor = devOpsProjectDescriptor;
  57. this.userSession = userSession;
  58. this.projectCreator = projectCreator;
  59. this.projectKeyGenerator = projectKeyGenerator;
  60. }
  61. @Override
  62. public boolean isScanAllowedUsingPermissionsFromDevopsPlatform() {
  63. throw new UnsupportedOperationException("Not Implemented");
  64. }
  65. @Override
  66. public ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, CreationMethod creationMethod, Boolean monorepo, @Nullable String projectKey,
  67. @Nullable String projectName) {
  68. String pat = findPersonalAccessTokenOrThrow(dbSession);
  69. String url = requireNonNull(almSettingDto.getUrl(), "DevOps Platform url cannot be null");
  70. String bitbucketRepo = devOpsProjectDescriptor.repositoryIdentifier();
  71. String bitbucketProject = getBitbucketProjectOrThrow();
  72. Repository repository = bitbucketServerRestClient.getRepo(url, pat, bitbucketProject, bitbucketRepo);
  73. String defaultBranchName = getDefaultBranchName(url, pat, bitbucketProject, bitbucketRepo);
  74. ComponentCreationData componentCreationData = projectCreator.createProject(
  75. dbSession,
  76. getProjectKey(projectKey, repository),
  77. getProjectName(projectName, repository),
  78. defaultBranchName,
  79. creationMethod);
  80. ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
  81. createProjectAlmSettingDto(dbSession, repository.getSlug(), projectDto, almSettingDto, monorepo);
  82. return componentCreationData;
  83. }
  84. private String findPersonalAccessTokenOrThrow(DbSession dbSession) {
  85. String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
  86. Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
  87. return almPatDto.map(AlmPatDto::getPersonalAccessToken)
  88. .orElseThrow(() -> new IllegalArgumentException(format("personal access token for '%s' is missing", almSettingDto.getKey())));
  89. }
  90. private String getBitbucketProjectOrThrow() {
  91. if (devOpsProjectDescriptor.projectIdentifier() == null) {
  92. throw new IllegalArgumentException(String.format("The BitBucket project, in which the repository %s is located, is mandatory",
  93. devOpsProjectDescriptor.repositoryIdentifier()));
  94. }
  95. return devOpsProjectDescriptor.projectIdentifier();
  96. }
  97. private String getDefaultBranchName(String url, String pat, String project, String repo) {
  98. BranchesList branches = bitbucketServerRestClient.getBranches(url, pat, project, repo);
  99. Optional<Branch> defaultBranch = branches.findDefaultBranch();
  100. return defaultBranch.map(Branch::getName).orElse(null);
  101. }
  102. private String getProjectKey(@Nullable String projectKey, Repository repo) {
  103. return Optional.ofNullable(projectKey).orElseGet(() -> projectKeyGenerator.generateUniqueProjectKey(repo.getProject().getKey(), repo.getSlug()));
  104. }
  105. private static String getProjectName(@Nullable String projectName, Repository repository) {
  106. return Optional.ofNullable(projectName).orElse(repository.getName());
  107. }
  108. private void createProjectAlmSettingDto(DbSession dbSession, String repoSlug, ProjectDto projectDto, AlmSettingDto almSettingDto,
  109. Boolean isMonorepo) {
  110. ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
  111. .setAlmSettingUuid(almSettingDto.getUuid())
  112. .setAlmRepo(repoSlug)
  113. .setAlmSlug(null)
  114. .setProjectUuid(projectDto.getUuid())
  115. .setSummaryCommentEnabled(true)
  116. .setMonorepo(isMonorepo);
  117. dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), projectDto.getName(), projectDto.getKey());
  118. }
  119. }