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.

AlmSettingsSupport.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.almsettings.ws;
  21. import java.util.regex.Pattern;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.server.ServerSide;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.alm.setting.ALM;
  27. import org.sonar.db.alm.setting.AlmSettingDto;
  28. import org.sonar.db.project.ProjectDto;
  29. import org.sonar.server.almsettings.MultipleAlmFeature;
  30. import org.sonar.server.component.ComponentFinder;
  31. import org.sonar.server.exceptions.BadRequestException;
  32. import org.sonar.server.exceptions.NotFoundException;
  33. import org.sonar.server.user.UserSession;
  34. import org.sonarqube.ws.AlmSettings;
  35. import static java.lang.String.format;
  36. import static org.apache.commons.lang.StringUtils.isEmpty;
  37. import static org.sonar.api.web.UserRole.ADMIN;
  38. @ServerSide
  39. public class AlmSettingsSupport {
  40. private static final Pattern WORKSPACE_ID_PATTERN = Pattern.compile("^[a-z0-9\\-_]+$");
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. private final ComponentFinder componentFinder;
  44. private final MultipleAlmFeature multipleAlmFeature;
  45. public AlmSettingsSupport(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder,
  46. MultipleAlmFeature multipleAlmFeature) {
  47. this.dbClient = dbClient;
  48. this.userSession = userSession;
  49. this.componentFinder = componentFinder;
  50. this.multipleAlmFeature = multipleAlmFeature;
  51. }
  52. public void checkAlmSettingDoesNotAlreadyExist(DbSession dbSession, String almSetting) {
  53. dbClient.almSettingDao().selectByKey(dbSession, almSetting)
  54. .ifPresent(a -> {
  55. throw new IllegalArgumentException(format("An DevOps Platform setting with key '%s' already exists", a.getKey()));
  56. });
  57. }
  58. public void checkAlmMultipleFeatureEnabled(ALM alm) {
  59. try (DbSession dbSession = dbClient.openSession(false)) {
  60. if (!multipleAlmFeature.isAvailable() && !dbClient.almSettingDao().selectByAlm(dbSession, alm).isEmpty()) {
  61. throw BadRequestException.create("A " + alm + " setting is already defined");
  62. }
  63. }
  64. }
  65. public void checkBitbucketCloudWorkspaceIDFormat(String workspaceId) {
  66. if (!WORKSPACE_ID_PATTERN.matcher(workspaceId).matches()) {
  67. throw BadRequestException.create(String.format(
  68. "Workspace ID '%s' has an incorrect format. Should only contain lowercase letters, numbers, dashes, and underscores.",
  69. workspaceId
  70. ));
  71. }
  72. }
  73. public ProjectDto getProjectAsAdmin(DbSession dbSession, String projectKey) {
  74. return getProject(dbSession, projectKey, ADMIN);
  75. }
  76. public ProjectDto getProject(DbSession dbSession, String projectKey, String projectPermission) {
  77. ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
  78. userSession.checkEntityPermission(projectPermission, project);
  79. return project;
  80. }
  81. public AlmSettingDto getAlmSetting(DbSession dbSession, String almSetting) {
  82. return dbClient.almSettingDao().selectByKey(dbSession, almSetting)
  83. .orElseThrow(() -> new NotFoundException(format("DevOps Platform setting with key '%s' cannot be found", almSetting)));
  84. }
  85. public static AlmSettings.Alm toAlmWs(ALM alm) {
  86. switch (alm) {
  87. case GITHUB:
  88. return AlmSettings.Alm.github;
  89. case BITBUCKET:
  90. return AlmSettings.Alm.bitbucket;
  91. case BITBUCKET_CLOUD:
  92. return AlmSettings.Alm.bitbucketcloud;
  93. case AZURE_DEVOPS:
  94. return AlmSettings.Alm.azure;
  95. case GITLAB:
  96. return AlmSettings.Alm.gitlab;
  97. default:
  98. throw new IllegalStateException(format("Unknown DevOps Platform '%s'", alm.name()));
  99. }
  100. }
  101. public void checkPrivateKeyOnUrlUpdate(AlmSettingDto almSettingDto, String url, @Nullable String privateKey) {
  102. checkCredentialArtifactOnUrlUpdate(url, almSettingDto, privateKey, "Please provide the Private Key to update the URL.");
  103. }
  104. public void checkPatOnUrlUpdate(AlmSettingDto almSettingDto, String url, @Nullable String pat) {
  105. checkCredentialArtifactOnUrlUpdate(url, almSettingDto, pat, "Please provide the Personal Access Token to update the URL.");
  106. }
  107. private static void checkCredentialArtifactOnUrlUpdate(String url, AlmSettingDto almSettingDto, @Nullable String credentialArtifact, String errorMessage) {
  108. if (!url.equals(almSettingDto.getUrl()) && isEmpty(credentialArtifact)) {
  109. throw new IllegalArgumentException(errorMessage);
  110. }
  111. }
  112. }