3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.almintegration.ws.bitbucketcloud;
22 import javax.annotation.Nullable;
23 import javax.inject.Inject;
24 import org.sonar.api.server.ws.Change;
25 import org.sonar.api.server.ws.Request;
26 import org.sonar.api.server.ws.Response;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.db.alm.setting.ALM;
29 import org.sonar.db.alm.setting.AlmSettingDto;
30 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
31 import org.sonar.server.almintegration.ws.ImportHelper;
32 import org.sonar.server.common.project.ImportProjectRequest;
33 import org.sonar.server.common.project.ImportProjectService;
34 import org.sonar.server.common.project.ImportedProject;
35 import org.sonarqube.ws.Projects;
37 import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
38 import static org.sonar.server.almintegration.ws.ImportHelper.toCreateResponse;
39 import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION;
40 import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION;
41 import static org.sonar.server.ws.WsUtils.writeProtobuf;
42 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
43 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
45 public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction {
47 private static final String PARAM_REPO_SLUG = "repositorySlug";
49 private final ImportHelper importHelper;
50 private final ImportProjectService importProjectService;
53 public ImportBitbucketCloudRepoAction(ImportHelper importHelper, ImportProjectService importProjectService) {
54 this.importHelper = importHelper;
55 this.importProjectService = importProjectService;
59 public void define(WebService.NewController context) {
60 WebService.NewAction action = context.createAction("import_bitbucketcloud_repo")
61 .setDescription("Create a SonarQube project with the information from the provided Bitbucket Cloud repository.<br/>" +
62 "Autoconfigure pull request decoration mechanism.<br/>" +
63 "Requires the 'Create Projects' permission")
68 new Change("10.5", "This endpoint is deprecated, please use its API v2 version /api/v2/dop-translation/bound-projects"),
69 new Change("10.3", String.format("Parameter %s becomes optional if you have only one configuration for BitBucket Cloud", PARAM_ALM_SETTING)),
70 new Change("10.3", "Endpoint visibility change from internal to public"))
71 .setDeprecatedSince("10.5");
73 action.createParam(PARAM_REPO_SLUG)
75 .setMaximumLength(200)
76 .setDescription("Bitbucket Cloud repository slug");
78 action.createParam(PARAM_ALM_SETTING)
79 .setMaximumLength(200)
80 .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one BitBucket Cloud integration.");
82 action.createParam(PARAM_NEW_CODE_DEFINITION_TYPE)
83 .setDescription(NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION)
86 action.createParam(PARAM_NEW_CODE_DEFINITION_VALUE)
87 .setDescription(NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION)
93 public void handle(Request request, Response response) {
94 Projects.CreateWsResponse createResponse = doHandle(request);
95 writeProtobuf(createResponse, request, response);
98 private Projects.CreateWsResponse doHandle(Request request) {
99 importHelper.checkProvisionProjectPermission();
100 AlmSettingDto almSettingDto = importHelper.getAlmSettingDtoForAlm(request, ALM.BITBUCKET_CLOUD);
101 String newCodeDefinitionType = request.param(PARAM_NEW_CODE_DEFINITION_TYPE);
102 String newCodeDefinitionValue = request.param(PARAM_NEW_CODE_DEFINITION_VALUE);
103 String repoSlug = request.mandatoryParam(PARAM_REPO_SLUG);
104 ImportedProject importedProject = importProjectService.importProject(toServiceRequest(almSettingDto, repoSlug, newCodeDefinitionType, newCodeDefinitionValue));
105 return toCreateResponse(importedProject.projectDto());
108 private static ImportProjectRequest toServiceRequest(AlmSettingDto almSettingDto, String slug, @Nullable String newCodeDefinitionType,
109 @Nullable String newCodeDefinitionValue) {
110 return new ImportProjectRequest(null, null, almSettingDto.getUuid(), slug, newCodeDefinitionType, newCodeDefinitionValue, false);