]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19556 Refactor use of DB columns referencing projects or branches for Entities
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Tue, 13 Jun 2023 08:22:02 +0000 (10:22 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 20 Jun 2023 13:10:19 +0000 (13:10 +0000)
27 files changed:
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImplIT.java
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/issue/SourceBranchComponentUuidsIT.java
server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java
server/sonar-db-dao/src/it/java/org/sonar/db/portfolio/PortfolioDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java
server/sonar-webserver-auth/src/it/java/org/sonar/server/user/ServerUserSessionIT.java
server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentFinderIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/TreeActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/developers/ws/SearchEventsActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/BulkChangeActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/IssueUpdaterIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/TagsActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/measure/ws/ComponentTreeActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/CreateEventActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectanalysis/ws/SearchActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/ProjectStatusActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/scannercache/ws/GetActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/CurrentActionHomepageIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/user/ws/SetHomepageActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/developers/ws/SearchEventsAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/TagsAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SetHomepageAction.java

index be5265cecf4491f9c5fd145453e796c4ad76e2a9..657bebd7d8e5e6835eb0f0f7eefb7b18f880d021 100644 (file)
@@ -40,6 +40,7 @@ import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.protobuf.DbProjectBranches;
 import org.sonar.server.project.Project;
 
@@ -58,7 +59,7 @@ public class BranchPersisterImplIT {
   private final static Component MAIN = builder(Component.Type.PROJECT, 1, "PROJECT_KEY").setUuid("PROJECT_UUID").setName("p1").build();
   private final static Component BRANCH1 = builder(Component.Type.PROJECT, 2, "BRANCH_KEY").setUuid("BRANCH_UUID").build();
   private final static Component PR1 = builder(Component.Type.PROJECT, 3, "develop").setUuid("PR_UUID").build();
-  private static final Project PROJECT = new Project(MAIN.getUuid(), MAIN.getKey(), MAIN.getName(), null, Collections.emptyList());
+  private static final Project PROJECT = new Project("PROJECT_UUID", MAIN.getKey(), MAIN.getName(), null, Collections.emptyList());
 
   @Rule
   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
@@ -262,14 +263,15 @@ public class BranchPersisterImplIT {
     String pullRequestId = "pr-123";
 
     // add project and branch in table PROJECTS
-    ComponentDto mainComponent = ComponentTesting.newPrivateProjectDto(MAIN.getUuid()).setKey(MAIN.getKey());
+    ProjectData projectData = dbTester.components().insertPrivateProject(p -> p.setKey(MAIN.getKey()).setUuid(MAIN.getUuid()));
+    ComponentDto mainComponent = projectData.getMainBranchComponent();
     ComponentDto component = ComponentTesting.newBranchComponent(mainComponent,
       new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(PULL_REQUEST));
-    dbTester.components().insertComponents(mainComponent, component);
+    dbTester.components().insertComponents(component);
     // set project in metadata
     treeRootHolder.setRoot(BRANCH1);
     analysisMetadataHolder.setBranch(createBranch(PULL_REQUEST, false, pullRequestId, "mergeBanchUuid"));
-    analysisMetadataHolder.setProject(Project.from(mainComponent));
+    analysisMetadataHolder.setProject(Project.from(projectData.getProjectDto()));
     analysisMetadataHolder.setPullRequestKey(pullRequestId);
 
     underTest.persist(dbTester.getSession());
@@ -282,7 +284,7 @@ public class BranchPersisterImplIT {
     assertThat(branchDto.get().getBranchType()).isEqualTo(PULL_REQUEST);
     assertThat(branchDto.get().getKey()).isEqualTo(pullRequestId);
     assertThat(branchDto.get().getMergeBranchUuid()).isEqualTo("mergeBanchUuid");
-    assertThat(branchDto.get().getProjectUuid()).isEqualTo(MAIN.getUuid());
+    assertThat(branchDto.get().getProjectUuid()).isEqualTo(projectData.projectUuid());
     assertThat(branchDto.get().getPullRequestData()).isEqualTo(DbProjectBranches.PullRequestData.newBuilder()
       .setBranch(pullRequestId)
       .setTarget("mergeBanchUuid")
index 7267983e3fcfcbb024c6016897d03ff4284d022d..0b142e55b322c35354e20d914126a0d901129734 100644 (file)
@@ -62,13 +62,13 @@ public class SourceBranchComponentUuidsIT {
     analysisMetadataHolder.setBranch(branch);
 
     ProjectData projectData = db.components().insertPublicProject();
-    ComponentDto projectDto = projectData.getMainBranchComponent();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
     when(project.getUuid()).thenReturn(projectData.projectUuid());
-    branch1 = db.components().insertProjectBranch(projectDto, b -> b.setKey(BRANCH_KEY));
-    ComponentDto pr1branch = db.components().insertProjectBranch(projectDto, b -> b.setKey(PR_KEY)
+    branch1 = db.components().insertProjectBranch(mainBranch, b -> b.setKey(BRANCH_KEY));
+    ComponentDto pr1branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(PR_KEY)
       .setBranchType(BranchType.PULL_REQUEST)
       .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch(BRANCH_KEY).build())
-      .setMergeBranchUuid(projectDto.uuid()));
+      .setMergeBranchUuid(mainBranch.uuid()));
     branch1File = ComponentTesting.newFileDto(branch1, null, "file").setUuid("branch1File");
     pr1File = ComponentTesting.newFileDto(pr1branch, null, "file").setUuid("file1");
     db.components().insertComponents(branch1File, pr1File);
index 4a6859125c2a1f547dfff629de29955160a3718a..470db8bc36d9b7385d38708902cb95c31f4e10b6 100644 (file)
@@ -66,7 +66,7 @@ public class BranchDaoIT {
   private System2 system2 = new TestSystem2().setNow(NOW);
 
   @Rule
-  public DbTester db = DbTester.create(system2);
+  public DbTester db = DbTester.create(system2, true);
 
   private DbSession dbSession = db.getSession();
   private BranchDao underTest = new BranchDao(system2);
@@ -571,20 +571,22 @@ public class BranchDaoIT {
 
   @Test
   public void selectByProjectUuid() {
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto mainBranch1 = projectData1.getMainBranchComponent();
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto mainBranch2 = projectData2.getMainBranchComponent();
 
-    ComponentDto branch1 = db.components().insertProjectBranch(project1);
-    ComponentDto branch2 = db.components().insertProjectBranch(project1);
-    ComponentDto branch3 = db.components().insertProjectBranch(project2);
-    ComponentDto branch4 = db.components().insertProjectBranch(project2);
+    ComponentDto branch1 = db.components().insertProjectBranch(mainBranch1);
+    ComponentDto branch2 = db.components().insertProjectBranch(mainBranch1);
+    ComponentDto branch3 = db.components().insertProjectBranch(mainBranch2);
+    ComponentDto branch4 = db.components().insertProjectBranch(mainBranch2);
 
-    assertThat(underTest.selectByProject(dbSession, new ProjectDto().setUuid(project1.uuid())))
+    assertThat(underTest.selectByProject(dbSession, new ProjectDto().setUuid(projectData1.projectUuid())))
       .extracting(BranchDto::getUuid)
-      .containsExactlyInAnyOrder(project1.uuid(), branch1.uuid(), branch2.uuid());
-    assertThat(underTest.selectByProject(dbSession, new ProjectDto().setUuid(project2.uuid())))
+      .containsExactlyInAnyOrder(mainBranch1.uuid(), branch1.uuid(), branch2.uuid());
+    assertThat(underTest.selectByProject(dbSession, new ProjectDto().setUuid(projectData2.projectUuid())))
       .extracting(BranchDto::getUuid)
-      .containsExactlyInAnyOrder(project2.uuid(), branch3.uuid(), branch4.uuid());
+      .containsExactlyInAnyOrder(mainBranch2.uuid(), branch3.uuid(), branch4.uuid());
   }
 
   @Test
@@ -602,17 +604,20 @@ public class BranchDaoIT {
 
   @Test
   public void countPrAndBranchByProjectUuid() {
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
     db.components().insertProjectBranch(project1, b -> b.setBranchType(BRANCH).setKey("p1-branch-1"));
     db.components().insertProjectBranch(project1, b -> b.setBranchType(BRANCH).setKey("p1-branch-2"));
     db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setKey("p1-pr-1"));
     db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setKey("p1-pr-2"));
     db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setKey("p1-pr-3"));
 
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto project2 = projectData2.getMainBranchComponent();
     db.components().insertProjectBranch(project2, b -> b.setBranchType(PULL_REQUEST).setKey("p2-pr-1"));
 
-    ComponentDto project3 = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData3 = db.components().insertPrivateProject();
+    ComponentDto project3 = projectData3.getMainBranchComponent();
     db.components().insertProjectBranch(project3, b -> b.setBranchType(BRANCH).setKey("p3-branch-1"));
 
     MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey("unanalyzed_c"));
@@ -626,9 +631,9 @@ public class BranchDaoIT {
       .extracting(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, PrBranchAnalyzedLanguageCountByProjectDto::getBranch,
         PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest)
       .containsExactlyInAnyOrder(
-        tuple(project1.uuid(), 3L, 3L),
-        tuple(project2.uuid(), 1L, 1L),
-        tuple(project3.uuid(), 2L, 0L)
+        tuple(projectData1.projectUuid(), 3L, 3L),
+        tuple(projectData2.projectUuid(), 1L, 1L),
+        tuple(projectData3.projectUuid(), 2L, 0L)
       );
   }
 
@@ -649,7 +654,7 @@ public class BranchDaoIT {
 
     assertThat(underTest.selectProjectUuidsWithIssuesNeedSync(db.getSession(),
       Sets.newHashSet(project1Dto.getUuid(), project2Dto.getUuid(), project3Dto.getUuid(), project4Dto.getUuid())))
-      .containsOnly(project1.uuid());
+      .containsOnly(project1Dto.getUuid());
   }
 
   @Test
@@ -733,20 +738,22 @@ public class BranchDaoIT {
 
   @Test
   public void selectBranchNeedingIssueSyncForProject() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    String uuid = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)).uuid();
-    db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    String uuid = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)).uuid();
+    db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false));
 
-    assertThat(underTest.selectBranchNeedingIssueSyncForProject(dbSession, project.uuid()))
+    assertThat(underTest.selectBranchNeedingIssueSyncForProject(dbSession, projectData.projectUuid()))
       .extracting(BranchDto::getUuid)
       .containsExactly(uuid);
   }
 
   @Test
   public void updateAllNeedIssueSync() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    String uuid1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)).uuid();
-    String uuid2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)).uuid();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    String uuid1 = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)).uuid();
+    String uuid2 = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)).uuid();
 
     underTest.updateAllNeedIssueSync(dbSession);
 
@@ -761,11 +768,12 @@ public class BranchDaoIT {
 
   @Test
   public void updateAllNeedIssueSyncForProject() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    String uuid1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)).uuid();
-    String uuid2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)).uuid();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    String uuid1 = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)).uuid();
+    String uuid2 = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)).uuid();
 
-    underTest.updateAllNeedIssueSyncForProject(dbSession, project.uuid());
+    underTest.updateAllNeedIssueSyncForProject(dbSession, projectData.projectUuid());
 
     Optional<BranchDto> project1 = underTest.selectByUuid(dbSession, uuid1);
     assertThat(project1).isPresent();
@@ -798,19 +806,20 @@ public class BranchDaoIT {
   public void doAnyOfComponentsNeedIssueSync() {
     assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, emptyList())).isFalse();
 
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    ProjectDto projectDto = db.components().getProjectDtoByMainBranch(project1);
-    db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true));
-    BranchDto projectBranch1 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true));
-    BranchDto projectBranch2 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false));
-    db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false));
-    BranchDto pullRequest1 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true));
-    BranchDto pullRequest2 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(false));
-    db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true));
-
-    assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, singletonList(project1.getKey()))).isTrue();
-    assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, singletonList(project2.getKey()))).isFalse();
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto project2 = projectData2.getMainBranchComponent();
+    db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true));
+    BranchDto projectBranch1 = db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true));
+    BranchDto projectBranch2 = db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false));
+    db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false));
+    BranchDto pullRequest1 = db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true));
+    BranchDto pullRequest2 = db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(false));
+    db.components().insertProjectBranch(projectData1.getProjectDto(), b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true));
+
+    assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, singletonList(projectData1.projectKey()))).isTrue();
+    assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, singletonList(projectData2.projectKey()))).isFalse();
   }
 
   @Test
index c46c401c87f8573d1bde0b1f4a53630c6b56ec8a..c8ca7a36a0b39e153011a5c1d3cd34ff3a54e00b 100644 (file)
@@ -195,8 +195,8 @@ public class PortfolioDaoIT {
     db.components().addPortfolioProjectBranch(p1, proj1, "branch1");
     db.components().addPortfolioProjectBranch(p2, proj1, "branch2");
 
-    portfolioDao.addReference(session, "p1", "app1");
-    portfolioDao.addReference(session, "p2", "app1");
+    portfolioDao.addReferenceBranch(session, "p1", "app1", "main1");
+    portfolioDao.addReferenceBranch(session, "p2", "app1", "main2");
     portfolioDao.addReference(session, "p4", "p1");
 
     portfolioDao.delete(session, p1);
@@ -254,7 +254,7 @@ public class PortfolioDaoIT {
 
     portfolioDao.addReference(session, "portfolio1", "portfolio2");
     portfolioDao.addReference(session, "portfolio2", "portfolio3");
-    portfolioDao.addReference(session, "portfolio3", "app1");
+    portfolioDao.addReferenceBranch(session, "portfolio3", "app1", "main1");
 
     assertThat(portfolioDao.selectAllReferencesToPortfolios(session))
       .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid, ReferenceDto::getBranchUuids)
@@ -266,18 +266,19 @@ public class PortfolioDaoIT {
     db.components().insertPrivatePortfolioDto("portfolio1");
     db.components().insertPrivatePortfolioDto("portfolio2");
     db.components().insertPrivatePortfolioDto("portfolio3");
-    ProjectDto app1 = db.components().insertPrivateApplication(p -> p.setKey("app1")).getProjectDto();
+    ProjectData appData1 = db.components().insertPrivateApplication(p -> p.setKey("app1"));
+    ProjectDto app1 = appData1.getProjectDto();
 
     portfolioDao.addReference(session, "portfolio1", "portfolio2");
     portfolioDao.addReference(session, "portfolio2", "portfolio3");
     portfolioDao.addReferenceBranch(session, "portfolio3", app1.getUuid(), "branch1");
-    portfolioDao.addReference(session, "portfolio2", app1.getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio2", app1.getUuid(), appData1.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectAllReferencesToApplications(session))
       .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid, ReferenceDto::getTargetRootUuid, ReferenceDto::getBranchUuids)
       .containsOnly(
         tuple("portfolio3", app1.getUuid(), app1.getUuid(), singleton("branch1")),
-        tuple("portfolio2", app1.getUuid(), app1.getUuid(), singleton(app1.getUuid())));
+        tuple("portfolio2", app1.getUuid(), app1.getUuid(), singleton(appData1.getMainBranchDto().getUuid())));
   }
 
   @Test
@@ -285,13 +286,16 @@ public class PortfolioDaoIT {
     var p1 = db.components().insertPrivatePortfolioDto("portfolio1");
     var p2 = db.components().insertPrivatePortfolioDto("portfolio2", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid()));
     var p3 = db.components().insertPrivatePortfolioDto("portfolio3", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid()));
-    ProjectDto app1 = db.components().insertPrivateApplication(p -> p.setKey("app1")).getProjectDto();
-    ProjectDto app2 = db.components().insertPrivateApplication(p -> p.setKey("app2")).getProjectDto();
-    ProjectDto app3 = db.components().insertPrivateApplication(p -> p.setKey("app3")).getProjectDto();
+    ProjectData appData1 = db.components().insertPrivateApplication(p -> p.setKey("app1"));
+    ProjectDto app1 = appData1.getProjectDto();
+    ProjectData appData2 = db.components().insertPrivateApplication(p -> p.setKey("app2"));
+    ProjectDto app2 = appData2.getProjectDto();
+    ProjectData appData3 = db.components().insertPrivateApplication(p -> p.setKey("app3"));
+    ProjectDto app3 = appData3.getProjectDto();
 
-    portfolioDao.addReference(session, "portfolio1", app1.getUuid());
-    portfolioDao.addReference(session, "portfolio2", app2.getUuid());
-    portfolioDao.addReference(session, "portfolio3", app3.getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio1", app1.getUuid(), appData1.getMainBranchDto().getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio2", app2.getUuid(), appData2.getMainBranchDto().getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio3", app3.getUuid(), appData3.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectApplicationReferenceUuids(session, p1.getUuid()))
       .containsOnly(app1.getUuid());
@@ -308,13 +312,16 @@ public class PortfolioDaoIT {
     var p1 = db.components().insertPrivatePortfolioDto("portfolio1");
     var p2 = db.components().insertPrivatePortfolioDto("portfolio2", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid()));
     var p3 = db.components().insertPrivatePortfolioDto("portfolio3", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid()));
-    ProjectDto app1 = db.components().insertPrivateApplication(p -> p.setKey("app1")).getProjectDto();
-    ProjectDto app2 = db.components().insertPrivateApplication(p -> p.setKey("app2")).getProjectDto();
-    ProjectDto app3 = db.components().insertPrivateApplication(p -> p.setKey("app3")).getProjectDto();
+    ProjectData appData1 = db.components().insertPrivateApplication(p -> p.setKey("app1"));
+    ProjectDto app1 = appData1.getProjectDto();
+    ProjectData appData2 = db.components().insertPrivateApplication(p -> p.setKey("app2"));
+    ProjectDto app2 = appData2.getProjectDto();
+    ProjectData appData3 = db.components().insertPrivateApplication(p -> p.setKey("app3"));
+    ProjectDto app3 = appData3.getProjectDto();
 
-    portfolioDao.addReference(session, "portfolio1", app1.getUuid());
-    portfolioDao.addReference(session, "portfolio2", app2.getUuid());
-    portfolioDao.addReference(session, "portfolio3", app3.getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio1", app1.getUuid(), appData1.getMainBranchDto().getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio2", app2.getUuid(), appData2.getMainBranchDto().getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio3", app3.getUuid(), appData3.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectAllReferencesToApplicationsInHierarchy(session, p1.getUuid()))
       .extracting(ReferenceDto::getTargetUuid)
@@ -342,15 +349,17 @@ public class PortfolioDaoIT {
   @Test
   public void selectAllApplicationProjectsBelongToTheSamePortfolio() {
     var portfolio = db.components().insertPrivatePortfolioDto("portfolio1");
-    var app1 = db.components().insertPrivateApplication(p -> p.setKey("app1")).getProjectDto();
-    var app2 = db.components().insertPrivateApplication(p -> p.setKey("app2")).getProjectDto();
+    ProjectData appData1 = db.components().insertPrivateApplication(p -> p.setKey("app1"));
+    var app1 = appData1.getProjectDto();
+    ProjectData appData2 = db.components().insertPrivateApplication(p -> p.setKey("app2"));
+    var app2 = appData2.getProjectDto();
     var project1 = db.components().insertPrivateProject(p -> p.setKey("project:one").setName("Projet Un")).getProjectDto();
     var project2 = db.components().insertPrivateProject(p -> p.setKey("project:two").setName("Projet Deux")).getProjectDto();
 
     db.components().addApplicationProject(app1, project1);
     db.components().addApplicationProject(app2, project2);
-    db.components().addPortfolioReference(portfolio, app1.getUuid());
-    db.components().addPortfolioReference(portfolio, app2.getUuid());
+    db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app1.getUuid(), appData1.getMainBranchDto().getUuid());
+    db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app2.getUuid(), appData2.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectAllApplicationProjects(session, portfolio.getRootUuid()))
       .extracting(ApplicationProjectDto::getAppUuid, ApplicationProjectDto::getAppKey, ApplicationProjectDto::getProjectUuid)
@@ -362,9 +371,9 @@ public class PortfolioDaoIT {
   @Test
   public void add_and_select_references_by_uuid() {
     assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
-    portfolioDao.addReference(session, "portfolio1", "app1");
-    portfolioDao.addReference(session, "portfolio1", "app2");
-    portfolioDao.addReference(session, "portfolio2", "app3");
+    portfolioDao.addReferenceBranch(session, "portfolio1", "app1", "main1");
+    portfolioDao.addReferenceBranch(session, "portfolio1", "app2", "main2");
+    portfolioDao.addReferenceBranch(session, "portfolio2", "app3", "main3");
     db.commit();
     assertThat(portfolioDao.selectReferenceUuids(session, "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
     assertThat(portfolioDao.selectReferenceUuids(session, "portfolio2")).containsExactlyInAnyOrder("app3");
@@ -379,8 +388,9 @@ public class PortfolioDaoIT {
   @Test
   public void select_reference_to_app_by_key() {
     PortfolioDto portfolio = db.components().insertPrivatePortfolioDto("portfolio1");
-    ProjectDto app1 = db.components().insertPrivateApplication(p -> p.setKey("app1")).getProjectDto();
-    db.components().addPortfolioReference(portfolio, app1.getUuid());
+    ProjectData projectData = db.components().insertPrivateApplication(p -> p.setKey("app1"));
+    ProjectDto app1 = projectData.getProjectDto();
+    db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app1.getUuid(), projectData.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio.getUuid(), app1.getKey()))
       .get()
@@ -397,11 +407,12 @@ public class PortfolioDaoIT {
   @Test
   public void select_reference_to_app_with_branches() {
     PortfolioDto portfolio = db.components().insertPrivatePortfolioDto("portfolio1");
-    ProjectDto app = db.components().insertPrivateApplication(p -> p.setKey("app").setName("app")).getProjectDto();
+    ProjectData projectData = db.components().insertPrivateApplication(p -> p.setKey("app").setName("app"));
+    ProjectDto app = projectData.getProjectDto();
     BranchDto branch1 = db.components().insertProjectBranch(app, b -> b.setExcludeFromPurge(true));
     BranchDto branch2 = db.components().insertProjectBranch(app, b -> b.setExcludeFromPurge(true));
 
-    db.components().addPortfolioReference(portfolio, app.getUuid());
+    db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app.getUuid(), projectData.getMainBranchDto().getUuid());
     db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app.getUuid(), branch1.getUuid());
     db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app.getUuid(), branch2.getUuid());
 
@@ -410,27 +421,20 @@ public class PortfolioDaoIT {
 
     assertThat(appFromDb.get())
       .extracting(ReferenceDto::getTargetKey, ReferenceDto::getTargetName, ReferenceDto::getBranchUuids)
-      .containsExactly("app", "app", Set.of(branch1.getUuid(), branch2.getUuid(), app.getUuid()));
+      .containsExactly("app", "app", Set.of(branch1.getUuid(), branch2.getUuid(), projectData.getMainBranchDto().getUuid()));
 
   }
 
   @Test
   public void select_root_reference_to_app_main_branch() {
     PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
-    ProjectDto app1 = db.components().insertPrivateApplication(p -> p.setKey("app1")).getProjectDto();
-    db.components().addPortfolioReference(portfolio1, app1.getUuid());
+    ProjectData appData1 = db.components().insertPrivateApplication(p -> p.setKey("app1"));
+    ProjectDto app1 = appData1.getProjectDto();
+    db.components().addPortfolioApplicationBranch(portfolio1.getUuid(), app1.getUuid(), appData1.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectRootOfReferencersToMainBranch(db.getSession(), app1.getUuid()))
       .extracting(PortfolioDto::getKey)
       .containsExactly(portfolio1.getKey());
-
-    PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
-    ProjectDto app2 = db.components().insertPrivateApplication(p -> p.setKey("app2")).getProjectDto();
-    db.components().addPortfolioApplicationBranch(portfolio2.getUuid(), app2.getUuid(), app2.getUuid());
-
-    assertThat(portfolioDao.selectRootOfReferencersToMainBranch(db.getSession(), app2.getUuid()))
-      .extracting(PortfolioDto::getKey)
-      .containsExactly(portfolio2.getKey());
   }
 
   @Test
@@ -469,10 +473,11 @@ public class PortfolioDaoIT {
     PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
     PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
 
-    ProjectDto app1 = db.components().insertPrivateApplication("app1").getProjectDto();
+    ProjectData appData1 = db.components().insertPrivateApplication("app1");
+    ProjectDto app1 = appData1.getProjectDto();
     portfolioDao.addReference(session, "portfolio1", "portfolio2");
-    portfolioDao.addReference(session, "portfolio1", app1.getUuid());
-    portfolioDao.addReference(session, "portfolio2", app1.getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio1", app1.getUuid(), appData1.getMainBranchDto().getUuid());
+    portfolioDao.addReferenceBranch(session, "portfolio2", app1.getUuid(), appData1.getMainBranchDto().getUuid());
 
     assertThat(portfolioDao.selectReferencers(session, portfolio2.getUuid()))
       .extracting("uuid").containsOnly("portfolio1");
@@ -556,25 +561,26 @@ public class PortfolioDaoIT {
   @Test
   public void deleteReferenceBranch() {
     PortfolioDto portfolio = db.components().insertPrivatePortfolioDto("portfolio1");
-    ProjectDto app = db.components().insertPrivateApplication(p -> p.setKey("app").setName("app")).getProjectDto();
+    ProjectData appData = db.components().insertPrivateApplication(p -> p.setKey("app").setName("app"));
+    ProjectDto app = appData.getProjectDto();
     BranchDto branch1 = db.components().insertProjectBranch(app, b -> b.setExcludeFromPurge(true));
     BranchDto branch2 = db.components().insertProjectBranch(app, b -> b.setExcludeFromPurge(true));
 
-    db.components().addPortfolioReference(portfolio, app.getUuid());
+    db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app.getUuid(), appData.getMainBranchDto().getUuid());
     db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app.getUuid(), branch1.getUuid());
     db.components().addPortfolioApplicationBranch(portfolio.getUuid(), app.getUuid(), branch2.getUuid());
 
     assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio.getUuid(), app.getKey()))
       .isPresent()
       .map(ReferenceDto::getBranchUuids)
-      .contains(Set.of(branch1.getUuid(), branch2.getUuid(), app.getUuid()));
+      .contains(Set.of(branch1.getUuid(), branch2.getUuid(), appData.getMainBranchDto().getUuid()));
 
     portfolioDao.deleteReferenceBranch(db.getSession(), portfolio.getUuid(), app.getUuid(), branch1.getUuid());
 
     assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio.getUuid(), app.getKey()))
       .isPresent()
       .map(ReferenceDto::getBranchUuids)
-      .contains(Set.of(branch2.getUuid(), app.getUuid()));
+      .contains(Set.of(branch2.getUuid(), appData.getMainBranchDto().getUuid()));
 
   }
 
index f2581c66438c194514ccc99418bf3d7bb469582d..2ab3b5aed1cb710ec9ae6742a4d62ede41a7d71a 100644 (file)
@@ -139,7 +139,7 @@ public class PortfolioDao implements Dao {
   public void addReference(DbSession dbSession, String portfolioUuid, String referenceUuid) {
     PortfolioDto portfolio = mapper(dbSession).selectByUuid(referenceUuid);
     if (portfolio == null) {
-      mapper(dbSession).insertReference(uuidFactory.create(), portfolioUuid, referenceUuid, referenceUuid, system2.now());
+      throw new IllegalArgumentException("Reference must be a portfolio");
     } else {
       mapper(dbSession).insertReference(uuidFactory.create(), portfolioUuid, referenceUuid, null, system2.now());
     }
index d38e6645f53c0411441ad7a2916e3dded7614137..78da4219aef5cda6fa83b3b3568d3ee3c3b79231 100644 (file)
     select
     case when exists
     (
-    select pb.project_uuid, pb.need_issue_sync from project_branches pb join components c on pb.project_uuid = c.branch_uuid
+    select pb.project_uuid, pb.need_issue_sync from project_branches pb
+      join components c on pb.uuid = c.branch_uuid
     where c.kee in
     <foreach collection="componentKeys" open="(" close=")" item="componentKey" separator=",">
       #{componentKey,jdbcType=VARCHAR}
index de2be68295bf6dabb3de8a154503c17670da2825..2071d83028c91ed767a6ef12d868cbf6c6a2b629 100644 (file)
     FROM portfolios c
     INNER JOIN portfolio_references pr on pr.portfolio_uuid = c.uuid
     INNER JOIN portfolios p on p.uuid = c.root_uuid
+    LEFT JOIN project_branches pb ON pr.branch_uuid = pb.uuid
     where
     pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
     AND
     (pr.branch_uuid is null
     OR
-    pr.branch_uuid=#{referenceUuid,jdbcType=VARCHAR}
+    pb.is_main = ${_true}
     )
   </select>
 
index c490bb3beae01c25de63df45ac507e3b1802e030..4a8d78515a69e6d1fa76a5853ffed540caed47d6 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.component;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import java.util.function.Consumer;
 import javax.annotation.Nullable;
 import org.sonar.api.resources.Qualifiers;
@@ -29,6 +30,7 @@ import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.db.portfolio.PortfolioDto;
 import org.sonar.db.portfolio.PortfolioProjectDto;
 import org.sonar.db.project.ProjectDto;
@@ -36,6 +38,9 @@ import org.sonar.db.project.ProjectDto;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Arrays.asList;
+import static org.sonar.api.resources.Qualifiers.APP;
+import static org.sonar.api.resources.Qualifiers.SUBVIEW;
+import static org.sonar.api.resources.Qualifiers.VIEW;
 import static org.sonar.db.component.BranchType.BRANCH;
 import static org.sonar.db.portfolio.PortfolioDto.SelectionMode.NONE;
 
@@ -157,6 +162,7 @@ public class ComponentDbTester {
   public ProjectData insertPrivateProject(Consumer<BranchDto> branchPopulator, Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
     return insertPrivateProjectWithCustomBranch(branchPopulator, componentDtoPopulator, projectDtoPopulator);
   }
+
   public final ComponentDto insertFile(BranchDto branch) {
     ComponentDto projectComponent = getComponentDto(branch);
     return insertComponent(ComponentTesting.newFileDto(projectComponent));
@@ -297,7 +303,17 @@ public class ComponentDbTester {
 
   public void addPortfolioReference(String portfolioUuid, String... referencerUuids) {
     for (String uuid : referencerUuids) {
-      dbClient.portfolioDao().addReference(dbSession, portfolioUuid, uuid);
+      EntityDto entityDto = dbClient.entityDao().selectByUuid(dbSession, uuid)
+          .orElseThrow();
+      switch (entityDto.getQualifier()) {
+        case APP -> {
+          BranchDto appMainBranch = dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, entityDto.getUuid())
+            .orElseThrow();
+          dbClient.portfolioDao().addReferenceBranch(dbSession, portfolioUuid, uuid, appMainBranch.getUuid());
+        }
+        case VIEW, SUBVIEW -> dbClient.portfolioDao().addReference(dbSession, portfolioUuid, uuid);
+        default -> throw new IllegalStateException("Unexpected value: " + entityDto.getQualifier());
+      }
     }
     db.commit();
   }
index 65137bdd543d5eb5d35d377e66136929e1350386..38a07f8df2b303363e96f2dd7e76c3568a3ac100 100644 (file)
@@ -46,6 +46,10 @@ public class Project {
     this.tags = tags;
   }
 
+  /**
+   * Should use {@link org.sonar.server.project.Project#fromProjectDtoWithTags(org.sonar.db.project.ProjectDto)} instead
+   */
+  @Deprecated
   public static Project from(ComponentDto project) {
     return new Project(project.uuid(), project.getKey(), project.name(), project.description(), emptyList());
   }
index b7f46f28dd2cab53521c482e91ae126c843ccf88..dcbf92665f9fb0b2210c08a9ba3d82814762f9c8 100644 (file)
@@ -645,7 +645,7 @@ public class ServerUserSessionIT {
     // Add private project6 to private app2
     db.components().addApplicationProject(app2, project6);
     var copyProject6 = db.components().insertComponent(newProjectCopy(project6, app2));
-    db.components().addPortfolioReference(portfolio, app2.getMainBranchComponent().uuid());
+    db.components().addPortfolioReference(portfolio, app2.getProjectDto().getUuid());
 
     assertThat(underTest.keepAuthorizedComponents(UserRole.ADMIN, List.of(portfolio))).isEmpty();
     assertThat(underTest.keepAuthorizedComponents(UserRole.USER, List.of(portfolio))).containsExactly(portfolio);
index a0cac5d1f75225d2cfddaf96951997ebf22121b1..b5a9a32cf0a1dba0a264da1c194940f5ff6e6b38 100644 (file)
@@ -215,6 +215,7 @@ public class UserSessionRule implements TestRule, UserSession {
     return this;
   }
 
+
   public UserSessionRule addPortfolioPermission(String portfolioPermission, PortfolioDto... portfolioDto) {
     ensureAbstractMockUserSession().addPortfolioPermission(portfolioPermission, portfolioDto);
     return this;
index 04441479d7a1103101be583ebdf87cbcfe31d9d4..26a7beb3916308658b565a3f0101501d29a9ee5e 100644 (file)
@@ -42,7 +42,7 @@ import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
 public class ComponentFinderIT {
 
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final DbSession dbSession = db.getSession();
   private final ComponentFinder underTest = TestComponentFinder.from(db);
index bae92500f1c3e4a8593ce5fb5b2428939d3d57d2..3d91ecee72f1a204b1d92cc6d8cf4471f46b4092 100644 (file)
@@ -83,7 +83,7 @@ public class TreeActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
   private final DbClient dbClient = db.getDbClient();
   private final ResourceTypes defaultResourceTypes = new ResourceTypes(new ResourceTypeTree[]{DefaultResourceTypes.get()});
   private final ResourceTypesRule resourceTypes = new ResourceTypesRule()
@@ -121,11 +121,11 @@ public class TreeActionIT {
 
   @Test
   public void json_example() throws IOException {
-    ProjectDto project = initJsonExampleComponents();
+    ProjectData project = initJsonExampleComponents();
     logInWithBrowsePermission(project);
 
     String response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, project.projectKey())
       .execute()
       .getInput();
 
@@ -149,7 +149,7 @@ public class TreeActionIT {
     db.components().insertComponent(directory);
     db.components().insertComponent(newFileDto(projectMainBranch, directory, 10));
     db.commit();
-    logInWithBrowsePermission(projectData.getProjectDto());
+    logInWithBrowsePermission(projectData);
 
     TreeWsResponse response = ws.newRequest()
       .setParam(PARAM_STRATEGY, "children")
@@ -181,7 +181,7 @@ public class TreeActionIT {
     db.components().insertComponent(directory);
     db.components().insertComponent(newFileDto(module, directory, 1));
     db.commit();
-    logInWithBrowsePermission(projectData.getProjectDto());
+    logInWithBrowsePermission(projectData);
 
     TreeWsResponse response = ws.newRequest()
       .setParam(PARAM_STRATEGY, "all")
@@ -206,7 +206,7 @@ public class TreeActionIT {
     db.components().insertComponent(newFileDto(projectMainBranch, 1));
     db.components().insertComponent(newFileDto(projectMainBranch, 2));
     db.commit();
-    logInWithBrowsePermission(projectData.getProjectDto());
+    logInWithBrowsePermission(projectData);
 
     TreeWsResponse response = ws.newRequest()
       .setParam(PARAM_STRATEGY, "all")
@@ -228,7 +228,7 @@ public class TreeActionIT {
     db.components().insertComponent(directory);
     db.components().insertComponent(newFileDto(mainBranch, directory, 3));
     db.commit();
-    logInWithBrowsePermission(projectData.getProjectDto());
+    logInWithBrowsePermission(projectData);
 
     TreeWsResponse response = ws.newRequest()
       .setParam(PARAM_STRATEGY, "leaves")
@@ -250,7 +250,7 @@ public class TreeActionIT {
     db.components().insertComponent(newFileDto(mainBranch, 2));
     db.components().insertComponent(newDirectory(mainBranch, "path/directory/", "directory-uuid-1"));
     db.commit();
-    logInWithBrowsePermission(projectData.getProjectDto());
+    logInWithBrowsePermission(projectData);
 
     TreeWsResponse response = ws.newRequest()
       .setParam(PARAM_STRATEGY, "all")
@@ -321,7 +321,7 @@ public class TreeActionIT {
     ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch)
       .setKey(applicationBranch.getKey() + project.getKey()).setMainBranchProjectUuid(application.getUuid()));
 
-    logInWithBrowsePermission(application);
+    logInWithBrowsePermission(applicationData);
     userSession.addProjectBranchMapping(application.getUuid(), applicationBranch);
 
     TreeWsResponse result = ws.newRequest()
@@ -339,8 +339,9 @@ public class TreeActionIT {
 
   @Test
   public void response_is_empty_on_provisioned_projects() {
-    ProjectDto project = db.components().insertPrivateProject("project-uuid").getProjectDto();
-    logInWithBrowsePermission(project);
+    ProjectData projectData = db.components().insertPrivateProject("project-uuid");
+    ProjectDto project = projectData.getProjectDto();
+    logInWithBrowsePermission(projectData);
 
     TreeWsResponse response = ws.newRequest()
       .setParam(PARAM_COMPONENT, project.getKey()).executeProtobuf(TreeWsResponse.class);
@@ -511,7 +512,7 @@ public class TreeActionIT {
 
     ProjectData projectData = db.components().insertPrivateProject(p->p.setKey("file-key").setEnabled(false));
     db.components().insertSnapshot(projectData.getMainBranchComponent());
-    logInWithBrowsePermission(projectData.getProjectDto());
+    logInWithBrowsePermission(projectData);
 
     TestRequest request = ws.newRequest()
       .setParam(PARAM_COMPONENT, "file-key");
@@ -553,7 +554,7 @@ public class TreeActionIT {
     return newFileDto(moduleOrProject, null, i);
   }
 
-  private ProjectDto initJsonExampleComponents() throws IOException {
+  private ProjectData initJsonExampleComponents() throws IOException {
     ProjectData projectData = db.components().insertPrivateProject(c -> c.setUuid("MY_PROJECT_ID")
         .setDescription("MY_PROJECT_DESCRIPTION")
         .setKey("MY_PROJECT_KEY")
@@ -581,7 +582,7 @@ public class TreeActionIT {
         .setCreatedAt(now));
     }
     db.commit();
-    return projectData.getProjectDto();
+    return projectData;
   }
 
   @CheckForNull
@@ -590,8 +591,9 @@ public class TreeActionIT {
     return jsonElement == null ? null : jsonElement.getAsString();
   }
 
-  private void logInWithBrowsePermission(ProjectDto project) {
-    userSession.logIn().addProjectPermission(UserRole.USER, project);
+  private void logInWithBrowsePermission(ProjectData project) {
+    userSession.logIn().addProjectPermission(UserRole.USER, project.getProjectDto())
+      .addProjectBranchMapping(project.projectUuid(), project.getMainBranchComponent());
   }
 
   @Test
@@ -599,7 +601,7 @@ public class TreeActionIT {
     ProjectData project = db.components().insertPrivateProject(p->p.setUuid("project-uuid").setBranchUuid("project-uuid"));
     db.components().insertSnapshot(project.getMainBranchComponent());
     db.commit();
-    logInWithBrowsePermission(project.getProjectDto());
+    logInWithBrowsePermission(project);
 
     TestRequest testRequest = ws.newRequest()
       .setParam(PARAM_QUALIFIERS, "BRC")
index d9196f9955a96b84b9996b59694b44c5bd2c4c72..0ac6f5d7c21a914db820fc46eb17748b7a2b97cf 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeQueueDto;
 import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.event.EventDto;
 import org.sonar.server.es.EsTester;
@@ -75,7 +76,7 @@ public class SearchEventsActionIT {
     .toArray(RuleType[]::new);
 
   @Rule
-  public DbTester db = DbTester.create();
+  public DbTester db = DbTester.create(true);
   @Rule
   public EsTester es = EsTester.create();
   @Rule
@@ -107,16 +108,17 @@ public class SearchEventsActionIT {
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertPrivateProject(p -> p.setName("My Project").setKey(KeyExamples.KEY_PROJECT_EXAMPLE_001)).getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto analysis = insertAnalysis(project, 1_500_000_000_000L);
+    ProjectData projectData = db.components().insertPrivateProject(p -> p.setName("My Project").setKey(KeyExamples.KEY_PROJECT_EXAMPLE_001));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    SnapshotDto analysis = insertAnalysis(mainBranch, 1_500_000_000_000L);
     EventDto e1 = db.events().insertEvent(newQualityGateEvent(analysis).setName("Failed").setDate(analysis.getCreatedAt()));
-    IntStream.range(0, 15).forEach(x -> insertIssue(project, analysis));
+    IntStream.range(0, 15).forEach(x -> insertIssue(mainBranch, analysis));
     issueIndexer.indexAllIssues();
     when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io");
 
     String result = ws.newRequest()
-      .setParam(PARAM_PROJECTS, project.getKey())
+      .setParam(PARAM_PROJECTS, mainBranch.getKey())
       .setParam(PARAM_FROM, formatDateTime(analysis.getCreatedAt() - 1_000L))
       .execute().getInput();
 
@@ -126,48 +128,50 @@ public class SearchEventsActionIT {
   @Test
   public void events() {
     when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io");
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
     String branchName = randomAlphanumeric(248);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
-    SnapshotDto projectAnalysis = insertAnalysis(project, 1_500_000_000_000L);
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
+    SnapshotDto projectAnalysis = insertAnalysis(mainBranch, 1_500_000_000_000L);
     db.events().insertEvent(newQualityGateEvent(projectAnalysis).setDate(projectAnalysis.getCreatedAt()).setName("Passed"));
-    insertIssue(project, projectAnalysis);
-    insertIssue(project, projectAnalysis);
-    SnapshotDto branchAnalysis = insertAnalysis(branch, project.uuid(), 1_501_000_000_000L);
+    insertIssue(mainBranch, projectAnalysis);
+    insertIssue(mainBranch, projectAnalysis);
+    SnapshotDto branchAnalysis = insertAnalysis(branch, mainBranch.uuid(), 1_501_000_000_000L);
     db.events().insertEvent(newQualityGateEvent(branchAnalysis).setDate(branchAnalysis.getCreatedAt()).setName("Failed"));
     insertIssue(branch, branchAnalysis);
     issueIndexer.indexAllIssues();
 
     SearchEventsWsResponse result = ws.newRequest()
-      .setParam(PARAM_PROJECTS, project.getKey())
+      .setParam(PARAM_PROJECTS, mainBranch.getKey())
       .setParam(PARAM_FROM, formatDateTime(1_499_000_000_000L))
       .executeProtobuf(SearchEventsWsResponse.class);
 
     assertThat(result.getEventsList())
       .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(), 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(), branchName)));
-    verify(issueIndexSyncProgressChecker).checkIfAnyComponentsNeedIssueSync(any(), argThat(arg -> arg.contains(project.getKey())));
+        tuple("QUALITY_GATE", mainBranch.getKey(), format("Quality Gate status of project '%s' changed to 'Passed'", mainBranch.name())),
+        tuple("QUALITY_GATE", mainBranch.getKey(), format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", mainBranch.name(), branchName)),
+        tuple("NEW_ISSUES", mainBranch.getKey(), format("You have 2 new issues on project '%s'", mainBranch.name())),
+        tuple("NEW_ISSUES", mainBranch.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", mainBranch.name(), branchName)));
+    verify(issueIndexSyncProgressChecker).checkIfAnyComponentsNeedIssueSync(any(), argThat(arg -> arg.contains(mainBranch.getKey())));
   }
 
   @Test
   public void does_not_return_old_events() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto analysis = insertAnalysis(project, 1_500_000_000_000L);
-    insertIssue(project, analysis);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData.getProjectDto());
+    SnapshotDto analysis = insertAnalysis(mainBranch, 1_500_000_000_000L);
+    insertIssue(mainBranch, analysis);
     db.events().insertEvent(newQualityGateEvent(analysis).setDate(analysis.getCreatedAt()).setName("Passed"));
-    SnapshotDto oldAnalysis = insertAnalysis(project, 1_400_000_000_000L);
-    insertIssue(project, oldAnalysis);
+    SnapshotDto oldAnalysis = insertAnalysis(mainBranch, 1_400_000_000_000L);
+    insertIssue(mainBranch, oldAnalysis);
     db.events().insertEvent(newQualityGateEvent(oldAnalysis).setDate(oldAnalysis.getCreatedAt()).setName("Failed"));
     issueIndexer.indexAllIssues();
 
     SearchEventsWsResponse result = ws.newRequest()
-      .setParam(PARAM_PROJECTS, project.getKey())
+      .setParam(PARAM_PROJECTS, mainBranch.getKey())
       .setParam(PARAM_FROM, formatDateTime(analysis.getCreatedAt() - 1450_000_000_000L))
       .executeProtobuf(SearchEventsWsResponse.class);
 
@@ -190,32 +194,34 @@ public class SearchEventsActionIT {
 
   @Test
   public void does_not_return_events_of_project_for_which_the_current_user_has_no_browse_permission() {
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.CODEVIEWER, project1);
-    userSession.addProjectPermission(UserRole.ISSUE_ADMIN, project1);
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto mainBranch1 = projectData1.getMainBranchComponent();
+    userSession.addProjectPermission(UserRole.CODEVIEWER, projectData1.getProjectDto());
+    userSession.addProjectPermission(UserRole.ISSUE_ADMIN, projectData1.getProjectDto());
 
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project2);
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto mainBranch2 = projectData2.getMainBranchComponent();
+    userSession.addProjectPermission(USER, projectData2.getProjectDto());
 
-    SnapshotDto a1 = insertAnalysis(project1, 1_500_000_000_000L);
+    SnapshotDto a1 = insertAnalysis(mainBranch1, 1_500_000_000_000L);
     EventDto e1 = db.events().insertEvent(newQualityGateEvent(a1).setDate(a1.getCreatedAt()));
-    insertIssue(project1, a1);
-    SnapshotDto a2 = insertAnalysis(project2, 1_500_000_000_000L);
+    insertIssue(mainBranch1, a1);
+    SnapshotDto a2 = insertAnalysis(mainBranch2, 1_500_000_000_000L);
     EventDto e2 = db.events().insertEvent(newQualityGateEvent(a2).setDate(a2.getCreatedAt()));
-    insertIssue(project2, a2);
+    insertIssue(mainBranch2, a2);
     issueIndexer.indexAllIssues();
 
     String stringFrom = formatDateTime(a1.getCreatedAt() - 1_000L);
     SearchEventsWsResponse result = ws.newRequest()
-      .setParam(PARAM_PROJECTS, String.join(",", project1.getKey(), project2.getKey()))
+      .setParam(PARAM_PROJECTS, String.join(",", mainBranch1.getKey(), mainBranch2.getKey()))
       .setParam(PARAM_FROM, String.join(",", stringFrom, stringFrom))
       .executeProtobuf(SearchEventsWsResponse.class);
 
     assertThat(result.getEventsList())
       .extracting(Event::getCategory, Event::getProject)
       .containsOnly(
-        tuple("NEW_ISSUES", project2.getKey()),
-        tuple(EventCategory.QUALITY_GATE.name(), project2.getKey()));
+        tuple("NEW_ISSUES", mainBranch2.getKey()),
+        tuple(EventCategory.QUALITY_GATE.name(), mainBranch2.getKey()));
   }
 
   @Test
index e2a4c2777104b6409f61636a522cf7639befda8b..ac353f6c22e3eb4b7849e199a5465de206428798 100644 (file)
@@ -38,8 +38,10 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.issue.IssueChangeDto;
 import org.sonar.db.issue.IssueDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.es.EsTester;
@@ -109,7 +111,7 @@ public class BulkChangeActionIT {
   private System2 system2 = new TestSystem2().setNow(NOW);
 
   @Rule
-  public DbTester db = DbTester.create(system2);
+  public DbTester db = DbTester.create(system2, true);
   @Rule
   public EsTester es = EsTester.create();
   @Rule
@@ -142,9 +144,10 @@ public class BulkChangeActionIT {
   public void set_type() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -167,9 +170,10 @@ public class BulkChangeActionIT {
   public void set_severity() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setSeverity(MAJOR).setType(CODE_SMELL)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -192,9 +196,10 @@ public class BulkChangeActionIT {
   public void add_tags() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setTags(asList("tag1", "tag2"))
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -218,9 +223,10 @@ public class BulkChangeActionIT {
   public void add_tags_when_issue_is_resolved_is_accepted() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setTags(asList("tag1", "tag2"))
       .setStatus(STATUS_RESOLVED).setResolution(RESOLUTION_FALSE_POSITIVE));
@@ -241,9 +247,10 @@ public class BulkChangeActionIT {
   public void remove_assignee() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     UserDto assignee = db.users().insertUser();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setAssigneeUuid(assignee.getUuid())
@@ -268,9 +275,10 @@ public class BulkChangeActionIT {
   public void bulk_change_with_comment() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -294,9 +302,10 @@ public class BulkChangeActionIT {
   public void bulk_change_many_issues() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     UserDto oldAssignee = db.users().insertUser();
     UserDto userToAssign = db.users().insertUser();
@@ -330,9 +339,10 @@ public class BulkChangeActionIT {
   public void send_notification() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -367,9 +377,10 @@ public class BulkChangeActionIT {
   public void should_ignore_hotspots() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, SECURITYHOTSPOT_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, SECURITYHOTSPOT_ADMIN);
     IssueDto issue = db.issues().insertHotspot(project, file);
 
     BulkChangeWsResponse response = call(builder()
@@ -386,11 +397,12 @@ public class BulkChangeActionIT {
   public void send_notification_on_branch() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature").setBranchType(BranchType.BRANCH));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     ComponentDto fileOnBranch = db.components().insertComponent(newFileDto(branch, project.uuid()));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, branch, fileOnBranch, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -424,11 +436,12 @@ public class BulkChangeActionIT {
   private void verifySendNoNotification(BranchType branchType) {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature").setBranchType(branchType));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     ComponentDto fileOnBranch = db.components().insertComponent(newFileDto(branch, project.uuid()));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, branch, fileOnBranch, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -448,9 +461,10 @@ public class BulkChangeActionIT {
   public void send_notification_only_on_changed_issues() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue1 = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -484,10 +498,11 @@ public class BulkChangeActionIT {
   public void ignore_the_issues_that_do_not_match_conditions() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file1 = db.components().insertComponent(newFileDto(project));
     ComponentDto file2 = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue1 = db.issues().insertIssue(rule, project, file1, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -518,10 +533,11 @@ public class BulkChangeActionIT {
   public void ignore_issues_when_there_is_nothing_to_do() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file1 = db.components().insertComponent(newFileDto(project));
     ComponentDto file2 = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue1 = db.issues().insertIssue(rule, project, file1, i -> i.setType(BUG).setSeverity(MINOR)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -552,10 +568,11 @@ public class BulkChangeActionIT {
   public void add_comment_only_on_changed_issues() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     ComponentDto file1 = db.components().insertComponent(newFileDto(project));
     ComponentDto file2 = db.components().insertComponent(newFileDto(project));
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue1 = db.issues().insertIssue(rule, project, file1, i -> i.setType(BUG).setSeverity(MINOR)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -583,8 +600,9 @@ public class BulkChangeActionIT {
   public void ignore_external_issue() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData, USER, ISSUE_ADMIN);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, project, i -> i.setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
     RuleDto externalRule = db.rules().insertIssueRule(r -> r.setIsExternal(true));
@@ -602,8 +620,9 @@ public class BulkChangeActionIT {
   public void issues_on_which_user_has_not_browse_permission_are_ignored() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project1, USER, ISSUE_ADMIN);
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData1, USER, ISSUE_ADMIN);
     ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto authorizedIssue = db.issues().insertIssue(rule, project1, project1, i -> i.setType(BUG)
@@ -634,10 +653,12 @@ public class BulkChangeActionIT {
   public void does_not_update_type_when_no_issue_admin_permission() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project1, USER, ISSUE_ADMIN);
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project2, USER);
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData1, USER, ISSUE_ADMIN);
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto project2 = projectData2.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData2, USER);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto authorizedIssue1 = db.issues().insertIssue(rule, project1, project1, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -666,10 +687,12 @@ public class BulkChangeActionIT {
   public void does_not_update_severity_when_no_issue_admin_permission() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project1, USER, ISSUE_ADMIN);
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project2, USER);
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData1, USER, ISSUE_ADMIN);
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto project2 = projectData2.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData2, USER);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto authorizedIssue1 = db.issues().insertIssue(rule, project1, project1, i -> i.setSeverity(MAJOR)
       .setStatus(STATUS_OPEN).setResolution(null).setType(CODE_SMELL));
@@ -698,8 +721,9 @@ public class BulkChangeActionIT {
   public void fail_when_only_comment_action() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    addUserProjectPermissions(user, project, USER);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
+    addUserProjectPermissions(user, projectData, USER);
     RuleDto rule = db.rules().insertIssueRule();
     IssueDto issue = db.issues().insertIssue(rule, project, project, i -> i.setType(BUG)
       .setStatus(STATUS_OPEN).setResolution(null));
@@ -764,10 +788,11 @@ public class BulkChangeActionIT {
     return request.executeProtobuf(BulkChangeWsResponse.class);
   }
 
-  private void addUserProjectPermissions(UserDto user, ComponentDto project, String... permissions) {
+  private void addUserProjectPermissions(UserDto user, ProjectData project, String... permissions) {
     for (String permission : permissions) {
-      db.users().insertProjectPermissionOnUser(user, permission, project);
-      userSession.addProjectPermission(permission, project);
+      db.users().insertProjectPermissionOnUser(user, permission, project.getProjectDto());
+      userSession.addProjectPermission(permission, project.getProjectDto());
+      userSession.addProjectBranchMapping(project.projectUuid(), project.getMainBranchComponent());
     }
   }
 
index 2ac0c3042a501fc1ec67ffded8685a59c419403b..7bcdcad81c5e7a6c34d8f29c951bff4e702d3e02 100644 (file)
@@ -70,7 +70,7 @@ public class IssueUpdaterIT {
   private System2 system2 = mock(System2.class);
 
   @Rule
-  public DbTester db = DbTester.create(system2);
+  public DbTester db = DbTester.create(system2, true);
 
   @Rule
   public EsTester es = EsTester.create();
index 5cf06174b7537159239c533dee1c2673dc13cca8..30f34056ab2ea778c825636bab112fec485006fe 100644 (file)
@@ -28,11 +28,13 @@ import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.issue.IssueDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.es.EsTester;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.issue.index.IssueIndex;
 import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
 import org.sonar.server.issue.index.IssueIndexer;
@@ -65,7 +67,7 @@ public class TagsActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create();
+  public DbTester db = DbTester.create(true);
   @Rule
   public EsTester es = EsTester.create();
 
@@ -81,11 +83,12 @@ public class TagsActionIT {
   @Test
   public void search_tags() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag3", "tag4", "tag5")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag3", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     TagsResponse result = ws.newRequest().executeProtobuf(TagsResponse.class);
 
@@ -95,14 +98,15 @@ public class TagsActionIT {
 
   @Test
   public void search_tags_ignores_hotspots() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
     RuleDto issueRule = db.rules().insertIssueRule();
     RuleDto hotspotRule = db.rules().insertHotspotRule();
     Consumer<IssueDto> setTags = issue -> issue.setTags(asList("tag1", "tag2"));
-    db.issues().insertIssue(issueRule, project, project, setTags);
-    db.issues().insertHotspot(hotspotRule, project, project, setTags);
+    db.issues().insertIssue(issueRule, mainBranch, mainBranch, setTags);
+    db.issues().insertHotspot(hotspotRule, mainBranch, mainBranch, setTags);
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     TestRequest testRequest = ws.newRequest();
 
     assertThat(tagListOf(testRequest)).containsExactly("tag1", "tag2");
@@ -111,11 +115,12 @@ public class TagsActionIT {
   @Test
   public void search_tags_by_query() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag12", "tag4", "tag5")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag12", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest().setParam("q", "ag1"))).containsExactly("tag1", "tag12");
   }
@@ -124,11 +129,12 @@ public class TagsActionIT {
   public void search_tags_by_query_ignores_hotspots() {
     RuleDto issueRule = db.rules().insertIssueRule();
     RuleDto hotspotRule = db.rules().insertHotspotRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(issueRule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
-    db.issues().insertHotspot(hotspotRule, project, project, issue -> issue.setTags(asList("tag1", "tag12", "tag4", "tag5")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.issues().insertIssue(issueRule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
+    db.issues().insertHotspot(hotspotRule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag12", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     TestRequest testRequest = ws.newRequest();
 
     assertThat(tagListOf(testRequest)).containsExactly("tag1", "tag2");
@@ -142,72 +148,78 @@ public class TagsActionIT {
   @Test
   public void search_tags_by_project() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(rule, project1, project1, issue -> issue.setTags(singletonList("tag1")));
-    db.issues().insertIssue(rule, project2, project2, issue -> issue.setTags(singletonList("tag2")));
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto mainBranch1 = projectData1.getMainBranchComponent();
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto mainBranch2 = projectData2.getMainBranchComponent();
+    db.issues().insertIssue(rule, mainBranch1, mainBranch1, issue -> issue.setTags(singletonList("tag1")));
+    db.issues().insertIssue(rule, mainBranch2, mainBranch2, issue -> issue.setTags(singletonList("tag2")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project1, project2);
+    permissionIndexer.allowOnlyAnyone(projectData1.getProjectDto(), projectData1.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest()
-      .setParam("project", project1.getKey()))).containsExactly("tag1");
-    verify(issueIndexSyncProgressChecker).checkIfComponentNeedIssueSync(any(), eq(project1.getKey()));
+      .setParam("project", mainBranch1.getKey()))).containsExactly("tag1");
+    verify(issueIndexSyncProgressChecker).checkIfComponentNeedIssueSync(any(), eq(mainBranch1.getKey()));
   }
 
   @Test
   public void search_tags_by_branch_equals_main_branch() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
     db.issues().insertIssue(rule, branch, branch, issue -> issue.setTags(asList("tag12", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project, branch);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest()
-      .setParam("project", project.getKey())
-      .setParam("branch", project.uuid()))).containsExactly("tag1", "tag2");
+      .setParam("project", mainBranch.getKey())
+      .setParam("branch", mainBranch.uuid()))).containsExactly("tag1", "tag2");
   }
 
   @Test
   public void search_tags_by_branch() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
     db.issues().insertIssue(rule, branch, branch, issue -> issue.setTags(asList("tag12", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project, branch);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest()
-      .setParam("project", project.getKey())
+      .setParam("project", mainBranch.getKey())
       .setParam("branch", "my_branch"))).containsExactly("tag12", "tag4", "tag5");
   }
 
   @Test
   public void search_tags_by_branch_not_exist_fall_back_to_main_branch() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
     db.issues().insertIssue(rule, branch, branch, issue -> issue.setTags(asList("tag12", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project, branch);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest()
-      .setParam("project", project.getKey())
+      .setParam("project", mainBranch.getKey())
       .setParam("branch", "not_exist"))).containsExactly("tag1", "tag2");
   }
 
   @Test
   public void search_all_tags_by_query() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
     db.issues().insertIssue(rule, branch, branch, issue -> issue.setTags(asList("tag12", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project, branch);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest()
       .setParam("q", "tag1")
@@ -218,14 +230,16 @@ public class TagsActionIT {
   public void search_tags_by_project_ignores_hotspots() {
     RuleDto issueRule = db.rules().insertIssueRule();
     RuleDto hotspotRule = db.rules().insertHotspotRule();
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData1 = db.components().insertPrivateProject();
+    ComponentDto project1 = projectData1.getMainBranchComponent();
+    ProjectData projectData2 = db.components().insertPrivateProject();
+    ComponentDto project2 = projectData2.getMainBranchComponent();
     db.issues().insertHotspot(hotspotRule, project1, project1, issue -> issue.setTags(singletonList("tag1")));
     db.issues().insertIssue(issueRule, project1, project1, issue -> issue.setTags(singletonList("tag2")));
     db.issues().insertHotspot(hotspotRule, project2, project2, issue -> issue.setTags(singletonList("tag3")));
     db.issues().insertIssue(issueRule, project2, project2, issue -> issue.setTags(singletonList("tag4")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project1, project2);
+    permissionIndexer.allowOnlyAnyone(projectData1.getProjectDto(), projectData2.getProjectDto());
 
     assertThat(tagListOf(ws.newRequest()
       .setParam("project", project1.getKey()))).containsExactly("tag2");
@@ -234,11 +248,12 @@ public class TagsActionIT {
   @Test
   public void search_tags_by_portfolio() {
     ComponentDto portfolio = db.components().insertPrivatePortfolio();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertComponent(newProjectCopy(project, portfolio));
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.components().insertComponent(newProjectCopy(mainBranch, portfolio));
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(singletonList("cwe")));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(singletonList("cwe")));
     indexIssues();
     viewIndexer.indexAll();
 
@@ -248,13 +263,14 @@ public class TagsActionIT {
   @Test
   public void search_tags_by_portfolio_ignores_hotspots() {
     ComponentDto portfolio = db.components().insertPrivatePortfolio();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertComponent(newProjectCopy(project, portfolio));
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.components().insertComponent(newProjectCopy(mainBranch, portfolio));
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto issueRule = db.rules().insertIssueRule();
     RuleDto hotspotRule = db.rules().insertHotspotRule();
-    db.issues().insertHotspot(hotspotRule, project, project, issue -> issue.setTags(singletonList("cwe")));
-    db.issues().insertIssue(issueRule, project, project, issue -> issue.setTags(singletonList("foo")));
+    db.issues().insertHotspot(hotspotRule, mainBranch, mainBranch, issue -> issue.setTags(singletonList("cwe")));
+    db.issues().insertIssue(issueRule, mainBranch, mainBranch, issue -> issue.setTags(singletonList("foo")));
     indexIssues();
     viewIndexer.indexAll();
 
@@ -263,24 +279,28 @@ public class TagsActionIT {
 
   @Test
   public void search_tags_by_application() {
-    ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertComponent(newProjectCopy(project, application));
-    permissionIndexer.allowOnlyAnyone(project);
+    ProjectData applicationData = db.components().insertPrivateApplication();
+    ComponentDto application = applicationData.getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.components().insertComponent(newProjectCopy(mainBranch, application));
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto(), applicationData.getProjectDto());
     RuleDto rule = db.rules().insertIssueRule();
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(singletonList("cwe")));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(singletonList("cwe")));
     indexIssues();
     viewIndexer.indexAll();
 
+
     assertThat(tagListOf(ws.newRequest().setParam("project", application.getKey()))).containsExactly("cwe");
   }
 
   @Test
   public void search_tags_by_application_ignores_hotspots() {
     ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     db.components().insertComponent(newProjectCopy(project, application));
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
     RuleDto issueRule = db.rules().insertIssueRule();
     RuleDto hotspotRule = db.rules().insertHotspotRule();
     db.issues().insertIssue(issueRule, project, project, issue -> issue.setTags(singletonList("cwe")));
@@ -294,11 +314,12 @@ public class TagsActionIT {
   @Test
   public void return_limited_size() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag1", "tag2")));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("tag3", "tag4", "tag5")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag1", "tag2")));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("tag3", "tag4", "tag5")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     TagsResponse result = ws.newRequest()
       .setParam("ps", "2")
@@ -310,13 +331,14 @@ public class TagsActionIT {
   @Test
   public void do_not_return_issues_without_permission() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(rule, project1, project1, issue -> issue.setTags(asList("tag1", "tag2")));
-    db.issues().insertIssue(rule, project2, project2, issue -> issue.setTags(asList("tag3", "tag4", "tag5")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch1 = projectData.getMainBranchComponent();
+    ComponentDto mainBranch2 = db.components().insertPrivateProject().getMainBranchComponent();
+    db.issues().insertIssue(rule, mainBranch1, mainBranch1, issue -> issue.setTags(asList("tag1", "tag2")));
+    db.issues().insertIssue(rule, mainBranch2, mainBranch2, issue -> issue.setTags(asList("tag3", "tag4", "tag5")));
     indexIssues();
     // Project 2 is not visible to current user
-    permissionIndexer.allowOnlyAnyone(project1);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     TagsResponse result = ws.newRequest().executeProtobuf(TagsResponse.class);
 
@@ -336,28 +358,30 @@ public class TagsActionIT {
 
   @Test
   public void fail_when_project_parameter_does_not_match_a_project() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project, project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     assertThatThrownBy(() -> {
       ws.newRequest()
         .setParam("project", file.getKey())
         .execute();
     })
-      .isInstanceOf(IllegalArgumentException.class)
-      .hasMessage(format("Component '%s' must be a project", file.getKey()));
+      .isInstanceOf(NotFoundException.class)
+      .hasMessage(format("Component '%s' not found", file.getKey()));
   }
 
   @Test
   public void json_example() {
     RuleDto rule = db.rules().insertIssueRule();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(asList("convention", "security")));
-    db.issues().insertIssue(rule, project, project, issue -> issue.setTags(singletonList("cwe")));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(asList("convention", "security")));
+    db.issues().insertIssue(rule, mainBranch, mainBranch, issue -> issue.setTags(singletonList("cwe")));
     indexIssues();
-    permissionIndexer.allowOnlyAnyone(project);
+    permissionIndexer.allowOnlyAnyone(projectData.getProjectDto());
 
     String result = ws.newRequest().execute().getInput();
 
index 4c4e0166f7b68270aa55571aa0bef03d2fd58e33..5781e8bf7e5cbc9878466b246513aea716c5752f 100644 (file)
@@ -103,7 +103,7 @@ public class ComponentTreeActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone().logIn();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final I18nRule i18n = new I18nRule();
 
@@ -123,27 +123,28 @@ public class ComponentTreeActionIT {
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("MY_PROJECT")
-      .setName("My Project")).getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
+    ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey("MY_PROJECT")
+      .setName("My Project"));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto analysis = db.components().insertSnapshot(mainBranch, s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
       .setPeriodMode("previous_version")
       .setPeriodParam("1.0-SNAPSHOT"));
-    ComponentDto file1 = db.components().insertComponent(newFileDto(project)
+    ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch)
       .setUuid("AVIwDXE-bJbJqrw6wFv5")
       .setKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/ElementImpl.java")
       .setName("ElementImpl.java")
       .setLanguage("java")
       .setQualifier(FILE)
       .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"));
-    ComponentDto file2 = db.components().insertComponent(newFileDto(project)
+    ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch)
       .setUuid("AVIwDXE_bJbJqrw6wFwJ")
       .setKey("com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java")
       .setName("ElementImplTest.java")
       .setLanguage("java")
       .setQualifier(UNIT_TEST_FILE)
       .setPath("src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java"));
-    ComponentDto dir = db.components().insertComponent(newDirectory(project, "src/main/java/com/sonarsource/markdown/impl")
+    ComponentDto dir = db.components().insertComponent(newDirectory(mainBranch, "src/main/java/com/sonarsource/markdown/impl")
       .setUuid("AVIwDXE-bJbJqrw6wFv8")
       .setKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl")
       .setQualifier(DIRECTORY));
@@ -151,22 +152,22 @@ public class ComponentTreeActionIT {
     MetricDto complexity = insertComplexityMetric();
     db.measures().insertLiveMeasure(file1, complexity, m -> m.setValue(12.0d));
     db.measures().insertLiveMeasure(dir, complexity, m -> m.setValue(35.0d));
-    db.measures().insertLiveMeasure(project, complexity, m -> m.setValue(42.0d));
+    db.measures().insertLiveMeasure(mainBranch, complexity, m -> m.setValue(42.0d));
 
     MetricDto ncloc = insertNclocMetric();
     db.measures().insertLiveMeasure(file1, ncloc, m -> m.setValue(114.0d));
     db.measures().insertLiveMeasure(dir, ncloc, m -> m.setValue(217.0d));
-    db.measures().insertLiveMeasure(project, ncloc, m -> m.setValue(1984.0d));
+    db.measures().insertLiveMeasure(mainBranch, ncloc, m -> m.setValue(1984.0d));
 
     MetricDto newViolations = insertNewViolationsMetric();
     db.measures().insertLiveMeasure(file1, newViolations, m -> m.setValue(25.0d));
     db.measures().insertLiveMeasure(dir, newViolations, m -> m.setValue(25.0d));
-    db.measures().insertLiveMeasure(project, newViolations, m -> m.setValue(255.0d));
+    db.measures().insertLiveMeasure(mainBranch, newViolations, m -> m.setValue(255.0d));
 
     db.commit();
 
     String response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, "ncloc, complexity, new_violations")
       .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period")
       .execute()
@@ -175,17 +176,23 @@ public class ComponentTreeActionIT {
     assertJson(response).isSimilarTo(getClass().getResource("component_tree-example.json"));
   }
 
+  private UserSessionRule addProjectPermission(ProjectData projectData) {
+    return userSession.addProjectPermission(USER, projectData.getProjectDto())
+      .addProjectBranchMapping(projectData.projectUuid(), projectData.getMainBranchComponent());
+  }
+
   @Test
   public void empty_response() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, "ncloc, complexity")
       .executeProtobuf(ComponentTreeWsResponse.class);
 
-    assertThat(response.getBaseComponent().getKey()).isEqualTo(project.getKey());
+    assertThat(response.getBaseComponent().getKey()).isEqualTo(mainBranch.getKey());
     assertThat(response.getComponentsList()).isEmpty();
     assertThat(response.getMetrics().getMetricsList()).isEmpty();
     assertThat(response.hasPeriod()).isFalse();
@@ -193,14 +200,14 @@ public class ComponentTreeActionIT {
 
   @Test
   public void load_measures_and_periods() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
     SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession,
-      newAnalysis(project)
+      newAnalysis(mainBranch)
         .setPeriodDate(System.currentTimeMillis())
         .setPeriodMode("last_version")
         .setPeriodDate(System.currentTimeMillis()));
-    userSession.anonymous().addProjectPermission(USER, project);
-    ComponentDto directory = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
+    userSession.anonymous().addProjectPermission(USER, mainBranch);
+    ComponentDto directory = newDirectory(mainBranch, "directory-uuid", "path/to/directory").setName("directory-1");
     db.components().insertComponent(directory);
     ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
     db.components().insertComponent(file);
@@ -212,7 +219,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(directory, coverage, m -> m.setValue(15.5d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, "ncloc,coverage")
       .setParam(PARAM_ADDITIONAL_FIELDS, ADDITIONAL_PERIOD)
       .executeProtobuf(ComponentTreeWsResponse.class);
@@ -227,10 +234,10 @@ public class ComponentTreeActionIT {
 
   @Test
   public void load_measures_with_best_value() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
-    userSession.anonymous().addProjectPermission(USER, project);
-    ComponentDto directory = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
+    userSession.anonymous().addProjectPermission(USER, mainBranch);
+    ComponentDto directory = newDirectory(mainBranch, "directory-uuid", "path/to/directory").setName("directory-1");
     db.components().insertComponent(directory);
     ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
     db.components().insertComponent(file);
@@ -251,7 +258,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(directory, coverage, m -> m.setValue(42.0d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, "ncloc,coverage,new_violations")
       .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
       .executeProtobuf(ComponentTreeWsResponse.class);
@@ -273,10 +280,10 @@ public class ComponentTreeActionIT {
 
   @Test
   public void return_is_best_value_on_leak_measures() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
-    userSession.anonymous().addProjectPermission(USER, project);
-    ComponentDto file = newFileDto(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
+    userSession.anonymous().addProjectPermission(USER, mainBranch);
+    ComponentDto file = newFileDto(mainBranch);
     db.components().insertComponent(file);
 
     MetricDto matchingBestValue = db.measures().insertMetric(m -> m
@@ -296,7 +303,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(file, noBestValue, m -> m.setData((String) null).setValue(42.0d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, "new_lines,new_lines_2,new_violations")
       .executeProtobuf(ComponentTreeWsResponse.class);
 
@@ -315,13 +322,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void use_best_value_for_rating() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.anonymous().addProjectPermission(USER, project);
-    SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    userSession.anonymous().addProjectPermission(USER, mainBranch);
+    SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch)
       .setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
       .setPeriodMode("previous_version")
       .setPeriodParam("1.0-SNAPSHOT"));
-    ComponentDto directory = newDirectory(project, "directory-uuid", "path/to/directory").setName("directory-1");
+    ComponentDto directory = newDirectory(mainBranch, "directory-uuid", "path/to/directory").setName("directory-1");
     db.components().insertComponent(directory);
     ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
     db.components().insertComponent(file);
@@ -334,7 +341,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(directory, metric, m -> m.setValue(2d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, NEW_SECURITY_RATING_KEY)
       .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
       .executeProtobuf(ComponentTreeWsResponse.class);
@@ -349,18 +356,19 @@ public class ComponentTreeActionIT {
 
   @Test
   public void load_measures_multi_sort_with_metric_key_and_paginated() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
-    ComponentDto file9 = db.components().insertComponent(newFileDto(project, null, "file-uuid-9").setName("file-1").setKey("file-9-key"));
-    ComponentDto file8 = db.components().insertComponent(newFileDto(project, null, "file-uuid-8").setName("file-1").setKey("file-8-key"));
-    ComponentDto file7 = db.components().insertComponent(newFileDto(project, null, "file-uuid-7").setName("file-1").setKey("file-7-key"));
-    ComponentDto file6 = db.components().insertComponent(newFileDto(project, null, "file-uuid-6").setName("file-1").setKey("file-6-key"));
-    ComponentDto file5 = db.components().insertComponent(newFileDto(project, null, "file-uuid-5").setName("file-1").setKey("file-5-key"));
-    ComponentDto file4 = db.components().insertComponent(newFileDto(project, null, "file-uuid-4").setName("file-1").setKey("file-4-key"));
-    ComponentDto file3 = db.components().insertComponent(newFileDto(project, null, "file-uuid-3").setName("file-1").setKey("file-3-key"));
-    ComponentDto file2 = db.components().insertComponent(newFileDto(project, null, "file-uuid-2").setName("file-1").setKey("file-2-key"));
-    ComponentDto file1 = db.components().insertComponent(newFileDto(project, null, "file-uuid-1").setName("file-1").setKey("file-1-key"));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
+    ComponentDto file9 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-9").setName("file-1").setKey("file-9-key"));
+    ComponentDto file8 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-8").setName("file-1").setKey("file-8-key"));
+    ComponentDto file7 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-7").setName("file-1").setKey("file-7-key"));
+    ComponentDto file6 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-6").setName("file-1").setKey("file-6-key"));
+    ComponentDto file5 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-5").setName("file-1").setKey("file-5-key"));
+    ComponentDto file4 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-4").setName("file-1").setKey("file-4-key"));
+    ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setName("file-1").setKey("file-3-key"));
+    ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setName("file-1").setKey("file-2-key"));
+    ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setName("file-1").setKey("file-1-key"));
     MetricDto coverage = insertCoverageMetric();
     db.commit();
     db.measures().insertLiveMeasure(file1, coverage, m -> m.setValue(1.0d));
@@ -374,7 +382,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(file9, coverage, m -> m.setValue(9.0d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(SORT, NAME_SORT + ", " + METRIC_SORT)
       .setParam(PARAM_METRIC_SORT, "coverage")
       .setParam(PARAM_METRIC_KEYS, "coverage")
@@ -392,13 +400,14 @@ public class ComponentTreeActionIT {
 
   @Test
   public void sort_by_metric_value() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
-    ComponentDto file4 = db.components().insertComponent(newFileDto(project, null, "file-uuid-4").setKey("file-4-key"));
-    ComponentDto file3 = db.components().insertComponent(newFileDto(project, null, "file-uuid-3").setKey("file-3-key"));
-    ComponentDto file1 = db.components().insertComponent(newFileDto(project, null, "file-uuid-1").setKey("file-1-key"));
-    ComponentDto file2 = db.components().insertComponent(newFileDto(project, null, "file-uuid-2").setKey("file-2-key"));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
+    ComponentDto file4 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-4").setKey("file-4-key"));
+    ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key"));
+    ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key"));
+    ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key"));
     MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
     dbClient.metricDao().insert(dbSession, ncloc);
     db.commit();
@@ -407,7 +416,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(file3, ncloc, m -> m.setValue(3.0d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(SORT, METRIC_SORT)
       .setParam(PARAM_METRIC_SORT, "ncloc")
       .setParam(PARAM_METRIC_KEYS, "ncloc")
@@ -419,13 +428,14 @@ public class ComponentTreeActionIT {
 
   @Test
   public void remove_components_without_measure_on_the_metric_sort() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
-    ComponentDto file1 = newFileDto(project, null, "file-uuid-1").setKey("file-1-key");
-    ComponentDto file2 = newFileDto(project, null, "file-uuid-2").setKey("file-2-key");
-    ComponentDto file3 = newFileDto(project, null, "file-uuid-3").setKey("file-3-key");
-    ComponentDto file4 = newFileDto(project, null, "file-uuid-4").setKey("file-4-key");
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
+    ComponentDto file1 = newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key");
+    ComponentDto file2 = newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key");
+    ComponentDto file3 = newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key");
+    ComponentDto file4 = newFileDto(mainBranch, null, "file-uuid-4").setKey("file-4-key");
     db.components().insertComponent(file1);
     db.components().insertComponent(file2);
     db.components().insertComponent(file3);
@@ -438,7 +448,7 @@ public class ComponentTreeActionIT {
     db.commit();
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(SORT, METRIC_SORT)
       .setParam(PARAM_METRIC_SORT, "ncloc")
       .setParam(PARAM_METRIC_KEYS, "ncloc")
@@ -453,12 +463,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void sort_by_metric_period() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
-    ComponentDto file3 = db.components().insertComponent(newFileDto(project, null, "file-uuid-3").setKey("file-3-key"));
-    ComponentDto file1 = db.components().insertComponent(newFileDto(project, null, "file-uuid-1").setKey("file-1-key"));
-    ComponentDto file2 = db.components().insertComponent(newFileDto(project, null, "file-uuid-2").setKey("file-2-key"));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
+    ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key"));
+    ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key"));
+    ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key"));
     MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
     dbClient.metricDao().insert(dbSession, ncloc);
     db.commit();
@@ -467,7 +478,7 @@ public class ComponentTreeActionIT {
     db.measures().insertLiveMeasure(file3, ncloc, m -> m.setValue(3.0d));
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(SORT, METRIC_PERIOD_SORT)
       .setParam(PARAM_METRIC_SORT, "ncloc")
       .setParam(PARAM_METRIC_KEYS, "ncloc")
@@ -479,13 +490,14 @@ public class ComponentTreeActionIT {
 
   @Test
   public void remove_components_without_measure_on_the_metric_period_sort() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto projectSnapshot = db.components().insertSnapshot(project);
-    ComponentDto file4 = db.components().insertComponent(newFileDto(project, null, "file-uuid-4").setKey("file-4-key"));
-    ComponentDto file3 = db.components().insertComponent(newFileDto(project, null, "file-uuid-3").setKey("file-3-key"));
-    ComponentDto file2 = db.components().insertComponent(newFileDto(project, null, "file-uuid-2").setKey("file-2-key"));
-    ComponentDto file1 = db.components().insertComponent(newFileDto(project, null, "file-uuid-1").setKey("file-1-key"));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
+    ComponentDto file4 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-4").setKey("file-4-key"));
+    ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key"));
+    ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key"));
+    ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key"));
     MetricDto ncloc = newMetricDto().setKey("new_ncloc").setValueType(INT.name()).setDirection(1);
     dbClient.metricDao().insert(dbSession, ncloc);
     db.measures().insertLiveMeasure(file1, ncloc, m -> m.setData((String) null).setValue(1.0d));
@@ -494,7 +506,7 @@ public class ComponentTreeActionIT {
     db.commit();
 
     ComponentTreeWsResponse response = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(SORT, METRIC_PERIOD_SORT + "," + NAME_SORT)
       .setParam(PARAM_METRIC_SORT, "new_ncloc")
       .setParam(PARAM_METRIC_KEYS, "new_ncloc")
@@ -510,31 +522,33 @@ public class ComponentTreeActionIT {
   @Test
   public void load_measures_when_no_leave_qualifier() {
     resourceTypes.setLeavesQualifiers();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
-    db.components().insertComponent(newFileDto(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    db.components().insertSnapshot(mainBranch);
+    db.components().insertComponent(newFileDto(mainBranch));
     insertNclocMetric();
 
     ComponentTreeWsResponse result = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_STRATEGY, LEAVES_STRATEGY)
       .setParam(PARAM_METRIC_KEYS, "ncloc")
       .executeProtobuf(ComponentTreeWsResponse.class);
 
-    assertThat(result.getBaseComponent().getKey()).isEqualTo(project.getKey());
+    assertThat(result.getBaseComponent().getKey()).isEqualTo(mainBranch.getKey());
     assertThat(result.getComponentsCount()).isZero();
   }
 
   @Test
   public void branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
     String branchName = "my-branch";
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
+    userSession.addProjectBranchMapping(projectData.getProjectDto().getUuid(), branch);
     db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
     MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
     LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
 
@@ -553,9 +567,10 @@ public class ComponentTreeActionIT {
 
   @Test
   public void dont_show_branch_if_main_branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
     MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
 
     ComponentTreeWsResponse response = ws.newRequest()
@@ -590,12 +605,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void pull_request() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     SnapshotDto analysis = db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()).setMainBranchProjectUuid(project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()).setMainBranchProjectUuid(mainBranch.uuid()));
     MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
     LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
 
@@ -614,12 +630,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fix_pull_request_new_issue_count_metrics() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     SnapshotDto analysis = db.components().insertSnapshot(branch);
-    ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
     MetricDto bug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.BUGS_KEY));
     MetricDto newBug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.NEW_BUGS_KEY));
 
@@ -644,12 +661,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void new_issue_count_measures_are_not_transformed_if_they_dont_exist_in_pr() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setKey("pr").setBranchType(PULL_REQUEST));
-    userSession.addProjectBranchMapping(project.uuid(), pr);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    ComponentDto pr = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr").setBranchType(PULL_REQUEST));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), pr);
     SnapshotDto analysis = db.components().insertSnapshot(pr);
-    ComponentDto file = db.components().insertComponent(newFileDto(pr, project.uuid()));
+    ComponentDto file = db.components().insertComponent(newFileDto(pr, mainBranch.uuid()));
     MetricDto bug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.BUGS_KEY));
     MetricDto newBug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.NEW_BUGS_KEY));
 
@@ -667,16 +685,17 @@ public class ComponentTreeActionIT {
 
   @Test
   public void metric_without_a_domain() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    SnapshotDto analysis = db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto analysis = db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     MetricDto metricWithoutDomain = db.measures().insertMetric(m -> m
       .setValueType(Metric.ValueType.INT.name())
       .setDomain(null));
-    db.measures().insertLiveMeasure(project, metricWithoutDomain);
+    db.measures().insertLiveMeasure(mainBranch, metricWithoutDomain);
 
     ComponentTreeWsResponse result = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
       .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
       .executeProtobuf(ComponentTreeWsResponse.class);
@@ -689,12 +708,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void project_reference_from_portfolio() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
     ComponentDto view = db.components().insertPrivatePortfolio();
     userSession.addProjectPermission(USER, view);
     SnapshotDto viewAnalysis = db.components().insertSnapshot(view);
-    ComponentDto projectCopy = db.components().insertComponent(newProjectCopy(project, view));
+    ComponentDto projectCopy = db.components().insertComponent(newProjectCopy(mainBranch, view));
     MetricDto ncloc = insertNclocMetric();
     db.measures().insertLiveMeasure(projectCopy, ncloc, m -> m.setValue(5d));
 
@@ -705,7 +725,7 @@ public class ComponentTreeActionIT {
 
     assertThat(result.getComponentsList())
       .extracting(Component::getKey, Component::getRefKey)
-      .containsExactlyInAnyOrder(tuple(projectCopy.getKey(), project.getKey()));
+      .containsExactlyInAnyOrder(tuple(projectCopy.getKey(), mainBranch.getKey()));
   }
 
   @Test
@@ -765,9 +785,9 @@ public class ComponentTreeActionIT {
     ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey(branchName), a -> a.setUuid("custom-uuid"));
     userSession.addProjectBranchMapping(applicationData.projectUuid(), applicationBranch);
     ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey("project-key"));
-    ComponentDto project = projectData.getMainBranchComponent();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
 
-    ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("project-branch"));
+    ComponentDto projectBranch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("project-branch"));
     userSession.addProjectBranchMapping(projectData.projectUuid(), projectBranch);
     ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch)
       .setKey(applicationBranch.getKey() + branchName + projectBranch.getKey()));
@@ -787,17 +807,17 @@ public class ComponentTreeActionIT {
       .containsExactlyInAnyOrder(applicationBranch.getKey(), branchName);
     assertThat(result.getComponentsList())
       .extracting(Component::getKey, Component::getBranch, Component::getRefKey)
-      .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), "project-branch", project.getKey()));
+      .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), "project-branch", mainBranch.getKey()));
   }
 
   @Test
   public void fail_when_metric_keys_parameter_is_empty() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "")
         .executeProtobuf(ComponentTreeWsResponse.class);
     })
@@ -807,15 +827,16 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_a_metric_is_not_found() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    db.components().insertSnapshot(mainBranch);
     insertNclocMetric();
     insertNewViolationsMetric();
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc, new_violations, unknown-metric, another-unknown-metric").executeProtobuf(ComponentTreeWsResponse.class);
     })
       .isInstanceOf(NotFoundException.class)
@@ -824,16 +845,17 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_using_DISTRIB_metrics() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    db.components().insertSnapshot(mainBranch);
     dbClient.metricDao().insert(dbSession, newMetricDto().setKey("distrib1").setValueType(DISTRIB.name()));
     dbClient.metricDao().insert(dbSession, newMetricDto().setKey("distrib2").setValueType(DISTRIB.name()));
     db.commit();
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "distrib1,distrib2")
         .execute();
     })
@@ -843,9 +865,10 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_using_DATA_metrics() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    db.components().insertSnapshot(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    db.components().insertSnapshot(mainBranch);
 
     dbClient.metricDao().insert(dbSession, newMetricDto().setKey("data1").setValueType(DISTRIB.name()));
     dbClient.metricDao().insert(dbSession, newMetricDto().setKey("data2").setValueType(DISTRIB.name()));
@@ -853,7 +876,7 @@ public class ComponentTreeActionIT {
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "data1,data2")
         .execute();
     })
@@ -863,8 +886,8 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_setting_more_than_15_metric_keys() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
     List<String> metrics = IntStream.range(0, 20)
       .mapToObj(i -> "metric" + i)
       .collect(MoreCollectors.toList());
@@ -872,7 +895,7 @@ public class ComponentTreeActionIT {
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, Joiner.on(",").join(metrics))
         .execute();
     })
@@ -882,14 +905,14 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_search_query_have_less_than_3_characters() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
     insertNclocMetric();
     insertNewViolationsMetric();
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc, new_violations")
         .setParam(Param.TEXT_QUERY, "fi")
         .executeProtobuf(ComponentTreeWsResponse.class);
@@ -901,11 +924,11 @@ public class ComponentTreeActionIT {
   @Test
   public void fail_when_insufficient_privileges() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
 
     var request = ws.newRequest()
-      .setParam(PARAM_COMPONENT, project.getKey())
+      .setParam(PARAM_COMPONENT, mainBranch.getKey())
       .setParam(PARAM_METRIC_KEYS, "ncloc");
     assertThatThrownBy(() -> request.executeProtobuf(ComponentTreeWsResponse.class))
       .isInstanceOf(ForbiddenException.class);
@@ -935,12 +958,12 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_sort_by_metric_and_no_metric_sort_provided() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc")
         // PARAM_METRIC_SORT is not set
         .setParam(SORT, METRIC_SORT)
@@ -952,12 +975,12 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_sort_by_metric_and_not_in_the_list_of_metric_keys() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc,violations")
         .setParam(PARAM_METRIC_SORT, "complexity")
         .setParam(SORT, METRIC_SORT)
@@ -969,12 +992,12 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_sort_by_metric_period_and_no_metric_period_sort_provided() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc")
         .setParam(PARAM_METRIC_SORT, "ncloc")
         // PARAM_METRIC_PERIOD_SORT_IS_NOT_SET
@@ -987,13 +1010,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_paging_parameter_is_too_big() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
     insertNclocMetric();
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc")
         .setParam(Param.PAGE_SIZE, "2540")
         .execute();
@@ -1004,13 +1027,13 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_with_measures_only_and_no_metric_sort() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
     insertNclocMetric();
 
     assertThatThrownBy(() -> {
       ws.newRequest()
-        .setParam(PARAM_COMPONENT, project.getKey())
+        .setParam(PARAM_COMPONENT, mainBranch.getKey())
         .setParam(PARAM_METRIC_KEYS, "ncloc")
         .setParam(PARAM_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER)
         .executeProtobuf(ComponentTreeWsResponse.class);
@@ -1035,10 +1058,10 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_when_component_is_removed() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.components().insertSnapshot(project);
-    ComponentDto file = db.components().insertComponent(newFileDto(project).setKey("file-key").setEnabled(false));
-    userSession.anonymous().addProjectPermission(USER, project);
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
+    db.components().insertSnapshot(mainBranch);
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch).setKey("file-key").setEnabled(false));
+    userSession.anonymous().addProjectPermission(USER, mainBranch);
     insertNclocMetric();
 
     assertThatThrownBy(() -> {
@@ -1053,10 +1076,11 @@ public class ComponentTreeActionIT {
 
   @Test
   public void fail_if_branch_does_not_exist() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
-    userSession.addProjectPermission(USER, project);
-    db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
+    addProjectPermission(projectData);
+    db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
 
     assertThatThrownBy(() -> {
       ws.newRequest()
@@ -1070,10 +1094,11 @@ public class ComponentTreeActionIT {
   }
 
   @Test
-  public void doHandle_whenPassingUnexpectedQualifier_shouldThrowException(){
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(USER, project);
-    ComponentDto file = db.components().insertComponent(newFileDto(project));
+  public void doHandle_whenPassingUnexpectedQualifier_shouldThrowException() {
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
     MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
 
     TestRequest testRequest = ws.newRequest()
@@ -1082,7 +1107,7 @@ public class ComponentTreeActionIT {
       .setParam(PARAM_QUALIFIERS, "BRC")
       .setParam(PARAM_METRIC_KEYS, complexity.getKey());
 
-    assertThatThrownBy(()->testRequest.execute()).isInstanceOf(IllegalArgumentException.class)
+    assertThatThrownBy(() -> testRequest.execute()).isInstanceOf(IllegalArgumentException.class)
       .hasMessage("Value of parameter 'qualifiers' (BRC) must be one of: [UTS, FIL, DIR, TRK]");
   }
 
index 1a89bacb97e846d76e2340b652199fb87a1f8504..2dafb7161cd52b90770e4e192fcda35b1874bcd9 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.event.EventDto;
 import org.sonar.db.project.ProjectDto;
@@ -63,7 +64,7 @@ public class CreateEventActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
   private DbClient dbClient = db.getDbClient();
   private DbSession dbSession = db.getSession();
 
@@ -210,7 +211,8 @@ public class CreateEventActionIT {
 
   @Test
   public void create_2_version_events_on_same_project() {
-    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ProjectDto project = projectData.getProjectDto();
     SnapshotDto firstAnalysis = db.components().insertSnapshot(project);
     SnapshotDto secondAnalysis = db.components().insertSnapshot(project);
     db.commit();
@@ -219,7 +221,7 @@ public class CreateEventActionIT {
     call(VERSION.name(), "5.6.3", firstAnalysis.getUuid());
     call(VERSION.name(), "6.3", secondAnalysis.getUuid());
 
-    List<EventDto> events = dbClient.eventDao().selectByComponentUuid(dbSession, project.getUuid());
+    List<EventDto> events = dbClient.eventDao().selectByComponentUuid(dbSession, projectData.getMainBranchComponent().uuid());
     assertThat(events).hasSize(2);
   }
 
index be7772152b2758ffce69be3e3e3e8b3c3aa969d4..daa1e96d2cf439c3c72320f2a15a28ae9f7e6c8f 100644 (file)
@@ -52,6 +52,7 @@ import org.sonar.db.event.EventDto;
 import org.sonar.db.event.EventPurgeData;
 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
 import org.sonar.db.newcodeperiod.NewCodePeriodType;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -100,7 +101,7 @@ public class SearchActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create();
+  public DbTester db = DbTester.create(true);
   @Rule
   public LogTester logTester = new LogTester();
 
@@ -110,7 +111,7 @@ public class SearchActionIT {
 
   @DataProvider
   public static Object[][] changedBranches() {
-    return new Object[][] {
+    return new Object[][]{
       {null, "newbranch"},
       {"newbranch", "anotherbranch"},
       {"newbranch", null},
@@ -119,22 +120,23 @@ public class SearchActionIT {
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto().setKey(KEY_PROJECT_EXAMPLE_001));
+    ProjectData projectData = db.components().insertPrivateProject(c -> c.setKey(KEY_PROJECT_EXAMPLE_001));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
 
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project)
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch)
       .setUuid("A1")
       .setCreatedAt(parseDateTime("2016-12-11T17:12:45+0100").getTime())
       .setProjectVersion("1.2")
       .setBuildString("1.2.0.322")
       .setRevision("bfe36592eb7f9f2708b5d358b5b5f33ed535c8cf"));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project)
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch)
       .setUuid("A2")
       .setCreatedAt(parseDateTime("2016-12-12T17:12:45+0100").getTime())
       .setProjectVersion("1.2.1")
       .setBuildString("1.2.1.423")
       .setRevision("be6c75b85da526349c44e3978374c95e0b80a96d"));
-    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project)
+    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(mainBranch)
       .setUuid("P1")
       .setCreatedAt(parseDateTime("2015-11-11T10:00:00+0100").getTime())
       .setProjectVersion("1.2")
@@ -145,11 +147,10 @@ public class SearchActionIT {
       .setKey(CorePropertyDefinitions.SONAR_ANALYSIS_DETECTEDCI)
       .setValue("Jenkins")
       .setCreatedAt(1L));
-    BranchDto branchDto = newMainBranchDto(project, project.uuid());
-    db.getDbClient().branchDao().insert(db.getSession(), branchDto);
+
     db.newCodePeriods().insert(new NewCodePeriodDto()
-      .setProjectUuid(project.uuid())
-      .setBranchUuid(branchDto.getUuid())
+      .setProjectUuid(projectData.getProjectDto().getUuid())
+      .setBranchUuid(mainBranch.uuid())
       .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
       .setValue(a1.getUuid()));
     db.commit();
@@ -172,9 +173,9 @@ public class SearchActionIT {
       .setData("{stillFailing: true, status: \"ERROR\"}")
       .setCategory(CATEGORY_ALERT)
       .setDescription(""));
-    EventComponentChangeDto changeDto1 = generateEventComponentChange(eventDto, FAILED_QUALITY_GATE, "My project", "app1", "master", project.uuid());
+    EventComponentChangeDto changeDto1 = generateEventComponentChange(eventDto, FAILED_QUALITY_GATE, "My project", "app1", "master", mainBranch.uuid());
     EventComponentChangeDto changeDto2 = generateEventComponentChange(eventDto, FAILED_QUALITY_GATE, "Another project", "app2", "master", uuidFactoryFast.create());
-    insertEventComponentChanges(project, a3, changeDto1, changeDto2);
+    insertEventComponentChanges(mainBranch, a3, changeDto1, changeDto2);
 
     String result = ws.newRequest()
       .setParam(PARAM_PROJECT, KEY_PROJECT_EXAMPLE_001)
@@ -183,15 +184,21 @@ public class SearchActionIT {
     assertJson(result).isSimilarTo(getClass().getResource("search-example.json"));
   }
 
+  private void addProjectPermission(ProjectData projectData) {
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto())
+      .addProjectBranchMapping(projectData.getProjectDto().getUuid(), projectData.getMainBranchComponent());
+  }
+
   @Test
   public void return_analyses_ordered_by_analysis_date() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setCreatedAt(1_000_000L));
-    db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setCreatedAt(2_000_000L));
-    db.components().insertSnapshot(newAnalysis(project).setUuid("A3").setCreatedAt(3_000_000L));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A1").setCreatedAt(1_000_000L));
+    db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A2").setCreatedAt(2_000_000L));
+    db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A3").setCreatedAt(3_000_000L));
 
-    List<Analysis> result = call(project.getKey()).getAnalysesList();
+    List<Analysis> result = call(mainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(3);
     assertThat(result).extracting(Analysis::getKey, a -> parseDateTime(a.getDate()).getTime()).containsExactly(
@@ -202,10 +209,11 @@ public class SearchActionIT {
 
   @Test
   public void return_only_processed_analyses() {
-    ComponentDto project = db.components().insertPrivateProject(c -> c.setKey("P1")).getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    db.components().insertSnapshot(newAnalysis(project).setUuid("A1"));
-    db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setStatus(SnapshotDto.STATUS_UNPROCESSED));
+    ProjectData projectData = db.components().insertPrivateProject(c -> c.setKey("P1"));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A1"));
+    db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A2").setStatus(SnapshotDto.STATUS_UNPROCESSED));
 
     List<Analysis> result = call("P1").getAnalysesList();
 
@@ -215,9 +223,10 @@ public class SearchActionIT {
 
   @Test
   public void return_events() {
-    ComponentDto project = db.components().insertPrivateProject(c -> c.setKey("P1")).getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1"));
+    ProjectData projectData = db.components().insertPrivateProject(c -> c.setKey("P1"));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A1"));
     SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(ComponentTesting.newPrivateProjectDto()).setUuid("A42"));
     EventDto e1 = db.events().insertEvent(newEvent(a1).setUuid("E1").setName("N1").setCategory(QUALITY_GATE.getLabel()).setDescription("D1"));
     EventDto e2 = db.events().insertEvent(newEvent(a1).setUuid("E2").setName("N2").setCategory(VERSION.getLabel()).setDescription("D2"));
@@ -235,13 +244,14 @@ public class SearchActionIT {
 
   @Test
   public void return_analyses_of_application() {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
-    SnapshotDto secondAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(2_000_000L));
-    SnapshotDto thirdAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(3_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto appMainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(1_000_000L));
+    SnapshotDto secondAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(2_000_000L));
+    SnapshotDto thirdAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(3_000_000L));
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(appMainBranch.getKey()).getAnalysesList();
 
     assertThat(result)
       .hasSize(3)
@@ -254,15 +264,16 @@ public class SearchActionIT {
 
   @Test
   public void return_definition_change_events_on_application_analyses() {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto mainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(mainBranch).setCreatedAt(1_000_000L));
     EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
     EventComponentChangeDto changeDto1 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
     EventComponentChangeDto changeDto2 = generateEventComponentChange(event, REMOVED, "Another project", "app2", "master", uuidFactoryFast.create());
-    insertEventComponentChanges(application, firstAnalysis, changeDto1, changeDto2);
+    insertEventComponentChanges(mainBranch, firstAnalysis, changeDto1, changeDto2);
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(mainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(1);
     List<Event> events = result.get(0).getEventsList();
@@ -279,15 +290,16 @@ public class SearchActionIT {
   @Test
   @UseDataProvider("changedBranches")
   public void application_definition_change_with_branch(@Nullable String oldBranch, @Nullable String newBranch) {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto appMainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(1_000_000L));
     EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
     EventComponentChangeDto changeDto1 = generateEventComponentChange(event, REMOVED, "My project", "app1", oldBranch, uuidFactoryFast.create());
     EventComponentChangeDto changeDto2 = generateEventComponentChange(event, ADDED, "My project", "app1", newBranch, changeDto1.getComponentUuid());
-    insertEventComponentChanges(application, firstAnalysis, changeDto1, changeDto2);
+    insertEventComponentChanges(appMainBranch, firstAnalysis, changeDto1, changeDto2);
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(appMainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(1);
     List<Event> events = result.get(0).getEventsList();
@@ -301,18 +313,19 @@ public class SearchActionIT {
 
   @Test
   public void incorrect_eventcomponentchange_two_identical_changes_added_on_same_project() {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto appMainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(1_000_000L));
     EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
     EventComponentChangeDto changeDto1 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
     EventComponentChangeDto changeDto2 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
-    EventPurgeData eventPurgeData = new EventPurgeData(application.uuid(), firstAnalysis.getUuid());
+    EventPurgeData eventPurgeData = new EventPurgeData(appMainBranch.uuid(), firstAnalysis.getUuid());
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto1, eventPurgeData);
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto2, eventPurgeData);
     db.getSession().commit();
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(appMainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(1);
     List<Event> events = result.get(0).getEventsList();
@@ -330,16 +343,17 @@ public class SearchActionIT {
 
   @Test
   public void incorrect_eventcomponentchange_incorrect_category() {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto appMainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(1_000_000L));
     EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
     EventComponentChangeDto changeDto1 = generateEventComponentChange(event, FAILED_QUALITY_GATE, "My project", "app1", "master", uuidFactoryFast.create());
-    EventPurgeData eventPurgeData = new EventPurgeData(application.uuid(), firstAnalysis.getUuid());
+    EventPurgeData eventPurgeData = new EventPurgeData(appMainBranch.uuid(), firstAnalysis.getUuid());
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto1, eventPurgeData);
     db.getSession().commit();
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(appMainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(1);
     List<Event> events = result.get(0).getEventsList();
@@ -356,20 +370,21 @@ public class SearchActionIT {
 
   @Test
   public void incorrect_eventcomponentchange_three_component_changes_on_same_project() {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto appMainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(1_000_000L));
     EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
     EventComponentChangeDto changeDto1 = generateEventComponentChange(event, ADDED, "My project", "app1", "master", uuidFactoryFast.create());
     EventComponentChangeDto changeDto2 = generateEventComponentChange(event, REMOVED, "Another project", "app1", "", uuidFactoryFast.create());
     EventComponentChangeDto changeDto3 = generateEventComponentChange(event, REMOVED, "Another project", "app1", "", uuidFactoryFast.create());
-    EventPurgeData eventPurgeData = new EventPurgeData(application.uuid(), firstAnalysis.getUuid());
+    EventPurgeData eventPurgeData = new EventPurgeData(appMainBranch.uuid(), firstAnalysis.getUuid());
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto1, eventPurgeData);
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto2, eventPurgeData);
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto3, eventPurgeData);
     db.getSession().commit();
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(appMainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(1);
     List<Event> events = result.get(0).getEventsList();
@@ -385,11 +400,17 @@ public class SearchActionIT {
         format("Too many changes on same project (3) for eventComponentChange uuids : %s,%s,%s", changeDto1.getUuid(), changeDto2.getUuid(), changeDto3.getUuid()));
   }
 
+  private void registerApplication(ProjectData appData) {
+    userSession.registerApplication(appData.getProjectDto())
+      .addProjectBranchMapping(appData.projectUuid(), appData.getMainBranchComponent());
+  }
+
   @Test
   public void incorrect_quality_gate_information() {
-    ComponentDto application = db.components().insertPublicApplication().getMainBranchComponent();
-    userSession.registerApplication(toProjectDto(application, 1L));
-    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    ProjectData appData = db.components().insertPublicApplication();
+    ComponentDto appMainBranch = appData.getMainBranchComponent();
+    registerApplication(appData);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(appMainBranch).setCreatedAt(1_000_000L));
     EventDto event = db.events().insertEvent(
       newEvent(firstAnalysis)
         .setName("")
@@ -397,11 +418,11 @@ public class SearchActionIT {
         .setCategory(CATEGORY_ALERT)
         .setData("UNPARSEABLE JSON")); // Error in Data
     EventComponentChangeDto changeDto1 = generateEventComponentChange(event, FAILED_QUALITY_GATE, "My project", "app1", "master", uuidFactoryFast.create());
-    EventPurgeData eventPurgeData = new EventPurgeData(application.uuid(), firstAnalysis.getUuid());
+    EventPurgeData eventPurgeData = new EventPurgeData(appMainBranch.uuid(), firstAnalysis.getUuid());
     db.getDbClient().eventComponentChangeDao().insert(db.getSession(), changeDto1, eventPurgeData);
     db.getSession().commit();
 
-    List<Analysis> result = call(application.getKey()).getAnalysesList();
+    List<Analysis> result = call(appMainBranch.getKey()).getAnalysesList();
 
     assertThat(result).hasSize(1);
     List<Event> events = result.get(0).getEventsList();
@@ -435,12 +456,13 @@ public class SearchActionIT {
 
   @Test
   public void paginate_analyses() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    IntStream.rangeClosed(1, 9).forEach(i -> db.components().insertSnapshot(newAnalysis(project).setCreatedAt(1_000_000L * i).setUuid("A" + i)));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    IntStream.rangeClosed(1, 9).forEach(i -> db.components().insertSnapshot(newAnalysis(mainBranch).setCreatedAt(1_000_000L * i).setUuid("A" + i)));
 
     SearchResponse result = call(SearchRequest.builder()
-      .setProject(project.getKey())
+      .setProject(mainBranch.getKey())
       .setPage(2)
       .setPageSize(3)
       .build());
@@ -451,11 +473,12 @@ public class SearchActionIT {
 
   @Test
   public void filter_by_category() {
-    ComponentDto project = db.components().insertPrivateProject(c -> c.setKey("P1")).getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1"));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("A2"));
-    SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(project).setUuid("A42"));
+    ProjectData projectData = db.components().insertPrivateProject(c -> c.setKey("P1"));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A1"));
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A2"));
+    SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A42"));
     db.events().insertEvent(newEvent(a1).setUuid("E11").setCategory(VERSION.getLabel()));
     db.events().insertEvent(newEvent(a1).setUuid("E12").setCategory(QUALITY_GATE.getLabel()));
     db.events().insertEvent(newEvent(a2).setUuid("E21").setCategory(QUALITY_GATE.getLabel()));
@@ -472,12 +495,13 @@ public class SearchActionIT {
 
   @Test
   public void paginate_with_filter_on_category() {
-    ComponentDto project = db.components().insertPrivateProject(c -> c.setKey("P1")).getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setCreatedAt(1_000_000L));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setCreatedAt(2_000_000L));
-    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project).setUuid("A3").setCreatedAt(3_000_000L));
-    SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(project).setUuid("A42"));
+    ProjectData projectData = db.components().insertPrivateProject(c -> c.setKey("P1"));
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A1").setCreatedAt(1_000_000L));
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A2").setCreatedAt(2_000_000L));
+    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A3").setCreatedAt(3_000_000L));
+    SnapshotDto a42 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("A42"));
     db.events().insertEvent(newEvent(a1).setUuid("E11").setCategory(VERSION.getLabel()));
     db.events().insertEvent(newEvent(a1).setUuid("E12").setCategory(QUALITY_GATE.getLabel()));
     db.events().insertEvent(newEvent(a2).setUuid("E21").setCategory(QUALITY_GATE.getLabel()));
@@ -499,15 +523,16 @@ public class SearchActionIT {
 
   @Test
   public void filter_from_date() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("a1").setCreatedAt(1_000_000_000L));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("a2").setCreatedAt(2_000_000_000L));
-    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project).setUuid("a3").setCreatedAt(3_000_000_000L));
-    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(project).setUuid("a4").setCreatedAt(4_000_000_000L));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a1").setCreatedAt(1_000_000_000L));
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a2").setCreatedAt(2_000_000_000L));
+    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a3").setCreatedAt(3_000_000_000L));
+    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a4").setCreatedAt(4_000_000_000L));
 
     SearchResponse result = call(SearchRequest.builder()
-      .setProject(project.getKey())
+      .setProject(mainBranch.getKey())
       .setFrom(formatDateTime(2_000_000_000L))
       .build());
 
@@ -519,15 +544,16 @@ public class SearchActionIT {
 
   @Test
   public void filter_to_date() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("a1").setCreatedAt(1_000_000_000L));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("a2").setCreatedAt(2_000_000_000L));
-    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project).setUuid("a3").setCreatedAt(3_000_000_000L));
-    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(project).setUuid("a4").setCreatedAt(4_000_000_000L));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a1").setCreatedAt(1_000_000_000L));
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a2").setCreatedAt(2_000_000_000L));
+    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a3").setCreatedAt(3_000_000_000L));
+    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a4").setCreatedAt(4_000_000_000L));
 
     SearchResponse result = call(SearchRequest.builder()
-      .setProject(project.getKey())
+      .setProject(mainBranch.getKey())
       .setTo(formatDateTime(2_000_000_000L))
       .build());
 
@@ -539,15 +565,16 @@ public class SearchActionIT {
 
   @Test
   public void filter_by_dates_using_datetime_format() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("a1").setCreatedAt(1_000_000_000L));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("a2").setCreatedAt(2_000_000_000L));
-    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project).setUuid("a3").setCreatedAt(3_000_000_000L));
-    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(project).setUuid("a4").setCreatedAt(4_000_000_000L));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a1").setCreatedAt(1_000_000_000L));
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a2").setCreatedAt(2_000_000_000L));
+    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a3").setCreatedAt(3_000_000_000L));
+    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a4").setCreatedAt(4_000_000_000L));
 
     SearchResponse result = call(SearchRequest.builder()
-      .setProject(project.getKey())
+      .setProject(mainBranch.getKey())
       .setFrom(formatDateTime(2_000_000_000L))
       .setTo(formatDateTime(3_000_000_000L))
       .build());
@@ -560,15 +587,16 @@ public class SearchActionIT {
 
   @Test
   public void filter_by_dates_using_date_format() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(project).setUuid("a1").setCreatedAt(1_000_000_000L));
-    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(project).setUuid("a2").setCreatedAt(2_000_000_000L));
-    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(project).setUuid("a3").setCreatedAt(3_000_000_000L));
-    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(project).setUuid("a4").setCreatedAt(4_000_000_000L));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto a1 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a1").setCreatedAt(1_000_000_000L));
+    SnapshotDto a2 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a2").setCreatedAt(2_000_000_000L));
+    SnapshotDto a3 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a3").setCreatedAt(3_000_000_000L));
+    SnapshotDto a4 = db.components().insertSnapshot(newAnalysis(mainBranch).setUuid("a4").setCreatedAt(4_000_000_000L));
 
     SearchResponse result = call(SearchRequest.builder()
-      .setProject(project.getKey())
+      .setProject(mainBranch.getKey())
       .setFrom(formatDate(new Date(2_000_000_000L)))
       .setTo(formatDate(new Date(3_000_000_000L)))
       .build());
@@ -581,18 +609,19 @@ public class SearchActionIT {
 
   @Test
   public void branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
-    userSession.addProjectBranchMapping(project.uuid(), branch);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
+    userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
     SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(branch));
     EventDto event = db.events().insertEvent(newEvent(analysis).setCategory(QUALITY_GATE.getLabel()));
 
     List<Analysis> result = call(SearchRequest.builder()
-      .setProject(project.getKey())
+      .setProject(mainBranch.getKey())
       .setBranch("my_branch")
       .build())
-        .getAnalysesList();
+      .getAnalysesList();
 
     assertThat(result).extracting(Analysis::getKey).containsExactlyInAnyOrder(analysis.getUuid());
     assertThat(result.get(0).getEventsList()).extracting(Event::getKey).containsExactlyInAnyOrder(event.getUuid());
@@ -600,10 +629,11 @@ public class SearchActionIT {
 
   @Test
   public void empty_response() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
 
-    SearchResponse result = call(project.getKey());
+    SearchResponse result = call(mainBranch.getKey());
 
     assertThat(result.hasPaging()).isTrue();
     assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 0);
@@ -612,16 +642,17 @@ public class SearchActionIT {
 
   @Test
   public void populates_projectVersion_and_buildString() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
-    SnapshotDto[] analyses = new SnapshotDto[] {
-      db.components().insertSnapshot(newAnalysis(project).setProjectVersion(null).setBuildString(null)),
-      db.components().insertSnapshot(newAnalysis(project).setProjectVersion("a").setBuildString(null)),
-      db.components().insertSnapshot(newAnalysis(project).setProjectVersion(null).setBuildString("b")),
-      db.components().insertSnapshot(newAnalysis(project).setProjectVersion("c").setBuildString("d"))
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    addProjectPermission(projectData);
+    SnapshotDto[] analyses = new SnapshotDto[]{
+      db.components().insertSnapshot(newAnalysis(mainBranch).setProjectVersion(null).setBuildString(null)),
+      db.components().insertSnapshot(newAnalysis(mainBranch).setProjectVersion("a").setBuildString(null)),
+      db.components().insertSnapshot(newAnalysis(mainBranch).setProjectVersion(null).setBuildString("b")),
+      db.components().insertSnapshot(newAnalysis(mainBranch).setProjectVersion("c").setBuildString("d"))
     };
 
-    SearchResponse result = call(project.getKey());
+    SearchResponse result = call(mainBranch.getKey());
 
     assertThat(result.getAnalysesList())
       .extracting(Analysis::getKey, Analysis::getProjectVersion, Analysis::getBuildString)
@@ -635,24 +666,23 @@ public class SearchActionIT {
   @Test
   public void fail_if_not_enough_permissions() {
     userSession.anonymous();
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
 
-    var projectDbKey = project.getKey();
+    var projectDbKey = mainBranch.getKey();
     assertThatThrownBy(() -> call(projectDbKey))
       .isInstanceOf(ForbiddenException.class);
   }
 
   @Test
   public void fail_if_not_enough_permissions_on_applications_projects() {
-    ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
-    ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto application = db.components().insertPrivateApplication().getProjectDto();
+    ProjectDto project1 = db.components().insertPrivateProject().getProjectDto();
+    ProjectDto project2 = db.components().insertPrivateProject().getProjectDto();
 
     userSession.logIn()
-      .registerApplication(
-        toProjectDto(application, 1L),
-        toProjectDto(project1, 1L),
-        toProjectDto(project2, 1L))
+      .registerApplication(application)
+      .registerProjects(project1, project2)
       .addProjectPermission(UserRole.USER, application, project1);
 
     var projectDbKey = application.getKey();
@@ -684,7 +714,7 @@ public class SearchActionIT {
   public void fail_if_branch_does_not_exist() {
     ProjectData projectData = db.components().insertPrivateProject();
     ComponentDto mainBranch = projectData.getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
+    addProjectPermission(projectData);
     db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
 
     var searchRequest = SearchRequest.builder()
index af475156e7bc364318de51708dd9f0f530a9818e..98204c9eb4a448a6ef5b0a2753c9b21c2a109afa 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.permission.GlobalPermission;
@@ -77,7 +78,7 @@ public class ProjectStatusActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final DbClient dbClient = db.getDbClient();
   private final DbSession dbSession = db.getSession();
@@ -105,16 +106,17 @@ public class ProjectStatusActionIT {
 
   @Test
   public void test_json_example() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
     MetricDto gateDetailsMetric = insertGateDetailMetric();
 
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch)
       .setPeriodMode("last_version")
       .setPeriodParam("2015-12-07")
       .setPeriodDate(956789123987L));
     dbClient.measureDao().insert(dbSession,
-      newMeasureDto(gateDetailsMetric, project, snapshot)
+      newMeasureDto(gateDetailsMetric, mainBranch, snapshot)
         .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionIT/measure_data.json"), StandardCharsets.UTF_8)));
     dbSession.commit();
 
@@ -127,26 +129,27 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_past_status_when_project_is_referenced_by_past_analysis_id() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto pastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto pastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch)
       .setLast(false)
       .setPeriodMode("last_version")
       .setPeriodParam("2015-12-07")
       .setPeriodDate(956789123987L));
-    SnapshotDto lastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
+    SnapshotDto lastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch)
       .setLast(true)
       .setPeriodMode("last_version")
       .setPeriodParam("2016-12-07")
       .setPeriodDate(1_500L));
     MetricDto gateDetailsMetric = insertGateDetailMetric();
     dbClient.measureDao().insert(dbSession,
-      newMeasureDto(gateDetailsMetric, project, pastAnalysis)
+      newMeasureDto(gateDetailsMetric, mainBranch, pastAnalysis)
         .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionIT/measure_data.json"))));
     dbClient.measureDao().insert(dbSession,
-      newMeasureDto(gateDetailsMetric, project, lastAnalysis)
+      newMeasureDto(gateDetailsMetric, mainBranch, lastAnalysis)
         .setData("not_used"));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     String response = ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, pastAnalysis.getUuid())
@@ -157,20 +160,21 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_live_status_when_project_is_referenced_by_its_id() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch)
       .setPeriodMode("last_version")
       .setPeriodParam("2015-12-07")
       .setPeriodDate(956789123987L));
     MetricDto gateDetailsMetric = insertGateDetailMetric();
     dbClient.liveMeasureDao().insert(dbSession,
-      newLiveMeasure(project, gateDetailsMetric)
+      newLiveMeasure(mainBranch, gateDetailsMetric)
         .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionIT/measure_data.json"))));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     String response = ws.newRequest()
-      .setParam(PARAM_PROJECT_ID, project.uuid())
+      .setParam(PARAM_PROJECT_ID, projectData.projectUuid())
       .execute().getInput();
 
     assertJson(response).isSimilarTo(getClass().getResource("project_status-example.json"));
@@ -178,8 +182,9 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_past_status_when_branch_is_referenced_by_past_analysis_id() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch);
     SnapshotDto pastAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(branch)
       .setLast(false)
       .setPeriodMode("last_version")
@@ -198,7 +203,7 @@ public class ProjectStatusActionIT {
       newMeasureDto(gateDetailsMetric, branch, lastAnalysis)
         .setData("not_used"));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     String response = ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, pastAnalysis.getUuid())
@@ -209,7 +214,8 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_live_status_when_project_is_referenced_by_its_key() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
       .setPeriodMode("last_version")
       .setPeriodParam("2015-12-07")
@@ -219,7 +225,7 @@ public class ProjectStatusActionIT {
       newLiveMeasure(project, gateDetailsMetric)
         .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionIT/measure_data.json"))));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     String response = ws.newRequest()
       .setParam(PARAM_PROJECT_KEY, project.getKey())
@@ -230,9 +236,10 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_live_status_when_branch_is_referenced_by_its_key() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
     String branchName = randomAlphanumeric(248);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
 
     dbClient.snapshotDao().insert(dbSession, newAnalysis(branch)
       .setPeriodMode("last_version")
@@ -243,10 +250,10 @@ public class ProjectStatusActionIT {
       newLiveMeasure(branch, gateDetailsMetric)
         .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionIT/measure_data.json"))));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     String response = ws.newRequest()
-      .setParam(PARAM_PROJECT_KEY, project.getKey())
+      .setParam(PARAM_PROJECT_KEY, mainBranch.getKey())
       .setParam(PARAM_BRANCH, branchName)
       .execute().getInput();
 
@@ -255,9 +262,10 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_live_status_when_pull_request_is_referenced_by_its_key() throws IOException {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
     String pullRequestKey = RandomStringUtils.randomAlphanumeric(100);
-    ComponentDto pr = db.components().insertProjectBranch(project, branch -> branch.setBranchType(BranchType.PULL_REQUEST)
+    ComponentDto pr = db.components().insertProjectBranch(mainBranch, branch -> branch.setBranchType(BranchType.PULL_REQUEST)
       .setKey(pullRequestKey));
 
     dbClient.snapshotDao().insert(dbSession, newAnalysis(pr)
@@ -269,10 +277,10 @@ public class ProjectStatusActionIT {
       newLiveMeasure(pr, gateDetailsMetric)
         .setData(IOUtils.toString(getClass().getResource("ProjectStatusActionIT/measure_data.json"))));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     String response = ws.newRequest()
-      .setParam(PARAM_PROJECT_KEY, project.getKey())
+      .setParam(PARAM_PROJECT_KEY, mainBranch.getKey())
       .setParam(PARAM_PULL_REQUEST, pullRequestKey)
       .execute().getInput();
 
@@ -281,10 +289,11 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_undefined_status_if_specified_analysis_is_not_found() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     ProjectStatusResponse result = ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
@@ -296,11 +305,12 @@ public class ProjectStatusActionIT {
 
   @Test
   public void return_undefined_status_if_project_is_not_analyzed() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    userSession.addProjectPermission(UserRole.USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     ProjectStatusResponse result = ws.newRequest()
-      .setParam(PARAM_PROJECT_ID, project.uuid())
+      .setParam(PARAM_PROJECT_ID, projectData.projectUuid())
       .executeProtobuf(ProjectStatusResponse.class);
 
     assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE);
@@ -309,10 +319,11 @@ public class ProjectStatusActionIT {
 
   @Test
   public void project_administrator_is_allowed_to_get_project_status() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.ADMIN, project);
+    userSession.addProjectPermission(UserRole.ADMIN, projectData.getProjectDto());
 
     ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
@@ -321,10 +332,11 @@ public class ProjectStatusActionIT {
 
   @Test
   public void project_user_is_allowed_to_get_project_status() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
@@ -333,13 +345,14 @@ public class ProjectStatusActionIT {
 
   @Test
   public void check_cayc_compliant_flag() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
     var qg = db.qualityGates().insertBuiltInQualityGate();
     db.qualityGates().setDefaultQualityGate(qg);
-    when(qualityGateCaycChecker.checkCaycCompliantFromProject(any(DbSession.class), eq(project.uuid()))).thenReturn(COMPLIANT);
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    when(qualityGateCaycChecker.checkCaycCompliantFromProject(any(DbSession.class), eq(projectData.projectUuid()))).thenReturn(COMPLIANT);
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.USER, project);
+    userSession.addProjectPermission(UserRole.USER, projectData.getProjectDto());
 
     ProjectStatusResponse result = ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
@@ -350,10 +363,11 @@ public class ProjectStatusActionIT {
 
   @Test
   public void user_with_project_scan_permission_is_allowed_to_get_project_status() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
-    userSession.addProjectPermission(UserRole.SCAN, project);
+    userSession.addProjectPermission(UserRole.SCAN, projectData.getProjectDto());
 
     var response = ws.newRequest()
       .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid()).execute();
@@ -363,8 +377,9 @@ public class ProjectStatusActionIT {
 
   @Test
   public void user_with_global_scan_permission_is_allowed_to_get_project_status() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
     userSession.addPermission(GlobalPermission.SCAN);
 
@@ -387,8 +402,9 @@ public class ProjectStatusActionIT {
 
   @Test
   public void fail_if_insufficient_privileges() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch));
     dbSession.commit();
     userSession.logIn();
 
@@ -437,9 +453,10 @@ public class ProjectStatusActionIT {
 
   @Test
   public void fail_when_using_branch_uuid() {
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
-    ComponentDto branch = db.components().insertProjectBranch(project);
+    ProjectData projectData = db.components().insertPublicProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    userSession.logIn().addProjectPermission(UserRole.ADMIN, projectData.getProjectDto());
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch);
     SnapshotDto snapshot = db.components().insertSnapshot(branch);
 
     assertThatThrownBy(() -> ws.newRequest()
index 26fac5d235ab2a5f36ed8211d9b8dcebadfb1e3c..1a47676e9dd83a265591973d1bad14eace79227e 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.db.DbTester;
 import org.sonar.db.audit.NoOpAuditPersister;
 import org.sonar.db.component.BranchDao;
 import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.project.ProjectDao;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.scannercache.ScannerAnalysisCacheDao;
@@ -55,7 +56,7 @@ public class GetActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+  public DbTester dbTester = DbTester.create(System2.INSTANCE, true);
 
   private final ScannerAnalysisCacheDao dao = new ScannerAnalysisCacheDao();
   private final ProjectDao projectDao = new ProjectDao(System2.INSTANCE, new NoOpAuditPersister());
@@ -67,17 +68,17 @@ public class GetActionIT {
 
   @Test
   public void get_data_for_project() throws IOException {
-    ProjectDto project1 = dbTester.components().insertPrivateProject().getProjectDto();
-    BranchDto branch = dbTester.components().insertProjectBranch(project1);
-    ProjectDto project2 = dbTester.components().insertPrivateProject().getProjectDto();
+    ProjectData projectData = dbTester.components().insertPrivateProject();
+    BranchDto branch = dbTester.components().insertProjectBranch(projectData.getProjectDto());
+    ProjectData projectData2 = dbTester.components().insertPrivateProject();
 
-    dao.insert(dbTester.getSession(), project1.getUuid(), stringToCompressedInputStream("test data1"));
+    dao.insert(dbTester.getSession(), projectData.getMainBranchDto().getUuid(), stringToCompressedInputStream("test data1"));
     dao.insert(dbTester.getSession(), branch.getUuid(), stringToCompressedInputStream("test data2"));
-    dao.insert(dbTester.getSession(), project2.getUuid(), stringToCompressedInputStream("test data3"));
+    dao.insert(dbTester.getSession(), projectData2.getMainBranchComponent().uuid(), stringToCompressedInputStream("test data3"));
 
-    userSession.logIn().addProjectPermission(SCAN, project1);
+    userSession.logIn().addProjectPermission(SCAN, projectData.getProjectDto());
     TestResponse response = wsTester.newRequest()
-      .setParam("project", project1.getKey())
+      .setParam("project", projectData.projectKey())
       .setHeader("Accept-Encoding", "gzip")
       .execute();
 
@@ -87,9 +88,10 @@ public class GetActionIT {
 
   @Test
   public void get_uncompressed_data_for_project() throws IOException {
-    ProjectDto project1 = dbTester.components().insertPrivateProject().getProjectDto();
+    ProjectData projectData = dbTester.components().insertPrivateProject();
+    ProjectDto project1 = projectData.getProjectDto();
 
-    dao.insert(dbTester.getSession(), project1.getUuid(), stringToCompressedInputStream("test data1"));
+    dao.insert(dbTester.getSession(), projectData.getMainBranchDto().getUuid(), stringToCompressedInputStream("test data1"));
 
     userSession.logIn().addProjectPermission(SCAN, project1);
 
index 1e118a61c8bbae0524690853cfbf0b07f7ce38b4..4925a930beb00eb6ce73deb9efa646fb91a88456 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.core.platform.PlatformEditionProvider;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.issue.AvatarResolverImpl;
 import org.sonar.server.permission.PermissionService;
@@ -57,7 +58,7 @@ public class CurrentActionHomepageIT {
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   private final DbClient dbClient = db.getDbClient();
 
@@ -144,15 +145,16 @@ public class CurrentActionHomepageIT {
   @UseDataProvider("allEditions")
   public void return_homepage_when_set_to_a_project(EditionProvider.Edition edition) {
     setPlatformEdition(edition);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(project.uuid()));
-    userSessionRule.logIn(user).addProjectPermission(USER, project);
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto mainBranch = projectData.getMainBranchComponent();
+    UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageParameter(mainBranch.uuid()));
+    userSessionRule.logIn(user).addProjectPermission(USER, projectData.getProjectDto());
 
     CurrentWsResponse response = call();
 
     assertThat(response.getHomepage())
       .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent)
-      .containsExactly(CurrentWsResponse.HomepageType.PROJECT, project.getKey());
+      .containsExactly(CurrentWsResponse.HomepageType.PROJECT, mainBranch.getKey());
   }
 
   @Test
@@ -172,11 +174,12 @@ public class CurrentActionHomepageIT {
 
   @Test
   public void return_homepage_when_set_to_a_branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ComponentDto project = projectData.getMainBranchComponent();
     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);
+    userSessionRule.logIn(user).addProjectPermission(USER, projectData.getProjectDto());
 
     CurrentWsResponse response = call();
 
index a84688abd6342e839b47a2609e09b717e3434f30..893033bd2a2481f829672d995be76ab6a1b3fc0a 100644 (file)
@@ -45,7 +45,7 @@ public class SetHomepageActionIT {
   public UserSessionRule userSession = UserSessionRule.standalone();
 
   @Rule
-  public DbTester db = DbTester.create();
+  public DbTester db = DbTester.create(true);
 
   private final DbClient dbClient = db.getDbClient();
   private final WsActionTester ws = new WsActionTester(new SetHomepageAction(userSession, dbClient, TestComponentFinder.from(db)));
@@ -78,7 +78,7 @@ public class SetHomepageActionIT {
 
   @Test
   public void set_project_homepage() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
 
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
@@ -86,20 +86,20 @@ public class SetHomepageActionIT {
     ws.newRequest()
       .setMethod("POST")
       .setParam(PARAM_TYPE, "PROJECT")
-      .setParam("component", project.getKey())
+      .setParam("component", mainBranch.getKey())
       .execute();
 
     UserDto actual = db.getDbClient().userDao().selectByLogin(db.getSession(), user.getLogin());
     assertThat(actual).isNotNull();
     assertThat(actual.getHomepageType()).isEqualTo("PROJECT");
-    assertThat(actual.getHomepageParameter()).isEqualTo(project.uuid());
+    assertThat(actual.getHomepageParameter()).isEqualTo(mainBranch.uuid());
   }
 
   @Test
   public void set_branch_homepage() {
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
+    ComponentDto mainBranch = db.components().insertPublicProject().getMainBranchComponent();
     String branchName = randomAlphanumeric(248);
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+    ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
 
index e1bd91e496d16e6d64e7ddc1d46482b2a9a416e0..b52267876b7f2fe5fb2fe6ab55728ab406d04428 100644 (file)
@@ -141,8 +141,8 @@ public class SearchEventsAction implements DevelopersWsAction {
         return Stream.empty();
       }
 
-      List<String> projectUuids = analyses.stream().map(SnapshotDto::getComponentUuid).collect(toList());
-      Map<String, BranchDto> branchesByUuids = dbClient.branchDao().selectByUuids(dbSession, projectUuids).stream().collect(uniqueIndex(BranchDto::getUuid));
+      List<String> branchUuids = analyses.stream().map(SnapshotDto::getComponentUuid).collect(toList());
+      Map<String, BranchDto> branchesByUuids = dbClient.branchDao().selectByUuids(dbSession, branchUuids).stream().collect(uniqueIndex(BranchDto::getUuid));
 
       return Stream.concat(
         computeQualityGateChangeEvents(dbSession, projectsByUuid, branchesByUuids, analyses),
index 94e070415cd67a6c27c03a3113a8b357a3d84451..8d075a46f95b79565f0fe1ba56b86874470e609d 100644 (file)
@@ -25,7 +25,6 @@ import java.util.Optional;
 import java.util.Set;
 import javax.annotation.Nullable;
 import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Scopes;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -34,14 +33,13 @@ import org.sonar.api.server.ws.WebService.NewAction;
 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.entity.EntityDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.issue.index.IssueIndex;
 import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
 import org.sonar.server.issue.index.IssueQuery;
 import org.sonarqube.ws.Issues;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
 import static org.sonar.server.issue.index.IssueQueryFactory.ISSUE_TYPE_NAMES;
@@ -65,8 +63,8 @@ public class TagsAction implements IssuesWsAction {
   private final ComponentFinder componentFinder;
 
   public TagsAction(IssueIndex issueIndex,
-                    IssueIndexSyncProgressChecker issueIndexSyncProgressChecker, DbClient dbClient,
-                    ComponentFinder componentFinder) {
+    IssueIndexSyncProgressChecker issueIndexSyncProgressChecker, DbClient dbClient,
+    ComponentFinder componentFinder) {
     this.issueIndex = issueIndex;
     this.issueIndexSyncProgressChecker = issueIndexSyncProgressChecker;
     this.dbClient = dbClient;
@@ -110,9 +108,9 @@ public class TagsAction implements IssuesWsAction {
       boolean all = request.mandatoryParamAsBoolean(PARAM_ALL);
       checkIfAnyComponentsNeedIssueSync(dbSession, projectKey);
 
-      Optional<ComponentDto> project = getProject(dbSession, projectKey);
-      Optional<BranchDto> branch = project.flatMap(p -> dbClient.branchDao().selectByBranchKey(dbSession, p.uuid(), branchKey));
-      List<String> tags = searchTags(project.orElse(null), branch.orElse(null), request, all);
+      Optional<EntityDto> entity = getProject(dbSession, projectKey);
+      Optional<BranchDto> branch = entity.flatMap(p -> dbClient.branchDao().selectByBranchKey(dbSession, p.getUuid(), branchKey));
+      List<String> tags = searchTags(entity.orElse(null), branch.orElse(null), request, all, dbSession);
 
       Issues.TagsResponse.Builder tagsResponseBuilder = Issues.TagsResponse.newBuilder();
       tags.forEach(tagsResponseBuilder::addTags);
@@ -120,13 +118,12 @@ public class TagsAction implements IssuesWsAction {
     }
   }
 
-  private Optional<ComponentDto> getProject(DbSession dbSession, @Nullable String projectKey) {
-    if (projectKey == null) {
+  private Optional<EntityDto> getProject(DbSession dbSession, @Nullable String entityKey) {
+    if (entityKey == null) {
       return Optional.empty();
     }
-    ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
-    checkArgument(project.scope().equals(Scopes.PROJECT), "Component '%s' must be a project", projectKey);
-    return Optional.of(project);
+    EntityDto entity = componentFinder.getEntityByKey(dbSession, entityKey);
+    return Optional.of(entity);
   }
 
   private void checkIfAnyComponentsNeedIssueSync(DbSession session, @Nullable String projectKey) {
@@ -137,24 +134,23 @@ public class TagsAction implements IssuesWsAction {
     }
   }
 
-  private List<String> searchTags(@Nullable ComponentDto project, @Nullable BranchDto branch, Request request, boolean all) {
+  private List<String> searchTags(@Nullable EntityDto entity, @Nullable BranchDto branch, Request request, boolean all, DbSession dbSession) {
     IssueQuery.Builder issueQueryBuilder = IssueQuery.builder()
       .types(ISSUE_TYPE_NAMES);
-    if (project != null) {
-      switch (project.qualifier()) {
-        case Qualifiers.PROJECT:
-          issueQueryBuilder.projectUuids(Set.of(project.uuid()));
-          break;
-        case Qualifiers.APP, Qualifiers.VIEW:
-          issueQueryBuilder.viewUuids(Set.of(project.uuid()));
-          break;
-        default:
-          throw new IllegalArgumentException(String.format("Component of type '%s' is not supported", project.qualifier()));
+    if (entity != null) {
+      switch (entity.getQualifier()) {
+        case Qualifiers.PROJECT -> issueQueryBuilder.projectUuids(Set.of(entity.getUuid()));
+        case Qualifiers.VIEW, Qualifiers.APP -> issueQueryBuilder.viewUuids(Set.of(entity.getUuid()));
+        default ->
+          throw new IllegalArgumentException(String.format("Entity of type '%s' is not supported", entity.getQualifier()));
       }
 
-      if (branch != null && !project.uuid().equals(branch.getUuid())) {
+      if (branch != null && !branch.isMain()) {
         issueQueryBuilder.branchUuid(branch.getUuid());
         issueQueryBuilder.mainBranch(false);
+      } else if (Qualifiers.APP.equals(entity.getQualifier())) {
+        dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, entity.getUuid())
+          .ifPresent(b -> issueQueryBuilder.branchUuid(b.getUuid()));
       }
     }
     if (all) {
index 3c9f7d18168cb02abc3612ad5d2f954c372bdd43..a3b1a9c60cbdee0c5e3fdba9f79e4fb5c39b6fa0 100644 (file)
@@ -116,11 +116,7 @@ public class SetHomepageAction implements UsersWsAction {
       case PROJECT:
         checkArgument(isNotBlank(componentParameter), PARAMETER_REQUIRED, type.name(), PARAM_COMPONENT);
         ProjectDto projectDto = componentFinder.getProjectByKey(dbSession, componentParameter);
-        if (branchParameter != null) {
-          return componentFinder.getBranchOrPullRequest(dbSession, projectDto, branchParameter, null).getUuid();
-        } else {
-          return projectDto.getUuid();
-        }
+        return componentFinder.getBranchOrPullRequest(dbSession, projectDto, branchParameter, null).getUuid();
       case PORTFOLIO, APPLICATION:
         checkArgument(isNotBlank(componentParameter), PARAMETER_REQUIRED, type.name(), PARAM_COMPONENT);
         return componentFinder.getByKey(dbSession, componentParameter).uuid();