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.

ImportHelper.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.almintegration.ws;
  21. import java.util.List;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.server.ServerSide;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.alm.setting.ALM;
  28. import org.sonar.db.alm.setting.AlmSettingDto;
  29. import org.sonar.db.project.ProjectDto;
  30. import org.sonar.server.exceptions.NotFoundException;
  31. import org.sonar.server.project.Visibility;
  32. import org.sonar.server.user.UserSession;
  33. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  34. import static java.util.Objects.requireNonNull;
  35. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  36. import static org.sonarqube.ws.Projects.CreateWsResponse;
  37. import static org.sonarqube.ws.Projects.CreateWsResponse.newBuilder;
  38. @ServerSide
  39. public class ImportHelper {
  40. public static final String PARAM_ALM_SETTING = "almSetting";
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. public ImportHelper(DbClient dbClient, UserSession userSession) {
  44. this.dbClient = dbClient;
  45. this.userSession = userSession;
  46. }
  47. public void checkProvisionProjectPermission() {
  48. userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
  49. }
  50. public AlmSettingDto getAlmSettingDto(Request request) {
  51. return getAlmSettingDto(request, null);
  52. }
  53. public AlmSettingDto getAlmSettingDtoForAlm(Request request, ALM alm) {
  54. return getAlmSettingDto(request, alm);
  55. }
  56. private AlmSettingDto getAlmSettingDto(Request request, @Nullable ALM alm) {
  57. try (DbSession dbSession = dbClient.openSession(false)) {
  58. String almSettingKey = request.param(PARAM_ALM_SETTING);
  59. if (almSettingKey != null) {
  60. return getAlmSettingDtoFromKey(dbSession, almSettingKey);
  61. }
  62. return getUniqueAlmSettingDtoForAlm(dbSession, alm);
  63. }
  64. }
  65. private AlmSettingDto getAlmSettingDtoFromKey(DbSession dbSession, String almSettingKey) {
  66. return dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
  67. .orElseThrow(() -> new NotFoundException(String.format("DevOps Platform configuration '%s' not found.", almSettingKey)));
  68. }
  69. private AlmSettingDto getUniqueAlmSettingDtoForAlm(DbSession dbSession, @Nullable ALM alm) {
  70. List<AlmSettingDto> almSettingDtos = getAlmSettingDtos(dbSession, alm);
  71. if (almSettingDtos.isEmpty()) {
  72. String almString = alm == null ? "" : (alm.name() + " ");
  73. throw new NotFoundException("There is no " + almString + "configuration for DevOps Platform. Please add one.");
  74. }
  75. if (almSettingDtos.size() == 1) {
  76. return almSettingDtos.get(0);
  77. }
  78. throw new IllegalArgumentException(String.format("Parameter %s is required as there are multiple DevOps Platform configurations.", PARAM_ALM_SETTING));
  79. }
  80. private List<AlmSettingDto> getAlmSettingDtos(DbSession dbSession, @Nullable ALM alm) {
  81. if (alm == null) {
  82. return dbClient.almSettingDao().selectAll(dbSession);
  83. }
  84. return dbClient.almSettingDao().selectByAlm(dbSession, alm);
  85. }
  86. public String getUserUuid() {
  87. return requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
  88. }
  89. public static CreateWsResponse toCreateResponse(ProjectDto projectDto) {
  90. return newBuilder()
  91. .setProject(Project.newBuilder()
  92. .setKey(projectDto.getKey())
  93. .setName(projectDto.getName())
  94. .setQualifier(projectDto.getQualifier())
  95. .setVisibility(Visibility.getLabel(projectDto.isPrivate())))
  96. .build();
  97. }
  98. }