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.

BitbucketCloudProjectCreator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.bitbucketcloud;
  21. import java.util.Optional;
  22. import org.jetbrains.annotations.Nullable;
  23. import org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient;
  24. import org.sonar.alm.client.bitbucket.bitbucketcloud.Repository;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.alm.pat.AlmPatDto;
  28. import org.sonar.db.alm.setting.AlmSettingDto;
  29. import org.sonar.db.alm.setting.ProjectAlmSettingDto;
  30. import org.sonar.db.project.CreationMethod;
  31. import org.sonar.db.project.ProjectDto;
  32. import org.sonar.server.common.almintegration.ProjectKeyGenerator;
  33. import org.sonar.server.common.almsettings.DevOpsProjectCreator;
  34. import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
  35. import org.sonar.server.common.project.ProjectCreator;
  36. import org.sonar.server.component.ComponentCreationData;
  37. import org.sonar.server.user.UserSession;
  38. import static java.lang.String.format;
  39. import static java.util.Objects.requireNonNull;
  40. import static java.util.Optional.ofNullable;
  41. public class BitbucketCloudProjectCreator implements DevOpsProjectCreator {
  42. private final DbClient dbClient;
  43. private final AlmSettingDto almSettingDto;
  44. private final DevOpsProjectDescriptor devOpsProjectDescriptor;
  45. private final UserSession userSession;
  46. private final BitbucketCloudRestClient bitbucketCloudRestClient;
  47. private final ProjectCreator projectCreator;
  48. private final ProjectKeyGenerator projectKeyGenerator;
  49. public BitbucketCloudProjectCreator(DbClient dbClient, AlmSettingDto almSettingDto, DevOpsProjectDescriptor devOpsProjectDescriptor, UserSession userSession,
  50. BitbucketCloudRestClient bitbucketCloudRestClient, ProjectCreator projectCreator, ProjectKeyGenerator projectKeyGenerator) {
  51. this.dbClient = dbClient;
  52. this.almSettingDto = almSettingDto;
  53. this.devOpsProjectDescriptor = devOpsProjectDescriptor;
  54. this.userSession = userSession;
  55. this.bitbucketCloudRestClient = bitbucketCloudRestClient;
  56. this.projectCreator = projectCreator;
  57. this.projectKeyGenerator = projectKeyGenerator;
  58. }
  59. @Override
  60. public boolean isScanAllowedUsingPermissionsFromDevopsPlatform() {
  61. throw new UnsupportedOperationException("Not Implemented");
  62. }
  63. @Override
  64. public ComponentCreationData createProjectAndBindToDevOpsPlatform(DbSession dbSession, CreationMethod creationMethod, Boolean monorepo, @Nullable String projectKey,
  65. @Nullable String projectName) {
  66. String pat = findPersonalAccessTokenOrThrow(dbSession, almSettingDto);
  67. String workspace = ofNullable(almSettingDto.getAppId())
  68. .orElseThrow(() -> new IllegalArgumentException(String.format("workspace for alm setting %s is missing", almSettingDto.getKey())));
  69. Repository repo = bitbucketCloudRestClient.getRepo(pat, workspace, devOpsProjectDescriptor.projectIdentifier());
  70. ComponentCreationData componentCreationData = projectCreator.createProject(
  71. dbSession,
  72. getProjectKey(workspace, projectKey, repo),
  73. getProjectName(projectName, repo),
  74. repo.getMainBranch().getName(),
  75. creationMethod);
  76. ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
  77. createProjectAlmSettingDto(dbSession, repo.getSlug(), projectDto, almSettingDto, monorepo);
  78. return componentCreationData;
  79. }
  80. private String findPersonalAccessTokenOrThrow(DbSession dbSession, AlmSettingDto almSettingDto) {
  81. String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
  82. Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
  83. return almPatDto.map(AlmPatDto::getPersonalAccessToken)
  84. .orElseThrow(() -> new IllegalArgumentException(format("personal access token for '%s' is missing", almSettingDto.getKey())));
  85. }
  86. private String getProjectKey(String workspace, @Nullable String projectKey, Repository repository) {
  87. return Optional.ofNullable(projectKey).orElseGet(() -> projectKeyGenerator.generateUniqueProjectKey(workspace, repository.getSlug()));
  88. }
  89. private static String getProjectName(@Nullable String projectName, Repository repository) {
  90. return Optional.ofNullable(projectName).orElse(repository.getName());
  91. }
  92. private void createProjectAlmSettingDto(DbSession dbSession, String repoSlug, ProjectDto projectDto, AlmSettingDto almSettingDto, Boolean monorepo) {
  93. ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
  94. .setAlmSettingUuid(almSettingDto.getUuid())
  95. .setAlmRepo(repoSlug)
  96. .setAlmSlug(null)
  97. .setProjectUuid(projectDto.getUuid())
  98. .setSummaryCommentEnabled(true)
  99. .setMonorepo(monorepo);
  100. dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), projectDto.getName(), projectDto.getKey());
  101. }
  102. }