From: Sébastien Lesaint Date: Tue, 6 Mar 2018 17:06:02 +0000 (+0100) Subject: SONAR-10261 don't update project name nor description when on branch X-Git-Tag: 7.5~1550 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a28a47c710081dc745f11890c9c96ab51a428128;p=sonarqube.git SONAR-10261 don't update project name nor description when on branch --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java index a6f7949bf7c..36365235fa3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java @@ -27,6 +27,7 @@ import javax.annotation.Nullable; import org.sonar.db.component.SnapshotDto; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus; +import org.sonar.server.computation.task.projectanalysis.analysis.Branch; import org.sonar.server.computation.task.projectanalysis.analysis.Project; import static com.google.common.base.MoreObjects.firstNonNull; @@ -47,7 +48,6 @@ public class ComponentTreeBuilder { *

*/ private final Function uuidSupplier; - /** * Will supply the {@link ScannerReport.Component} of all the components in the component tree as we crawl it from the * root. @@ -56,9 +56,8 @@ public class ComponentTreeBuilder { *

*/ private final Function scannerComponentSupplier; - private final Project project; - + private final Branch branch; @Nullable private final SnapshotDto baseAnalysis; @@ -68,13 +67,14 @@ public class ComponentTreeBuilder { Function uuidSupplier, Function scannerComponentSupplier, Project project, - @Nullable SnapshotDto baseAnalysis) { + Branch branch, @Nullable SnapshotDto baseAnalysis) { this.keyGenerator = keyGenerator; this.publicKeyGenerator = publicKeyGenerator; this.uuidSupplier = uuidSupplier; this.scannerComponentSupplier = scannerComponentSupplier; this.project = project; + this.branch = branch; this.baseAnalysis = baseAnalysis; } @@ -98,18 +98,17 @@ public class ComponentTreeBuilder { String projectKey = keyGenerator.generateKey(component, null); String uuid = uuidSupplier.apply(projectKey); String projectPublicKey = publicKeyGenerator.generateKey(component, null); - return ComponentImpl.builder(Component.Type.PROJECT) + ComponentImpl.Builder builder = ComponentImpl.builder(Component.Type.PROJECT) .setUuid(uuid) .setKey(projectKey) .setPublicKey(projectPublicKey) - .setName(nameOfProject(component)) .setStatus(convertStatus(component.getStatus())) - .setDescription(trimToNull(component.getDescription())) .setReportAttributes(createAttributesBuilder(component, scmBasePath) .setVersion(createProjectVersion(component)) .build()) - .addChildren(buildChildren(component, component, scmBasePath)) - .build(); + .addChildren(buildChildren(component, component, scmBasePath)); + setNameAndDescription(component, builder); + return builder.build(); case MODULE: String moduleKey = keyGenerator.generateKey(component, null); @@ -146,6 +145,18 @@ public class ComponentTreeBuilder { } } + private void setNameAndDescription(ScannerReport.Component component, ComponentImpl.Builder builder) { + if (branch.isMain()) { + builder + .setName(nameOfProject(component)) + .setDescription(trimToNull(component.getDescription())); + } else { + builder + .setName(project.getName()) + .setDescription(project.getDescription()); + } + } + private static Component.Status convertStatus(FileStatus status) { switch (status) { case ADDED: diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java index 2bef4ec53a0..36dcfd5845d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java @@ -81,6 +81,7 @@ public class BuildComponentTreeStep implements ComputationStep { componentUuidFactory::getOrCreateForKey, reportReader::readComponent, analysisMetadataHolder.getProject(), + analysisMetadataHolder.getBranch(), baseAnalysis); String relativePathFromScmRoot = reportReader.readMetadata().getRelativePathFromScmRoot(); Component project = builder.buildProject(reportProject, relativePathFromScmRoot); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilderTest.java index 8bee0cda795..bc1096dc551 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilderTest.java @@ -30,21 +30,24 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.ExternalResource; +import org.mockito.Mockito; import org.sonar.core.component.ComponentKeys; import org.sonar.db.component.SnapshotDto; import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.server.computation.task.projectanalysis.analysis.Branch; import org.sonar.server.computation.task.projectanalysis.analysis.Project; import static com.google.common.base.Preconditions.checkArgument; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; -import static org.sonar.scanner.protocol.output.ScannerReport.Component.newBuilder; +import static org.mockito.Mockito.when; import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.DIRECTORY; import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.FILE; import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.MODULE; import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.PROJECT; import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.UNRECOGNIZED; +import static org.sonar.scanner.protocol.output.ScannerReport.Component.newBuilder; import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; public class ComponentTreeBuilderTest { @@ -116,6 +119,32 @@ public class ComponentTreeBuilderTest { assertThat(root.getName()).isEqualTo(projectInDb.getName()); } + @Test + public void project_name_is_loaded_from_report_if_present_and_on_main_branch() { + String reportName = randomAlphabetic(5); + ScannerReport.Component reportProject = newBuilder() + .setType(PROJECT) + .setName(reportName) + .build(); + + Component root = newUnderTest(null, true).buildProject(reportProject, NO_SCM_BASE_PATH); + + assertThat(root.getName()).isEqualTo(reportName); + } + + @Test + public void project_name_is_loaded_from_db_if_not_on_main_branch() { + String reportName = randomAlphabetic(5); + ScannerReport.Component reportProject = newBuilder() + .setType(PROJECT) + .setName(reportName) + .build(); + + Component root = newUnderTest(null, false).buildProject(reportProject, NO_SCM_BASE_PATH); + + assertThat(root.getName()).isEqualTo(projectInDb.getName()); + } + @Test public void project_version_is_loaded_from_db_if_absent_from_report() { SnapshotDto baseAnalysis = new SnapshotDto().setVersion("6.5"); @@ -147,12 +176,38 @@ public class ComponentTreeBuilderTest { } @Test - public void project_description_is_null_if_absent_from_report() { + public void project_description_is_loaded_from_db_if_absent_from_report() { Component root = call(newBuilder() .setType(PROJECT) .build()); - assertThat(root.getDescription()).isNull(); + assertThat(root.getDescription()).isEqualTo(projectInDb.getDescription()); + } + + @Test + public void project_description_is_loaded_from_report_if_present_and_on_main_branch() { + String reportDescription = randomAlphabetic(5); + ScannerReport.Component reportProject = newBuilder() + .setType(PROJECT) + .setDescription(reportDescription) + .build(); + + Component root = newUnderTest(null, true).buildProject(reportProject, NO_SCM_BASE_PATH); + + assertThat(root.getDescription()).isEqualTo(reportDescription); + } + + @Test + public void project_description_is_loaded_from_db_if_not_on_main_branch() { + String reportDescription = randomAlphabetic(5); + ScannerReport.Component reportProject = newBuilder() + .setType(PROJECT) + .setDescription(reportDescription) + .build(); + + Component root = newUnderTest(null, false).buildProject(reportProject, NO_SCM_BASE_PATH); + + assertThat(root.getDescription()).isEqualTo(projectInDb.getDescription()); } @Test @@ -846,7 +901,7 @@ public class ComponentTreeBuilderTest { } private Component call(ScannerReport.Component project, String scmBasePath) { - return newUnderTest(null).buildProject(project, scmBasePath); + return newUnderTest(null, true).buildProject(project, scmBasePath); } private Component call(ScannerReport.Component project, @Nullable SnapshotDto baseAnalysis) { @@ -854,11 +909,13 @@ public class ComponentTreeBuilderTest { } private Component call(ScannerReport.Component project, @Nullable SnapshotDto baseAnalysis, String scmBasePath) { - return newUnderTest(baseAnalysis).buildProject(project, scmBasePath); + return newUnderTest(baseAnalysis, true).buildProject(project, scmBasePath); } - private ComponentTreeBuilder newUnderTest(@Nullable SnapshotDto baseAnalysis) { - return new ComponentTreeBuilder(KEY_GENERATOR, PUBLIC_KEY_GENERATOR, UUID_SUPPLIER, scannerComponentProvider, projectInDb, baseAnalysis); + private ComponentTreeBuilder newUnderTest(@Nullable SnapshotDto baseAnalysis, boolean mainBranch) { + Branch branch = Mockito.mock(Branch.class); + when(branch.isMain()).thenReturn(mainBranch); + return new ComponentTreeBuilder(KEY_GENERATOR, PUBLIC_KEY_GENERATOR, UUID_SUPPLIER, scannerComponentProvider, projectInDb, branch, baseAnalysis); } private static Map indexComponentByRef(Component root) {