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.

ImportGitLabProjectAction.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.gitlab;
  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.CreateWsResponse;
  35. import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
  36. import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION;
  37. import static org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION;
  38. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  39. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
  40. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
  41. public class ImportGitLabProjectAction implements AlmIntegrationsWsAction {
  42. public static final String PARAM_GITLAB_PROJECT_ID = "gitlabProjectId";
  43. private final ImportProjectService importProjectService;
  44. private final ImportHelper importHelper;
  45. @Inject
  46. public ImportGitLabProjectAction(ImportProjectService importProjectService, ImportHelper importHelper) {
  47. this.importProjectService = importProjectService;
  48. this.importHelper = importHelper;
  49. }
  50. @Override
  51. public void define(WebService.NewController context) {
  52. WebService.NewAction action = context.createAction("import_gitlab_project")
  53. .setDescription("Import a GitLab project to SonarQube, creating a new project and configuring MR decoration<br/>" +
  54. "Requires the 'Create Projects' permission")
  55. .setPost(true)
  56. .setSince("8.5")
  57. .setHandler(this)
  58. .setChangelog(
  59. new Change("10.5", "This endpoint is deprecated, please use its API v2 version /api/v2/dop-translation/bound-projects"),
  60. new Change("10.3", String.format("Parameter %s becomes optional if you have only one configuration for GitLab", PARAM_ALM_SETTING)))
  61. .setDeprecatedSince("10.5");
  62. action.createParam(ImportHelper.PARAM_ALM_SETTING)
  63. .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one GitLab integration.");
  64. action.createParam(PARAM_GITLAB_PROJECT_ID)
  65. .setRequired(true)
  66. .setDescription("GitLab project ID");
  67. action.createParam(PARAM_NEW_CODE_DEFINITION_TYPE)
  68. .setDescription(NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION)
  69. .setSince("10.1");
  70. action.createParam(PARAM_NEW_CODE_DEFINITION_VALUE)
  71. .setDescription(NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION)
  72. .setSince("10.1");
  73. }
  74. @Override
  75. public void handle(Request request, Response response) throws Exception {
  76. CreateWsResponse createResponse = doHandle(request);
  77. writeProtobuf(createResponse, request, response);
  78. }
  79. private CreateWsResponse doHandle(Request request) {
  80. importHelper.checkProvisionProjectPermission();
  81. AlmSettingDto almSettingDto = importHelper.getAlmSettingDtoForAlm(request, ALM.GITLAB);
  82. String gitlabProjectId = request.mandatoryParam(PARAM_GITLAB_PROJECT_ID);
  83. String newCodeDefinitionType = request.param(PARAM_NEW_CODE_DEFINITION_TYPE);
  84. String newCodeDefinitionValue = request.param(PARAM_NEW_CODE_DEFINITION_VALUE);
  85. ImportedProject importedProject = importProjectService.importProject(toServiceRequest(almSettingDto, gitlabProjectId, newCodeDefinitionType, newCodeDefinitionValue));
  86. return ImportHelper.toCreateResponse(importedProject.projectDto());
  87. }
  88. private static ImportProjectRequest toServiceRequest(AlmSettingDto almSettingDto, String gitlabProjectId, @Nullable String newCodeDefinitionType,
  89. @Nullable String newCodeDefinitionValue) {
  90. return new ImportProjectRequest(null, null, almSettingDto.getUuid(), gitlabProjectId, newCodeDefinitionType, newCodeDefinitionValue, false);
  91. }
  92. }