From: Lukasz Jarocki Date: Fri, 30 Sep 2022 14:34:23 +0000 (+0200) Subject: SONAR-17352 Refactor component keys to not include branch suffix X-Git-Tag: 9.7.0.61563~52 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c85e433a567b8580a1fd5e185b4f5bc73b6e53e6;p=sonarqube.git SONAR-17352 Refactor component keys to not include branch suffix --- diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ConfigurationRepositoryTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ConfigurationRepositoryTest.java index 4d2e84b7895..072eec8a5ba 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ConfigurationRepositoryTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ConfigurationRepositoryTest.java @@ -34,6 +34,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.property.PropertyDto; import org.sonar.server.project.Project; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -121,9 +122,10 @@ public class ConfigurationRepositoryTest { @Test public void branch_settings() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branchDto = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branchDto = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(branchDto.getBranch()); + when(branch.getName()).thenReturn(branchName); analysisMetadataHolder .setProject(Project.from(project)) .setBranch(branch); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepTest.java index c6e5069d34f..b94d867192d 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepTest.java @@ -50,6 +50,7 @@ import org.sonar.server.project.Project; import static java.util.Optional.ofNullable; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; @@ -274,21 +275,22 @@ public class BuildComponentTreeStepTest { @Test public void generate_keys_when_using_existing_branch() { ComponentDto projectDto = dbTester.components().insertPublicProject(); - ComponentDto branchDto = dbTester.components().insertProjectBranch(projectDto); + String branchName = randomAlphanumeric(248); + ComponentDto componentDto = dbTester.components().insertProjectBranch(projectDto, b -> b.setKey(branchName)); Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(branchDto.getBranch()); + when(branch.getName()).thenReturn(branchName); when(branch.isMain()).thenReturn(false); - when(branch.generateKey(any(), any())).thenReturn(branchDto.getKey()); + when(branch.generateKey(any(), any())).thenReturn(componentDto.getKey()); analysisMetadataHolder.setRootComponentRef(ROOT_REF) .setAnalysisDate(ANALYSIS_DATE) .setProject(Project.from(projectDto)) .setBranch(branch); BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder, reportModulesPath); - reportReader.putComponent(component(ROOT_REF, PROJECT, branchDto.getKey())); + reportReader.putComponent(component(ROOT_REF, PROJECT, componentDto.getKey())); underTest.execute(new TestComputationStepContext()); - verifyComponentByRef(ROOT_REF, branchDto.getKey(), analysisMetadataHolder.getProject().getName(), branchDto.uuid()); + verifyComponentByRef(ROOT_REF, componentDto.getKey(), analysisMetadataHolder.getProject().getName(), componentDto.uuid()); } @Test diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepTest.java index 0ba046193a3..52e02ab9b0a 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepTest.java @@ -459,7 +459,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { .setUuid(PROJECT_VIEW_1_UUID) .setName(PROJECT_VIEW_1_NAME) .setDescription("project view description is not persisted") - .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, project.getBranch())); + .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, null)); } private void persistComponents(ComponentDto... componentDtos) { diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListener.java b/server/sonar-ce/src/main/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListener.java index 9c30c2f01f3..942135a776c 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListener.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListener.java @@ -35,6 +35,7 @@ import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeTaskTypes; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.server.notification.NotificationService; @@ -74,10 +75,12 @@ public class ReportAnalysisFailureNotificationExecutionListener implements CeWor if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.class))) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid); + BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, projectDto.branchUuid()) + .orElseThrow(() -> new IllegalStateException("Could not find a branch for component uuid " + projectDto.uuid())); checkScopeAndQualifier(projectDto); CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid()) .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid()))); - ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, error); + ReportAnalysisFailureNotificationBuilder taskFailureNotification = buildNotification(ceActivityDto, projectDto, branchDto, error); ReportAnalysisFailureNotification notification = taskFailureNotificationSerializer.toNotification(taskFailureNotification); notificationService.deliverEmails(singleton(notification)); @@ -98,14 +101,16 @@ public class ReportAnalysisFailureNotificationExecutionListener implements CeWor "Component %s must be a project (scope=%s, qualifier=%s)", projectDto.uuid(), scope, qualifier); } - private ReportAnalysisFailureNotificationBuilder buildNotification(CeActivityDto ceActivityDto, ComponentDto projectDto, @Nullable Throwable error) { + private ReportAnalysisFailureNotificationBuilder buildNotification(CeActivityDto ceActivityDto, ComponentDto projectDto, BranchDto branchDto, + @Nullable Throwable error) { + String projectBranch = branchDto.isMain() ? null : branchDto.getBranchKey(); Long executedAt = ceActivityDto.getExecutedAt(); return new ReportAnalysisFailureNotificationBuilder( new ReportAnalysisFailureNotificationBuilder.Project( projectDto.uuid(), projectDto.getKey(), projectDto.name(), - projectDto.getBranch()), + projectBranch), new ReportAnalysisFailureNotificationBuilder.Task( ceActivityDto.getUuid(), ceActivityDto.getSubmittedAt(), diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListenerTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListenerTest.java index 78815154694..c91133baf29 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListenerTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListenerTest.java @@ -57,6 +57,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; +import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; import static org.sonar.db.component.ComponentTesting.newDirectory; import static org.sonar.db.component.ComponentTesting.newModuleDto; @@ -207,7 +208,7 @@ public class ReportAnalysisFailureNotificationExecutionListenerTest { assertThat(notificationProject.getName()).isEqualTo(project.name()); assertThat(notificationProject.getKey()).isEqualTo(project.getKey()); assertThat(notificationProject.getUuid()).isEqualTo(project.uuid()); - assertThat(notificationProject.getBranchName()).isEqualTo(project.getBranch()); + assertThat(notificationProject.getBranchName()).isEqualTo(DEFAULT_MAIN_BRANCH_NAME); ReportAnalysisFailureNotificationBuilder.Task notificationTask = reportAnalysisFailureNotificationBuilder.getTask(); assertThat(notificationTask.getUuid()).isEqualTo(taskUuid); assertThat(notificationTask.getCreatedAt()).isEqualTo(createdAt); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java index 4a3e4fe3844..d3303761465 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java @@ -47,7 +47,6 @@ public class ComponentDto { public static final String PULL_REQUEST_SEPARATOR = ":PULL_REQUEST:"; private static final Splitter BRANCH_KEY_SPLITTER = Splitter.on(BRANCH_KEY_SEPARATOR); - private static final Splitter PULL_REQUEST_SPLITTER = Splitter.on(PULL_REQUEST_SEPARATOR); public static final String UUID_PATH_SEPARATOR = "."; public static final String UUID_PATH_OF_ROOT = UUID_PATH_SEPARATOR; @@ -184,15 +183,6 @@ public class ComponentDto { return this; } - /** - * @return the key of the branch. It will be null on the main branch and when the component is not on a branch - */ - @CheckForNull - public String getBranch() { - List split = BRANCH_KEY_SPLITTER.splitToList(kee); - return split.size() == 2 ? split.get(1) : null; - } - public String scope() { return scope; } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDtoTest.java index 061dbac8515..3cea2b59323 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDtoTest.java @@ -42,7 +42,6 @@ public class ComponentDtoTest { .setRootUuid("uuid_3"); assertThat(componentDto.getKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java"); - assertThat(componentDto.getBranch()).isNull(); assertThat(componentDto.name()).isEqualTo("RequestContext.java"); assertThat(componentDto.longName()).isEqualTo("org.struts.RequestContext"); assertThat(componentDto.qualifier()).isEqualTo("FIL"); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java index 790e937b628..963b007a7e8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java @@ -22,8 +22,6 @@ package org.sonar.db.component; import com.google.common.base.Strings; import java.util.List; import java.util.Map; -import java.util.function.Predicate; -import org.apache.commons.lang.RandomStringUtils; import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; @@ -35,6 +33,7 @@ import org.sonar.db.DbTester; import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.ComponentKeyNewValue; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; @@ -159,8 +158,8 @@ public class ComponentKeyUpdaterDaoTest { @Test public void updateKey_updates_branches_too() { ComponentDto project = db.components().insertPublicProject(); - String branchKey = RandomStringUtils.randomAlphanumeric(100); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchKey)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); db.components().insertComponent(newFileDto(branch)); db.components().insertComponent(newFileDto(branch)); int prComponentCount = 3; @@ -174,7 +173,7 @@ public class ComponentKeyUpdaterDaoTest { assertThat(dbClient.componentDao().selectByKey(dbSession, oldProjectKey)).isEmpty(); assertThat(dbClient.componentDao().selectByKey(dbSession, newProjectKey)).isPresent(); - assertThat(dbClient.componentDao().selectByKeyAndBranch(dbSession, newProjectKey, branchKey)).isPresent(); + assertThat(dbClient.componentDao().selectByKeyAndBranch(dbSession, newProjectKey, branchName)).isPresent(); assertThat(dbClient.componentDao().selectByBranchUuid(project.uuid(), dbSession)).hasSize(1); assertThat(dbClient.componentDao().selectByBranchUuid(branch.uuid(), dbSession)).hasSize(prComponentCount); @@ -185,7 +184,7 @@ public class ComponentKeyUpdaterDaoTest { @Test public void updateKey_updates_pull_requests_too() { ComponentDto project = db.components().insertPublicProject(); - String pullRequestKey1 = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey1 = randomAlphanumeric(100); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey1)); db.components().insertComponent(newFileDto(pullRequest)); db.components().insertComponent(newFileDto(pullRequest)); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java index 2d17a46c7b3..1cbf60e75fc 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java @@ -28,6 +28,7 @@ import org.sonar.db.project.ProjectDto; import static com.google.common.base.Preconditions.checkArgument; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT; import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR; import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent; @@ -204,7 +205,7 @@ public class ComponentTesting { public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) { boolean isMain = branchComponent.getMainBranchProjectUuid() == null; String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid(); - String key = isMain ? "master" : "branch_" + randomAlphanumeric(248); + String key = isMain ? DEFAULT_MAIN_BRANCH_NAME : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java index 3bb7a74ca8a..adc23e044ac 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java @@ -43,6 +43,7 @@ import org.sonar.server.tester.UserSessionRule; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -541,17 +542,18 @@ public class IssueQueryFactoryTest { @Test public void search_issue_from_branch() { ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); assertThat(underTest.create(new SearchRequest() .setProjects(singletonList(branch.getKey())) - .setBranch(branch.getBranch()))) + .setBranch(branchName))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(project.uuid()), false); assertThat(underTest.create(new SearchRequest() .setComponents(singletonList(branch.getKey())) - .setBranch(branch.getBranch()))) + .setBranch(branchName))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(project.uuid()), false); } @@ -559,26 +561,27 @@ public class IssueQueryFactoryTest { @Test public void search_file_issue_from_branch() { ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); assertThat(underTest.create(new SearchRequest() .setComponents(singletonList(file.getKey())) - .setBranch(branch.getBranch()))) + .setBranch(branchName))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.componentUuids()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(file.uuid()), false); assertThat(underTest.create(new SearchRequest() .setComponents(singletonList(branch.getKey())) .setFiles(singletonList(file.path())) - .setBranch(branch.getBranch()))) + .setBranch(branchName))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.files()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(file.path()), false); assertThat(underTest.create(new SearchRequest() .setProjects(singletonList(branch.getKey())) .setFiles(singletonList(file.path())) - .setBranch(branch.getBranch()))) + .setBranch(branchName))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.files()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(file.path()), false); } @@ -586,12 +589,13 @@ public class IssueQueryFactoryTest { @Test public void search_issue_on_component_only_from_branch() { ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); assertThat(underTest.create(new SearchRequest() .setComponents(singletonList(file.getKey())) - .setBranch(branch.getBranch()) + .setBranch(branchName) .setOnComponentOnly(true))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.componentUuids()), IssueQuery::isMainBranch) .containsOnly(branch.uuid(), singletonList(file.uuid()), false); @@ -600,7 +604,7 @@ public class IssueQueryFactoryTest { @Test public void search_issues_from_main_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + db.components().insertProjectBranch(project); assertThat(underTest.create(new SearchRequest() .setProjects(singletonList(project.getKey())) @@ -634,11 +638,13 @@ public class IssueQueryFactoryTest { @Test public void search_by_application_key_and_branch() { ComponentDto application = db.components().insertPublicProject(c -> c.setQualifier(APP).setKey("app")); - ComponentDto applicationBranch1 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch1")); - ComponentDto applicationBranch2 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch2")); + String branchName1 = "app-branch1"; + String branchName2 = "app-branch2"; + ComponentDto applicationBranch1 = db.components().insertProjectBranch(application, a -> a.setKey(branchName1)); + ComponentDto applicationBranch2 = db.components().insertProjectBranch(application, a -> a.setKey(branchName2)); ComponentDto project1 = db.components().insertPrivateProject(p -> p.setKey("prj1")); ComponentDto project1Branch1 = db.components().insertProjectBranch(project1); - ComponentDto fileOnProject1Branch1 = db.components().insertComponent(newFileDto(project1Branch1)); + db.components().insertComponent(newFileDto(project1Branch1)); ComponentDto project1Branch2 = db.components().insertProjectBranch(project1); ComponentDto project2 = db.components().insertPrivateProject(p -> p.setKey("prj2")); db.components().insertComponents(newProjectCopy(project1Branch1, applicationBranch1)); @@ -648,7 +654,7 @@ public class IssueQueryFactoryTest { // Search on applicationBranch1 assertThat(underTest.create(new SearchRequest() .setComponents(singletonList(applicationBranch1.getKey())) - .setBranch(applicationBranch1.getBranch()))) + .setBranch(branchName1))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch) .containsOnly(applicationBranch1.uuid(), Collections.emptyList(), false); @@ -656,7 +662,7 @@ public class IssueQueryFactoryTest { assertThat(underTest.create(new SearchRequest() .setComponents(singletonList(applicationBranch1.getKey())) .setProjects(singletonList(project1.getKey())) - .setBranch(applicationBranch1.getBranch()))) + .setBranch(branchName1))) .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch) .containsOnly(applicationBranch1.uuid(), singletonList(project1.uuid()), false); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java index 5c3120d2c55..868249ea30d 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java @@ -84,7 +84,7 @@ class ComponentDtoToWsComponent { }); if (QUALIFIERS_WITH_VISIBILITY.contains(dto.qualifier())) { wsComponent.setVisibility(Visibility.getLabel(dto.isPrivate())); - if (Arrays.asList(Qualifiers.PROJECT, Qualifiers.APP).contains(dto.qualifier()) && dto.getBranch() != null && parentProjectDto != null) { + if (Arrays.asList(Qualifiers.PROJECT, Qualifiers.APP).contains(dto.qualifier()) && dto.getMainBranchProjectUuid() != null && parentProjectDto != null) { wsComponent.getTagsBuilder().addAllTags(parentProjectDto.getTags()); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java index 270c704bb68..12deba7ab25 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java @@ -44,10 +44,12 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.permission.GlobalPermission; import org.sonar.db.property.PropertyDto; import org.sonar.server.component.ComponentFinder; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Settings; import org.sonarqube.ws.Settings.ValuesWsResponse; @@ -122,14 +124,23 @@ public class ValuesAction implements SettingsWsAction { try (DbSession dbSession = dbClient.openSession(true)) { ValuesRequest valuesRequest = ValuesRequest.from(request); Optional component = loadComponent(dbSession, valuesRequest); + BranchDto branchDto = loadBranch(dbSession, component); Set keys = loadKeys(valuesRequest); Map keysToDisplayMap = getKeysToDisplayMap(keys); - List settings = loadSettings(dbSession, component, keysToDisplayMap.keySet()); + List settings = loadSettings(dbSession, component, keysToDisplayMap.keySet(), branchDto); return new ValuesResponseBuilder(settings, component, keysToDisplayMap).build(); } } + private BranchDto loadBranch(DbSession dbSession, Optional component) { + if (component.isEmpty()) { + return null; + } + return dbClient.branchDao().selectByUuid(dbSession, component.get().branchUuid()) + .orElseThrow(() -> new NotFoundException("Could not find a branch for component uuid " + component.get().branchUuid())); + } + private Set loadKeys(ValuesRequest valuesRequest) { List keys = valuesRequest.getKeys(); Set result; @@ -156,12 +167,13 @@ public class ValuesAction implements SettingsWsAction { return Optional.of(component); } - private List loadSettings(DbSession dbSession, Optional component, Set keys) { + private List loadSettings(DbSession dbSession, Optional component, Set keys, @Nullable BranchDto branchDto) { // List of settings must be kept in the following orders : default -> global -> component -> branch List settings = new ArrayList<>(); settings.addAll(loadDefaultValues(keys)); settings.addAll(loadGlobalSettings(dbSession, keys)); - if (component.isPresent() && component.get().getBranch() != null && component.get().getMainBranchProjectUuid() != null) { + String branch = getBranchKeySafely(branchDto); + if (component.isPresent() && branch != null && component.get().getMainBranchProjectUuid() != null) { ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, component.get().getMainBranchProjectUuid()); settings.addAll(loadComponentSettings(dbSession, keys, project).values()); } @@ -171,6 +183,13 @@ public class ValuesAction implements SettingsWsAction { .collect(Collectors.toList()); } + private String getBranchKeySafely(@Nullable BranchDto branchDto) { + if(branchDto != null) { + return branchDto.isMain() ? null : branchDto.getBranchKey(); + } + return null; + } + private List loadDefaultValues(Set keys) { return propertyDefinitions.getAll().stream() .filter(definition -> keys.contains(definition.key())) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index d49d0cefbb8..ce3b0d9ad6e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -43,6 +43,7 @@ import org.sonar.api.web.UserRole; import org.sonar.api.web.page.Page; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.measure.LiveMeasureDto; @@ -52,6 +53,7 @@ import org.sonar.db.property.PropertyQuery; import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.project.Visibility; import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualityprofile.QPMeasureData; @@ -59,6 +61,7 @@ import org.sonar.server.qualityprofile.QualityProfile; import org.sonar.server.ui.PageRepository; import org.sonar.server.user.UserSession; +import static java.lang.String.format; import static java.util.Collections.emptySortedSet; import static org.sonar.api.CoreProperties.CORE_ALLOW_PERMISSION_MANAGEMENT_FOR_PROJECT_ADMINS_DEFAULT_VALUE; import static org.sonar.api.CoreProperties.CORE_ALLOW_PERMISSION_MANAGEMENT_FOR_PROJECT_ADMINS_PROPERTY; @@ -121,7 +124,7 @@ public class ComponentAction implements NavigationWsAction { .setSince("5.2") .setChangelog( new Change("8.8", "Deprecated parameter 'componentKey' has been removed. Please use parameter 'component' instead"), - new Change("7.6", String.format("The use of module keys in parameter '%s' is deprecated", PARAM_COMPONENT)), + new Change("7.6", format("The use of module keys in parameter '%s' is deprecated", PARAM_COMPONENT)), new Change("7.3", "The 'almRepoUrl' and 'almId' fields are added"), new Change("6.4", "The 'visibility' field is added")); @@ -157,11 +160,14 @@ public class ComponentAction implements NavigationWsAction { throw insufficientPrivilegesException(); } Optional analysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(session, component.branchUuid()); + BranchDto branchDto = dbClient.branchDao().selectByUuid(session, component.branchUuid()) + .orElseThrow(() -> new NotFoundException(format("Could not find a branch for component uuid " + component.uuid()))); try (JsonWriter json = response.newJsonWriter()) { json.beginObject(); boolean isFavourite = isFavourite(session, rootProject, component); - writeComponent(json, component, analysis.orElse(null), isFavourite); + String branchKey = branchDto.isMain() ? null : branchDto.getBranchKey(); + writeComponent(json, component, analysis.orElse(null), isFavourite, branchKey); writeProfiles(json, session, component); writeQualityGate(json, session, rootProject); if (userSession.hasComponentPermission(ADMIN, component) || @@ -203,15 +209,14 @@ public class ComponentAction implements NavigationWsAction { .endObject(); } - private void writeComponent(JsonWriter json, ComponentDto component, @Nullable SnapshotDto analysis, boolean isFavourite) { + private void writeComponent(JsonWriter json, ComponentDto component, @Nullable SnapshotDto analysis, boolean isFavourite, String branchKey) { json.prop("key", component.getKey()) .prop("id", component.uuid()) .prop("name", component.name()) .prop("description", component.description()) .prop("isFavorite", isFavourite); - String branch = component.getBranch(); - if (branch != null) { - json.prop("branch", branch); + if (branchKey != null) { + json.prop("branch", branchKey); } if (Qualifiers.APP.equals(component.qualifier())) { json.prop("canBrowseAllChildProjects", userSession.hasChildProjectsPermission(USER, component)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java index 811dc5c69fd..505b4487f39 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java @@ -53,6 +53,7 @@ import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -246,13 +247,13 @@ public class MeasureActionTest { ComponentDto project = db.components().insertPublicProject(p -> p.setPrivate(false)); userSession.registerComponents(project); MetricDto metric = createIntMetricAndMeasure(project, BUGS_KEY, 5_000); - - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); db.measures().insertLiveMeasure(branch, metric, m -> m.setValue(10_000d)); TestResponse response = ws.newRequest() .setParam("project", branch.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .setParam("metric", metric.getKey()) .execute(); @@ -262,7 +263,7 @@ public class MeasureActionTest { response = ws.newRequest() .setHeader("If-None-Match", response.getHeader("ETag")) .setParam("project", branch.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .setParam("metric", metric.getKey()) .execute(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java index 5a672956ecd..0e75f785f81 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java @@ -50,6 +50,7 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -216,12 +217,13 @@ public class QualityGateActionTest { userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData(OK.name())); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); db.measures().insertLiveMeasure(branch, metric, m -> m.setData(ERROR.name())); TestResponse response = ws.newRequest() .setParam("project", branch.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .execute(); checkResponse(response, ERROR); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java index b1937071caa..19d6da98d65 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java @@ -418,10 +418,11 @@ public class ActivityActionTest { logInAsSystemAdministrator(); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = "branch1"; + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); SnapshotDto analysis = db.components().insertSnapshot(branch); CeActivityDto activity = insertActivity("T1", project, SUCCESS, analysis); - insertCharacteristic(activity, BRANCH_KEY, branch.getBranch()); + insertCharacteristic(activity, BRANCH_KEY, branchName); insertCharacteristic(activity, BRANCH_TYPE_KEY, BRANCH.name()); ActivityResponse response = ws.newRequest().executeProtobuf(ActivityResponse.class); @@ -429,7 +430,7 @@ public class ActivityActionTest { assertThat(response.getTasksList()) .extracting(Task::getId, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getStatus, Ce.Task::getComponentKey) .containsExactlyInAnyOrder( - tuple("T1", branch.getBranch(), Common.BranchType.BRANCH, Ce.TaskStatus.SUCCESS, branch.getKey())); + tuple("T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.SUCCESS, branch.getKey())); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java index a08844c3293..db659332b2f 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java @@ -48,6 +48,7 @@ import org.sonarqube.ws.Common; import org.sonarqube.ws.MediaTypes; import static java.util.Collections.emptyList; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -172,10 +173,11 @@ public class ComponentActionTest { public void branch_in_activity() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); SnapshotDto analysis = db.components().insertSnapshot(branch); CeActivityDto activity = insertActivity("T1", project, SUCCESS, analysis); - insertCharacteristic(activity, BRANCH_KEY, branch.getBranch()); + insertCharacteristic(activity, BRANCH_KEY, branchName); insertCharacteristic(activity, BRANCH_TYPE_KEY, BRANCH.name()); Ce.ComponentResponse response = ws.newRequest() @@ -185,19 +187,20 @@ public class ComponentActionTest { assertThat(response.getCurrent()) .extracting(Ce.Task::getId, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getStatus, Ce.Task::getComponentKey, Ce.Task::getWarningCount, Ce.Task::getWarningsList) .containsOnly( - "T1", branch.getBranch(), Common.BranchType.BRANCH, Ce.TaskStatus.SUCCESS, project.getKey(), 0, emptyList()); + "T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.SUCCESS, project.getKey(), 0, emptyList()); } @Test public void branch_in_queue_analysis() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); CeQueueDto queue1 = insertQueue("T1", project, IN_PROGRESS); - insertCharacteristic(queue1, BRANCH_KEY, branch.getBranch()); + insertCharacteristic(queue1, BRANCH_KEY, branchName); insertCharacteristic(queue1, BRANCH_TYPE_KEY, BRANCH.name()); CeQueueDto queue2 = insertQueue("T2", project, PENDING); - insertCharacteristic(queue2, BRANCH_KEY, branch.getBranch()); + insertCharacteristic(queue2, BRANCH_KEY, branchName); insertCharacteristic(queue2, BRANCH_TYPE_KEY, BRANCH.name()); Ce.ComponentResponse response = ws.newRequest() @@ -207,8 +210,8 @@ public class ComponentActionTest { assertThat(response.getQueueList()) .extracting(Ce.Task::getId, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getStatus, Ce.Task::getComponentKey, Ce.Task::getWarningCount, Ce.Task::getWarningsList) .containsOnly( - tuple("T1", branch.getBranch(), Common.BranchType.BRANCH, Ce.TaskStatus.IN_PROGRESS, project.getKey(), 0, emptyList()), - tuple("T2", branch.getBranch(), Common.BranchType.BRANCH, Ce.TaskStatus.PENDING, project.getKey(), 0, emptyList())); + tuple("T1", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.IN_PROGRESS, project.getKey(), 0, emptyList()), + tuple("T2", branchName, Common.BranchType.BRANCH, Ce.TaskStatus.PENDING, project.getKey(), 0, emptyList())); } @Test @@ -216,13 +219,15 @@ public class ComponentActionTest { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); insertQueue("Main", project, IN_PROGRESS); + String branchName1 = "Branch1"; ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey("branch1")); CeQueueDto branchQueue1 = insertQueue("Branch1", project, IN_PROGRESS); - insertCharacteristic(branchQueue1, BRANCH_KEY, branch1.getBranch()); + insertCharacteristic(branchQueue1, BRANCH_KEY, branchName1); insertCharacteristic(branchQueue1, BRANCH_TYPE_KEY, BRANCH.name()); ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey("branch2")); + String branchName2 = "Branch2"; CeQueueDto branchQueue2 = insertQueue("Branch2", project, PENDING); - insertCharacteristic(branchQueue2, BRANCH_KEY, branch2.getBranch()); + insertCharacteristic(branchQueue2, BRANCH_KEY, branchName2); insertCharacteristic(branchQueue2, BRANCH_TYPE_KEY, BRANCH.name()); Ce.ComponentResponse response = ws.newRequest() @@ -233,8 +238,8 @@ public class ComponentActionTest { .extracting(Ce.Task::getId, Ce.Task::getComponentKey, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getWarningCount, Ce.Task::getWarningsList) .containsOnly( tuple("Main", project.getKey(), "", Common.BranchType.UNKNOWN_BRANCH_TYPE, 0, emptyList()), - tuple("Branch1", branch1.getKey(), branch1.getBranch(), Common.BranchType.BRANCH, 0, emptyList()), - tuple("Branch2", branch2.getKey(), branch2.getBranch(), Common.BranchType.BRANCH, 0, emptyList())); + tuple("Branch1", branch1.getKey(), branchName1, Common.BranchType.BRANCH, 0, emptyList()), + tuple("Branch2", branch2.getKey(), branchName2, Common.BranchType.BRANCH, 0, emptyList())); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java index 13d79e5507e..6a91596ee94 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java @@ -51,6 +51,7 @@ import org.sonarqube.ws.Ce; import org.sonarqube.ws.Common; import static java.util.Collections.singleton; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.api.web.UserRole.ADMIN; @@ -162,10 +163,11 @@ public class TaskActionTest { logInAsSystemAdministrator(); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); db.components().insertSnapshot(branch); CeActivityDto activity = createAndPersistArchivedTask(project); - insertCharacteristic(activity, BRANCH_KEY, branch.getBranch()); + insertCharacteristic(activity, BRANCH_KEY, branchName); insertCharacteristic(activity, BRANCH_TYPE_KEY, BRANCH.name()); Ce.TaskResponse taskResponse = ws.newRequest() @@ -174,7 +176,7 @@ public class TaskActionTest { assertThat(taskResponse.getTask()) .extracting(Ce.Task::getId, Ce.Task::getBranch, Ce.Task::getBranchType, Ce.Task::getComponentKey) - .containsExactlyInAnyOrder(SOME_TASK_UUID, branch.getBranch(), Common.BranchType.BRANCH, branch.getKey()); + .containsExactlyInAnyOrder(SOME_TASK_UUID, branchName, Common.BranchType.BRANCH, branch.getKey()); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java index 24786cb54af..8b111eedccb 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java @@ -33,6 +33,7 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY; @@ -253,7 +254,8 @@ public class AppActionTest { public void branch() { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn("john").addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto module = db.components().insertComponent(newModuleDto(branch)); ComponentDto directory = db.components().insertComponent(newDirectory(module, "src")); ComponentDto file = db.components().insertComponent(newFileDto(module, directory)); @@ -262,13 +264,13 @@ public class AppActionTest { String result = ws.newRequest() .setParam("component", file.getKey()) - .setParam("branch", file.getBranch()) + .setParam("branch", branchName) .execute() .getInput(); assertJson(result).isSimilarTo("{\n" + " \"key\": \"" + file.getKey() + "\",\n" + - " \"branch\": \"" + file.getBranch() + "\",\n" + + " \"branch\": \"" + branchName + "\",\n" + " \"uuid\": \"" + file.uuid() + "\",\n" + " \"path\": \"" + file.path() + "\",\n" + " \"name\": \"" + file.name() + "\",\n" + @@ -296,18 +298,19 @@ public class AppActionTest { public void component_and_branch_parameters_provided() { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn("john").addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); String result = ws.newRequest() .setParam("component", file.getKey()) - .setParam("branch", file.getBranch()) + .setParam("branch", branchName) .execute() .getInput(); assertJson(result).isSimilarTo("{\n" + " \"key\": \"" + file.getKey() + "\",\n" + - " \"branch\": \"" + file.getBranch() + "\",\n" + + " \"branch\": \"" + branchName + "\",\n" + " \"uuid\": \"" + file.uuid() + "\",\n" + " \"path\": \"" + file.path() + "\",\n" + " \"name\": \"" + file.name() + "\",\n" + diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java index 487625c0e8c..6084ef4f5a7 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java @@ -22,7 +22,6 @@ package org.sonar.server.component.ws; import java.util.Date; import java.util.Optional; import javax.annotation.Nullable; -import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.resources.Qualifiers; @@ -42,6 +41,7 @@ import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Components.Component; import org.sonarqube.ws.Components.ShowWsResponse; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -316,7 +316,7 @@ public class ShowActionTest { ComponentDto portfolio2 = db.components().insertPublicPortfolio(); ComponentDto subview = db.components().insertSubView(portfolio1); - String pullRequestKey1 = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey1 = randomAlphanumeric(100); ComponentDto project1 = db.components().insertPrivateProject(); ComponentDto branch1 = db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey1) .setNeedIssueSync(true)); @@ -325,16 +325,19 @@ public class ShowActionTest { ComponentDto file = db.components().insertComponent(newFileDto(directory)); ComponentDto project2 = db.components().insertPrivateProject(); - ComponentDto branch2 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(true)); - ComponentDto branch3 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(false)); + String branchName2 = randomAlphanumeric(248); + ComponentDto branch2 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(true).setKey(branchName2)); + String branchName3 = randomAlphanumeric(248); + ComponentDto branch3 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(false).setKey(branchName3)); ComponentDto project3 = db.components().insertPrivateProject(); - String pullRequestKey4 = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey4 = randomAlphanumeric(100); ComponentDto branch4 = db.components().insertProjectBranch(project3, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey4).setNeedIssueSync(false)); ComponentDto moduleOfBranch4 = db.components().insertComponent(newModuleDto(branch4)); ComponentDto directoryOfBranch4 = db.components().insertComponent(newDirectory(moduleOfBranch4, "dir")); ComponentDto fileOfBranch4 = db.components().insertComponent(newFileDto(directoryOfBranch4)); - ComponentDto branch5 = db.components().insertProjectBranch(project3, b -> b.setBranchType(BRANCH).setNeedIssueSync(false)); + String branchName5 = randomAlphanumeric(248); + ComponentDto branch5 = db.components().insertProjectBranch(project3, b -> b.setBranchType(BRANCH).setNeedIssueSync(false).setKey(branchName5)); userSession.addProjectPermission(UserRole.USER, project1, project2, project3); userSession.registerComponents(portfolio1, portfolio2, subview, project1, project2, project3); @@ -352,8 +355,8 @@ public class ShowActionTest { assertNeedIssueSyncEqual(pullRequestKey1, null, file, true); assertNeedIssueSyncEqual(null, null, project2, true); - assertNeedIssueSyncEqual(null, branch2.getBranch(), branch2, true); - assertNeedIssueSyncEqual(null, branch3.getBranch(), branch3, true); + assertNeedIssueSyncEqual(null, branchName2, branch2, true); + assertNeedIssueSyncEqual(null, branchName3, branch3, true); // if all branches are synced, need issue sync on project is is set to false assertNeedIssueSyncEqual(null, null, project3, false); @@ -361,7 +364,7 @@ public class ShowActionTest { assertNeedIssueSyncEqual(pullRequestKey4, null, moduleOfBranch4, false); assertNeedIssueSyncEqual(pullRequestKey4, null, directoryOfBranch4, false); assertNeedIssueSyncEqual(pullRequestKey4, null, fileOfBranch4, false); - assertNeedIssueSyncEqual(null, branch5.getBranch(), branch5, false); + assertNeedIssueSyncEqual(null, branchName5, branch5, false); } private void assertNeedIssueSyncEqual(@Nullable String pullRequest, @Nullable String branch, ComponentDto component, boolean needIssueSync) { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java index b334c89bf4b..2bdb91b0310 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java @@ -274,24 +274,26 @@ public class TreeActionTest { @Test public void project_branch_reference_from_application_branch() { ComponentDto application = db.components().insertPrivateProject(c -> c.setQualifier(APP).setKey("app-key")); - ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey("app-branch")); + String appBranchName = "app-branch"; + String projectBranchName = "project-branch"; + ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey(appBranchName)); ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("project-key")); - ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("project-branch")); + ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey(projectBranchName)); ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch) - .setKey(applicationBranch.getKey() + applicationBranch.getBranch() + projectBranch.getKey())); + .setKey(applicationBranch.getKey() + appBranchName + projectBranch.getKey())); logInWithBrowsePermission(application); TreeWsResponse result = ws.newRequest() .setParam(MeasuresWsParameters.PARAM_COMPONENT, applicationBranch.getKey()) - .setParam(MeasuresWsParameters.PARAM_BRANCH, applicationBranch.getBranch()) + .setParam(MeasuresWsParameters.PARAM_BRANCH, appBranchName) .executeProtobuf(TreeWsResponse.class); assertThat(result.getBaseComponent()) .extracting(Component::getKey, Component::getBranch) - .containsExactlyInAnyOrder(applicationBranch.getKey(), applicationBranch.getBranch()); + .containsExactlyInAnyOrder(applicationBranch.getKey(), appBranchName); assertThat(result.getComponentsList()) .extracting(Component::getKey, Component::getBranch, Component::getRefId, Component::getRefKey) - .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), projectBranch.getBranch(), projectBranch.uuid(), project.getKey())); + .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), projectBranchName, projectBranch.uuid(), project.getKey())); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java index 7314720e63c..9455d686943 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java @@ -173,11 +173,13 @@ public class SearchEventsActionNewIssuesTest { when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io"); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(USER, project); - ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey("branch1")); + String branchName1 = "branch1"; + ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName1)); SnapshotDto branch1Analysis = insertAnalysis(branch1, 1_500_000_000_000L); insertIssue(branch1, branch1Analysis); insertIssue(branch1, branch1Analysis); - ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setKey("branch")); + String branchName2 = "branch2"; + ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setKey(branchName2)); SnapshotDto branch2Analysis = insertAnalysis(branch2, 1_300_000_000_000L); insertIssue(branch2, branch2Analysis); issueIndexer.indexAllIssues(); @@ -191,13 +193,13 @@ public class SearchEventsActionNewIssuesTest { assertThat(result.getEventsList()) .extracting(Event::getCategory, Event::getProject, Event::getMessage, Event::getLink, Event::getDate) .containsOnly( - tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s' on branch '%s'", project.name(), branch1.getBranch()), + tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s' on branch '%s'", project.name(), branchName1), format("https://sonarcloud.io/project/issues?id=%s&createdAfter=%s&assignees=%s&resolved=false&branch=%s", branch1.getKey(), encode(formatDateTime(from + 1_000L)), - userSession.getLogin(), branch1.getBranch()), + userSession.getLogin(), branchName1), formatDateTime(branch1Analysis.getCreatedAt())), - tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", project.name(), branch2.getBranch()), + tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", project.name(), branchName2), format("https://sonarcloud.io/project/issues?id=%s&createdAfter=%s&assignees=%s&resolved=false&branch=%s", branch2.getKey(), encode(formatDateTime(from + 1_000L)), - userSession.getLogin(), branch2.getBranch()), + userSession.getLogin(), branchName2), formatDateTime(branch2Analysis.getCreatedAt()))); } @@ -207,7 +209,8 @@ public class SearchEventsActionNewIssuesTest { when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io"); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(USER, project); - ComponentDto nonMainBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey("nonMain")); + String nonMainBranchName = "nonMain"; + ComponentDto nonMainBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(nonMainBranchName)); SnapshotDto nonMainBranchAnalysis = insertAnalysis(nonMainBranch, 1_500_000_000_000L); insertIssue(nonMainBranch, nonMainBranchAnalysis); insertIssue(nonMainBranch, nonMainBranchAnalysis); @@ -226,9 +229,9 @@ public class SearchEventsActionNewIssuesTest { assertThat(result.getEventsList()) .extracting(Event::getCategory, Event::getProject, Event::getMessage, Event::getLink, Event::getDate) .containsOnly( - tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s' on branch '%s'", project.name(), nonMainBranch.getBranch()), + tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s' on branch '%s'", project.name(), nonMainBranchName), format("https://sonarcloud.io/project/issues?id=%s&createdAfter=%s&assignees=%s&resolved=false&branch=%s", nonMainBranch.getKey(), encode(formatDateTime(from + 1_000L)), - userSession.getLogin(), nonMainBranch.getBranch()), + userSession.getLogin(), nonMainBranchName), formatDateTime(nonMainBranchAnalysis.getCreatedAt())), tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on pull request '%s'", project.name(), pullRequestKey), format("https://sonarcloud.io/project/issues?id=%s&createdAfter=%s&assignees=%s&resolved=false&pullRequest=%s", pullRequest.getKey(), diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionQualityGateTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionQualityGateTest.java index 03d72252460..3c60c956a07 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionQualityGateTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionQualityGateTest.java @@ -98,8 +98,9 @@ public class SearchEventsActionQualityGateTest { when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io"); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); - SnapshotDto projectAnalysis = insertSuccessfulActivity(project, 1_500_000_000_000L); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); + insertSuccessfulActivity(project, 1_500_000_000_000L); SnapshotDto branchAnalysis = insertSuccessfulActivity(branch, 1_500_000_000_000L); insertActivity(branch, branchAnalysis, CeActivityDto.Status.SUCCESS); db.events().insertEvent(newQualityGateEvent(branchAnalysis).setDate(branchAnalysis.getCreatedAt()).setName("Failed")); @@ -113,8 +114,8 @@ public class SearchEventsActionQualityGateTest { .extracting(Event::getCategory, Event::getProject, Event::getMessage, Event::getLink) .containsOnly( tuple("QUALITY_GATE", project.getKey(), - format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", project.name(), branch.getBranch()), - format("https://sonarcloud.io/dashboard?id=%s&branch=%s", project.getKey(), branch.getBranch())) + format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", project.name(), branchName), + format("https://sonarcloud.io/dashboard?id=%s&branch=%s", project.getKey(), branchName)) ); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionTest.java index b8e93f8d6af..3b1303a81e2 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionTest.java @@ -128,7 +128,8 @@ public class SearchEventsActionTest { when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io"); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); SnapshotDto projectAnalysis = insertAnalysis(project, 1_500_000_000_000L); db.events().insertEvent(newQualityGateEvent(projectAnalysis).setDate(projectAnalysis.getCreatedAt()).setName("Passed")); insertIssue(project, projectAnalysis); @@ -147,9 +148,9 @@ public class SearchEventsActionTest { .extracting(Event::getCategory, Event::getProject, Event::getMessage) .containsOnly( tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' changed to 'Passed'", project.name())), - tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", project.name(), branch.getBranch())), + tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", project.name(), branchName)), tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s'", project.name())), - tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", project.name(), branch.getBranch()))); + tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", project.name(), branchName))); verify(issueIndexSyncProgressChecker).checkIfAnyComponentsNeedIssueSync(any(), argThat(arg -> arg.contains(project.getKey()))); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java index a5160407b37..ebaa21cf718 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java @@ -30,6 +30,7 @@ import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; import static java.lang.String.format; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.db.component.ComponentTesting.newFileDto; @@ -248,10 +249,11 @@ public class DuplicationsParserTest { @Test public void duplication_on_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file1 = db.components().insertComponent(newFileDto(branch)); ComponentDto file2 = db.components().insertComponent(newFileDto(branch)); - List blocks = parser.parse(db.getSession(), file1, branch.getBranch(), null, + List blocks = parser.parse(db.getSession(), file1, branchName, null, format("\n" + " \n" + " \n" + diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java index f55873152e7..2c0af116c21 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java @@ -20,7 +20,6 @@ package org.sonar.server.duplication.ws; import java.util.function.Function; -import org.apache.commons.lang.RandomStringUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -41,6 +40,7 @@ import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.component.BranchType.PULL_REQUEST; @@ -106,7 +106,8 @@ public class ShowActionTest { public void duplications_by_file_key_and_branch() { ComponentDto project = db.components().insertPrivateProject(); userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.measures().insertLiveMeasure(file, dataMetric, m -> m.setData(format("\n" + " \n" + @@ -117,7 +118,7 @@ public class ShowActionTest { String result = ws.newRequest() .setParam("key", file.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .execute() .getInput(); @@ -151,14 +152,14 @@ public class ShowActionTest { " }\n" + " }\n" + "}", - file.getKey(), file.longName(), file.uuid(), branch.getKey(), branch.uuid(), project.longName(), file.getBranch())); + file.getKey(), file.longName(), file.uuid(), branch.getKey(), branch.uuid(), project.longName(), branchName)); } @Test public void duplications_by_file_key_and_pull_request() { ComponentDto project = db.components().insertPrivateProject(); userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project); - String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey = randomAlphanumeric(100); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file = db.components().insertComponent(newFileDto(pullRequest)); db.measures().insertLiveMeasure(file, dataMetric, m -> m.setData(format("\n" + diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java index d764b0f1fe0..58fb3a06473 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java @@ -23,7 +23,6 @@ import java.io.StringWriter; import java.util.Collections; import java.util.List; import javax.annotation.Nullable; -import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.text.JsonWriter; @@ -33,6 +32,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.test.JsonAssert; import static com.google.common.collect.Lists.newArrayList; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.sonar.db.component.BranchType.PULL_REQUEST; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newModuleDto; @@ -204,7 +204,8 @@ public class ShowResponseBuilderTest { @Test public void write_duplications_on_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file1 = db.components().insertComponent(newFileDto(branch)); ComponentDto file2 = db.components().insertComponent(newFileDto(branch)); List blocks = newArrayList(); @@ -212,7 +213,7 @@ public class ShowResponseBuilderTest { Duplication.newComponent(file1, 57, 12), Duplication.newComponent(file2, 73, 12)))); - test(blocks, branch.getBranch(), null, + test(blocks, branchName, null, "{\n" + " \"duplications\": [\n" + " {\n" + @@ -232,14 +233,14 @@ public class ShowResponseBuilderTest { " \"name\": \"" + file1.longName() + "\",\n" + " \"project\": \"" + branch.getKey() + "\",\n" + " \"projectName\": \"" + branch.longName() + "\",\n" + - " \"branch\": \"" + branch.getBranch() + "\",\n" + + " \"branch\": \"" + branchName + "\",\n" + " },\n" + " \"2\": {\n" + " \"key\": \"" + file2.getKey() + "\",\n" + " \"name\": \"" + file2.longName() + "\",\n" + " \"project\": \"" + branch.getKey() + "\",\n" + " \"projectName\": \"" + branch.longName() + "\",\n" + - " \"branch\": \"" + branch.getBranch() + "\",\n" + + " \"branch\": \"" + branchName + "\",\n" + " }\n" + " }" + "}"); @@ -248,7 +249,7 @@ public class ShowResponseBuilderTest { @Test public void write_duplications_on_pull_request() { ComponentDto project = db.components().insertPublicProject(); - String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey = randomAlphanumeric(100); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file1 = db.components().insertComponent(newFileDto(pullRequest)); ComponentDto file2 = db.components().insertComponent(newFileDto(pullRequest)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java index 8ac8fd6d910..06be33aaaa5 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java @@ -36,7 +36,6 @@ import java.util.function.Consumer; import java.util.stream.IntStream; import java.util.stream.Stream; import javax.annotation.Nullable; -import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -86,6 +85,7 @@ import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.groups.Tuple.tuple; @@ -684,7 +684,7 @@ public class SearchActionTest { ComponentDto project = dbTester.components().insertPublicProject(); userSessionRule.registerComponents(project); indexPermissions(); - ComponentDto branch = dbTester.components().insertProjectBranch(project); + ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey("branch")); ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST).setKey("prKey")); ComponentDto fileProject = dbTester.components().insertComponent(newFileDto(project)); ComponentDto fileBranch = dbTester.components().insertComponent(newFileDto(branch)); @@ -711,7 +711,7 @@ public class SearchActionTest { SearchWsResponse responseProject = newRequest(project) .executeProtobuf(SearchWsResponse.class); - SearchWsResponse responseBranch = newRequest(branch) + SearchWsResponse responseBranch = newRequest(branch, res -> res.setParam(PARAM_BRANCH, "branch")) .executeProtobuf(SearchWsResponse.class); SearchWsResponse responsePR = newRequest(pullRequest, res -> res.setParam(PARAM_PULL_REQUEST, "prKey")) .executeProtobuf(SearchWsResponse.class); @@ -1087,7 +1087,8 @@ public class SearchActionTest { @Test public void returns_branch_field_of_components_of_branch() { ComponentDto project = dbTester.components().insertPublicProject(); - ComponentDto branch = dbTester.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey(branchName)); userSessionRule.registerComponents(project, branch); indexPermissions(); ComponentDto directory = dbTester.components().insertComponent(newDirectory(branch, "donut/acme")); @@ -1098,7 +1099,7 @@ public class SearchActionTest { IssueDto projectHotspot = insertHotspot(branch, branch, rule); indexIssues(); - SearchWsResponse response = newRequest(branch) + SearchWsResponse response = newRequest(branch, r -> r.setParam(PARAM_BRANCH, branchName)) .executeProtobuf(SearchWsResponse.class); assertThat(response.getHotspotsList()) @@ -1109,20 +1110,20 @@ public class SearchActionTest { .containsOnly(project.getKey(), directory.getKey(), file.getKey()); Map componentByKey = response.getComponentsList().stream().collect(uniqueIndex(Component::getKey)); Component actualProject = componentByKey.get(project.getKey()); - assertThat(actualProject.getBranch()).isEqualTo(branch.getBranch()); + assertThat(actualProject.getBranch()).isEqualTo(branchName); assertThat(actualProject.hasPullRequest()).isFalse(); Component actualDirectory = componentByKey.get(directory.getKey()); - assertThat(actualDirectory.getBranch()).isEqualTo(branch.getBranch()); + assertThat(actualDirectory.getBranch()).isEqualTo(branchName); assertThat(actualDirectory.hasPullRequest()).isFalse(); Component actualFile = componentByKey.get(file.getKey()); - assertThat(actualFile.getBranch()).isEqualTo(branch.getBranch()); + assertThat(actualFile.getBranch()).isEqualTo(branchName); assertThat(actualFile.hasPullRequest()).isFalse(); } @Test public void returns_pullRequest_field_of_components_of_pullRequest() { ComponentDto project = dbTester.components().insertPublicProject(); - String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey = randomAlphanumeric(100); ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST) .setKey(pullRequestKey)); userSessionRule.registerComponents(project, pullRequest); @@ -2034,10 +2035,6 @@ public class SearchActionTest { private TestRequest newRequest(ComponentDto project, @Nullable String status, @Nullable String resolution, Consumer consumer) { TestRequest res = actionTester.newRequest() .setParam(PARAM_PROJECT_KEY, project.getKey()); - String branch = project.getBranch(); - if (branch != null) { - res.setParam(PARAM_BRANCH, branch); - } if (status != null) { res.setParam(PARAM_STATUS, status); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java index 9e6d58a003d..e235ffc4398 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java @@ -35,7 +35,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; import javax.annotation.Nullable; -import org.apache.commons.lang.RandomStringUtils; import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; @@ -86,6 +85,7 @@ import org.sonarqube.ws.Common.User; import org.sonarqube.ws.Hotspots; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -911,7 +911,8 @@ public class ShowActionTest { @Test public void returns_branch_but_no_pullRequest_on_component_and_project_on_non_main_branch() { ComponentDto project = dbTester.components().insertPublicProject(); - ComponentDto branch = dbTester.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = dbTester.components().insertComponent(newFileDto(branch)); userSessionRule.registerComponents(project); RuleDto rule = newRule(SECURITY_HOTSPOT); @@ -924,14 +925,14 @@ public class ShowActionTest { Hotspots.ShowWsResponse response = newRequest(hotspot) .executeProtobuf(Hotspots.ShowWsResponse.class); - verifyComponent(response.getProject(), branch, branch.getBranch(), null); - verifyComponent(response.getComponent(), file, branch.getBranch(), null); + verifyComponent(response.getProject(), branch, branchName, null); + verifyComponent(response.getComponent(), file, branchName, null); } @Test public void returns_pullRequest_but_no_branch_on_component_and_project_on_pullRequest() { ComponentDto project = dbTester.components().insertPublicProject(); - String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey = randomAlphanumeric(100); ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file = dbTester.components().insertComponent(newFileDto(pullRequest)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java index a5228b2add3..8b937abf8bf 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java @@ -22,7 +22,6 @@ package org.sonar.server.issue.ws; import java.time.Clock; import java.util.Arrays; import java.util.Date; -import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.resources.Languages; @@ -58,6 +57,7 @@ import org.sonarqube.ws.Issues.Component; import org.sonarqube.ws.Issues.Issue; import org.sonarqube.ws.Issues.SearchWsResponse; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.api.resources.Qualifiers.APP; @@ -364,8 +364,10 @@ public class SearchActionComponentsTest { @Test public void search_by_application_key_and_branch() { ComponentDto application = db.components().insertPrivateProject(c -> c.setQualifier(APP).setKey("app")); - ComponentDto applicationBranch1 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch1")); - ComponentDto applicationBranch2 = db.components().insertProjectBranch(application, a -> a.setKey("app-branch2")); + String appBranch1 = "app-branch1"; + String appBranch2 = "app-branch2"; + ComponentDto applicationBranch1 = db.components().insertProjectBranch(application, a -> a.setKey(appBranch1)); + ComponentDto applicationBranch2 = db.components().insertProjectBranch(application, a -> a.setKey(appBranch2)); ComponentDto project1 = db.components().insertPrivateProject(p -> p.setKey("prj1")); ComponentDto project1Branch1 = db.components().insertProjectBranch(project1); ComponentDto fileOnProject1Branch1 = db.components().insertComponent(newFileDto(project1Branch1)); @@ -391,24 +393,24 @@ public class SearchActionComponentsTest { // All issues on applicationBranch1 assertThat(ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, applicationBranch1.getKey()) - .setParam(PARAM_BRANCH, applicationBranch1.getBranch()) + .setParam(PARAM_BRANCH, appBranch1) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getProject, Issue::getBranch, Issue::hasBranch) .containsExactlyInAnyOrder( - tuple(issueOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getBranch(), true), - tuple(issueOnFileOnProject1Branch1.getKey(), fileOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getBranch(), true), + tuple(issueOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getKey(), appBranch1, true), + tuple(issueOnFileOnProject1Branch1.getKey(), fileOnProject1Branch1.getKey(), project1Branch1.getKey(), appBranch1, true), tuple(issueOnProject2.getKey(), project2.getKey(), project2.getKey(), "", false)); // Issues on project1Branch1 assertThat(ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, applicationBranch1.getKey()) .setParam(PARAM_PROJECTS, project1.getKey()) - .setParam(PARAM_BRANCH, applicationBranch1.getBranch()) + .setParam(PARAM_BRANCH, appBranch1) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) .containsExactlyInAnyOrder( - tuple(issueOnProject1Branch1.getKey(), project1Branch1.getKey(), project1Branch1.getBranch()), - tuple(issueOnFileOnProject1Branch1.getKey(), fileOnProject1Branch1.getKey(), project1Branch1.getBranch())); + tuple(issueOnProject1Branch1.getKey(), project1Branch1.getKey(), appBranch1), + tuple(issueOnFileOnProject1Branch1.getKey(), fileOnProject1Branch1.getKey(), appBranch1)); } @Test @@ -568,7 +570,8 @@ public class SearchActionComponentsTest { ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issue = db.issues().insertIssue(rule, project, file); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); ComponentDto branchFile = db.components().insertComponent(newFileDto(branch)); IssueDto branchIssue = db.issues().insertIssue(rule, branch, branchFile); allowAnyoneOnProjects(project); @@ -577,25 +580,25 @@ public class SearchActionComponentsTest { // On component key + branch assertThat(ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, project.getKey()) - .setParam(PARAM_BRANCH, branch.getBranch()) + .setParam(PARAM_BRANCH, branchName) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) - .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch())); + .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchName)); // On project key + branch assertThat(ws.newRequest() .setParam(PARAM_PROJECTS, project.getKey()) - .setParam(PARAM_BRANCH, branch.getBranch()) + .setParam(PARAM_BRANCH, branchName) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) - .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch())); + .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchName)); // On file key + branch assertThat(ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, branchFile.getKey()) - .setParam(PARAM_BRANCH, branch.getBranch()) + .setParam(PARAM_BRANCH, branchName) .executeProtobuf(SearchWsResponse.class).getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getBranch) - .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchFile.getBranch())); + .containsExactlyInAnyOrder(tuple(branchIssue.getKey(), branchFile.getKey(), branchName)); } @Test @@ -604,7 +607,8 @@ public class SearchActionComponentsTest { ComponentDto project = db.components().insertPrivateProject(); ComponentDto projectFile = db.components().insertComponent(newFileDto(project)); IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH)); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH).setKey(branchName)); ComponentDto branchFile = db.components().insertComponent(newFileDto(branch)); IssueDto branchIssue = db.issues().insertIssue(rule, branch, branchFile); allowAnyoneOnProjects(project); @@ -612,14 +616,14 @@ public class SearchActionComponentsTest { SearchWsResponse result = ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, branch.getKey()) - .setParam(PARAM_BRANCH, branch.getBranch()) + .setParam(PARAM_BRANCH, branchName) .executeProtobuf(SearchWsResponse.class); assertThat(result.getComponentsList()) .extracting(Issues.Component::getKey, Issues.Component::getBranch) .containsExactlyInAnyOrder( - tuple(branchFile.getKey(), branchFile.getBranch()), - tuple(branch.getKey(), branch.getBranch())); + tuple(branchFile.getKey(), branchName), + tuple(branch.getKey(), branchName)); } @Test @@ -629,7 +633,7 @@ public class SearchActionComponentsTest { ComponentDto projectFile = db.components().insertComponent(newFileDto(project)); IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile); - String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + String pullRequestKey = randomAlphanumeric(100); ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto pullRequestFile = db.components().insertComponent(newFileDto(pullRequest)); IssueDto pullRequestIssue = db.issues().insertIssue(rule, pullRequest, pullRequestFile); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java index cf185e9ed75..2aefccfc0fa 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java @@ -167,11 +167,12 @@ public class SearchResponseFormatFormatOperationTest { @Test public void formatOperation_should_add_branch_on_issue() { - componentDto.setKey(randomAlphanumeric(5) + BRANCH_KEY_SEPARATOR + randomAlphanumeric(5)); + String branchName = randomAlphanumeric(5); + componentDto.setKey(randomAlphanumeric(5) + BRANCH_KEY_SEPARATOR + branchName); Operation result = searchResponseFormat.formatOperation(searchResponseData); - assertThat(result.getIssue().getBranch()).isEqualTo(componentDto.getBranch()); + assertThat(result.getIssue().getBranch()).isEqualTo(branchName); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java index 3f65a751b79..0d4f7684181 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java @@ -118,20 +118,21 @@ public class ComponentActionTest { public void branch() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); - SnapshotDto analysis = db.components().insertSnapshot(branch); + String branchName = "my_branch"; + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); + db.components().insertSnapshot(branch); ComponentDto file = db.components().insertComponent(newFileDto(branch)); MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT")); LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d).setVariation(2.0d)); ComponentWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT, file.getKey()) - .setParam(PARAM_BRANCH, file.getBranch()) + .setParam(PARAM_BRANCH, branchName) .setParam(PARAM_METRIC_KEYS, complexity.getKey()) .executeProtobuf(ComponentWsResponse.class); assertThat(response.getComponent()).extracting(Component::getKey, Component::getBranch) - .containsExactlyInAnyOrder(file.getKey(), file.getBranch()); + .containsExactlyInAnyOrder(file.getKey(), branchName); assertThat(response.getComponent().getMeasuresList()) .extracting(Measures.Measure::getMetric, m -> parseDouble(m.getValue())) .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue())); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java index 7f4738caae0..0a9738731fa 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java @@ -535,20 +535,21 @@ public class ComponentTreeActionTest { public void branch() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); - SnapshotDto analysis = db.components().insertSnapshot(branch); + String branchName = "my-branch"; + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); + db.components().insertSnapshot(branch); ComponentDto file = db.components().insertComponent(newFileDto(branch)); MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name())); LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d)); ComponentTreeWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT, file.getKey()) - .setParam(PARAM_BRANCH, file.getBranch()) + .setParam(PARAM_BRANCH, branchName) .setParam(PARAM_METRIC_KEYS, complexity.getKey()) .executeProtobuf(ComponentTreeWsResponse.class); assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getBranch) - .containsExactlyInAnyOrder(file.getKey(), file.getBranch()); + .containsExactlyInAnyOrder(file.getKey(), branchName); assertThat(response.getBaseComponent().getMeasuresList()) .extracting(Measure::getMetric, m -> parseDouble(m.getValue())) .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue())); @@ -723,27 +724,28 @@ public class ComponentTreeActionTest { MetricDto ncloc = insertNclocMetric(); ComponentDto application = db.components().insertPublicProject(c -> c.setQualifier(APP).setKey("app-key")); userSession.registerApplication(application); - ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey("app-branch"), a -> a.setUuid("custom-uuid")); + String branchName = "app-branch"; + ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey(branchName), a -> a.setUuid("custom-uuid")); ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("project-key")); ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("project-branch")); ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch) - .setKey(applicationBranch.getKey() + applicationBranch.getBranch() + projectBranch.getKey())); + .setKey(applicationBranch.getKey() + branchName + projectBranch.getKey())); SnapshotDto applicationBranchAnalysis = db.components().insertSnapshot(applicationBranch); db.measures().insertLiveMeasure(applicationBranch, ncloc, m -> m.setValue(5d)); db.measures().insertLiveMeasure(techProjectBranch, ncloc, m -> m.setValue(1d)); ComponentTreeWsResponse result = ws.newRequest() .setParam(PARAM_COMPONENT, applicationBranch.getKey()) - .setParam(PARAM_BRANCH, applicationBranch.getBranch()) + .setParam(PARAM_BRANCH, branchName) .setParam(PARAM_METRIC_KEYS, ncloc.getKey()) .executeProtobuf(ComponentTreeWsResponse.class); assertThat(result.getBaseComponent()) .extracting(Component::getKey, Component::getBranch) - .containsExactlyInAnyOrder(applicationBranch.getKey(), applicationBranch.getBranch()); + .containsExactlyInAnyOrder(applicationBranch.getKey(), branchName); assertThat(result.getComponentsList()) .extracting(Component::getKey, Component::getBranch, Component::getRefKey) - .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), projectBranch.getBranch(), project.getKey())); + .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), branchName, project.getKey())); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java index 3e0e0235361..25d6eb6c67b 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java @@ -50,6 +50,7 @@ import org.sonar.server.ws.WsActionTester; import static java.util.Optional.ofNullable; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH; @@ -86,11 +87,12 @@ public class UnsetBaselineActionTest { @Test public void does_not_fail_and_has_no_effect_when_there_is_no_baseline_on_non_main_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); SnapshotDto analysis = db.components().insertSnapshot(project); logInAsProjectAdministrator(project); - call(project.getKey(), branch.getBranch()); + call(project.getKey(), branchName); verifyManualBaseline(branch, null); } @@ -112,14 +114,15 @@ public class UnsetBaselineActionTest { @Test public void unset_baseline_when_it_is_set_non_main_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); db.components().insertSnapshot(branch); SnapshotDto branchAnalysis = db.components().insertSnapshot(project); db.newCodePeriods().insert(project.branchUuid(), branch.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, branchAnalysis.getUuid()); logInAsProjectAdministrator(project); - call(project.getKey(), branch.getBranch()); + call(project.getKey(), branchName); verifyManualBaseline(branch, null); } @@ -137,9 +140,10 @@ public class UnsetBaselineActionTest { @Test public void fail_when_user_is_not_admin_on_project_of_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + db.components().insertProjectBranch(project, b -> b.setKey(branchName)); - assertThatThrownBy(() -> call(project.getKey(), branch.getBranch())) + assertThatThrownBy(() -> call(project.getKey(), branchName)) .isInstanceOf(ForbiddenException.class) .hasMessage("Insufficient privileges"); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java index d3bb4ac62f4..f449ca16c31 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java @@ -47,6 +47,7 @@ import org.sonarqube.ws.Qualitygates.ProjectStatusResponse; import org.sonarqube.ws.Qualitygates.ProjectStatusResponse.Status; import static java.lang.String.format; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -215,7 +216,8 @@ public class ProjectStatusActionTest { @Test public void return_live_status_when_branch_is_referenced_by_its_key() throws IOException { ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); dbClient.snapshotDao().insert(dbSession, newAnalysis(branch) .setPeriodMode("last_version") @@ -230,7 +232,7 @@ public class ProjectStatusActionTest { String response = ws.newRequest() .setParam(PARAM_PROJECT_KEY, project.getKey()) - .setParam(PARAM_BRANCH, branch.getBranch()) + .setParam(PARAM_BRANCH, branchName) .execute().getInput(); assertJson(response).isSimilarTo(getClass().getResource("project_status-example.json")); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java index 07654489d7c..9f211a30e43 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java @@ -55,6 +55,7 @@ import org.sonarqube.ws.MediaTypes; import static java.lang.String.format; import static java.net.HttpURLConnection.HTTP_NO_CONTENT; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.api.resources.Qualifiers.PROJECT; @@ -205,7 +206,8 @@ public class ResetActionTest { @Test public void remove_setting_on_branch() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build()); propertyDb.insertProperties(null, branch.name(), null, null, newComponentPropertyDto(branch).setKey("foo").setValue("value")); userSession.logIn().addProjectPermission(ADMIN, project); @@ -214,7 +216,7 @@ public class ResetActionTest { .setMediaType(MediaTypes.PROTOBUF) .setParam("keys", "foo") .setParam("component", branch.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .execute(); assertProjectPropertyDoesNotExist(branch, "foo"); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java index a4982ca69e0..9735c50b7c6 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java @@ -121,7 +121,8 @@ public class LinesActionTest { public void branch() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto() .setUuid(Uuids.createFast()) @@ -135,7 +136,7 @@ public class LinesActionTest { tester.newRequest() .setParam("key", file.getKey()) - .setParam("branch", file.getBranch()) + .setParam("branch", branchName) .execute() .assertJson(getClass(), "show_source.json"); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/RawActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/RawActionTest.java index 62ba5286878..6aa78127f70 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/RawActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/RawActionTest.java @@ -35,6 +35,7 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.component.ComponentTesting.newFileDto; @@ -76,7 +77,8 @@ public class RawActionTest { public void raw_from_branch_file() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.CODEVIEWER, project); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.fileSources().insertFileSource(file, s -> s.setSourceData( Data.newBuilder() @@ -86,7 +88,7 @@ public class RawActionTest { String result = ws.newRequest() .setParam("key", file.getKey()) - .setParam("branch", file.getBranch()) + .setParam("branch", branchName) .execute().getInput(); assertThat(result).isEqualTo("public class HelloWorld {\n}\n"); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java index 999dd32e094..f817b25019c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java @@ -168,7 +168,8 @@ public class ComponentActionTest { @Test public void return_favourite_for_branch() { ComponentDto project = insertProject(); - ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey("feature1").setUuid("xyz")); + String branchName = "feature1"; + componentDbTester.insertProjectBranch(project, b -> b.setKey(branchName).setUuid("xyz")); UserDto user = db.users().insertUser("obiwan"); propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setComponentUuid(project.uuid()).setUserUuid(user.getUuid()), project.getKey(), project.name(), project.qualifier(), user.getLogin()); @@ -177,7 +178,7 @@ public class ComponentActionTest { String json = ws.newRequest() .setParam("component", project.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .execute() .getInput(); @@ -314,7 +315,8 @@ public class ComponentActionTest { public void return_component_info_when_file_on_branch() { db.qualityGates().createDefaultQualityGate(); ComponentDto project = componentDbTester.insertPrivateProject(p -> p.setName("Sample").setKey("sample")); - ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey("feature1")); + String branchName = "feature1"; + ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey(branchName)); userSession.addProjectPermission(UserRole.USER, project); init(); ComponentDto dirDto = componentDbTester.insertComponent(newDirectory(branch, "src")); @@ -324,7 +326,7 @@ public class ComponentActionTest { String json = ws.newRequest() .setParam("component", fileDto.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .execute() .getInput(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java index a87a725128c..932e2e49e47 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java @@ -45,6 +45,7 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Users.CurrentWsResponse; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -172,7 +173,8 @@ public class CurrentActionHomepageTest { @Test public void return_homepage_when_set_to_a_branch() { ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(branch.uuid())); userSessionRule.logIn(user).addProjectPermission(USER, project); @@ -180,7 +182,7 @@ public class CurrentActionHomepageTest { assertThat(response.getHomepage()) .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent, CurrentWsResponse.Homepage::getBranch) - .containsExactly(CurrentWsResponse.HomepageType.PROJECT, branch.getKey(), branch.getBranch()); + .containsExactly(CurrentWsResponse.HomepageType.PROJECT, branch.getKey(), branchName); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SetHomepageActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SetHomepageActionTest.java index 5b53ec3601e..2788a58a474 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SetHomepageActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SetHomepageActionTest.java @@ -33,6 +33,7 @@ import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.server.user.ws.SetHomepageAction.PARAM_COMPONENT; @@ -97,7 +98,8 @@ public class SetHomepageActionTest { @Test public void set_branch_homepage() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); + String branchName = randomAlphanumeric(248); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); UserDto user = db.users().insertUser(); userSession.logIn(user); @@ -105,7 +107,7 @@ public class SetHomepageActionTest { .setMethod("POST") .setParam(PARAM_TYPE, "PROJECT") .setParam("component", branch.getKey()) - .setParam("branch", branch.getBranch()) + .setParam("branch", branchName) .execute(); UserDto actual = db.getDbClient().userDao().selectByLogin(db.getSession(), user.getLogin());