]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17352 Refactor component keys to not include branch suffix
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Fri, 30 Sep 2022 14:34:23 +0000 (16:34 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 12 Oct 2022 20:03:44 +0000 (20:03 +0000)
41 files changed:
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ConfigurationRepositoryTest.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStepTest.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistComponentsStepTest.java
server/sonar-ce/src/main/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListener.java
server/sonar-ce/src/test/java/org/sonar/ce/notification/ReportAnalysisFailureNotificationExecutionListenerTest.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDtoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentTesting.java
server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionQualityGateTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/RawActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionHomepageTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SetHomepageActionTest.java

index 4d2e84b78958ae6cc31ee54d5aa9e9763c78f898..072eec8a5ba97fc9b224def05974ebc6f7eeb63a 100644 (file)
@@ -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);
index c6e5069d34ff5de210a963c792814f7af6dbc9e8..b94d867192d31d7600ae9befe9f9e99f3d3dc167 100644 (file)
@@ -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
index 0ba046193a39f96069abcdc8fd5da3b027d4a8fa..52e02ab9b0aac776d0c1433fc637f4183c42a0b0 100644 (file)
@@ -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) {
index 9c30c2f01f3e7c01c3e680db46431d473ff5961a..942135a776ca805c20657666ce3af05edc40a1e0 100644 (file)
@@ -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(),
index 7881515469414ffad382b6814c1141469671a2da..c91133baf29e6116b6931ca72e12689bbcdf4670 100644 (file)
@@ -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);
index 4a3e4fe38448f1cf749c683b2b85887fa36f5619..d3303761465535f140e3800648af2528ea0ac2dd 100644 (file)
@@ -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<String> split = BRANCH_KEY_SPLITTER.splitToList(kee);
-    return split.size() == 2 ? split.get(1) : null;
-  }
-
   public String scope() {
     return scope;
   }
index 061dbac8515c24587c6364e2b6d67181310fdd5d..3cea2b593230d0b79664a40c471341349a79698c 100644 (file)
@@ -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");
index 790e937b628cee2264b58f110b47695ccb68e358..963b007a7e84547ac9b84d7d786100444bc31503 100644 (file)
@@ -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));
index 2d17a46c7b337e1cae8ca4d1d30b3b34549b8336..1cbf60e75fcb82d156cebec18976cde6d006d032 100644 (file)
@@ -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)
index 3bb7a74ca8a2faa9a1b21d9f37ac3bd038d34d96..adc23e044acc51a273526e4613b1bc625ca40c07 100644 (file)
@@ -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);
   }
index 5c3120d2c559d2997447763abdf3b480af90db59..868249ea30d4346321afc322d9469592d7082f95 100644 (file)
@@ -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());
       }
     }
index 270c704bb682541a8a64cf5f9529b69380cd1e3d..12deba7ab252634be52b7b73b2846ed8ca028997 100644 (file)
@@ -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<ComponentDto> component = loadComponent(dbSession, valuesRequest);
+      BranchDto branchDto = loadBranch(dbSession, component);
 
       Set<String> keys = loadKeys(valuesRequest);
       Map<String, String> keysToDisplayMap = getKeysToDisplayMap(keys);
-      List<Setting> settings = loadSettings(dbSession, component, keysToDisplayMap.keySet());
+      List<Setting> settings = loadSettings(dbSession, component, keysToDisplayMap.keySet(), branchDto);
       return new ValuesResponseBuilder(settings, component, keysToDisplayMap).build();
     }
   }
 
+  private BranchDto loadBranch(DbSession dbSession, Optional<ComponentDto> 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<String> loadKeys(ValuesRequest valuesRequest) {
     List<String> keys = valuesRequest.getKeys();
     Set<String> result;
@@ -156,12 +167,13 @@ public class ValuesAction implements SettingsWsAction {
     return Optional.of(component);
   }
 
-  private List<Setting> loadSettings(DbSession dbSession, Optional<ComponentDto> component, Set<String> keys) {
+  private List<Setting> loadSettings(DbSession dbSession, Optional<ComponentDto> component, Set<String> keys, @Nullable BranchDto branchDto) {
     // List of settings must be kept in the following orders : default -> global -> component -> branch
     List<Setting> 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<Setting> loadDefaultValues(Set<String> keys) {
     return propertyDefinitions.getAll().stream()
       .filter(definition -> keys.contains(definition.key()))
index d49d0cefbb8f28c2519acb0e547e405adec48923..ce3b0d9ad6ebb46ca41151a6142f9d79fc5fcdf9 100644 (file)
@@ -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<SnapshotDto> 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));
index 811dc5c69fdc183c4181a276c6cce02e79edc645..505b4487f398606574bd96ac3f23fa8d282a641b 100644 (file)
@@ -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();
 
index 5a672956ecdbb8ee29610475046f259a4c210c09..0e75f785f819b9f54a4cee518ffbc320b3de8cb5 100644 (file)
@@ -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);
index b1937071caaf7b6ee1a5fcdff88fe2c3a490152a..19d6da98d65123ff9e283acf84141411d565862b 100644 (file)
@@ -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
index a08844c3293e17cb848a2e6c31e4c692252fe553..db659332b2f29354fad863d3549a130b103490b7 100644 (file)
@@ -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
index 13d79e5507e124064ede1c96131cdae103e3e7da..6a91596ee94bdef61cc7d2b2239d54bbcfeee8f5 100644 (file)
@@ -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
index 24786cb54af4237980d031c72859609b4298da44..8b111eedccb72857ad519ca0d3b4d13e22126fee 100644 (file)
@@ -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" +
index 487625c0e8c196dc3f7f83393e1eabe1b0cdf8a2..6084ef4f5a7ee84c8780a95d24181f8e3cc3d48f 100644 (file)
@@ -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) {
index b334c89bf4bc9db5c0c8c5d517107c8a1e707ebc..2bdb91b031079e711d0488816b8209d64c97436c 100644 (file)
@@ -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
index 7314720e63cd1249c1b726df2d8a2d23ea3b9712..9455d6869433f84117b6e03225ce1b9c8b6da47b 100644 (file)
@@ -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(),
index 03d722524601d90d36a26c005b907a6935e85946..3c60c956a07be947b038eb2cf629bf89a8241f6a 100644 (file)
@@ -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))
       );
   }
 
index b8e93f8d6afa84725a4d0a2ea6d33b02a30047aa..3b1303a81e29f7ce1eacd170114ebb434f98c528 100644 (file)
@@ -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())));
   }
 
index a5160407b37f6e77964b16e5fc7eeb1bfed0d34b..ebaa21cf718f3035f7d4583b1efe751f8109c68d 100644 (file)
@@ -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<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, branch.getBranch(), null,
+    List<DuplicationsParser.Block> blocks = parser.parse(db.getSession(), file1, branchName, null,
       format("<duplications>\n" +
         "  <g>\n" +
         "    <b s=\"20\" l=\"5\" r=\"%s\"/>\n" +
index f55873152e7b0be02ad274cba9dd35afcb66ddf6..2c0af116c213e1988e5bcf08908754256d16791f 100644 (file)
@@ -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("<duplications>\n" +
       "  <g>\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("<duplications>\n" +
index d764b0f1fe011a9efa9cb0d918f1ae757a2bc0d6..58fb3a064737f55d26112ff490aa623d56ee66ef 100644 (file)
@@ -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<DuplicationsParser.Block> 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));
index 8ac8fd6d910139887480871b679926b629e51b9e..06be33aaaa5d2a2a716210993707992d9810c031 100644 (file)
@@ -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<String, Component> 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<TestRequest> 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);
     }
index 9e6d58a003de726c90318d70043c53b3834af538..e235ffc4398d261d3759278485cbb1e5eb7231bc 100644 (file)
@@ -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));
index a5228b2add3fadf4a5edd3926a4e30f5c8579b73..8b937abf8bfc7d60bbbfdf7413bdd240e8e59300 100644 (file)
@@ -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);
index cf185e9ed753c364c2a7bbde6e1713c7e5ff09c9..2aefccfc0fae0edef8084003885ce7928e8d48d4 100644 (file)
@@ -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
index 3f65a751b790c98e3142d06b63533981d825970b..0d4f7684181efba0fe86196b4aa4c623c6cc7993 100644 (file)
@@ -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()));
index 7f4738caae0a5b28e2843064af03f100578acace..0a9738731fa0ade82951040d84b6ca15a3ca6a4c 100644 (file)
@@ -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
index 3e0e0235361db3ea27dab89cf94f18b2f24459d0..25d6eb6c67bac8055f1dcd28043a5dcd6f489e98 100644 (file)
@@ -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");
   }
index d3bb4ac62f4214aa73d5c8897ef07caf86abc8b7..f449ca16c313acc89cc9e76549db8089eb10d08a 100644 (file)
@@ -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"));
index 07654489d7cf35cc03b6093ee64ee7a54141445d..9f211a30e432fef2e270ad8563931f892d50efed 100644 (file)
@@ -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");
index a4982ca69e0614b11919ac977d39ffabbc96ff24..9735c50b7c6fb82d40b596219686273f0e22ff5b 100644 (file)
@@ -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");
   }
index 62ba528687867c9609f1c05cd93d1bbd1e35112b..6aa78127f701539152670c23a076ec62e9abd0bc 100644 (file)
@@ -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");
index 999dd32e094e5b20e7ebe02c1bdfd7489f267966..f817b25019c782fa635c410197857b1664ea6c2e 100644 (file)
@@ -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();
 
index a87a725128c745bc7b27634ef63dce7b36b9387d..932e2e49e47bf261e7f1d21514ab7d11a31db807 100644 (file)
@@ -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
index 5b53ec3601eb872a64277e24c589d522cce5c08f..2788a58a47424878bd0725d28c7f208c299dd280 100644 (file)
@@ -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());