From 02f358bec4d5636d8c23bf34d4d086b0405f65cf Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Fri, 31 Mar 2023 10:48:41 +0200 Subject: [PATCH] SONAR-18850 update branch mapper to use is main and update test framework --- .../component/BranchPersisterImplIT.java | 10 +-- .../SiblingComponentsWithOpenIssuesIT.java | 20 +++--- .../issue/SiblingsIssueMergerIT.java | 6 +- ...ssProjectDuplicationsRepositoryStepIT.java | 4 +- .../step/PersistIssuesStepIT.java | 16 ++--- .../step/ReportPersistComponentsStepIT.java | 8 ++- .../step/SendIssueNotificationsStepIT.java | 12 +++- .../analysis/ExportAnalysesStepIT.java | 3 +- .../component/ExportComponentsStepIT.java | 6 +- .../steps/ExportMeasuresStepIT.java | 2 +- .../step/PersistComponentsStep.java | 6 +- .../PullRequestSourceBranchMergerTest.java | 6 +- .../sonar/db/component/ComponentDaoIT.java | 12 ++-- .../component/ComponentKeyUpdaterDaoIT.java | 6 +- .../org/sonar/db/issue/IssueMapperIT.java | 8 +-- .../ProjectMeasuresIndexerIteratorIT.java | 2 +- .../org/sonar/db/purge/PurgeCommandsIT.java | 27 +++++--- .../java/org/sonar/db/purge/PurgeDaoIT.java | 38 +++++++---- .../main/java/org/sonar/db/ce/CeQueueDto.java | 15 ---- .../org/sonar/db/component/BranchDao.java | 8 +-- .../org/sonar/db/component/BranchMapper.java | 1 - .../org/sonar/db/component/ComponentDao.java | 21 +++--- .../sonar/db/component/ComponentMapper.xml | 3 +- .../sonar/db/component/ComponentDbTester.java | 13 ++-- .../sonar/db/component/ComponentTesting.java | 48 ++++++++++--- .../java/org/sonar/db/user/UserDbTester.java | 13 +++- .../server/issue/index/IssueIndexerIT.java | 2 +- .../server/user/ServerUserSessionTest.java | 2 +- .../issue/index/IssueIndexDebtTest.java | 24 +++---- .../issue/index/IssueIndexFacetsTest.java | 68 +++++++++---------- .../issue/index/IssueIndexFiltersTest.java | 62 ++++++++--------- .../index/IssueIndexSecurityHotspotsTest.java | 6 +- .../issue/index/IssueIndexSortTest.java | 10 +-- .../server/issue/index/IssueIndexTest.java | 24 +++---- .../issue/index/IssueQueryFactoryTest.java | 6 +- .../server/batch/ProjectDataLoaderIT.java | 4 +- .../sonar/server/ce/ws/ActivityActionIT.java | 8 ++- .../server/ce/ws/ActivityStatusActionIT.java | 10 ++- .../server/ce/ws/AnalysisStatusActionIT.java | 3 +- .../sonar/server/ce/ws/ComponentActionIT.java | 6 +- .../ce/ws/DismissAnalysisWarningActionIT.java | 3 +- .../component/ComponentCleanerServiceIT.java | 4 +- .../server/component/ComponentFinderIT.java | 8 +-- .../ComponentServiceUpdateKeyIT.java | 4 +- .../server/component/ws/AppActionIT.java | 10 +-- .../server/component/ws/ShowActionIT.java | 17 ++--- .../server/component/ws/TreeActionIT.java | 11 +-- .../duplication/ws/DuplicationsParserIT.java | 10 +-- .../server/duplication/ws/ShowActionIT.java | 4 +- .../duplication/ws/ShowResponseBuilderIT.java | 4 +- .../server/hotspot/ws/AssignActionIT.java | 4 +- .../server/hotspot/ws/SearchActionIT.java | 15 ++-- .../sonar/server/hotspot/ws/ShowActionIT.java | 2 +- .../server/issue/ws/BulkChangeActionIT.java | 4 +- .../sonar/server/issue/ws/IssueUpdaterIT.java | 4 +- .../issue/ws/SearchActionComponentsIT.java | 10 +-- .../sonar/server/issue/ws/SearchActionIT.java | 6 +- .../server/measure/ws/ComponentActionIT.java | 8 +-- .../measure/ws/ComponentTreeActionIT.java | 20 +++--- .../measure/ws/SearchHistoryActionIT.java | 4 +- .../project/ws/UpdateVisibilityActionIT.java | 3 +- .../projectanalysis/ws/SearchActionIT.java | 3 +- .../sonar/server/source/ws/LinesActionIT.java | 6 +- .../sonar/server/source/ws/RawActionIT.java | 6 +- .../sonar/server/source/ws/ScmActionIT.java | 2 +- .../sonar/server/source/ws/ShowActionIT.java | 2 +- .../sonar/server/ui/ws/ComponentActionIT.java | 2 +- .../server/component/ComponentUpdater.java | 2 +- 68 files changed, 390 insertions(+), 337 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImplIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImplIT.java index f77eb52d624..8da1da4efe2 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImplIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/BranchPersisterImplIT.java @@ -156,7 +156,7 @@ public class BranchPersisterImplIT { ComponentDto mainComponent = dbTester.components().insertPublicProject(p -> p.setKey(MAIN.getKey()).setUuid(MAIN.getUuid())); ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH)); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component, false); dbTester.commit(); underTest.persist(dbTester.getSession()); @@ -174,7 +174,7 @@ public class BranchPersisterImplIT { ComponentDto mainComponent = dbTester.components().insertPublicProject(p -> p.setKey(MAIN.getKey()).setUuid(MAIN.getUuid())); ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH)); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component, false); settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "BRANCH.*"); dbTester.commit(); @@ -193,7 +193,7 @@ public class BranchPersisterImplIT { ComponentDto mainComponent = dbTester.components().insertPublicProject(p -> p.setKey(MAIN.getKey()).setUuid(MAIN.getUuid())); ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH)); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component, false); settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "abc.*"); dbTester.commit(); @@ -220,7 +220,7 @@ public class BranchPersisterImplIT { .setProjectUuid(MAIN.getUuid()) .setBranchType(PULL_REQUEST) .setMergeBranchUuid(MAIN.getUuid())); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component, false); dbTester.commit(); underTest.persist(dbTester.getSession()); @@ -239,7 +239,7 @@ public class BranchPersisterImplIT { ComponentDto mainComponent = dbTester.components().insertPublicProject(p -> p.setKey(MAIN.getKey()).setUuid(MAIN.getUuid())); ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH)); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component, false); dbTester.commit(); underTest.persist(dbTester.getSession()); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/SiblingComponentsWithOpenIssuesIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/SiblingComponentsWithOpenIssuesIT.java index b35f634bd3e..1e9fa01615b 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/SiblingComponentsWithOpenIssuesIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/SiblingComponentsWithOpenIssuesIT.java @@ -77,26 +77,26 @@ public class SiblingComponentsWithOpenIssuesIT { b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(branch1.uuid())); - fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1, null)); + fileWithNoIssuesOnBranch1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1)); RuleDto rule = db.rules().insert(); - fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null)); + fileWithOneOpenIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1)); db.issues().insert(rule, branch1pr1, fileWithOneOpenIssueOnBranch1Pr1); - fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null)); + fileWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1)); db.issues().insert(rule, branch1pr1, fileWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED")); - fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null)); + fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1)); db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1); db.issues().insert(rule, branch1pr1, fileWithOneOpenTwoResolvedIssuesOnBranch1Pr1, i -> i.setStatus("RESOLVED")); String fileKey = "file-x"; - fileXWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1, null) + fileXWithOneResolvedIssueOnBranch1Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr1) .setKey(fileKey)); db.issues().insert(rule, branch1pr1, fileXWithOneResolvedIssueOnBranch1Pr1, i -> i.setStatus("RESOLVED")); - fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2, null) + fileXWithOneResolvedIssueOnBranch1Pr2 = db.components().insertComponent(ComponentTesting.newFileDto(branch1pr2) .setKey(fileKey)); db.issues().insert(rule, branch1pr2, fileXWithOneResolvedIssueOnBranch1Pr2, i -> i.setStatus("RESOLVED")); @@ -106,10 +106,10 @@ public class SiblingComponentsWithOpenIssuesIT { b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(branch2.uuid())); - fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null)); + fileWithOneOpenIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1)); db.issues().insert(rule, branch2pr1, fileWithOneOpenIssueOnBranch2Pr1); - fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1, null)); + fileWithOneResolvedIssueOnBranch2Pr1 = db.components().insertComponent(ComponentTesting.newFileDto(branch2pr1)); db.issues().insert(rule, branch2pr1, fileWithOneResolvedIssueOnBranch2Pr1, i -> i.setStatus("RESOLVED")); setRoot(branch1); underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient()); @@ -168,7 +168,7 @@ public class SiblingComponentsWithOpenIssuesIT { RuleDto rule = db.rules().insert(); - ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest, null)); + ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest)); db.issues().insert(rule, pullRequest, fileWithResolvedIssueOnPullrequest, i -> i.setStatus("RESOLVED")); underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient()); @@ -187,7 +187,7 @@ public class SiblingComponentsWithOpenIssuesIT { RuleDto rule = db.rules().insert(); - ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch, null)); + ComponentDto fileWithResolvedIssueOnDerivedBranch = db.components().insertComponent(ComponentTesting.newFileDto(derivedBranch)); db.issues().insert(rule, derivedBranch, fileWithResolvedIssueOnDerivedBranch, i -> i.setStatus("RESOLVED")); underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient()); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMergerIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMergerIT.java index 870e352dcdb..ccc993af92f 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMergerIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/issue/SiblingsIssueMergerIT.java @@ -115,9 +115,9 @@ public class SiblingsIssueMergerIT { branch3Dto = db.components().insertProjectBranch(projectDto, b -> b.setKey("myBranch3") .setBranchType(BranchType.PULL_REQUEST) .setMergeBranchUuid(projectDto.uuid())); - fileOnBranch1Dto = db.components().insertComponent(newFileDto(branch1Dto).setKey(FILE_1_KEY)); - fileOnBranch2Dto = db.components().insertComponent(newFileDto(branch2Dto).setKey(FILE_1_KEY)); - fileOnBranch3Dto = db.components().insertComponent(newFileDto(branch3Dto).setKey(FILE_1_KEY)); + fileOnBranch1Dto = db.components().insertComponent(newFileDto(branch1Dto, projectDto.uuid()).setKey(FILE_1_KEY)); + fileOnBranch2Dto = db.components().insertComponent(newFileDto(branch2Dto, projectDto.uuid()).setKey(FILE_1_KEY)); + fileOnBranch3Dto = db.components().insertComponent(newFileDto(branch3Dto, projectDto.uuid()).setKey(FILE_1_KEY)); rule = db.rules().insert(); when(branch.getReferenceBranchUuid()).thenReturn(projectDto.uuid()); metadataHolder.setBranch(branch); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepIT.java index 1816523c111..1db962d0d15 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepIT.java @@ -327,10 +327,10 @@ public class LoadCrossProjectDuplicationsRepositoryStepIT { } private ComponentDto createFile(String fileKey, ComponentDto project) { - ComponentDto file = ComponentTesting.newFileDto(project, null) + ComponentDto file = ComponentTesting.newFileDto(project) .setKey(fileKey) .setLanguage(XOO_LANGUAGE); - dbClient.componentDao().insert(dbSession, file); + dbClient.componentDao().insert(dbSession, file, false); dbSession.commit(); return file; } diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistIssuesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistIssuesStepIT.java index 99d9bac1ebc..ed30205c8ab 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistIssuesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistIssuesStepIT.java @@ -121,7 +121,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); when(system2.now()).thenReturn(NOW); String issueKey = "ISSUE-1"; @@ -185,7 +185,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); when(system2.now()).thenReturn(NOW); String issueKey = "ISSUE-2"; @@ -231,7 +231,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); when(system2.now()).thenReturn(NOW); String issueKey = "ISSUE-3"; @@ -290,7 +290,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setStatus(STATUS_OPEN) .setResolution(null) @@ -325,7 +325,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); session.commit(); String issueKey = "ISSUE-4"; @@ -397,7 +397,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); when(system2.now()).thenReturn(NOW); String issueKey = "ISSUE-5"; @@ -450,7 +450,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); when(system2.now()).thenReturn(NOW); String issueKey = "ISSUE-6"; @@ -499,7 +499,7 @@ public class PersistIssuesStepIT extends BaseStepTest { RuleDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01")); db.rules().insert(rule); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project, null)); + ComponentDto file = db.components().insertComponent(newFileDto(project)); when(system2.now()).thenReturn(NOW); String issueKey = "ISSUE-7"; diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistComponentsStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistComponentsStepIT.java index 1a4c4c031cc..68390b46134 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistComponentsStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistComponentsStepIT.java @@ -21,7 +21,9 @@ package org.sonar.ce.task.projectanalysis.step; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.function.Consumer; import java.util.stream.Stream; import org.junit.Before; @@ -248,7 +250,7 @@ public class ReportPersistComponentsStepIT extends BaseStepTest { ComponentDto directory = ComponentTesting.newDirectory(project, "src").setUuid("CDEF").setKey("PROJECT_KEY:src"); ComponentDto file = ComponentTesting.newFileDto(project, directory, "DEFG").setPath("src/foo").setName("foo") .setKey("PROJECT_KEY:src/foo"); - dbClient.componentDao().insert(db.getSession(), directory, file); + dbClient.componentDao().insert(db.getSession(), Set.of(directory, file), true); db.getSession().commit(); assertThat(dbClient.componentDao().selectByKey(db.getSession(), PROJECT_KEY + ":src/foo").get().scope()).isEqualTo("FIL"); @@ -326,7 +328,7 @@ public class ReportPersistComponentsStepIT extends BaseStepTest { ComponentDto directory = ComponentTesting.newDirectory(project, "src/main/java/dir").setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir"); ComponentDto file = ComponentTesting.newFileDto(project, directory, "DEFG").setPath("src/main/java/dir/Foo.java").setName("Foo.java") .setKey("PROJECT_KEY:src/main/java/dir/Foo.java"); - dbClient.componentDao().insert(db.getSession(), directory, file); + dbClient.componentDao().insert(db.getSession(), Set.of(directory, file), true); db.getSession().commit(); treeRootHolder.setRoot( @@ -399,7 +401,7 @@ public class ReportPersistComponentsStepIT extends BaseStepTest { .setName("Foo.java") .setKey("PROJECT_KEY:src/main/java/dir/Foo.java") .setEnabled(false); - dbClient.componentDao().insert(db.getSession(), removedDirectory, removedFile); + dbClient.componentDao().insert(db.getSession(), Set.of(removedDirectory, removedFile), true); db.getSession().commit(); treeRootHolder.setRoot( diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/SendIssueNotificationsStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/SendIssueNotificationsStepIT.java index 5b315e9dbda..0f898673c43 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/SendIssueNotificationsStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/SendIssueNotificationsStepIT.java @@ -104,6 +104,7 @@ import static org.sonar.db.component.BranchType.PULL_REQUEST; import static org.sonar.db.component.ComponentTesting.newBranchComponent; import static org.sonar.db.component.ComponentTesting.newBranchDto; import static org.sonar.db.component.ComponentTesting.newFileDto; +import static org.sonar.db.component.ComponentTesting.newMainBranchDto; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; import static org.sonar.db.issue.IssueTesting.newIssue; import static org.sonar.db.rule.RuleTesting.newRule; @@ -568,7 +569,7 @@ public class SendIssueNotificationsStepIT extends BaseStepTest { private void sendIssueChangeNotificationOnBranch(long issueCreatedAt) { ComponentDto project = newPrivateProjectDto(); ComponentDto branch = newBranchComponent(project, newBranchDto(project).setKey(BRANCH_NAME)); - ComponentDto file = newFileDto(branch); + ComponentDto file = newFileDto(branch, project.uuid()); treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren( builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build()).build()); analysisMetadataHolder.setProject(Project.from(project)); @@ -700,8 +701,13 @@ public class SendIssueNotificationsStepIT extends BaseStepTest { } private ComponentDto setUpBranch(ComponentDto project, BranchType branchType) { - ComponentDto branch = newBranchComponent(project, newBranchDto(project, branchType).setKey(BRANCH_NAME)); - ComponentDto file = newFileDto(branch); + ComponentDto branch = null; + if(branchType == PULL_REQUEST) { + branch = newBranchComponent(project, newBranchDto(project, PULL_REQUEST, project.uuid())); + } else { + branch = newBranchComponent(project, newMainBranchDto(project).setKey(BRANCH_NAME)); + } + ComponentDto file = newFileDto(branch, project.uuid()); treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren( builder(Type.FILE, 11).setKey(file.getKey()).setName(file.longName()).build()).build()); return branch; diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java index c69ac175982..95a7f770898 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java @@ -24,6 +24,7 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.List; +import java.util.Set; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; @@ -108,7 +109,7 @@ public class ExportAnalysesStepIT { public void setUp() { ComponentDto projectDto = dbTester.components().insertPublicProject(PROJECT); componentRepository.register(1, projectDto.uuid(), false); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), DIR, FILE); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), Set.of(DIR, FILE), true); dbTester.commit(); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(projectDto)); } diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepIT.java index 4b3d12f2be8..e39789dfffc 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepIT.java @@ -94,7 +94,7 @@ public class ExportComponentsStepIT { @Test public void export_components_including_project() { dbTester.components().insertPublicProject(PROJECT); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true); dbTester.commit(); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT)); @@ -111,7 +111,7 @@ public class ExportComponentsStepIT { @Test public void execute_register_all_components_uuids_as_their_id_in_ComponentRepository() { dbTester.components().insertPublicProject(PROJECT); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true); dbTester.commit(); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT)); @@ -125,7 +125,7 @@ public class ExportComponentsStepIT { @Test public void throws_ISE_if_error() { dbTester.components().insertPublicProject(PROJECT); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true); dbTester.commit(); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT)); dumpWriter.failIfMoreThan(1, DumpElement.COMPONENTS); diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepIT.java index 4bed242c8de..8d16133ac45 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepIT.java @@ -109,7 +109,7 @@ public class ExportMeasuresStepIT { public void setUp() { String projectUuid = dbTester.components().insertPublicProject(PROJECT).uuid(); componentRepository.register(1, projectUuid, false); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, ANOTHER_PROJECT); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), List.of(FILE, ANOTHER_PROJECT), true); dbTester.getDbClient().metricDao().insert(dbTester.getSession(), NCLOC, DISABLED_METRIC, NEW_NCLOC); dbTester.commit(); when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT)); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java index 8e591b95e81..6b16eab2068 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java @@ -110,10 +110,6 @@ public class PersistComponentsStep implements ComputationStep { } } - /** - * See {@link ComponentDto#mainBranchProjectUuid} : value is null on main branches, otherwise it is - * the uuid of the main branch. - */ @CheckForNull private String loadProjectUuidOfMainBranch() { if (!analysisMetadataHolder.getBranch().isMain()) { @@ -225,7 +221,7 @@ public class PersistComponentsStep implements ComputationStep { private ComponentDto persistComponent(ComponentDto componentDto) { ComponentDto existingComponent = existingComponentDtosByUuids.remove(componentDto.uuid()); if (existingComponent == null) { - dbClient.componentDao().insert(dbSession, componentDto); + dbClient.componentDao().insert(dbSession, componentDto, analysisMetadataHolder.getBranch().isMain()); return componentDto; } Optional update = compareForUpdate(existingComponent, componentDto); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/PullRequestSourceBranchMergerTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/PullRequestSourceBranchMergerTest.java index f351eb644b2..f6a5d6dfdf2 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/PullRequestSourceBranchMergerTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/PullRequestSourceBranchMergerTest.java @@ -98,9 +98,9 @@ public class PullRequestSourceBranchMergerTest { ComponentDto branch3Dto = db.components().insertProjectBranch(projectDto, b -> b.setKey("myBranch3") .setBranchType(BranchType.PULL_REQUEST) .setMergeBranchUuid(projectDto.uuid())); - db.components().insertComponent(newFileDto(branch1Dto).setKey(FILE_1_KEY + ":PULL_REQUEST:myBranch1")); - db.components().insertComponent(newFileDto(branch2Dto).setKey(FILE_1_KEY + ":PULL_REQUEST:myBranch2")); - db.components().insertComponent(newFileDto(branch3Dto).setKey(FILE_1_KEY + ":PULL_REQUEST:myBranch3")); + db.components().insertComponent(newFileDto(branch1Dto, projectDto.uuid()).setKey(FILE_1_KEY + ":PULL_REQUEST:myBranch1")); + db.components().insertComponent(newFileDto(branch2Dto, projectDto.uuid()).setKey(FILE_1_KEY + ":PULL_REQUEST:myBranch2")); + db.components().insertComponent(newFileDto(branch3Dto, projectDto.uuid()).setKey(FILE_1_KEY + ":PULL_REQUEST:myBranch3")); rule = db.rules().insert(); rawIssue = createIssue("issue1", rule.getKey(), Issue.STATUS_OPEN, new Date()); rawIssuesInput = new DefaultTrackingInput(singletonList(rawIssue), mock(LineHashSequence.class), mock(BlockHashSequence.class)); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java index 5bb1d10d941..9a08ca17b6c 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java @@ -228,7 +228,7 @@ public class ComponentDaoIT { public void select_by_key_and_branch() { ComponentDto project = db.components().insertPublicProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch").setBranchType(BRANCH)); - ComponentDto file = db.components().insertComponent(newFileDto(branch)); + ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid())); assertThat(underTest.selectByKeyAndBranch(dbSession, project.getKey(), DEFAULT_MAIN_BRANCH_NAME).get().uuid()).isEqualTo(project.uuid()); assertThat(underTest.selectByKeyAndBranch(dbSession, branch.getKey(), "my_branch").get().uuid()).isEqualTo(branch.uuid()); @@ -305,8 +305,8 @@ public class ComponentDaoIT { String branchKey = "my_branch"; ComponentDto project = db.components().insertPublicProject(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchKey)); - ComponentDto file1 = db.components().insertComponent(newFileDto(branch)); - ComponentDto file2 = db.components().insertComponent(newFileDto(branch)); + ComponentDto file1 = db.components().insertComponent(newFileDto(branch, project.uuid())); + ComponentDto file2 = db.components().insertComponent(newFileDto(branch, project.uuid())); ComponentDto anotherBranch = db.components().insertProjectBranch(project, b -> b.setKey("another_branch")); ComponentDto fileOnAnotherBranch = db.components().insertComponent(newFileDto(anotherBranch)); @@ -1117,7 +1117,7 @@ public class ComponentDaoIT { ComponentDto dto1 = newPrivateProjectDto("U1"); ComponentDto dto2 = newPrivateProjectDto("U2"); ComponentDto dto3 = newPrivateProjectDto("U3"); - underTest.insert(dbSession, dto1, dto2, dto3); + underTest.insert(dbSession, List.of(dto1, dto2, dto3), true); underTest.updateBEnabledToFalse(dbSession, asList("U1", "U2")); dbSession.commit(); @@ -1789,7 +1789,7 @@ public class ComponentDaoIT { public void insert_auditPersisterIsCalled() { ComponentDto app = ComponentTesting.newApplication(); - underTestWithAuditPersister.insert(dbSession, app); + underTestWithAuditPersister.insert(dbSession, app, true); verify(auditPersister).addComponent(any(DbSession.class), any(ComponentNewValue.class)); } @@ -1800,7 +1800,7 @@ public class ComponentDaoIT { BranchDto branch = newBranchDto(project); ComponentDto branchComponent = newBranchComponent(project, branch); - underTestWithAuditPersister.insert(dbSession, branchComponent); + underTestWithAuditPersister.insert(dbSession, branchComponent, false); verifyNoInteractions(auditPersister); } diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java index 8e62ecd2c12..b2a5fff2f8f 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java @@ -98,8 +98,8 @@ public class ComponentKeyUpdaterDaoIT { ComponentDto project = db.components().insertPublicProject(); String branchName = randomAlphanumeric(248); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); - db.components().insertComponent(newFileDto(branch)); - db.components().insertComponent(newFileDto(branch)); + db.components().insertComponent(newFileDto(branch, project.uuid())); + db.components().insertComponent(newFileDto(branch, project.uuid())); int prComponentCount = 3; String oldProjectKey = project.getKey(); @@ -159,7 +159,7 @@ public class ComponentKeyUpdaterDaoIT { public void updateKey_throws_IAE_when_sub_component_key_is_too_long() { ComponentDto project = newPrivateProjectDto("project-uuid").setKey("old-project-key"); db.components().insertComponent(project); - db.components().insertComponent(newFileDto(project, null).setKey("old-project-key:file")); + db.components().insertComponent(newFileDto(project).setKey("old-project-key:file")); String newLongProjectKey = Strings.repeat("a", 400); assertThatThrownBy(() -> underTest.updateKey(dbSession, project.uuid(), newLongProjectKey)) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/issue/IssueMapperIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/issue/IssueMapperIT.java index 40af62b9d7a..aae4b56442e 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/issue/IssueMapperIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/issue/IssueMapperIT.java @@ -72,10 +72,10 @@ public class IssueMapperIT { @Before public void setUp() { project = dbTester.components().insertPrivateProject(); - file = ComponentTesting.newFileDto(project, null); - dbTester.getDbClient().componentDao().insert(dbSession, file); - file2 = ComponentTesting.newFileDto(project, null).setUuid("file2 uuid"); - dbTester.getDbClient().componentDao().insert(dbSession, file2); + file = ComponentTesting.newFileDto(project); + dbTester.getDbClient().componentDao().insert(dbSession, file, true); + file2 = ComponentTesting.newFileDto(project).setUuid("file2 uuid"); + dbTester.getDbClient().componentDao().insert(dbSession, file2, true); rule = RuleTesting.newXooX1(); dbTester.rules().insert(rule); dbSession.commit(); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java index b91b833a53b..f63586e9d6a 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java @@ -314,7 +314,7 @@ public class ProjectMeasuresIndexerIteratorIT { Consumer componentDtoPopulator, Consumer projectDtoPopulator) { componentDtoPopulator.accept(component); checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag"); - dbClient.componentDao().insert(dbSession, component); + dbClient.componentDao().insert(dbSession, component, true); ProjectDto projectDto = toProjectDto(component, System2.INSTANCE.now()); projectDtoPopulator.accept(projectDto); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java index 0a147e4c53a..17fd4cc182d 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java @@ -155,13 +155,20 @@ public class PurgeCommandsIT { public void deleteNonMainBranchComponentsByProjectUuid_shouldDeletesAllBranchesOfAProjectExceptMainBranch(ComponentDto project) { dbTester.components().insertComponent(project); ComponentDto branch = dbTester.components().insertProjectBranch(project); - Stream.of(project, branch).forEach(prj -> { - ComponentDto directory1 = dbTester.components().insertComponent(ComponentTesting.newDirectory(prj, "a")); - ComponentDto directory2 = dbTester.components().insertComponent(ComponentTesting.newDirectory(prj, "b")); - dbTester.components().insertComponent(newFileDto(prj)); - dbTester.components().insertComponent(newFileDto(directory1)); - dbTester.components().insertComponent(newFileDto(directory2)); - }); + + ComponentDto directory1 = dbTester.components().insertComponent(ComponentTesting.newDirectory(project, "a")); + ComponentDto directory2 = dbTester.components().insertComponent(ComponentTesting.newDirectory(project, "b")); + dbTester.components().insertComponent(newFileDto(project)); + dbTester.components().insertComponent(newFileDto(directory1)); + dbTester.components().insertComponent(newFileDto(directory2)); + + directory1 = dbTester.components().insertComponent(ComponentTesting.newDirectory(branch, "a") + .setMainBranchProjectUuid(project.uuid())); + directory2 = dbTester.components().insertComponent(ComponentTesting.newDirectory(branch, "b") + .setMainBranchProjectUuid(project.uuid())); + dbTester.components().insertComponent(newFileDto(branch, project.uuid()).setMainBranchProjectUuid(project.uuid())); + dbTester.components().insertComponent(newFileDto(directory1).setMainBranchProjectUuid(project.uuid())); + dbTester.components().insertComponent(newFileDto(directory2).setMainBranchProjectUuid(project.uuid())); underTest.deleteNonMainBranchComponentsByProjectUuid(project.uuid()); @@ -580,11 +587,11 @@ public class PurgeCommandsIT { @Test public void deletePermissions_deletes_permissions_of_view() { - ComponentDto project = dbTester.components().insertPublicPortfolio(); - addPermissions(project); + ComponentDto portfolio = dbTester.components().insertPublicPortfolio(); + addPermissions(portfolio); PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler, system2); - purgeCommands.deletePermissions(project.uuid()); + purgeCommands.deletePermissions(portfolio.uuid()); assertThat(dbTester.countRowsOfTable("group_roles")).isEqualTo(2); assertThat(dbTester.countRowsOfTable("user_roles")).isOne(); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java index 0d35792599b..171c524c1a6 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java @@ -366,7 +366,7 @@ public class PurgeDaoIT { @Test public void deleteAnalyses_deletes_rows_in_events_and_event_component_changes() { ComponentDto project = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project); + dbClient.componentDao().insert(dbSession, project, true); SnapshotDto projectAnalysis1 = db.components().insertSnapshot(project); SnapshotDto projectAnalysis2 = db.components().insertSnapshot(project); SnapshotDto projectAnalysis3 = db.components().insertSnapshot(project); @@ -685,8 +685,7 @@ public class PurgeDaoIT { public void delete_row_in_ce_activity_when_deleting_project() { ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(); ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject); - + insertComponents(List.of(anotherLivingProject), List.of(projectToBeDeleted)); // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project CeActivityDto toBeDeletedActivity = insertCeActivity(projectToBeDeleted); CeActivityDto notDeletedActivity = insertCeActivity(anotherLivingProject); @@ -706,7 +705,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeActivityDto projectTask = insertCeActivity(project); insertCeTaskInput(projectTask.getUuid()); @@ -738,7 +738,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeActivityDto projectTask = insertCeActivity(project); insertCeScannerContext(projectTask.getUuid()); @@ -770,7 +771,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeActivityDto projectTask = insertCeActivity(project); insertCeTaskCharacteristics(projectTask.getUuid(), 3); @@ -802,7 +804,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeActivityDto projectTask = insertCeActivity(project); insertCeTaskMessages(projectTask.getUuid(), 3); @@ -872,7 +875,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeQueueDto projectTask = insertCeQueue(project); insertCeTaskInput(projectTask.getUuid()); @@ -904,7 +908,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeQueueDto projectTask = insertCeQueue(project); insertCeScannerContext(projectTask.getUuid()); @@ -931,13 +936,19 @@ public class PurgeDaoIT { assertThat(taskUuidsIn("ce_scanner_context")).containsOnly(anotherProjectTask.getUuid(), "non existing task"); } + private void insertComponents(List componentsOnMainBranch, List componentsNotOnMainBranch) { + componentsOnMainBranch.forEach(c -> dbClient.componentDao().insert(dbSession, c, true)); + componentsNotOnMainBranch.forEach(c -> dbClient.componentDao().insert(dbSession, c, false)); + } + @Test public void delete_row_in_ce_task_characteristics_referring_to_a_row_in_ce_queue_when_deleting_project() { ComponentDto project = ComponentTesting.newPrivateProjectDto(); ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeQueueDto projectTask = insertCeQueue(project); insertCeTaskCharacteristics(projectTask.getUuid(), 3); @@ -970,7 +981,8 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); CeQueueDto projectTask = insertCeQueue(project); insertCeTaskMessages(projectTask.getUuid(), 3); @@ -1003,7 +1015,9 @@ public class PurgeDaoIT { ComponentDto branch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherBranch = ComponentTesting.newBranchComponent(project, newBranchDto(project)); ComponentDto anotherProject = ComponentTesting.newPrivateProjectDto(); - dbClient.componentDao().insert(dbSession, project, branch, anotherBranch, anotherProject); + + insertComponents(List.of(project, anotherProject), List.of(branch, anotherBranch)); + SnapshotDto projectAnalysis1 = db.components().insertSnapshot(project); SnapshotDto projectAnalysis2 = db.components().insertSnapshot(project); EventDto projectEvent1 = db.events().insertEvent(projectAnalysis1); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDto.java index c037d412e67..deb5452010e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDto.java @@ -70,21 +70,6 @@ public class CeQueueDto { return this; } - /** - * Helper methods which sets both {@link #componentUuid} and {@link #mainComponentUuid} from the specified - * {@link ComponentDto}. - */ - public CeQueueDto setComponent(@Nullable ComponentDto component) { - if (component == null) { - this.componentUuid = null; - this.mainComponentUuid = null; - } else { - this.componentUuid = requireNonNull(component.uuid()); - this.mainComponentUuid = firstNonNull(component.getMainBranchProjectUuid(), component.uuid()); - } - return this; - } - @CheckForNull public String getComponentUuid() { return componentUuid; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index 759bea823ed..39d000faec9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -94,11 +94,11 @@ public class BranchDao implements Dao { * Returns collection of branches that are in the same project as the component */ public Collection selectByComponent(DbSession dbSession, ComponentDto component) { - String projectUuid = component.getMainBranchProjectUuid(); - if (projectUuid == null) { - projectUuid = component.branchUuid(); + BranchDto branchDto = mapper(dbSession).selectByUuid(component.branchUuid()); + if(branchDto == null) { + return List.of(); } - return mapper(dbSession).selectByProjectUuid(projectUuid); + return mapper(dbSession).selectByProjectUuid(branchDto.getProjectUuid()); } public Collection selectByProject(DbSession dbSession, ProjectDto project) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java index 1973e29b687..3d3a7daa5d2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; -import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; public interface BranchMapper { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 9044d64ad57..71dbafe1e4b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -20,7 +20,6 @@ package org.sonar.db.component; import com.google.common.collect.Ordering; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -319,23 +318,23 @@ public class ComponentDao implements Dao { /* INSERT / UPDATE */ - public void insert(DbSession session, ComponentDto item) { + public void insert(DbSession session, ComponentDto item, boolean isMainBranch) { mapper(session).insert(item); - if (!isBranchOrPullRequest(item)) { + if (isMainBranch) { auditPersister.addComponent(session, new ComponentNewValue(item)); } } - public void insert(DbSession session, Collection items) { - insert(session, items.stream()); + public void insertOnMainBranch(DbSession session, ComponentDto item) { + insert(session, item, true); } - private void insert(DbSession session, Stream items) { - items.forEach(item -> insert(session, item)); + public void insert(DbSession session, Collection items, boolean isMainBranch) { + insert(session, items.stream(), isMainBranch); } - public void insert(DbSession session, ComponentDto item, ComponentDto... others) { - insert(session, Stream.concat(Stream.of(item), Arrays.stream(others))); + private void insert(DbSession session, Stream items, boolean isMainBranch) { + items.forEach(item -> insert(session, item, isMainBranch)); } public void update(DbSession session, ComponentUpdateDto component, String qualifier) { @@ -378,8 +377,4 @@ public class ComponentDao implements Dao { checkThatNotTooManyConditions(query.getComponentUuids(), "Too many component UUIDs in query"); } - private static boolean isBranchOrPullRequest(ComponentDto item) { - return item.getMainBranchProjectUuid() != null; - } - } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 996adb5462a..78e92a39d7a 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -34,9 +34,10 @@ SELECT FROM components p + inner join project_branches pb on pb.uuid = p.branch_uuid where lower(p.kee)=lower(#{key,jdbcType=VARCHAR}) - and p.main_branch_project_uuid is null + and pb.is_main = ${_true}