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.

ImportGithubProjectAction.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.github;
  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.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 ImportGithubProjectAction implements AlmIntegrationsWsAction {
  42. public static final String PARAM_REPOSITORY_KEY = "repositoryKey";
  43. private final ImportProjectService importProjectService;
  44. private final ImportHelper importHelper;
  45. @Inject
  46. public ImportGithubProjectAction(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_github_project")
  53. .setDescription("Create a SonarQube project with the information from the provided GitHub repository.<br/>" +
  54. "Autoconfigure pull request decoration mechanism. If Automatic Provisioning is enable for GitHub, " +
  55. "it will also synchronize permissions from the repository.<br/>" +
  56. "Requires the 'Create Projects' permission")
  57. .setPost(true)
  58. .setSince("8.4")
  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", "Parameter organization is not necessary anymore"),
  63. new Change("10.3", String.format("Parameter %s becomes optional if you have only one configuration for GitHub", PARAM_ALM_SETTING)),
  64. new Change("10.3", "Endpoint visibility change from internal to public"))
  65. .setDeprecatedSince("10.5");
  66. action.createParam(PARAM_ALM_SETTING)
  67. .setMaximumLength(200)
  68. .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one GitHub integration.");
  69. action.createParam(PARAM_REPOSITORY_KEY)
  70. .setRequired(true)
  71. .setMaximumLength(256)
  72. .setDescription("GitHub repository key (organization/repoSlug");
  73. action.createParam(PARAM_NEW_CODE_DEFINITION_TYPE)
  74. .setDescription(NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION)
  75. .setSince("10.1");
  76. action.createParam(PARAM_NEW_CODE_DEFINITION_VALUE)
  77. .setDescription(NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION)
  78. .setSince("10.1");
  79. }
  80. @Override
  81. public void handle(Request request, Response response) {
  82. Projects.CreateWsResponse createResponse = doHandle(request);
  83. writeProtobuf(createResponse, request, response);
  84. }
  85. private Projects.CreateWsResponse doHandle(Request request) {
  86. importHelper.checkProvisionProjectPermission();
  87. AlmSettingDto almSettingDto = importHelper.getAlmSettingDtoForAlm(request, ALM.GITHUB);
  88. String repositoryKey = request.mandatoryParam(PARAM_REPOSITORY_KEY);
  89. String newCodeDefinitionType = request.param(PARAM_NEW_CODE_DEFINITION_TYPE);
  90. String newCodeDefinitionValue = request.param(PARAM_NEW_CODE_DEFINITION_VALUE);
  91. ImportedProject importedProject = importProjectService.importProject(toServiceRequest(almSettingDto, repositoryKey, newCodeDefinitionType, newCodeDefinitionValue));
  92. return ImportHelper.toCreateResponse(importedProject.projectDto());
  93. }
  94. private static ImportProjectRequest toServiceRequest(AlmSettingDto almSettingDto, String githubRepositoryKey, @Nullable String newCodeDefinitionType,
  95. @Nullable String newCodeDefinitionValue) {
  96. return new ImportProjectRequest(null, null, almSettingDto.getUuid(), githubRepositoryKey, newCodeDefinitionType, newCodeDefinitionValue, false);
  97. }
  98. }