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.

SearchGitlabReposActionIT.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.gitlab;
  21. import java.util.Arrays;
  22. import java.util.LinkedList;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.alm.client.gitlab.GitlabHttpClient;
  26. import org.sonar.alm.client.gitlab.Project;
  27. import org.sonar.alm.client.gitlab.ProjectList;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.db.alm.pat.AlmPatDto;
  31. import org.sonar.db.alm.setting.AlmSettingDto;
  32. import org.sonar.db.project.ProjectDto;
  33. import org.sonar.db.user.UserDto;
  34. import org.sonar.server.exceptions.ForbiddenException;
  35. import org.sonar.server.exceptions.NotFoundException;
  36. import org.sonar.server.exceptions.UnauthorizedException;
  37. import org.sonar.server.tester.UserSessionRule;
  38. import org.sonar.server.ws.TestRequest;
  39. import org.sonar.server.ws.WsActionTester;
  40. import org.sonarqube.ws.AlmIntegrations;
  41. import org.sonarqube.ws.AlmIntegrations.GitlabRepository;
  42. import org.sonarqube.ws.AlmIntegrations.SearchGitlabReposWsResponse;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  45. import static org.assertj.core.api.Assertions.tuple;
  46. import static org.mockito.ArgumentMatchers.any;
  47. import static org.mockito.ArgumentMatchers.anyInt;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.when;
  50. import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
  51. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  52. public class SearchGitlabReposActionIT {
  53. @Rule
  54. public UserSessionRule userSession = UserSessionRule.standalone();
  55. @Rule
  56. public DbTester db = DbTester.create();
  57. private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class);
  58. private final WsActionTester ws = new WsActionTester(new SearchGitlabReposAction(db.getDbClient(), userSession,
  59. gitlabHttpClient));
  60. @Test
  61. public void list_gitlab_repos() {
  62. Project gitlabProject1 = new Project(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1");
  63. Project gitlabProject2 = new Project(2L, "repoName2", "path1 / repoName2", "repo-slug-2", "path-1/repo-slug-2", "url-2");
  64. Project gitlabProject3 = new Project(3L, "repoName3", "repoName3 / repoName3", "repo-slug-3", "repo-slug-3/repo-slug-3", "url-3");
  65. Project gitlabProject4 = new Project(4L, "repoName4", "repoName4 / repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4/repo-slug-4", "url-4");
  66. when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt()))
  67. .thenReturn(
  68. new ProjectList(Arrays.asList(gitlabProject1, gitlabProject2, gitlabProject3, gitlabProject4), 1, 10, 4));
  69. UserDto user = db.users().insertUser();
  70. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  71. AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
  72. db.almPats().insert(dto -> {
  73. dto.setAlmSettingUuid(almSetting.getUuid());
  74. dto.setUserUuid(user.getUuid());
  75. dto.setPersonalAccessToken("some-pat");
  76. });
  77. ProjectDto projectDto = db.components().insertPrivateProject().getProjectDto();
  78. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto);
  79. AlmIntegrations.SearchGitlabReposWsResponse response = ws.newRequest()
  80. .setParam("almSetting", almSetting.getKey())
  81. .executeProtobuf(SearchGitlabReposWsResponse.class);
  82. assertThat(response.getRepositoriesCount()).isEqualTo(4);
  83. assertThat(response.getRepositoriesList())
  84. .extracting(GitlabRepository::getId,
  85. GitlabRepository::getName,
  86. GitlabRepository::getPathName,
  87. GitlabRepository::getSlug,
  88. GitlabRepository::getPathSlug,
  89. GitlabRepository::getUrl,
  90. GitlabRepository::hasSqProjectKey,
  91. GitlabRepository::hasSqProjectName)
  92. .containsExactlyInAnyOrder(
  93. tuple(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1", false, false),
  94. tuple(2L, "repoName2", "path1", "repo-slug-2", "path-1", "url-2", false, false),
  95. tuple(3L, "repoName3", "repoName3", "repo-slug-3", "repo-slug-3", "url-3", false, false),
  96. tuple(4L, "repoName4", "repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4", "url-4", false, false));
  97. }
  98. @Test
  99. public void list_gitlab_repos_some_projects_already_set_up() {
  100. Project gitlabProject1 = new Project(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1");
  101. Project gitlabProject2 = new Project(2L, "repoName2", "path1 / repoName2", "repo-slug-2", "path-1/repo-slug-2", "url-2");
  102. Project gitlabProject3 = new Project(3L, "repoName3", "repoName3 / repoName3", "repo-slug-3", "repo-slug-3/repo-slug-3", "url-3");
  103. Project gitlabProject4 = new Project(4L, "repoName4", "repoName4 / repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4/repo-slug-4", "url-4");
  104. when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt()))
  105. .thenReturn(
  106. new ProjectList(Arrays.asList(gitlabProject1, gitlabProject2, gitlabProject3, gitlabProject4), 1, 10, 4));
  107. UserDto user = db.users().insertUser();
  108. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  109. AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
  110. db.almPats().insert(dto -> {
  111. dto.setAlmSettingUuid(almSetting.getUuid());
  112. dto.setUserUuid(user.getUuid());
  113. dto.setPersonalAccessToken("some-pat");
  114. });
  115. ProjectDto projectDto1 = db.components().insertPrivateProject().getProjectDto();
  116. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto1);
  117. ProjectDto projectDto2 = db.components().insertPrivateProject().getProjectDto();
  118. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto2, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("2"));
  119. ProjectDto projectDto3 = db.components().insertPrivateProject().getProjectDto();
  120. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto3, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("3"));
  121. ProjectDto projectDto4 = db.components().insertPrivateProject().getProjectDto();
  122. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto4, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("3"));
  123. AlmIntegrations.SearchGitlabReposWsResponse response = ws.newRequest()
  124. .setParam("almSetting", almSetting.getKey())
  125. .executeProtobuf(SearchGitlabReposWsResponse.class);
  126. assertThat(response.getRepositoriesCount()).isEqualTo(4);
  127. assertThat(response.getRepositoriesList())
  128. .extracting(GitlabRepository::getId,
  129. GitlabRepository::getName,
  130. GitlabRepository::getPathName,
  131. GitlabRepository::getSlug,
  132. GitlabRepository::getPathSlug,
  133. GitlabRepository::getUrl,
  134. GitlabRepository::getSqProjectKey,
  135. GitlabRepository::getSqProjectName)
  136. .containsExactlyInAnyOrder(
  137. tuple(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1", "", ""),
  138. tuple(2L, "repoName2", "path1", "repo-slug-2", "path-1", "url-2", projectDto2.getKey(), projectDto2.getName()),
  139. tuple(3L, "repoName3", "repoName3", "repo-slug-3", "repo-slug-3", "url-3", projectDto3.getKey(), projectDto3.getName()),
  140. tuple(4L, "repoName4", "repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4", "url-4", "", ""));
  141. }
  142. @Test
  143. public void return_empty_list_when_no_gitlab_projects() {
  144. when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt())).thenReturn(new ProjectList(new LinkedList<>(), 1, 10, 0));
  145. UserDto user = db.users().insertUser();
  146. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  147. AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
  148. db.almPats().insert(dto -> {
  149. dto.setAlmSettingUuid(almSetting.getUuid());
  150. dto.setUserUuid(user.getUuid());
  151. });
  152. ProjectDto projectDto = db.components().insertPrivateProject().getProjectDto();
  153. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto);
  154. AlmIntegrations.SearchGitlabReposWsResponse response = ws.newRequest()
  155. .setParam("almSetting", almSetting.getKey())
  156. .executeProtobuf(SearchGitlabReposWsResponse.class);
  157. assertThat(response.getRepositoriesList()).isEmpty();
  158. }
  159. @Test
  160. public void check_pat_is_missing() {
  161. UserDto user = db.users().insertUser();
  162. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  163. AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
  164. TestRequest request = ws.newRequest()
  165. .setParam("almSetting", almSetting.getKey());
  166. assertThatThrownBy(request::execute)
  167. .isInstanceOf(IllegalArgumentException.class)
  168. .hasMessage("No personal access token found");
  169. }
  170. @Test
  171. public void fail_when_alm_setting_not_found() {
  172. UserDto user = db.users().insertUser();
  173. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  174. AlmPatDto almPatDto = newAlmPatDto();
  175. db.getDbClient().almPatDao().insert(db.getSession(), almPatDto, user.getLogin(), null);
  176. TestRequest request = ws.newRequest()
  177. .setParam("almSetting", "testKey");
  178. assertThatThrownBy(request::execute)
  179. .isInstanceOf(NotFoundException.class)
  180. .hasMessage("DevOps Platform Setting 'testKey' not found");
  181. }
  182. @Test
  183. public void fail_when_not_logged_in() {
  184. TestRequest request = ws.newRequest()
  185. .setParam("almSetting", "anyvalue");
  186. assertThatThrownBy(request::execute)
  187. .isInstanceOf(UnauthorizedException.class)
  188. .hasMessage("Authentication is required");
  189. }
  190. @Test
  191. public void fail_when_no_creation_project_permission() {
  192. UserDto user = db.users().insertUser();
  193. userSession.logIn(user);
  194. TestRequest request = ws.newRequest()
  195. .setParam("almSetting", "anyvalue");
  196. assertThatThrownBy(request::execute)
  197. .isInstanceOf(ForbiddenException.class)
  198. .hasMessage("Insufficient privileges");
  199. }
  200. @Test
  201. public void verify_response_example() {
  202. Project gitlabProject1 = new Project(1L, "Gitlab repo name 1", "Group / Gitlab repo name 1", "gitlab-repo-name-1", "group/gitlab-repo-name-1",
  203. "https://example.gitlab.com/group/gitlab-repo-name-1");
  204. Project gitlabProject2 = new Project(2L, "Gitlab repo name 2", "Group / Gitlab repo name 2", "gitlab-repo-name-2", "group/gitlab-repo-name-2",
  205. "https://example.gitlab.com/group/gitlab-repo-name-2");
  206. Project gitlabProject3 = new Project(3L, "Gitlab repo name 3", "Group / Gitlab repo name 3", "gitlab-repo-name-3", "group/gitlab-repo-name-3",
  207. "https://example.gitlab.com/group/gitlab-repo-name-3");
  208. when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt()))
  209. .thenReturn(
  210. new ProjectList(Arrays.asList(gitlabProject1, gitlabProject2, gitlabProject3), 1, 3, 10));
  211. UserDto user = db.users().insertUser();
  212. userSession.logIn(user).addPermission(PROVISION_PROJECTS);
  213. AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
  214. db.almPats().insert(dto -> {
  215. dto.setAlmSettingUuid(almSetting.getUuid());
  216. dto.setUserUuid(user.getUuid());
  217. dto.setPersonalAccessToken("some-pat");
  218. });
  219. ProjectDto projectDto = db.components().insertPrivateProject().getProjectDto();
  220. db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto);
  221. WebService.Action def = ws.getDef();
  222. String responseExample = def.responseExampleAsString();
  223. assertThat(responseExample).isNotBlank();
  224. ws.newRequest()
  225. .setParam("almSetting", almSetting.getKey())
  226. .execute().assertJson(
  227. responseExample);
  228. }
  229. @Test
  230. public void definition() {
  231. WebService.Action def = ws.getDef();
  232. assertThat(def.since()).isEqualTo("8.5");
  233. assertThat(def.isPost()).isFalse();
  234. assertThat(def.params())
  235. .extracting(WebService.Param::key, WebService.Param::isRequired)
  236. .containsExactlyInAnyOrder(
  237. tuple("almSetting", true),
  238. tuple("p", false),
  239. tuple("ps", false),
  240. tuple("projectName", false));
  241. }
  242. }