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

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