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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 java.util.Objects;
  22. import java.util.Optional;
  23. import javax.inject.Inject;
  24. import org.sonar.alm.client.github.GithubApplicationClient;
  25. import org.sonar.alm.client.github.GithubApplicationClient.Repository;
  26. import org.sonar.alm.client.github.GithubApplicationClientImpl;
  27. import org.sonar.alm.client.github.security.AccessToken;
  28. import org.sonar.alm.client.github.security.UserAccessToken;
  29. import org.sonar.api.server.ws.Change;
  30. import org.sonar.api.server.ws.Request;
  31. import org.sonar.api.server.ws.Response;
  32. import org.sonar.api.server.ws.WebService;
  33. import org.sonar.auth.github.GitHubSettings;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.alm.pat.AlmPatDto;
  37. import org.sonar.db.alm.setting.ALM;
  38. import org.sonar.db.alm.setting.AlmSettingDto;
  39. import org.sonar.db.alm.setting.ProjectAlmSettingDto;
  40. import org.sonar.db.component.BranchDto;
  41. import org.sonar.db.project.ProjectDto;
  42. import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
  43. import org.sonar.server.almintegration.ws.ImportHelper;
  44. import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
  45. import org.sonar.server.component.ComponentCreationData;
  46. import org.sonar.server.component.ComponentCreationParameters;
  47. import org.sonar.server.component.ComponentUpdater;
  48. import org.sonar.server.component.NewComponent;
  49. import org.sonar.server.exceptions.NotFoundException;
  50. import org.sonar.server.management.ManagedProjectService;
  51. import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
  52. import org.sonar.server.project.DefaultBranchNameResolver;
  53. import org.sonar.server.project.ProjectDefaultVisibility;
  54. import org.sonar.server.user.UserSession;
  55. import org.sonarqube.ws.Projects;
  56. import static java.util.Objects.requireNonNull;
  57. import static org.sonar.api.resources.Qualifiers.PROJECT;
  58. import static org.sonar.db.project.CreationMethod.Category.ALM_IMPORT;
  59. import static org.sonar.db.project.CreationMethod.getCreationMethod;
  60. import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
  61. import static org.sonar.server.almintegration.ws.ImportHelper.toCreateResponse;
  62. import static org.sonar.server.component.NewComponent.newComponentBuilder;
  63. import static org.sonar.server.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION;
  64. import static org.sonar.server.newcodeperiod.NewCodeDefinitionResolver.NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION;
  65. import static org.sonar.server.newcodeperiod.NewCodeDefinitionResolver.checkNewCodeDefinitionParam;
  66. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  67. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
  68. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
  69. public class ImportGithubProjectAction implements AlmIntegrationsWsAction {
  70. public static final String PARAM_ORGANIZATION = "organization";
  71. public static final String PARAM_REPOSITORY_KEY = "repositoryKey";
  72. private final DbClient dbClient;
  73. private final ManagedProjectService managedProjectService;
  74. private final UserSession userSession;
  75. private final ProjectDefaultVisibility projectDefaultVisibility;
  76. private final GithubApplicationClient githubApplicationClient;
  77. private final ComponentUpdater componentUpdater;
  78. private final ImportHelper importHelper;
  79. private final ProjectKeyGenerator projectKeyGenerator;
  80. private final NewCodeDefinitionResolver newCodeDefinitionResolver;
  81. private final DefaultBranchNameResolver defaultBranchNameResolver;
  82. private final GitHubSettings gitHubSettings;
  83. @Inject
  84. public ImportGithubProjectAction(DbClient dbClient, ManagedProjectService managedProjectService, UserSession userSession, ProjectDefaultVisibility projectDefaultVisibility,
  85. GithubApplicationClientImpl githubApplicationClient, ComponentUpdater componentUpdater, ImportHelper importHelper,
  86. ProjectKeyGenerator projectKeyGenerator, NewCodeDefinitionResolver newCodeDefinitionResolver,
  87. DefaultBranchNameResolver defaultBranchNameResolver, GitHubSettings gitHubSettings) {
  88. this.dbClient = dbClient;
  89. this.managedProjectService = managedProjectService;
  90. this.userSession = userSession;
  91. this.projectDefaultVisibility = projectDefaultVisibility;
  92. this.githubApplicationClient = githubApplicationClient;
  93. this.componentUpdater = componentUpdater;
  94. this.importHelper = importHelper;
  95. this.projectKeyGenerator = projectKeyGenerator;
  96. this.newCodeDefinitionResolver = newCodeDefinitionResolver;
  97. this.defaultBranchNameResolver = defaultBranchNameResolver;
  98. this.gitHubSettings = gitHubSettings;
  99. }
  100. @Override
  101. public void define(WebService.NewController context) {
  102. WebService.NewAction action = context.createAction("import_github_project")
  103. .setDescription("Create a SonarQube project with the information from the provided GitHub repository.<br/>" +
  104. "Autoconfigure pull request decoration mechanism. If Automatic Provisioning is enable for GitHub, it will also synchronize permissions from the repository.<br/>" +
  105. "Requires the 'Create Projects' permission")
  106. .setPost(true)
  107. .setSince("8.4")
  108. .setHandler(this)
  109. .setChangelog(
  110. new Change("10.3", String.format("Parameter %s becomes optional if you have only one configuration for GitHub", PARAM_ALM_SETTING)),
  111. new Change("10.3", "Endpoint visibility change from internal to public"));
  112. action.createParam(PARAM_ALM_SETTING)
  113. .setMaximumLength(200)
  114. .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one GitHub integration.");
  115. action.createParam(PARAM_ORGANIZATION)
  116. .setRequired(true)
  117. .setMaximumLength(200)
  118. .setDescription("GitHub organization");
  119. action.createParam(PARAM_REPOSITORY_KEY)
  120. .setRequired(true)
  121. .setMaximumLength(256)
  122. .setDescription("GitHub repository key");
  123. action.createParam(PARAM_NEW_CODE_DEFINITION_TYPE)
  124. .setDescription(NEW_CODE_PERIOD_TYPE_DESCRIPTION_PROJECT_CREATION)
  125. .setSince("10.1");
  126. action.createParam(PARAM_NEW_CODE_DEFINITION_VALUE)
  127. .setDescription(NEW_CODE_PERIOD_VALUE_DESCRIPTION_PROJECT_CREATION)
  128. .setSince("10.1");
  129. }
  130. @Override
  131. public void handle(Request request, Response response) {
  132. Projects.CreateWsResponse createResponse = doHandle(request);
  133. writeProtobuf(createResponse, request, response);
  134. }
  135. private Projects.CreateWsResponse doHandle(Request request) {
  136. importHelper.checkProvisionProjectPermission();
  137. AlmSettingDto almSettingDto = importHelper.getAlmSettingDtoForAlm(request, ALM.GITHUB);
  138. String newCodeDefinitionType = request.param(PARAM_NEW_CODE_DEFINITION_TYPE);
  139. String newCodeDefinitionValue = request.param(PARAM_NEW_CODE_DEFINITION_VALUE);
  140. try (DbSession dbSession = dbClient.openSession(false)) {
  141. AccessToken accessToken = getAccessToken(dbSession, almSettingDto);
  142. String githubOrganization = request.mandatoryParam(PARAM_ORGANIZATION);
  143. String repositoryKey = request.mandatoryParam(PARAM_REPOSITORY_KEY);
  144. String url = requireNonNull(almSettingDto.getUrl(), "DevOps Platform url cannot be null");
  145. Repository repository = githubApplicationClient.getRepository(url, accessToken, githubOrganization, repositoryKey)
  146. .orElseThrow(() -> new NotFoundException(String.format("GitHub repository '%s' not found", repositoryKey)));
  147. ComponentCreationData componentCreationData = createProject(dbSession, repository, repository.getDefaultBranch());
  148. ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
  149. BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
  150. populatePRSetting(dbSession, repository, projectDto, almSettingDto);
  151. checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue);
  152. if (newCodeDefinitionType != null) {
  153. newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
  154. Optional.ofNullable(repository.getDefaultBranch()).orElse(defaultBranchNameResolver.getEffectiveMainBranchName()),
  155. newCodeDefinitionType, newCodeDefinitionValue);
  156. }
  157. componentUpdater.commitAndIndex(dbSession, componentCreationData);
  158. String userUuid = Objects.requireNonNull(userSession.getUuid());
  159. managedProjectService.queuePermissionSyncTask(userUuid, mainBranchDto.getUuid(), projectDto.getUuid());
  160. return toCreateResponse(projectDto);
  161. }
  162. }
  163. private AccessToken getAccessToken(DbSession dbSession, AlmSettingDto almSettingDto) {
  164. String userUuid = importHelper.getUserUuid();
  165. return dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto)
  166. .map(AlmPatDto::getPersonalAccessToken)
  167. .map(UserAccessToken::new)
  168. .orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
  169. }
  170. private ComponentCreationData createProject(DbSession dbSession, Repository repo, String mainBranchName) {
  171. boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
  172. String uniqueProjectKey = projectKeyGenerator.generateUniqueProjectKey(repo.getFullName());
  173. NewComponent projectComponent = newComponentBuilder()
  174. .setKey(uniqueProjectKey)
  175. .setName(repo.getName())
  176. .setPrivate(visibility)
  177. .setQualifier(PROJECT)
  178. .build();
  179. ComponentCreationParameters componentCreationParameters = ComponentCreationParameters.builder()
  180. .newComponent(projectComponent)
  181. .userLogin(userSession.getLogin())
  182. .userUuid(userSession.getUuid())
  183. .mainBranchName(mainBranchName)
  184. .isManaged(gitHubSettings.isProvisioningEnabled())
  185. .creationMethod(getCreationMethod(ALM_IMPORT, userSession.isAuthenticatedBrowserSession()))
  186. .build();
  187. return componentUpdater.createWithoutCommit(dbSession, componentCreationParameters);
  188. }
  189. private void populatePRSetting(DbSession dbSession, Repository repo, ProjectDto projectDto, AlmSettingDto almSettingDto) {
  190. ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
  191. .setAlmSettingUuid(almSettingDto.getUuid())
  192. .setAlmRepo(repo.getFullName())
  193. .setAlmSlug(null)
  194. .setProjectUuid(projectDto.getUuid())
  195. .setSummaryCommentEnabled(true)
  196. .setMonorepo(false);
  197. dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(),
  198. projectDto.getName(), projectDto.getKey());
  199. }
  200. }