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.

ImportGitLabProjectActionIT.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 java.util.List;
  22. import java.util.Optional;
  23. import org.assertj.core.api.Assertions;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.alm.client.gitlab.GitLabBranch;
  28. import org.sonar.alm.client.gitlab.GitlabApplicationClient;
  29. import org.sonar.alm.client.gitlab.Project;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.i18n.I18n;
  32. import org.sonar.core.platform.EditionProvider;
  33. import org.sonar.core.platform.PlatformEditionProvider;
  34. import org.sonar.core.util.SequenceUuidFactory;
  35. import org.sonar.db.DbTester;
  36. import org.sonar.db.alm.setting.AlmSettingDto;
  37. import org.sonar.db.component.BranchDto;
  38. import org.sonar.db.newcodeperiod.NewCodePeriodDto;
  39. import org.sonar.db.project.CreationMethod;
  40. import org.sonar.db.project.ProjectDto;
  41. import org.sonar.db.user.UserDto;
  42. import org.sonar.server.almintegration.ws.ImportHelper;
  43. import org.sonar.server.common.almintegration.ProjectKeyGenerator;
  44. import org.sonar.server.common.almsettings.gitlab.GitlabProjectCreatorFactory;
  45. import org.sonar.server.common.component.ComponentUpdater;
  46. import org.sonar.server.es.TestIndexers;
  47. import org.sonar.server.exceptions.NotFoundException;
  48. import org.sonar.server.favorite.FavoriteUpdater;
  49. import org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver;
  50. import org.sonar.server.permission.PermissionService;
  51. import org.sonar.server.common.permission.PermissionTemplateService;
  52. import org.sonar.server.common.permission.PermissionUpdater;
  53. import org.sonar.server.project.DefaultBranchNameResolver;
  54. import org.sonar.server.project.ProjectDefaultVisibility;
  55. import org.sonar.server.project.Visibility;
  56. import org.sonar.server.common.project.ProjectCreator;
  57. import org.sonar.server.tester.UserSessionRule;
  58. import org.sonar.server.ws.TestRequest;
  59. import org.sonar.server.ws.WsActionTester;
  60. import org.sonarqube.ws.Projects;
  61. import static java.util.Collections.emptyList;
  62. import static java.util.Collections.singletonList;
  63. import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
  64. import static org.assertj.core.api.Assertions.assertThat;
  65. import static org.assertj.core.api.Assertions.assertThatNoException;
  66. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  67. import static org.assertj.core.api.Assertions.tuple;
  68. import static org.mockito.ArgumentMatchers.any;
  69. import static org.mockito.Mockito.mock;
  70. import static org.mockito.Mockito.verify;
  71. import static org.mockito.Mockito.when;
  72. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  73. import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
  74. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  75. import static org.sonar.server.tester.UserSessionRule.standalone;
  76. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
  77. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
  78. public class ImportGitLabProjectActionIT {
  79. private static final String PROJECT_KEY_NAME = "PROJECT_NAME";
  80. private final System2 system2 = mock(System2.class);
  81. @Rule
  82. public UserSessionRule userSession = standalone();
  83. @Rule
  84. public DbTester db = DbTester.create(system2);
  85. DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
  86. private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
  87. mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(),
  88. defaultBranchNameResolver, mock(PermissionUpdater.class), mock(PermissionService.class));
  89. private final GitlabApplicationClient gitlabApplicationClient = mock(GitlabApplicationClient.class);
  90. private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
  91. private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
  92. private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
  93. private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
  94. private final NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
  95. private final ProjectCreator projectCreator = new ProjectCreator(userSession, projectDefaultVisibility, componentUpdater);
  96. private final GitlabProjectCreatorFactory gitlabProjectCreatorFactory = new GitlabProjectCreatorFactory(db.getDbClient(), projectKeyGenerator, projectCreator,
  97. gitlabApplicationClient, userSession);
  98. private final ImportGitLabProjectAction importGitLabProjectAction = new ImportGitLabProjectAction(
  99. db.getDbClient(), userSession, componentUpdater, importHelper, newCodeDefinitionResolver, gitlabProjectCreatorFactory);
  100. private final WsActionTester ws = new WsActionTester(importGitLabProjectAction);
  101. @Before
  102. public void before() {
  103. when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
  104. when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
  105. }
  106. @Test
  107. public void import_project_developer_edition() {
  108. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
  109. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  110. Project project = mockGitlabProject(singletonList(new GitLabBranch("master", true)));
  111. Projects.CreateWsResponse response = ws.newRequest()
  112. .setParam("almSetting", almSetting.getKey())
  113. .setParam("gitlabProjectId", "12345")
  114. .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
  115. .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
  116. .executeProtobuf(Projects.CreateWsResponse.class);
  117. verify(gitlabApplicationClient).getProject(almSetting.getUrl(), "PAT", 12345L);
  118. Projects.CreateWsResponse.Project result = response.getProject();
  119. assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
  120. assertThat(result.getName()).isEqualTo(project.getName());
  121. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  122. assertThat(projectDto).isPresent();
  123. assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
  124. assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
  125. .isPresent()
  126. .get()
  127. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
  128. .containsExactly(NUMBER_OF_DAYS, "30", null);
  129. }
  130. @Test
  131. public void import_project_community_edition() {
  132. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
  133. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  134. mockGitlabProject(singletonList(new GitLabBranch("master", true)));
  135. Projects.CreateWsResponse response = ws.newRequest()
  136. .setParam("almSetting", almSetting.getKey())
  137. .setParam("gitlabProjectId", "12345")
  138. .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
  139. .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
  140. .executeProtobuf(Projects.CreateWsResponse.class);
  141. Projects.CreateWsResponse.Project result = response.getProject();
  142. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  143. BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
  144. String projectUuid = projectDto.get().getUuid();
  145. assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
  146. .isPresent()
  147. .get()
  148. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
  149. .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
  150. }
  151. @Test
  152. public void import_project_with_specific_different_default_branch() {
  153. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  154. Project project = mockGitlabProject(singletonList(new GitLabBranch("main", true)));
  155. Projects.CreateWsResponse response = ws.newRequest()
  156. .setParam("almSetting", almSetting.getKey())
  157. .setParam("gitlabProjectId", "12345")
  158. .executeProtobuf(Projects.CreateWsResponse.class);
  159. verify(gitlabApplicationClient).getProject(almSetting.getUrl(), "PAT", 12345L);
  160. verify(gitlabApplicationClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
  161. Projects.CreateWsResponse.Project result = response.getProject();
  162. assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
  163. assertThat(result.getName()).isEqualTo(project.getName());
  164. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  165. assertThat(projectDto).isPresent();
  166. assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
  167. Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
  168. .extracting(BranchDto::getKey, BranchDto::isMain)
  169. .containsExactlyInAnyOrder(tuple("main", true));
  170. }
  171. @Test
  172. public void import_project_no_gitlab_default_branch() {
  173. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  174. Project project = mockGitlabProject(emptyList());
  175. Projects.CreateWsResponse response = ws.newRequest()
  176. .setParam("almSetting", almSetting.getKey())
  177. .setParam("gitlabProjectId", "12345")
  178. .executeProtobuf(Projects.CreateWsResponse.class);
  179. verify(gitlabApplicationClient).getProject(almSetting.getUrl(), "PAT", 12345L);
  180. verify(gitlabApplicationClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
  181. Projects.CreateWsResponse.Project result = response.getProject();
  182. assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
  183. assertThat(result.getName()).isEqualTo(project.getName());
  184. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  185. assertThat(projectDto).isPresent();
  186. assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
  187. Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
  188. .extracting(BranchDto::getKey, BranchDto::isMain)
  189. .containsExactlyInAnyOrder(tuple(DEFAULT_MAIN_BRANCH_NAME, true));
  190. }
  191. @Test
  192. public void import_project_without_NCD() {
  193. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  194. Project project = mockGitlabProject(singletonList(new GitLabBranch("master", true)));
  195. Projects.CreateWsResponse response = ws.newRequest()
  196. .setParam("almSetting", almSetting.getKey())
  197. .setParam("gitlabProjectId", "12345")
  198. .executeProtobuf(Projects.CreateWsResponse.class);
  199. verify(gitlabApplicationClient).getProject(almSetting.getUrl(), "PAT", 12345L);
  200. Projects.CreateWsResponse.Project result = response.getProject();
  201. assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
  202. assertThat(result.getName()).isEqualTo(project.getName());
  203. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  204. assertThat(projectDto).isPresent();
  205. assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
  206. }
  207. @Test
  208. public void importProject_whenNonBrowserCall_setsCreationMethodToApi() {
  209. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  210. mockGitlabProject(singletonList(new GitLabBranch("master", true)));
  211. Projects.CreateWsResponse response = ws.newRequest()
  212. .setParam("almSetting", almSetting.getKey())
  213. .setParam("gitlabProjectId", "12345")
  214. .executeProtobuf(Projects.CreateWsResponse.class);
  215. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), response.getProject().getKey());
  216. assertThat(projectDto.orElseThrow().getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
  217. }
  218. @Test
  219. public void importProject_whenBrowserCall_setsCreationMethodToBrowser() {
  220. AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
  221. userSession.flagSessionAsGui();
  222. mockGitlabProject(singletonList(new GitLabBranch("master", true)));
  223. Projects.CreateWsResponse response = ws.newRequest()
  224. .setParam("almSetting", almSetting.getKey())
  225. .setParam("gitlabProjectId", "12345")
  226. .executeProtobuf(Projects.CreateWsResponse.class);
  227. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), response.getProject().getKey());
  228. assertThat(projectDto.orElseThrow().getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_BROWSER);
  229. }
  230. @Test
  231. public void importProject_whenAlmSettingKeyDoesNotExist_shouldThrow() {
  232. UserDto user = db.users().insertUser();
  233. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  234. TestRequest request = ws.newRequest()
  235. .setParam("almSetting", "unknown")
  236. .setParam("gitlabProjectId", "12345");
  237. assertThatThrownBy(request::execute)
  238. .isInstanceOf(NotFoundException.class)
  239. .hasMessage("DevOps Platform configuration 'unknown' not found.");
  240. }
  241. @Test
  242. public void importProject_whenNoAlmSettingKeyAndNoConfig_shouldThrow() {
  243. UserDto user = db.users().insertUser();
  244. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  245. TestRequest request = ws.newRequest()
  246. .setParam("gitlabProjectId", "12345");
  247. assertThatThrownBy(request::execute)
  248. .isInstanceOf(NotFoundException.class)
  249. .hasMessage("There is no GITLAB configuration for DevOps Platform. Please add one.");
  250. }
  251. @Test
  252. public void importProject_whenNoAlmSettingKeyAndMultipleConfigs_shouldThrow() {
  253. UserDto user = db.users().insertUser();
  254. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  255. db.almSettings().insertGitlabAlmSetting();
  256. db.almSettings().insertGitlabAlmSetting();
  257. TestRequest request = ws.newRequest()
  258. .setParam("gitlabProjectId", "12345");
  259. assertThatThrownBy(request::execute)
  260. .isInstanceOf(IllegalArgumentException.class)
  261. .hasMessage("Parameter almSetting is required as there are multiple DevOps Platform configurations.");
  262. }
  263. @Test
  264. public void importProject_whenNoAlmSettingKeyAndOnlyOneConfig_shouldImport() {
  265. configureUserAndPatAndAlmSettings();
  266. mockGitlabProject(emptyList());
  267. TestRequest request = ws.newRequest()
  268. .setParam("gitlabProjectId", "12345");
  269. assertThatNoException().isThrownBy(request::execute);
  270. }
  271. private AlmSettingDto configureUserAndPatAndAlmSettings() {
  272. UserDto user = db.users().insertUser();
  273. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  274. return insertGitLabConfigurationAndPat(user);
  275. }
  276. private AlmSettingDto insertGitLabConfigurationAndPat(UserDto user) {
  277. AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
  278. db.almPats().insert(dto -> {
  279. dto.setAlmSettingUuid(almSetting.getUuid());
  280. dto.setUserUuid(user.getUuid());
  281. dto.setPersonalAccessToken("PAT");
  282. });
  283. return almSetting;
  284. }
  285. private Project mockGitlabProject(List<GitLabBranch> master) {
  286. Project project = new Project(randomAlphanumeric(5), randomAlphanumeric(5));
  287. when(gitlabApplicationClient.getProject(any(), any(), any())).thenReturn(project);
  288. when(gitlabApplicationClient.getBranches(any(), any(), any())).thenReturn(master);
  289. when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
  290. return project;
  291. }
  292. }