]> source.dussan.org Git - sonarqube.git/blob
cd81e8c245203fe319a0172c9a9123d4a2033444
[sonarqube.git] /
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.bitbucketserver;
21
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
31 import org.sonar.alm.client.bitbucketserver.Branch;
32 import org.sonar.alm.client.bitbucketserver.BranchesList;
33 import org.sonar.alm.client.bitbucketserver.Project;
34 import org.sonar.alm.client.bitbucketserver.Repository;
35 import org.sonar.api.server.ws.WebService;
36 import org.sonar.api.utils.System2;
37 import org.sonar.core.util.SequenceUuidFactory;
38 import org.sonar.db.DbTester;
39 import org.sonar.db.alm.pat.AlmPatDto;
40 import org.sonar.db.alm.setting.AlmSettingDto;
41 import org.sonar.db.component.BranchDto;
42 import org.sonar.db.project.ProjectDto;
43 import org.sonar.db.user.UserDto;
44 import org.sonar.server.almintegration.ws.ImportHelper;
45 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
46 import org.sonar.server.component.ComponentUpdater;
47 import org.sonar.server.es.TestProjectIndexers;
48 import org.sonar.server.exceptions.BadRequestException;
49 import org.sonar.server.exceptions.ForbiddenException;
50 import org.sonar.server.exceptions.NotFoundException;
51 import org.sonar.server.exceptions.UnauthorizedException;
52 import org.sonar.server.favorite.FavoriteUpdater;
53 import org.sonar.server.l18n.I18nRule;
54 import org.sonar.server.permission.PermissionTemplateService;
55 import org.sonar.server.project.DefaultBranchNameResolver;
56 import org.sonar.server.project.ProjectDefaultVisibility;
57 import org.sonar.server.project.Visibility;
58 import org.sonar.server.tester.UserSessionRule;
59 import org.sonar.server.ws.WsActionTester;
60 import org.sonarqube.ws.Projects;
61
62 import static java.util.Objects.requireNonNull;
63 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
64 import static org.apache.commons.lang.math.JVMRandom.nextLong;
65 import static org.assertj.core.api.Assertions.assertThat;
66 import static org.assertj.core.api.Assertions.assertThatThrownBy;
67 import static org.assertj.core.api.Assertions.tuple;
68 import static org.mockito.ArgumentMatchers.any;
69 import static org.mockito.Mockito.mock;
70 import static org.mockito.Mockito.verify;
71 import static org.mockito.Mockito.when;
72 import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
73 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
74 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
75 import static org.sonar.db.permission.GlobalPermission.SCAN;
76
77 public class ImportBitbucketServerProjectActionIT {
78   private static final String GENERATED_PROJECT_KEY = "TEST_PROJECT_KEY";
79
80   @Rule
81   public UserSessionRule userSession = UserSessionRule.standalone();
82   @Rule
83   public DbTester db = DbTester.create(true);
84   @Rule
85   public final I18nRule i18n = new I18nRule();
86
87   private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
88   private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
89   private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
90
91   private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE,
92     mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(),
93     defaultBranchNameResolver, true);
94
95   private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
96   private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
97   private final WsActionTester ws = new WsActionTester(new ImportBitbucketServerProjectAction(db.getDbClient(), userSession,
98     bitbucketServerRestClient, projectDefaultVisibility, componentUpdater, importHelper, projectKeyGenerator));
99
100   private static BranchesList defaultBranchesList;
101
102   @BeforeClass
103   public static void beforeAll() {
104     Branch defaultBranch = new Branch("default", true);
105     defaultBranchesList = new BranchesList(Collections.singletonList(defaultBranch));
106   }
107
108   @Before
109   public void before() {
110     when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
111     when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY);
112     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
113   }
114
115   @Test
116   public void import_project() {
117     UserDto user = db.users().insertUser();
118     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
119     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
120     db.almPats().insert(dto -> {
121       dto.setAlmSettingUuid(almSetting.getUuid());
122       dto.setUserUuid(user.getUuid());
123     });
124     Project project = getGsonBBSProject();
125     Repository repo = getGsonBBSRepo(project);
126     when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
127     when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(defaultBranchesList);
128
129     Projects.CreateWsResponse response = ws.newRequest()
130       .setParam("almSetting", almSetting.getKey())
131       .setParam("projectKey", "projectKey")
132       .setParam("repositorySlug", "repo-slug")
133       .executeProtobuf(Projects.CreateWsResponse.class);
134
135     Projects.CreateWsResponse.Project result = response.getProject();
136     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
137     assertThat(result.getName()).isEqualTo(repo.getName());
138
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     verify(projectKeyGenerator).generateUniqueProjectKey(requireNonNull(project.getKey()), repo.getSlug());
143   }
144
145   @Test
146   public void fail_project_already_exist() {
147     UserDto user = db.users().insertUser();
148     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
149     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
150     db.almPats().insert(dto -> {
151       dto.setAlmSettingUuid(almSetting.getUuid());
152       dto.setUserUuid(user.getUuid());
153     });
154     Project project = getGsonBBSProject();
155     Repository repo = getGsonBBSRepo(project);
156     db.components().insertPublicProject(p -> p.setKey(GENERATED_PROJECT_KEY)).getMainBranchComponent();
157
158     assertThatThrownBy(() -> {
159       when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
160       when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(defaultBranchesList);
161
162       ws.newRequest()
163         .setParam("almSetting", almSetting.getKey())
164         .setParam("projectKey", "projectKey")
165         .setParam("repositorySlug", "repo-slug")
166         .execute();
167     })
168       .isInstanceOf(BadRequestException.class)
169       .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", GENERATED_PROJECT_KEY, GENERATED_PROJECT_KEY);
170   }
171
172   @Test
173   public void fail_when_not_logged_in() {
174     assertThatThrownBy(() -> {
175       ws.newRequest()
176         .setParam("almSetting", "sdgfdshfjztutz")
177         .setParam("projectKey", "projectKey")
178         .setParam("repositorySlug", "repo-slug")
179         .execute();
180     })
181       .isInstanceOf(UnauthorizedException.class);
182   }
183
184   @Test
185   public void fail_when_missing_project_creator_permission() {
186     UserDto user = db.users().insertUser();
187     userSession.logIn(user).addPermission(SCAN);
188
189     assertThatThrownBy(() -> {
190       ws.newRequest()
191         .setParam("almSetting", "sdgfdshfjztutz")
192         .setParam("projectKey", "projectKey")
193         .setParam("repositorySlug", "repo-slug")
194         .execute();
195     })
196       .isInstanceOf(ForbiddenException.class)
197       .hasMessage("Insufficient privileges");
198   }
199
200   @Test
201   public void check_pat_is_missing() {
202     UserDto user = db.users().insertUser();
203     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
204     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
205
206     assertThatThrownBy(() -> {
207       ws.newRequest()
208         .setParam("almSetting", almSetting.getKey())
209         .execute();
210     })
211       .isInstanceOf(IllegalArgumentException.class)
212       .hasMessage("personal access token for '" + almSetting.getKey() + "' is missing");
213   }
214
215   @Test
216   public void fail_check_alm_setting_not_found() {
217     UserDto user = db.users().insertUser();
218     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
219     AlmPatDto almPatDto = newAlmPatDto();
220     db.getDbClient().almPatDao().insert(db.getSession(), almPatDto, user.getLogin(), null);
221
222     assertThatThrownBy(() -> {
223       ws.newRequest()
224         .setParam("almSetting", "testKey")
225         .execute();
226     })
227       .isInstanceOf(NotFoundException.class)
228       .hasMessage("DevOps Platform Setting 'testKey' not found");
229   }
230
231   @Test
232   public void fail_when_no_creation_project_permission() {
233     UserDto user = db.users().insertUser();
234     userSession.logIn(user);
235
236     assertThatThrownBy(() -> {
237       ws.newRequest()
238         .setParam("almSetting", "anyvalue")
239         .execute();
240     })
241       .isInstanceOf(ForbiddenException.class)
242       .hasMessage("Insufficient privileges");
243   }
244
245   @Test
246   public void handle_givenNoDefaultBranchFound_doNotUpdateDefaultBranchName() {
247     BranchesList branchesList = new BranchesList();
248     Branch branch = new Branch("not_a_master", false);
249     branchesList.addBranch(branch);
250
251     UserDto user = db.users().insertUser();
252     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
253     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
254     db.almPats().insert(dto -> {
255       dto.setAlmSettingUuid(almSetting.getUuid());
256       dto.setUserUuid(user.getUuid());
257     });
258     Project project = getGsonBBSProject();
259     Repository repo = getGsonBBSRepo(project);
260     when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
261     when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(branchesList);
262
263     Projects.CreateWsResponse response = ws.newRequest()
264       .setParam("almSetting", almSetting.getKey())
265       .setParam("projectKey", "projectKey")
266       .setParam("repositorySlug", "repo-slug")
267       .executeProtobuf(Projects.CreateWsResponse.class);
268
269     Projects.CreateWsResponse.Project result = response.getProject();
270
271     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
272
273     Collection<BranchDto> branchDtos = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get());
274     List<BranchDto> collect = branchDtos.stream().filter(BranchDto::isMain).toList();
275     String mainBranchName = collect.iterator().next().getKey();
276     assertThat(mainBranchName).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
277   }
278
279   @Test
280   public void handle_givenDefaultBranchNamedDefault_updateDefaultBranchNameToDefault() {
281     BranchesList branchesList = new BranchesList();
282     Branch branch = new Branch("default", true);
283     branchesList.addBranch(branch);
284
285     UserDto user = db.users().insertUser();
286     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
287     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
288     db.almPats().insert(dto -> {
289       dto.setAlmSettingUuid(almSetting.getUuid());
290       dto.setUserUuid(user.getUuid());
291     });
292     Project project = getGsonBBSProject();
293     Repository repo = getGsonBBSRepo(project);
294     when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
295     when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(branchesList);
296
297     Projects.CreateWsResponse response = ws.newRequest()
298       .setParam("almSetting", almSetting.getKey())
299       .setParam("projectKey", "projectKey")
300       .setParam("repositorySlug", "repo-slug")
301       .executeProtobuf(Projects.CreateWsResponse.class);
302
303     Projects.CreateWsResponse.Project result = response.getProject();
304
305     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
306
307     Collection<BranchDto> branchDtos = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get());
308     List<BranchDto> collect = branchDtos.stream().filter(BranchDto::isMain).toList();
309     String mainBranchName = collect.iterator().next().getKey();
310     assertThat(mainBranchName).isEqualTo("default");
311   }
312
313   @Test
314   public void definition() {
315     WebService.Action def = ws.getDef();
316
317     assertThat(def.since()).isEqualTo("8.2");
318     assertThat(def.isPost()).isTrue();
319     assertThat(def.params())
320       .extracting(WebService.Param::key, WebService.Param::isRequired)
321       .containsExactlyInAnyOrder(
322         tuple("almSetting", true),
323         tuple("repositorySlug", true),
324         tuple("projectKey", true));
325   }
326
327   private Repository getGsonBBSRepo(Project project) {
328     Repository bbsResult = new Repository();
329     bbsResult.setProject(project);
330     bbsResult.setSlug(randomAlphanumeric(5));
331     bbsResult.setName(randomAlphanumeric(5));
332     bbsResult.setId(nextLong(100));
333     return bbsResult;
334   }
335
336   private Project getGsonBBSProject() {
337     return new Project()
338       .setKey(randomAlphanumeric(5))
339       .setId(nextLong(100))
340       .setName(randomAlphanumeric(5));
341   }
342
343 }