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

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