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.

ImportBitbucketCloudRepoAction.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.almintegration.ws.bitbucketcloud;
  21. import javax.annotation.Nullable;
  22. import javax.inject.Inject;
  23. import org.sonar.api.server.ws.Change;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.db.alm.setting.ALM;
  28. import org.sonar.db.alm.setting.AlmSettingDto;
  29. import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
  30. import org.sonar.server.almintegration.ws.ImportHelper;
  31. import org.sonar.server.common.project.ImportProjectRequest;
  32. import org.sonar.server.common.project.ImportProjectService;
  33. import org.sonar.server.common.project.ImportedProject;
  34. import org.sonarqube.ws.Projects;
  35. import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
  36. import static org.sonar.server.almintegration.ws.ImportHelper.toCreateResponse;
  37. import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION;
  38. import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION;
  39. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  40. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
  41. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
  42. public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction {
  43. private static final String PARAM_REPO_SLUG = "repositorySlug";
  44. private final ImportHelper importHelper;
  45. private final ImportProjectService importProjectService;
  46. @Inject
  47. public ImportBitbucketCloudRepoAction(ImportHelper importHelper, ImportProjectService importProjectService) {
  48. this.importHelper = importHelper;
  49. this.importProjectService = importProjectService;
  50. }
  51. @Override
  52. public void define(WebService.NewController context) {
  53. WebService.NewAction action = context.createAction("import_bitbucketcloud_repo")
  54. .setDescription("Create a SonarQube project with the information from the provided Bitbucket Cloud repository.<br/>" +
  55. "Autoconfigure pull request decoration mechanism.<br/>" +
  56. "Requires the 'Create Projects' permission")
  57. .setPost(true)
  58. .setSince("9.0")
  59. .setHandler(this)
  60. .setChangelog(
  61. new Change("10.5", "This endpoint is deprecated, please use its API v2 version /api/v2/dop-translation/bound-projects"),
  62. new Change("10.3", String.format("Parameter %s becomes optional if you have only one configuration for BitBucket Cloud", PARAM_ALM_SETTING)),
  63. new Change("10.3", "Endpoint visibility change from internal to public"))
  64. .setDeprecatedSince("10.5");
  65. action.createParam(PARAM_REPO_SLUG)
  66. .setRequired(true)
  67. .setMaximumLength(200)
  68. .setDescription("Bitbucket Cloud repository slug");
  69. action.createParam(PARAM_ALM_SETTING)
  70. .setMaximumLength(200)
  71. .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one BitBucket Cloud integration.");
  72. action.createParam(PARAM_NEW_CODE_DEFINITION_TYPE)
  73. .setDescription(NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION)
  74. .setSince("10.1");
  75. action.createParam(PARAM_NEW_CODE_DEFINITION_VALUE)
  76. .setDescription(NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION)
  77. .setSince("10.1");
  78. }
  79. @Override
  80. public void handle(Request request, Response response) {
  81. Projects.CreateWsResponse createResponse = doHandle(request);
  82. writeProtobuf(createResponse, request, response);
  83. }
  84. private Projects.CreateWsResponse doHandle(Request request) {
  85. importHelper.checkProvisionProjectPermission();
  86. AlmSettingDto almSettingDto = importHelper.getAlmSettingDtoForAlm(request, ALM.BITBUCKET_CLOUD);
  87. String newCodeDefinitionType = request.param(PARAM_NEW_CODE_DEFINITION_TYPE);
  88. String newCodeDefinitionValue = request.param(PARAM_NEW_CODE_DEFINITION_VALUE);
  89. String repoSlug = request.mandatoryParam(PARAM_REPO_SLUG);
  90. ImportedProject importedProject = importProjectService.importProject(toServiceRequest(almSettingDto, repoSlug, newCodeDefinitionType, newCodeDefinitionValue));
  91. return toCreateResponse(importedProject.projectDto());
  92. }
  93. private static ImportProjectRequest toServiceRequest(AlmSettingDto almSettingDto, String slug, @Nullable String newCodeDefinitionType,
  94. @Nullable String newCodeDefinitionValue) {
  95. return new ImportProjectRequest(null, null, almSettingDto.getUuid(), slug, newCodeDefinitionType, newCodeDefinitionValue, false);
  96. }
  97. }