3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.almintegration.ws.bitbucketserver;
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.platform.EditionProvider;
38 import org.sonar.core.platform.PlatformEditionProvider;
39 import org.sonar.core.util.SequenceUuidFactory;
40 import org.sonar.db.DbTester;
41 import org.sonar.db.alm.setting.AlmSettingDto;
42 import org.sonar.db.component.BranchDto;
43 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
44 import org.sonar.db.project.CreationMethod;
45 import org.sonar.db.project.ProjectDto;
46 import org.sonar.db.user.UserDto;
47 import org.sonar.server.almintegration.ws.ImportHelper;
48 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
49 import org.sonar.server.component.ComponentUpdater;
50 import org.sonar.server.es.TestIndexers;
51 import org.sonar.server.exceptions.BadRequestException;
52 import org.sonar.server.exceptions.ForbiddenException;
53 import org.sonar.server.exceptions.NotFoundException;
54 import org.sonar.server.exceptions.UnauthorizedException;
55 import org.sonar.server.favorite.FavoriteUpdater;
56 import org.sonar.server.l18n.I18nRule;
57 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
58 import org.sonar.server.permission.PermissionService;
59 import org.sonar.server.permission.PermissionTemplateService;
60 import org.sonar.server.permission.PermissionUpdater;
61 import org.sonar.server.project.DefaultBranchNameResolver;
62 import org.sonar.server.project.ProjectDefaultVisibility;
63 import org.sonar.server.project.Visibility;
64 import org.sonar.server.tester.UserSessionRule;
65 import org.sonar.server.ws.TestRequest;
66 import org.sonar.server.ws.WsActionTester;
67 import org.sonarqube.ws.Projects;
69 import static java.util.Objects.requireNonNull;
70 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
71 import static org.apache.commons.lang.math.JVMRandom.nextLong;
72 import static org.assertj.core.api.Assertions.assertThat;
73 import static org.assertj.core.api.Assertions.assertThatNoException;
74 import static org.assertj.core.api.Assertions.assertThatThrownBy;
75 import static org.assertj.core.api.Assertions.tuple;
76 import static org.mockito.ArgumentMatchers.any;
77 import static org.mockito.Mockito.mock;
78 import static org.mockito.Mockito.verify;
79 import static org.mockito.Mockito.when;
80 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
81 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
82 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
83 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
84 import static org.sonar.db.permission.GlobalPermission.SCAN;
85 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
86 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
88 public class ImportBitbucketServerProjectActionIT {
89 private static final String GENERATED_PROJECT_KEY = "TEST_PROJECT_KEY";
92 public UserSessionRule userSession = UserSessionRule.standalone();
94 public DbTester db = DbTester.create();
96 public final I18nRule i18n = new I18nRule();
98 private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
99 private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
100 private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
101 private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
102 private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
104 private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE,
105 mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(),
106 defaultBranchNameResolver, mock(PermissionUpdater.class), mock(PermissionService.class));
108 private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
109 private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
110 private final WsActionTester ws = new WsActionTester(new ImportBitbucketServerProjectAction(db.getDbClient(), userSession,
111 bitbucketServerRestClient, projectDefaultVisibility, componentUpdater, importHelper, projectKeyGenerator, newCodeDefinitionResolver, defaultBranchNameResolver));
113 private static BranchesList defaultBranchesList;
116 public static void beforeAll() {
117 Branch defaultBranch = new Branch("default", true);
118 defaultBranchesList = new BranchesList(Collections.singletonList(defaultBranch));
122 public void before() {
123 when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
124 when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY);
125 when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
129 public void import_project() {
130 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
131 Project project = getGsonBBSProject();
132 Repository repo = mockBitbucketServerRepo(project);
134 Projects.CreateWsResponse response = ws.newRequest()
135 .setParam("almSetting", almSetting.getKey())
136 .setParam("projectKey", "projectKey")
137 .setParam("repositorySlug", "repo-slug")
138 .executeProtobuf(Projects.CreateWsResponse.class);
140 Projects.CreateWsResponse.Project result = response.getProject();
141 assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
142 assertThat(result.getName()).isEqualTo(repo.getName());
144 ProjectDto projectDto = getProjectDto(result);
145 assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
147 assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto)).isPresent();
148 verify(projectKeyGenerator).generateUniqueProjectKey(requireNonNull(project.getKey()), repo.getSlug());
152 public void importProject_whenCallIsNotFromBrowser_shouldFlagTheProjectAsCreatedFromApi() {
153 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
154 Project project = getGsonBBSProject();
155 mockBitbucketServerRepo(project);
157 Projects.CreateWsResponse response = ws.newRequest()
158 .setParam("almSetting", almSetting.getKey())
159 .setParam("projectKey", "projectKey")
160 .setParam("repositorySlug", "repo-slug")
161 .executeProtobuf(Projects.CreateWsResponse.class);
163 ProjectDto projectDto = getProjectDto(response.getProject());
164 assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
168 public void importProject_whenCallIsFromBrowser_shouldFlagTheProjectAsCreatedFromBrowser() {
169 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
170 userSession.flagSessionAsGui();
171 Project project = getGsonBBSProject();
172 mockBitbucketServerRepo(project);
174 Projects.CreateWsResponse response = ws.newRequest()
175 .setParam("almSetting", almSetting.getKey())
176 .setParam("projectKey", "projectKey")
177 .setParam("repositorySlug", "repo-slug")
178 .executeProtobuf(Projects.CreateWsResponse.class);
180 ProjectDto projectDto = getProjectDto(response.getProject());
181 assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_BROWSER);
185 public void import_project_with_NCD_developer_edition_sets_project_NCD() {
186 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
188 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
189 Project project = getGsonBBSProject();
190 Repository repo = mockBitbucketServerRepo(project);
192 Projects.CreateWsResponse response = ws.newRequest()
193 .setParam("almSetting", almSetting.getKey())
194 .setParam("projectKey", "projectKey")
195 .setParam("repositorySlug", "repo-slug")
196 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
197 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
198 .executeProtobuf(Projects.CreateWsResponse.class);
200 Projects.CreateWsResponse.Project result = response.getProject();
201 assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
202 assertThat(result.getName()).isEqualTo(repo.getName());
204 ProjectDto projectDto = getProjectDto(result);
205 assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto)).isPresent();
206 verify(projectKeyGenerator).generateUniqueProjectKey(requireNonNull(project.getKey()), repo.getSlug());
208 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
211 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
212 .containsExactly(NUMBER_OF_DAYS, "30", null);
216 public void import_project_with_NCD_community_edition_sets_branch_NCD() {
217 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
219 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
220 Project project = getGsonBBSProject();
221 mockBitbucketServerRepo(project);
223 Projects.CreateWsResponse response = ws.newRequest()
224 .setParam("almSetting", almSetting.getKey())
225 .setParam("projectKey", "projectKey")
226 .setParam("repositorySlug", "repo-slug")
227 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
228 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
229 .executeProtobuf(Projects.CreateWsResponse.class);
231 Projects.CreateWsResponse.Project result = response.getProject();
233 ProjectDto projectDto = getProjectDto(result);
234 BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.getUuid()).orElseThrow();
236 String projectUuid = projectDto.getUuid();
237 assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
240 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
241 .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
245 public void import_project_reference_branch_ncd_no_default_branch() {
246 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
247 when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("default-branch");
249 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
250 Project project = getGsonBBSProject();
251 mockBitbucketServerRepo(project, new BranchesList());
253 Projects.CreateWsResponse response = ws.newRequest()
254 .setParam("almSetting", almSetting.getKey())
255 .setParam("projectKey", "projectKey")
256 .setParam("repositorySlug", "repo-slug")
257 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
258 .executeProtobuf(Projects.CreateWsResponse.class);
260 Projects.CreateWsResponse.Project result = response.getProject();
262 ProjectDto projectDto = getProjectDto(result);
264 String projectUuid = projectDto.getUuid();
265 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectUuid))
268 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
269 .containsExactly(REFERENCE_BRANCH, "default-branch");
273 public void import_project_reference_branch_ncd() {
274 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
276 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
277 Project project = getGsonBBSProject();
278 mockBitbucketServerRepo(project);
280 Projects.CreateWsResponse response = ws.newRequest()
281 .setParam("almSetting", almSetting.getKey())
282 .setParam("projectKey", "projectKey")
283 .setParam("repositorySlug", "repo-slug")
284 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
285 .executeProtobuf(Projects.CreateWsResponse.class);
287 Projects.CreateWsResponse.Project result = response.getProject();
289 ProjectDto projectDto = getProjectDto(result);
291 String projectUuid = projectDto.getUuid();
292 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectUuid))
295 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
296 .containsExactly(REFERENCE_BRANCH, "default");
300 public void fail_project_already_exist() {
301 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
302 Project project = getGsonBBSProject();
303 mockBitbucketServerRepo(project);
304 db.components().insertPublicProject(p -> p.setKey(GENERATED_PROJECT_KEY)).getMainBranchComponent();
306 assertThatThrownBy(() -> {
309 .setParam("almSetting", almSetting.getKey())
310 .setParam("projectKey", "projectKey")
311 .setParam("repositorySlug", "repo-slug")
314 .isInstanceOf(BadRequestException.class)
315 .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", GENERATED_PROJECT_KEY, GENERATED_PROJECT_KEY);
319 public void fail_when_not_logged_in() {
320 assertThatThrownBy(() -> {
322 .setParam("almSetting", "sdgfdshfjztutz")
323 .setParam("projectKey", "projectKey")
324 .setParam("repositorySlug", "repo-slug")
327 .isInstanceOf(UnauthorizedException.class);
331 public void fail_when_missing_project_creator_permission() {
332 UserDto user = db.users().insertUser();
333 userSession.logIn(user).addPermission(SCAN);
335 assertThatThrownBy(() -> {
337 .setParam("almSetting", "sdgfdshfjztutz")
338 .setParam("projectKey", "projectKey")
339 .setParam("repositorySlug", "repo-slug")
342 .isInstanceOf(ForbiddenException.class)
343 .hasMessage("Insufficient privileges");
347 public void check_pat_is_missing() {
348 UserDto user = db.users().insertUser();
349 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
350 AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
352 assertThatThrownBy(() -> {
354 .setParam("almSetting", almSetting.getKey())
357 .isInstanceOf(IllegalArgumentException.class)
358 .hasMessage("personal access token for '" + almSetting.getKey() + "' is missing");
362 public void fail_when_no_creation_project_permission() {
363 UserDto user = db.users().insertUser();
364 userSession.logIn(user);
366 assertThatThrownBy(() -> {
368 .setParam("almSetting", "anyvalue")
371 .isInstanceOf(ForbiddenException.class)
372 .hasMessage("Insufficient privileges");
376 public void handle_givenNoDefaultBranchFound_doNotUpdateDefaultBranchName() {
377 BranchesList branchesList = new BranchesList();
378 Branch branch = new Branch("not_a_master", false);
379 branchesList.addBranch(branch);
381 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
382 Project project = getGsonBBSProject();
383 mockBitbucketServerRepo(project, branchesList);
385 Projects.CreateWsResponse response = ws.newRequest()
386 .setParam("almSetting", almSetting.getKey())
387 .setParam("projectKey", "projectKey")
388 .setParam("repositorySlug", "repo-slug")
389 .executeProtobuf(Projects.CreateWsResponse.class);
391 Projects.CreateWsResponse.Project result = response.getProject();
393 ProjectDto projectDto = getProjectDto(result);
394 Collection<BranchDto> branchDtos = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto);
395 List<BranchDto> collect = branchDtos.stream().filter(BranchDto::isMain).toList();
396 String mainBranchName = collect.iterator().next().getKey();
397 assertThat(mainBranchName).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
401 public void handle_givenDefaultBranchNamedDefault_updateDefaultBranchNameToDefault() {
402 BranchesList branchesList = new BranchesList();
403 Branch branch = new Branch("default", true);
404 branchesList.addBranch(branch);
406 AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
407 Project project = getGsonBBSProject();
408 mockBitbucketServerRepo(project, branchesList);
410 Projects.CreateWsResponse response = ws.newRequest()
411 .setParam("almSetting", almSetting.getKey())
412 .setParam("projectKey", "projectKey")
413 .setParam("repositorySlug", "repo-slug")
414 .executeProtobuf(Projects.CreateWsResponse.class);
416 Projects.CreateWsResponse.Project result = response.getProject();
418 ProjectDto projectDto = getProjectDto(result);
419 Collection<BranchDto> branchDtos = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto);
420 List<BranchDto> collect = branchDtos.stream().filter(BranchDto::isMain).toList();
421 String mainBranchName = collect.iterator().next().getKey();
422 assertThat(mainBranchName).isEqualTo("default");
426 public void importProject_whenAlmSettingKeyDoesNotExist_shouldThrow() {
427 UserDto user = db.users().insertUser();
428 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
430 TestRequest request = ws.newRequest()
431 .setParam("almSetting", "unknown")
432 .setParam("projectKey", "projectKey")
433 .setParam("repositorySlug", "repo-slug");
435 assertThatThrownBy(request::execute)
436 .isInstanceOf(NotFoundException.class)
437 .hasMessage("DevOps Platform configuration 'unknown' not found.");
441 public void importProject_whenNoAlmSettingKeyAndNoConfig_shouldThrow() {
442 UserDto user = db.users().insertUser();
443 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
445 TestRequest request = ws.newRequest()
446 .setParam("projectKey", "projectKey")
447 .setParam("repositorySlug", "repo-slug");
449 assertThatThrownBy(request::execute)
450 .isInstanceOf(NotFoundException.class)
451 .hasMessage("There is no BITBUCKET configuration for DevOps Platform. Please add one.");
455 public void importProject_whenNoAlmSettingKeyAndMultipleConfigs_shouldThrow() {
456 UserDto user = db.users().insertUser();
457 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
459 db.almSettings().insertBitbucketAlmSetting();
460 db.almSettings().insertBitbucketAlmSetting();
462 TestRequest request = ws.newRequest()
463 .setParam("projectKey", "projectKey")
464 .setParam("repositorySlug", "repo-slug");
466 assertThatThrownBy(request::execute)
467 .isInstanceOf(IllegalArgumentException.class)
468 .hasMessage("Parameter almSetting is required as there are multiple DevOps Platform configurations.");
472 public void importProject_whenNoAlmSettingKeyAndOnlyOneConfig_shouldImport() {
473 configureUserAndPatAndAlmSettings();
474 Project project = getGsonBBSProject();
475 mockBitbucketServerRepo(project);
477 TestRequest request = ws.newRequest()
478 .setParam("projectKey", "projectKey")
479 .setParam("repositorySlug", "repo-slug");
481 assertThatNoException().isThrownBy(request::execute);
485 public void definition() {
486 WebService.Action def = ws.getDef();
488 assertThat(def.since()).isEqualTo("8.2");
489 assertThat(def.isPost()).isTrue();
490 assertThat(def.params())
491 .extracting(WebService.Param::key, WebService.Param::isRequired)
492 .containsExactlyInAnyOrder(
493 tuple("almSetting", false),
494 tuple("repositorySlug", true),
495 tuple("projectKey", true),
496 tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
497 tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
500 private AlmSettingDto configureUserAndPatAndAlmSettings() {
501 UserDto user = db.users().insertUser();
502 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
503 AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
504 db.almPats().insert(dto -> {
505 dto.setAlmSettingUuid(almSetting.getUuid());
506 dto.setUserUuid(user.getUuid());
511 private Repository mockBitbucketServerRepo(Project project) {
512 return mockBitbucketServerRepo(project, defaultBranchesList);
515 private Repository mockBitbucketServerRepo(Project project, BranchesList branchesList) {
516 Repository bbsResult = new Repository();
517 bbsResult.setProject(project);
518 bbsResult.setSlug(randomAlphanumeric(5));
519 bbsResult.setName(randomAlphanumeric(5));
520 bbsResult.setId(nextLong(100));
521 when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(bbsResult);
522 when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(branchesList);
526 private Project getGsonBBSProject() {
528 .setKey(randomAlphanumeric(5))
529 .setId(nextLong(100))
530 .setName(randomAlphanumeric(5));
533 private ProjectDto getProjectDto(Projects.CreateWsResponse.Project result) {
534 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
535 assertThat(projectDto).isPresent();
536 return projectDto.orElseThrow();