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.

ImportGithubProjectActionIT.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 java.util.Optional;
  22. import java.util.Set;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.mockito.ArgumentCaptor;
  27. import org.sonar.alm.client.github.GithubApplicationClientImpl;
  28. import org.sonar.alm.client.github.GithubPermissionConverter;
  29. import org.sonar.api.resources.Qualifiers;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.auth.github.AppInstallationToken;
  33. import org.sonar.auth.github.GitHubSettings;
  34. import org.sonar.auth.github.GsonRepositoryCollaborator;
  35. import org.sonar.auth.github.GsonRepositoryPermissions;
  36. import org.sonar.auth.github.client.GithubApplicationClient;
  37. import org.sonar.core.i18n.I18n;
  38. import org.sonar.core.platform.EditionProvider;
  39. import org.sonar.core.platform.PlatformEditionProvider;
  40. import org.sonar.core.util.SequenceUuidFactory;
  41. import org.sonar.db.DbSession;
  42. import org.sonar.db.DbTester;
  43. import org.sonar.db.alm.setting.AlmSettingDto;
  44. import org.sonar.db.component.BranchDto;
  45. import org.sonar.db.component.ResourceTypesRule;
  46. import org.sonar.db.entity.EntityDto;
  47. import org.sonar.db.newcodeperiod.NewCodePeriodDto;
  48. import org.sonar.db.permission.GlobalPermission;
  49. import org.sonar.db.project.CreationMethod;
  50. import org.sonar.db.project.ProjectDto;
  51. import org.sonar.db.user.UserDto;
  52. import org.sonar.server.almintegration.ws.ImportHelper;
  53. import org.sonar.server.common.almintegration.ProjectKeyGenerator;
  54. import org.sonar.server.common.almsettings.github.GithubProjectCreatorFactory;
  55. import org.sonar.server.common.component.ComponentUpdater;
  56. import org.sonar.server.common.newcodeperiod.NewCodeDefinitionResolver;
  57. import org.sonar.server.common.permission.GroupPermissionChanger;
  58. import org.sonar.server.common.permission.PermissionTemplateService;
  59. import org.sonar.server.common.permission.PermissionUpdater;
  60. import org.sonar.server.common.permission.UserPermissionChange;
  61. import org.sonar.server.common.permission.UserPermissionChanger;
  62. import org.sonar.server.common.project.ImportProjectService;
  63. import org.sonar.server.common.project.ProjectCreator;
  64. import org.sonar.server.es.EsTester;
  65. import org.sonar.server.es.IndexersImpl;
  66. import org.sonar.server.es.TestIndexers;
  67. import org.sonar.server.exceptions.BadConfigurationException;
  68. import org.sonar.server.exceptions.NotFoundException;
  69. import org.sonar.server.exceptions.UnauthorizedException;
  70. import org.sonar.server.favorite.FavoriteUpdater;
  71. import org.sonar.server.management.ManagedProjectService;
  72. import org.sonar.server.permission.PermissionService;
  73. import org.sonar.server.permission.PermissionServiceImpl;
  74. import org.sonar.server.permission.index.FooIndexDefinition;
  75. import org.sonar.server.permission.index.PermissionIndexer;
  76. import org.sonar.server.project.DefaultBranchNameResolver;
  77. import org.sonar.server.project.ProjectDefaultVisibility;
  78. import org.sonar.server.project.Visibility;
  79. import org.sonar.server.tester.UserSessionRule;
  80. import org.sonar.server.ws.TestRequest;
  81. import org.sonar.server.ws.WsActionTester;
  82. import org.sonarqube.ws.Projects;
  83. import static java.lang.String.format;
  84. import static java.util.Objects.requireNonNull;
  85. import static org.assertj.core.api.Assertions.assertThat;
  86. import static org.assertj.core.api.Assertions.assertThatNoException;
  87. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  88. import static org.assertj.core.api.Assertions.tuple;
  89. import static org.mockito.ArgumentMatchers.any;
  90. import static org.mockito.ArgumentMatchers.eq;
  91. import static org.mockito.Mockito.mock;
  92. import static org.mockito.Mockito.never;
  93. import static org.mockito.Mockito.verify;
  94. import static org.mockito.Mockito.when;
  95. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  96. import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
  97. import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
  98. import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
  99. import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_REPOSITORY_KEY;
  100. import static org.sonar.server.tester.UserSessionRule.standalone;
  101. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
  102. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
  103. public class ImportGithubProjectActionIT {
  104. private static final String PROJECT_KEY_NAME = "PROJECT_NAME";
  105. private static final String GENERATED_PROJECT_KEY = "generated_" + PROJECT_KEY_NAME;
  106. @Rule
  107. public UserSessionRule userSession = standalone();
  108. private final System2 system2 = mock(System2.class);
  109. private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
  110. private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
  111. @Rule
  112. public DbTester db = DbTester.create(system2);
  113. private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
  114. public EsTester es = EsTester.createCustom(new FooIndexDefinition());
  115. private final PermissionUpdater<UserPermissionChange> userPermissionUpdater = new PermissionUpdater(
  116. new IndexersImpl(new PermissionIndexer(db.getDbClient(), es.client())),
  117. Set.of(new UserPermissionChanger(db.getDbClient(), new SequenceUuidFactory()),
  118. new GroupPermissionChanger(db.getDbClient(), new SequenceUuidFactory())));
  119. private final PermissionService permissionService = new PermissionServiceImpl(new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT));
  120. private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
  121. permissionTemplateService, new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(),
  122. defaultBranchNameResolver, userPermissionUpdater, permissionService);
  123. private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
  124. private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
  125. private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
  126. private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
  127. private final GitHubSettings gitHubSettings = mock(GitHubSettings.class);
  128. private final NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
  129. private final ManagedProjectService managedProjectService = mock(ManagedProjectService.class);
  130. private final GithubPermissionConverter githubPermissionConverter = mock();
  131. private final ProjectCreator projectCreator = new ProjectCreator(userSession, projectDefaultVisibility, componentUpdater);
  132. private final GithubProjectCreatorFactory gitHubProjectCreatorFactory = new GithubProjectCreatorFactory(db.getDbClient(),
  133. null, appClient, projectKeyGenerator, userSession, projectCreator, gitHubSettings, githubPermissionConverter, userPermissionUpdater, permissionService,
  134. managedProjectService);
  135. private final ImportProjectService importProjectService = new ImportProjectService(db.getDbClient(), gitHubProjectCreatorFactory, userSession, componentUpdater,
  136. newCodeDefinitionResolver);
  137. private final WsActionTester ws = new WsActionTester(new ImportGithubProjectAction(importProjectService, importHelper));
  138. @Before
  139. public void before() {
  140. when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC);
  141. when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
  142. }
  143. @Test
  144. public void importProject_ifProjectWithSameNameDoesNotExist_importSucceed() {
  145. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  146. GithubApplicationClient.Repository repository = mockGithubDevOpsAppInteractions();
  147. mockGithubAuthAppInteractions();
  148. when(gitHubSettings.isProvisioningEnabled()).thenReturn(true);
  149. Projects.CreateWsResponse response = callWebService(githubAlmSetting);
  150. Projects.CreateWsResponse.Project result = response.getProject();
  151. assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  152. assertThat(result.getName()).isEqualTo(repository.getName());
  153. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  154. assertThat(projectDto).isPresent();
  155. assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
  156. Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()).stream().filter(BranchDto::isMain).findAny();
  157. assertThat(mainBranch).isPresent();
  158. assertThat(mainBranch.get().getKey()).isEqualTo("default-branch");
  159. verify(managedProjectService).queuePermissionSyncTask(userSession.getUuid(), mainBranch.get().getUuid(), projectDto.get().getUuid());
  160. }
  161. @Test
  162. public void importProject_withNCD_developer_edition() {
  163. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
  164. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  165. mockGithubDevOpsAppInteractions();
  166. Projects.CreateWsResponse response = ws.newRequest()
  167. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  168. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  169. .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
  170. .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
  171. .executeProtobuf(Projects.CreateWsResponse.class);
  172. Projects.CreateWsResponse.Project result = response.getProject();
  173. assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  174. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  175. assertThat(projectDto).isPresent();
  176. assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
  177. .isPresent()
  178. .get()
  179. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
  180. .containsExactly(NUMBER_OF_DAYS, "30", null);
  181. }
  182. @Test
  183. public void importProject_withNCD_community_edition() {
  184. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
  185. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  186. mockGithubDevOpsAppInteractions();
  187. Projects.CreateWsResponse response = ws.newRequest()
  188. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  189. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  190. .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
  191. .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
  192. .executeProtobuf(Projects.CreateWsResponse.class);
  193. Projects.CreateWsResponse.Project result = response.getProject();
  194. assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  195. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  196. assertThat(projectDto).isPresent();
  197. BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
  198. String projectUuid = projectDto.get().getUuid();
  199. assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
  200. .isPresent()
  201. .get()
  202. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
  203. .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
  204. }
  205. @Test
  206. public void importProject_reference_branch_ncd_no_default_branch() {
  207. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
  208. when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("default-branch");
  209. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  210. mockGithubDevOpsAppInteractions();
  211. Projects.CreateWsResponse response = ws.newRequest()
  212. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  213. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  214. .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "reference_branch")
  215. .executeProtobuf(Projects.CreateWsResponse.class);
  216. Projects.CreateWsResponse.Project result = response.getProject();
  217. assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  218. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  219. assertThat(projectDto).isPresent();
  220. assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
  221. .isPresent()
  222. .get()
  223. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
  224. .containsExactly(REFERENCE_BRANCH, "default-branch");
  225. }
  226. @Test
  227. public void importProject_reference_branch_ncd() {
  228. when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
  229. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  230. mockGithubDevOpsAppInteractions();
  231. Projects.CreateWsResponse response = ws.newRequest()
  232. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  233. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  234. .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "reference_branch")
  235. .executeProtobuf(Projects.CreateWsResponse.class);
  236. Projects.CreateWsResponse.Project result = response.getProject();
  237. assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  238. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
  239. assertThat(projectDto).isPresent();
  240. assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
  241. .isPresent()
  242. .get()
  243. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
  244. .containsExactly(REFERENCE_BRANCH, "default-branch");
  245. }
  246. @Test
  247. public void importProject_ifProjectWithSameNameAlreadyExists_importSucceed() {
  248. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  249. db.components().insertPublicProject(p -> p.setKey("Hello-World")).getMainBranchComponent();
  250. GithubApplicationClient.Repository repository = mockGithubDevOpsAppInteractions();
  251. Projects.CreateWsResponse response = ws.newRequest()
  252. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  253. .setParam(PARAM_REPOSITORY_KEY, "Hello-World")
  254. .executeProtobuf(Projects.CreateWsResponse.class);
  255. Projects.CreateWsResponse.Project result = response.getProject();
  256. assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  257. assertThat(result.getName()).isEqualTo(repository.getName());
  258. }
  259. @Test
  260. public void importProject_whenGithubProvisioningIsDisabled_shouldApplyPermissionTemplateAndSetDefaultVisibility() {
  261. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  262. mockGithubDevOpsAppInteractions();
  263. when(gitHubSettings.isProvisioningEnabled()).thenReturn(false);
  264. ws.newRequest()
  265. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  266. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  267. .executeProtobuf(Projects.CreateWsResponse.class);
  268. ArgumentCaptor<EntityDto> projectDtoArgumentCaptor = ArgumentCaptor.forClass(EntityDto.class);
  269. verify(permissionTemplateService).applyDefaultToNewComponent(any(DbSession.class), projectDtoArgumentCaptor.capture(), eq(userSession.getUuid()));
  270. EntityDto capturedProjectDto = projectDtoArgumentCaptor.getValue();
  271. assertThat(capturedProjectDto.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  272. assertThat(capturedProjectDto.isPrivate()).isFalse();
  273. }
  274. @Test
  275. public void importProject_whenGithubProvisioningIsEnabled_shouldNotApplyPermissionTemplate() {
  276. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  277. when(gitHubSettings.isProvisioningEnabled()).thenReturn(true);
  278. mockGithubDevOpsAppInteractions();
  279. mockGithubAuthAppInteractions();
  280. ws.newRequest()
  281. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  282. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  283. .executeProtobuf(Projects.CreateWsResponse.class);
  284. verify(permissionTemplateService, never()).applyDefaultToNewComponent(any(), any(), any());
  285. }
  286. private void mockGithubAuthAppInteractions() {
  287. when(gitHubSettings.appId()).thenReturn("432");
  288. when(gitHubSettings.privateKey()).thenReturn("private key");
  289. when(gitHubSettings.apiURL()).thenReturn("http://www.url.com");
  290. AppInstallationToken appInstallationToken = mock();
  291. when(appClient.getInstallationId(any(), any())).thenReturn(Optional.of(321L));
  292. when(appClient.createAppInstallationToken(any(), eq(321L))).thenReturn(Optional.of(appInstallationToken));
  293. GsonRepositoryCollaborator gsonRepositoryCollaborator = new GsonRepositoryCollaborator("toto", 2, "admin", new GsonRepositoryPermissions(true, true, true, true, true));
  294. when(appClient.getRepositoryCollaborators(gitHubSettings.apiURL(), appInstallationToken, "octocat", PROJECT_KEY_NAME)).thenReturn(Set.of(gsonRepositoryCollaborator));
  295. String role = gsonRepositoryCollaborator.roleName();
  296. GsonRepositoryPermissions permissions = gsonRepositoryCollaborator.permissions();
  297. when(githubPermissionConverter.toSonarqubeRolesWithFallbackOnRepositoryPermissions(Set.of(), role, permissions)).thenReturn(Set.of("scan"));
  298. }
  299. @Test
  300. public void importProject_shouldSetCreationMethodToApi_ifNonBrowserRequest() {
  301. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  302. mockGithubDevOpsAppInteractions();
  303. Projects.CreateWsResponse response = callWebService(githubAlmSetting);
  304. assertThat(response.getProject().getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  305. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), response.getProject().getKey());
  306. assertThat(projectDto.orElseThrow().getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
  307. }
  308. @Test
  309. public void importProject_shouldSetCreationMethodToBrowser_ifBrowserRequest() {
  310. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  311. userSession.flagSessionAsGui();
  312. mockGithubDevOpsAppInteractions();
  313. Projects.CreateWsResponse response = callWebService(githubAlmSetting);
  314. assertThat(response.getProject().getKey()).isEqualTo(GENERATED_PROJECT_KEY);
  315. Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), response.getProject().getKey());
  316. assertThat(projectDto.orElseThrow().getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_BROWSER);
  317. }
  318. @Test
  319. public void importProject_whenAlmSettingKeyDoesNotExist_shouldThrow() {
  320. UserDto user = db.users().insertUser();
  321. userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
  322. TestRequest request = ws.newRequest()
  323. .setParam(PARAM_ALM_SETTING, "unknown")
  324. .setParam(PARAM_REPOSITORY_KEY, "test/repo");
  325. assertThatThrownBy(request::execute)
  326. .isInstanceOf(NotFoundException.class)
  327. .hasMessage("DevOps Platform configuration 'unknown' not found.");
  328. }
  329. @Test
  330. public void importProject_whenNoAlmSettingKeyAndNoConfig_shouldThrow() {
  331. UserDto user = db.users().insertUser();
  332. userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
  333. TestRequest request = ws.newRequest()
  334. .setParam(PARAM_REPOSITORY_KEY, "test/repo");
  335. assertThatThrownBy(request::execute)
  336. .isInstanceOf(NotFoundException.class)
  337. .hasMessage("There is no GITHUB configuration for DevOps Platform. Please add one.");
  338. }
  339. @Test
  340. public void importProject_whenNoAlmSettingKeyAndMultipleConfigs_shouldThrow() {
  341. UserDto user = db.users().insertUser();
  342. userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
  343. db.almSettings().insertGitHubAlmSetting();
  344. db.almSettings().insertGitHubAlmSetting();
  345. TestRequest request = ws.newRequest()
  346. .setParam(PARAM_REPOSITORY_KEY, "test/repo");
  347. assertThatThrownBy(request::execute)
  348. .isInstanceOf(IllegalArgumentException.class)
  349. .hasMessage("Parameter almSetting is required as there are multiple DevOps Platform configurations.");
  350. }
  351. @Test
  352. public void importProject_whenProvisioningIsEnabledButConfigDoesNotAllowAccessToRepo_shouldThrow() {
  353. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  354. when(gitHubSettings.isProvisioningEnabled()).thenReturn(true);
  355. mockGithubDevOpsAppInteractions();
  356. mockGithubAuthAppInteractions();
  357. when(appClient.getInstallationId(any(), any())).thenReturn(Optional.empty());
  358. TestRequest request = ws.newRequest()
  359. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  360. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME);
  361. assertThatThrownBy(request::execute)
  362. .isInstanceOf(BadConfigurationException.class)
  363. .hasMessage(format("GitHub auto-provisioning is activated. However the repo %s is not in the scope of the authentication application. "
  364. + "The permissions can't be checked, and the project can not be created.",
  365. "octocat/" + PROJECT_KEY_NAME));
  366. }
  367. @Test
  368. public void importProject_whenNoAlmSettingKeyAndOnlyOneConfig_shouldImport() {
  369. AlmSettingDto githubAlmSetting = setupUserWithPatAndAlmSettings();
  370. mockGithubDevOpsAppInteractions();
  371. TestRequest request = ws.newRequest()
  372. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  373. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME);
  374. assertThatNoException().isThrownBy(request::execute);
  375. }
  376. private Projects.CreateWsResponse callWebService(AlmSettingDto githubAlmSetting) {
  377. return ws.newRequest()
  378. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  379. .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
  380. .executeProtobuf(Projects.CreateWsResponse.class);
  381. }
  382. private GithubApplicationClient.Repository mockGithubDevOpsAppInteractions() {
  383. GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, PROJECT_KEY_NAME, false,
  384. "octocat/" + PROJECT_KEY_NAME,
  385. "https://github.sonarsource.com/api/v3/repos/octocat/" + PROJECT_KEY_NAME, "default-branch");
  386. when(appClient.getRepository(any(), any(), any())).thenReturn(Optional.of(repository));
  387. when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(GENERATED_PROJECT_KEY);
  388. return repository;
  389. }
  390. @Test
  391. public void fail_when_not_logged_in() {
  392. TestRequest request = ws.newRequest()
  393. .setParam(PARAM_ALM_SETTING, "asdfghjkl")
  394. .setParam(PARAM_REPOSITORY_KEY, "test/repo");
  395. assertThatThrownBy(request::execute)
  396. .isInstanceOf(UnauthorizedException.class);
  397. }
  398. @Test
  399. public void fail_when_missing_create_project_permission() {
  400. TestRequest request = ws.newRequest();
  401. assertThatThrownBy(request::execute)
  402. .isInstanceOf(UnauthorizedException.class);
  403. }
  404. @Test
  405. public void fail_when_personal_access_token_doesnt_exist() {
  406. AlmSettingDto githubAlmSetting = setupUserAndAlmSettings();
  407. TestRequest request = ws.newRequest()
  408. .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
  409. .setParam(PARAM_REPOSITORY_KEY, "test/repo");
  410. assertThatThrownBy(request::execute)
  411. .isInstanceOf(IllegalArgumentException.class)
  412. .hasMessage("No personal access token found");
  413. }
  414. @Test
  415. public void definition() {
  416. WebService.Action def = ws.getDef();
  417. assertThat(def.since()).isEqualTo("8.4");
  418. assertThat(def.isPost()).isTrue();
  419. assertThat(def.params())
  420. .extracting(WebService.Param::key, WebService.Param::isRequired)
  421. .containsExactlyInAnyOrder(
  422. tuple(PARAM_ALM_SETTING, false),
  423. tuple(PARAM_REPOSITORY_KEY, true),
  424. tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
  425. tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
  426. assertThat(def.deprecatedSince()).isEqualTo("10.5");
  427. }
  428. private AlmSettingDto setupUserWithPatAndAlmSettings() {
  429. AlmSettingDto almSettings = setupUserAndAlmSettings();
  430. db.almPats().insert(p -> p.setAlmSettingUuid(almSettings.getUuid()).setUserUuid(requireNonNull(userSession.getUuid())));
  431. return almSettings;
  432. }
  433. private AlmSettingDto setupUserAndAlmSettings() {
  434. UserDto user = db.users().insertUser();
  435. userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
  436. return db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_123").setClientSecret("client_secret_123"));
  437. }
  438. }