]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10261 don't update project name nor description when on branch
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 6 Mar 2018 17:06:02 +0000 (18:06 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 13 Mar 2018 08:12:31 +0000 (09:12 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilderTest.java

index a6f7949bf7cfda5546e0f6d00b6691c3bbe8553c..36365235fa313080660f3b9a200de3958059713c 100644 (file)
@@ -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 {
    * </p>
    */
   private final Function<String, String> 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 {
    * </p>
    */
   private final Function<Integer, ScannerReport.Component> scannerComponentSupplier;
-
   private final Project project;
-
+  private final Branch branch;
   @Nullable
   private final SnapshotDto baseAnalysis;
 
@@ -68,13 +67,14 @@ public class ComponentTreeBuilder {
     Function<String, String> uuidSupplier,
     Function<Integer, ScannerReport.Component> 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:
index 2bef4ec53a09c193bf112033c7b845a022b53127..36dcfd5845d8efdbb42728b24bbde603b11ab85c 100644 (file)
@@ -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);
index 8bee0cda795cefd70fbb5e937e34bd9b28df241d..bc1096dc5519af1f3b9b9f594e1acdac151790be 100644 (file)
@@ -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<Integer, Component> indexComponentByRef(Component root) {