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;
* </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.
* </p>
*/
private final Function<Integer, ScannerReport.Component> scannerComponentSupplier;
-
private final Project project;
-
+ private final Branch branch;
@Nullable
private final SnapshotDto baseAnalysis;
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;
}
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);
}
}
+ 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:
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 {
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");
}
@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
}
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) {
}
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) {