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.gitlab;
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.CreateWsResponse;
37 import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
38 import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION;
39 import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION;
40 import static org.sonar.server.ws.WsUtils.writeProtobuf;
41 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
42 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
44 public class ImportGitLabProjectAction implements AlmIntegrationsWsAction {
46 public static final String PARAM_GITLAB_PROJECT_ID = "gitlabProjectId";
48 private final ImportProjectService importProjectService;
49 private final ImportHelper importHelper;
52 public ImportGitLabProjectAction(ImportProjectService importProjectService, ImportHelper importHelper) {
53 this.importProjectService = importProjectService;
54 this.importHelper = importHelper;
58 public void define(WebService.NewController context) {
59 WebService.NewAction action = context.createAction("import_gitlab_project")
60 .setDescription("Import a GitLab project to SonarQube, creating a new project and configuring MR decoration<br/>" +
61 "Requires the 'Create Projects' permission")
66 new Change("10.5", "This endpoint is deprecated, please use its API v2 version /api/v2/dop-translation/bound-projects"),
67 new Change("10.3", String.format("Parameter %s becomes optional if you have only one configuration for GitLab", PARAM_ALM_SETTING)))
68 .setDeprecatedSince("10.5");
70 action.createParam(ImportHelper.PARAM_ALM_SETTING)
71 .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one GitLab integration.");
73 action.createParam(PARAM_GITLAB_PROJECT_ID)
75 .setDescription("GitLab project ID");
77 action.createParam(PARAM_NEW_CODE_DEFINITION_TYPE)
78 .setDescription(NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION)
81 action.createParam(PARAM_NEW_CODE_DEFINITION_VALUE)
82 .setDescription(NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION)
87 public void handle(Request request, Response response) throws Exception {
88 CreateWsResponse createResponse = doHandle(request);
89 writeProtobuf(createResponse, request, response);
92 private CreateWsResponse doHandle(Request request) {
93 importHelper.checkProvisionProjectPermission();
94 AlmSettingDto almSettingDto = importHelper.getAlmSettingDtoForAlm(request, ALM.GITLAB);
95 String gitlabProjectId = request.mandatoryParam(PARAM_GITLAB_PROJECT_ID);
96 String newCodeDefinitionType = request.param(PARAM_NEW_CODE_DEFINITION_TYPE);
97 String newCodeDefinitionValue = request.param(PARAM_NEW_CODE_DEFINITION_VALUE);
98 ImportedProject importedProject = importProjectService.importProject(toServiceRequest(almSettingDto, gitlabProjectId, newCodeDefinitionType, newCodeDefinitionValue));
100 return ImportHelper.toCreateResponse(importedProject.projectDto());
103 private static ImportProjectRequest toServiceRequest(AlmSettingDto almSettingDto, String gitlabProjectId, @Nullable String newCodeDefinitionType,
104 @Nullable String newCodeDefinitionValue) {
105 return new ImportProjectRequest(null, null, almSettingDto.getUuid(), gitlabProjectId, newCodeDefinitionType, newCodeDefinitionValue, false);