From: Julien Lancelot Date: Mon, 23 Oct 2017 09:37:36 +0000 (+0200) Subject: SONAR-9991 Set BranchPersister as Inteface X-Git-Tag: 6.7-RC1~118 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=95f0b3565ceba9e5b3a5b30b69fd10df4663b984;p=sonarqube.git SONAR-9991 Set BranchPersister as Inteface This will allow privileged plugin as GOV to do nothing on branch --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java index 7c3764bac7c..b63b8c49811 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java @@ -19,87 +19,9 @@ */ package org.sonar.server.computation.task.projectanalysis.component; -import java.util.Date; -import javax.annotation.Nullable; -import org.sonar.api.utils.System2; -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.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder; -import org.sonar.server.computation.task.projectanalysis.analysis.Branch; -import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT; -import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR; +public interface BranchPersister { -public class BranchPersister { - private final DbClient dbClient; - private final System2 system2; - private final TreeRootHolder treeRootHolder; - private final AnalysisMetadataHolder analysisMetadataHolder; - - public BranchPersister(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) { - this.dbClient = dbClient; - this.system2 = system2; - this.treeRootHolder = treeRootHolder; - this.analysisMetadataHolder = analysisMetadataHolder; - } - - public void persist(DbSession dbSession) { - Branch branch = analysisMetadataHolder.getBranch(); - String branchUuid = treeRootHolder.getRoot().getUuid(); - - com.google.common.base.Optional branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid); - - ComponentDto branchComponentDto; - if (branch.isMain()) { - checkState(branchComponentDtoOpt.isPresent(), "Project has been deleted by end-user during analysis"); - branchComponentDto = branchComponentDtoOpt.get(); - - } else { - // inserts new row in table projects if it's the first time branch is analyzed - branchComponentDto = branchComponentDtoOpt.or(() -> insertIntoProjectsTable(dbSession, branchUuid)); - - } - // insert or update in table project_branches - dbClient.branchDao().upsert(dbSession, toBranchDto(branchComponentDto, branch)); - } - - private static void checkState(boolean condition, String msg) { - if (!condition) { - throw new IllegalStateException(msg); - } - } - - private static T firstNonNull(@Nullable T first, T second) { - return (first != null) ? first : second; - } - - private static BranchDto toBranchDto(ComponentDto componentDto, Branch branch) { - BranchDto dto = new BranchDto(); - dto.setUuid(componentDto.uuid()); - // MainBranchProjectUuid will be null if it's a main branch - dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid())); - dto.setKey(branch.getName()); - dto.setBranchType(branch.getType()); - // merge branch is only present if it's a short living branch - dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null)); - return dto; - } - - private ComponentDto insertIntoProjectsTable(DbSession dbSession, String branchUuid) { - String mainBranchProjectUuid = analysisMetadataHolder.getProject().getUuid(); - ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, mainBranchProjectUuid); - ComponentDto branchDto = project.copy(); - branchDto.setUuid(branchUuid); - branchDto.setProjectUuid(branchUuid); - branchDto.setRootUuid(branchUuid); - branchDto.setUuidPath(UUID_PATH_OF_ROOT); - branchDto.setModuleUuidPath(UUID_PATH_SEPARATOR + branchUuid + UUID_PATH_SEPARATOR); - branchDto.setMainBranchProjectUuid(mainBranchProjectUuid); - branchDto.setDbKey(treeRootHolder.getRoot().getKey()); - branchDto.setCreatedAt(new Date(system2.now())); - dbClient.componentDao().insert(dbSession, branchDto); - return branchDto; - } + void persist(DbSession dbSession); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImpl.java new file mode 100644 index 00000000000..8a159a7ad50 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImpl.java @@ -0,0 +1,105 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.computation.task.projectanalysis.component; + +import java.util.Date; +import javax.annotation.Nullable; +import org.sonar.api.utils.System2; +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.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder; +import org.sonar.server.computation.task.projectanalysis.analysis.Branch; + +import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT; +import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR; + +public class BranchPersisterImpl implements BranchPersister { + private final DbClient dbClient; + private final System2 system2; + private final TreeRootHolder treeRootHolder; + private final AnalysisMetadataHolder analysisMetadataHolder; + + public BranchPersisterImpl(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) { + this.dbClient = dbClient; + this.system2 = system2; + this.treeRootHolder = treeRootHolder; + this.analysisMetadataHolder = analysisMetadataHolder; + } + + public void persist(DbSession dbSession) { + Branch branch = analysisMetadataHolder.getBranch(); + String branchUuid = treeRootHolder.getRoot().getUuid(); + + com.google.common.base.Optional branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid); + + ComponentDto branchComponentDto; + if (branch.isMain()) { + checkState(branchComponentDtoOpt.isPresent(), "Project has been deleted by end-user during analysis"); + branchComponentDto = branchComponentDtoOpt.get(); + + } else { + // inserts new row in table projects if it's the first time branch is analyzed + branchComponentDto = branchComponentDtoOpt.or(() -> insertIntoProjectsTable(dbSession, branchUuid)); + + } + // insert or update in table project_branches + dbClient.branchDao().upsert(dbSession, toBranchDto(branchComponentDto, branch)); + } + + private static void checkState(boolean condition, String msg) { + if (!condition) { + throw new IllegalStateException(msg); + } + } + + private static T firstNonNull(@Nullable T first, T second) { + return (first != null) ? first : second; + } + + private static BranchDto toBranchDto(ComponentDto componentDto, Branch branch) { + BranchDto dto = new BranchDto(); + dto.setUuid(componentDto.uuid()); + // MainBranchProjectUuid will be null if it's a main branch + dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid())); + dto.setKey(branch.getName()); + dto.setBranchType(branch.getType()); + // merge branch is only present if it's a short living branch + dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null)); + return dto; + } + + private ComponentDto insertIntoProjectsTable(DbSession dbSession, String branchUuid) { + String mainBranchProjectUuid = analysisMetadataHolder.getProject().getUuid(); + ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, mainBranchProjectUuid); + ComponentDto branchDto = project.copy(); + branchDto.setUuid(branchUuid); + branchDto.setProjectUuid(branchUuid); + branchDto.setRootUuid(branchUuid); + branchDto.setUuidPath(UUID_PATH_OF_ROOT); + branchDto.setModuleUuidPath(UUID_PATH_SEPARATOR + branchUuid + UUID_PATH_SEPARATOR); + branchDto.setMainBranchProjectUuid(mainBranchProjectUuid); + branchDto.setDbKey(treeRootHolder.getRoot().getKey()); + branchDto.setCreatedAt(new Date(system2.now())); + dbClient.componentDao().insert(dbSession, branchDto); + return branchDto; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java index a68aa9e5340..6c49120776f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/container/ProjectAnalysisTaskContainerPopulator.java @@ -34,7 +34,7 @@ import org.sonar.server.computation.task.projectanalysis.api.posttask.PostProjec import org.sonar.server.computation.task.projectanalysis.batch.BatchReportDirectoryHolderImpl; import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderImpl; import org.sonar.server.computation.task.projectanalysis.component.BranchLoader; -import org.sonar.server.computation.task.projectanalysis.component.BranchPersister; +import org.sonar.server.computation.task.projectanalysis.component.BranchPersisterImpl; import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepositoryImpl; import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepositoryImpl; import org.sonar.server.computation.task.projectanalysis.component.DisabledComponentsHolderImpl; @@ -64,7 +64,6 @@ import org.sonar.server.computation.task.projectanalysis.issue.IssueCache; import org.sonar.server.computation.task.projectanalysis.issue.IssueCounter; import org.sonar.server.computation.task.projectanalysis.issue.IssueCreationDateCalculator; import org.sonar.server.computation.task.projectanalysis.issue.IssueLifecycle; -import org.sonar.server.computation.task.projectanalysis.issue.ShortBranchIssueMerger; import org.sonar.server.computation.task.projectanalysis.issue.IssueTrackingDelegator; import org.sonar.server.computation.task.projectanalysis.issue.IssueVisitors; import org.sonar.server.computation.task.projectanalysis.issue.IssuesRepositoryVisitor; @@ -73,12 +72,13 @@ import org.sonar.server.computation.task.projectanalysis.issue.MergeBranchTracke import org.sonar.server.computation.task.projectanalysis.issue.MovedIssueVisitor; import org.sonar.server.computation.task.projectanalysis.issue.NewEffortAggregator; import org.sonar.server.computation.task.projectanalysis.issue.RemoveProcessedComponentsVisitor; -import org.sonar.server.computation.task.projectanalysis.issue.ShortBranchIssuesLoader; import org.sonar.server.computation.task.projectanalysis.issue.RuleRepositoryImpl; import org.sonar.server.computation.task.projectanalysis.issue.RuleTagsCopier; import org.sonar.server.computation.task.projectanalysis.issue.RuleTypeCopier; import org.sonar.server.computation.task.projectanalysis.issue.ScmAccountToUser; import org.sonar.server.computation.task.projectanalysis.issue.ScmAccountToUserLoader; +import org.sonar.server.computation.task.projectanalysis.issue.ShortBranchIssueMerger; +import org.sonar.server.computation.task.projectanalysis.issue.ShortBranchIssuesLoader; import org.sonar.server.computation.task.projectanalysis.issue.ShortBranchTrackerExecution; import org.sonar.server.computation.task.projectanalysis.issue.TrackerBaseInputFactory; import org.sonar.server.computation.task.projectanalysis.issue.TrackerExecution; @@ -251,7 +251,7 @@ public final class ProjectAnalysisTaskContainerPopulator implements ContainerPop ComponentIssuesLoader.class, BaseIssuesLoader.class, IssueTrackingDelegator.class, - BranchPersister.class, + BranchPersisterImpl.class, ShortBranchIssuesLoader.class, ShortBranchIssueMerger.class, diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImplTest.java new file mode 100644 index 00000000000..492b535223d --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImplTest.java @@ -0,0 +1,95 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.computation.task.projectanalysis.component; + +import java.util.Optional; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.component.BranchType; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; +import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; +import org.sonar.server.computation.task.projectanalysis.analysis.Branch; +import org.sonar.server.computation.task.projectanalysis.analysis.Project; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT; +import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder; + +public class BranchPersisterImplTest { + private final static Component MAIN = builder(PROJECT, 1).setUuid("PROJECT_UUID").setKey("PROJECT_KEY").build(); + private final static Component BRANCH = builder(PROJECT, 1).setUuid("BRANCH_UUID").setKey("BRANCH_KEY").build(); + + @Rule + public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + @Rule + public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); + @Rule + public ExpectedException exception = ExpectedException.none(); + + BranchPersister underTest = new BranchPersisterImpl(dbTester.getDbClient(), System2.INSTANCE, treeRootHolder, analysisMetadataHolder); + + @Test + public void fail_if_no_component_for_main_branches() { + analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, true, "master")); + treeRootHolder.setRoot(MAIN); + + exception.expect(IllegalStateException.class); + exception.expectMessage("Project has been deleted by end-user during analysis"); + + underTest.persist(dbTester.getSession()); + } + + @Test + public void persist_secondary_branch() { + analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, false, "branch")); + treeRootHolder.setRoot(BRANCH); + + // add main branch in project table and in metadata + ComponentDto dto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), MAIN.getUuid()).setDbKey(MAIN.getKey()); + analysisMetadataHolder.setProject(Project.copyOf(dto)); + dbTester.getDbClient().componentDao().insert(dbTester.getSession(), dto); + + // this should add new columns in project and project_branches + underTest.persist(dbTester.getSession()); + + dbTester.getSession().commit(); + + assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(2); + assertThat(dbTester.countRowsOfTable("project_branches")).isEqualTo(1); + + } + + private static Branch createBranch(BranchType type, boolean isMain, String name) { + Branch branch = mock(Branch.class); + when(branch.getType()).thenReturn(type); + when(branch.getName()).thenReturn(name); + when(branch.isMain()).thenReturn(isMain); + when(branch.getMergeBranchUuid()).thenReturn(Optional.empty()); + return branch; + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java deleted file mode 100644 index 3bb6b653b72..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.computation.task.projectanalysis.component; - -import java.util.Optional; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.utils.System2; -import org.sonar.db.DbTester; -import org.sonar.db.component.BranchType; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; -import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; -import org.sonar.server.computation.task.projectanalysis.analysis.Branch; -import org.sonar.server.computation.task.projectanalysis.analysis.Project; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT; -import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder; - -public class BranchPersisterTest { - private final static Component MAIN = builder(PROJECT, 1).setUuid("PROJECT_UUID").setKey("PROJECT_KEY").build(); - private final static Component BRANCH = builder(PROJECT, 1).setUuid("BRANCH_UUID").setKey("BRANCH_KEY").build(); - - @Rule - public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); - @Rule - public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); - @Rule - public ExpectedException exception = ExpectedException.none(); - - BranchPersister underTest = new BranchPersister(dbTester.getDbClient(), System2.INSTANCE, treeRootHolder, analysisMetadataHolder); - - @Test - public void fail_if_no_component_for_main_branches() { - analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, true, "master")); - treeRootHolder.setRoot(MAIN); - - exception.expect(IllegalStateException.class); - exception.expectMessage("Project has been deleted by end-user during analysis"); - - underTest.persist(dbTester.getSession()); - } - - @Test - public void persist_secondary_branch() { - analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, false, "branch")); - treeRootHolder.setRoot(BRANCH); - - // add main branch in project table and in metadata - ComponentDto dto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), MAIN.getUuid()).setDbKey(MAIN.getKey()); - analysisMetadataHolder.setProject(Project.copyOf(dto)); - dbTester.getDbClient().componentDao().insert(dbTester.getSession(), dto); - - // this should add new columns in project and project_branches - underTest.persist(dbTester.getSession()); - - dbTester.getSession().commit(); - - assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(2); - assertThat(dbTester.countRowsOfTable("project_branches")).isEqualTo(1); - - } - - private static Branch createBranch(BranchType type, boolean isMain, String name) { - Branch branch = mock(Branch.class); - when(branch.getType()).thenReturn(type); - when(branch.getName()).thenReturn(name); - when(branch.isMain()).thenReturn(isMain); - when(branch.getMergeBranchUuid()).thenReturn(Optional.empty()); - return branch; - } -}