@@ -34,21 +34,18 @@ import org.apache.commons.lang.StringUtils; | |||
import org.sonar.ce.task.projectanalysis.analysis.Branch; | |||
import org.sonar.ce.task.projectanalysis.issue.IssueRelocationToRoot; | |||
import org.sonar.core.util.stream.MoreCollectors; | |||
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.project.Project; | |||
import static com.google.common.base.MoreObjects.firstNonNull; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
import static java.lang.String.format; | |||
import static java.util.Objects.requireNonNull; | |||
import static org.apache.commons.lang.StringUtils.removeStart; | |||
import static org.apache.commons.lang.StringUtils.trimToNull; | |||
public class ComponentTreeBuilder { | |||
private static final String DEFAULT_PROJECT_VERSION = "not provided"; | |||
private final ComponentKeyGenerator keyGenerator; | |||
private final ComponentKeyGenerator publicKeyGenerator; | |||
/** | |||
@@ -69,9 +66,7 @@ public class ComponentTreeBuilder { | |||
private final Project project; | |||
private final Branch branch; | |||
@Nullable | |||
private final SnapshotDto baseAnalysis; | |||
@Nullable | |||
private final String projectVersion; | |||
private final ProjectAttributes projectAttributes; | |||
private final IssueRelocationToRoot issueRelocationToRoot; | |||
private ScannerReport.Component rootComponent; | |||
@@ -83,8 +78,8 @@ public class ComponentTreeBuilder { | |||
Function<String, String> uuidSupplier, | |||
Function<Integer, ScannerReport.Component> scannerComponentSupplier, | |||
Project project, | |||
Branch branch, @Nullable SnapshotDto baseAnalysis, | |||
@Nullable String projectVersion, | |||
Branch branch, | |||
ProjectAttributes projectAttributes, | |||
IssueRelocationToRoot issueRelocationToRoot) { | |||
this.keyGenerator = keyGenerator; | |||
@@ -93,8 +88,7 @@ public class ComponentTreeBuilder { | |||
this.scannerComponentSupplier = scannerComponentSupplier; | |||
this.project = project; | |||
this.branch = branch; | |||
this.baseAnalysis = baseAnalysis; | |||
this.projectVersion = projectVersion; | |||
this.projectAttributes = requireNonNull(projectAttributes, "projectAttributes can't be null"); | |||
this.issueRelocationToRoot = issueRelocationToRoot; | |||
} | |||
@@ -198,7 +192,7 @@ public class ComponentTreeBuilder { | |||
.setDbKey(projectKey) | |||
.setKey(projectPublicKey) | |||
.setStatus(convertStatus(rootComponent.getStatus())) | |||
.setProjectAttributes(new ProjectAttributes(createProjectVersion())) | |||
.setProjectAttributes(projectAttributes) | |||
.setReportAttributes(createAttributesBuilder(rootComponent.getRef(), rootComponent.getProjectRelativePath(), scmBasePath).build()) | |||
.addChildren(children); | |||
setNameAndDescription(rootComponent, builder); | |||
@@ -269,7 +263,7 @@ public class ComponentTreeBuilder { | |||
private static Component buildChangedProject(Component component) { | |||
return changedComponentBuilder(component) | |||
.setProjectAttributes(new ProjectAttributes(component.getProjectAttributes().getVersion())) | |||
.setProjectAttributes(new ProjectAttributes(component.getProjectAttributes())) | |||
.addChildren(buildChangedComponentChildren(component)) | |||
.build(); | |||
} | |||
@@ -339,18 +333,6 @@ public class ComponentTreeBuilder { | |||
return project.getName(); | |||
} | |||
private String createProjectVersion() { | |||
String cleanedProjectVersion = trimToNull(this.projectVersion); | |||
if (cleanedProjectVersion != null) { | |||
return cleanedProjectVersion; | |||
} | |||
// FIXME SONAR-11631 code below applies to the analysisVersion, not the project version, fix it | |||
if (baseAnalysis != null) { | |||
return firstNonNull(baseAnalysis.getCodePeriodVersion(), DEFAULT_PROJECT_VERSION); | |||
} | |||
return DEFAULT_PROJECT_VERSION; | |||
} | |||
private static ReportAttributes.Builder createAttributesBuilder(@Nullable Integer ref, String path, @Nullable String scmBasePath) { | |||
return ReportAttributes.newBuilder(ref) | |||
.setScmPath(computeScmPath(scmBasePath, path)); |
@@ -19,23 +19,38 @@ | |||
*/ | |||
package org.sonar.ce.task.projectanalysis.component; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import javax.annotation.Nullable; | |||
import static java.util.Objects.requireNonNull; | |||
public class ProjectAttributes { | |||
private final String version; | |||
private final String projectVersion; | |||
private final String codePeriodVersion; | |||
public ProjectAttributes(@Nullable String projectVersion, String codePeriodVersion) { | |||
this.projectVersion = projectVersion; | |||
this.codePeriodVersion = requireNonNull(codePeriodVersion, "codePeriod version can't be null"); | |||
} | |||
public ProjectAttributes(ProjectAttributes projectAttributes) { | |||
this.projectVersion = projectAttributes.projectVersion; | |||
this.codePeriodVersion = projectAttributes.codePeriodVersion; | |||
} | |||
public ProjectAttributes(String version) { | |||
this.version = Objects.requireNonNull(version, "version can't be null"); | |||
public Optional<String> getProjectVersion() { | |||
return Optional.ofNullable(projectVersion); | |||
} | |||
public String getVersion() { | |||
return version; | |||
public String getCodePeriodVersion() { | |||
return codePeriodVersion; | |||
} | |||
@Override | |||
public String toString() { | |||
return "ProjectAttributes{" + | |||
"version='" + version + '\'' + | |||
"projectVersion='" + projectVersion + '\'' + | |||
"codePeriodVersion='" + codePeriodVersion + '\'' + | |||
'}'; | |||
} | |||
} |
@@ -32,6 +32,7 @@ import org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder; | |||
import org.sonar.ce.task.projectanalysis.component.ComponentUuidFactoryWithMigration; | |||
import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl; | |||
import org.sonar.ce.task.projectanalysis.component.MutableTreeRootHolder; | |||
import org.sonar.ce.task.projectanalysis.component.ProjectAttributes; | |||
import org.sonar.ce.task.projectanalysis.component.ReportModulesPath; | |||
import org.sonar.ce.task.projectanalysis.issue.IssueRelocationToRoot; | |||
import org.sonar.ce.task.step.ComputationStep; | |||
@@ -41,11 +42,16 @@ import org.sonar.db.component.SnapshotDto; | |||
import org.sonar.db.component.SnapshotQuery; | |||
import org.sonar.scanner.protocol.output.ScannerReport; | |||
import static com.google.common.base.MoreObjects.firstNonNull; | |||
import static org.apache.commons.lang.StringUtils.trimToNull; | |||
/** | |||
* Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader} | |||
*/ | |||
public class BuildComponentTreeStep implements ComputationStep { | |||
private static final String DEFAULT_PROJECT_VERSION = "not provided"; | |||
private final DbClient dbClient; | |||
private final BatchReportReader reportReader; | |||
private final MutableTreeRootHolder treeRootHolder; | |||
@@ -90,8 +96,7 @@ public class BuildComponentTreeStep implements ComputationStep { | |||
reportReader::readComponent, | |||
analysisMetadataHolder.getProject(), | |||
analysisMetadataHolder.getBranch(), | |||
baseAnalysis, | |||
metadata.getProjectVersion(), | |||
createProjectAttributes(metadata, baseAnalysis), | |||
issueRelocationToRoot); | |||
String relativePathFromScmRoot = metadata.getRelativePathFromScmRoot(); | |||
@@ -110,6 +115,27 @@ public class BuildComponentTreeStep implements ComputationStep { | |||
} | |||
} | |||
private static ProjectAttributes createProjectAttributes(ScannerReport.Metadata metadata, @Nullable SnapshotDto baseAnalysis) { | |||
String projectVersion = trimToNull(metadata.getProjectVersion()); | |||
String codePeriodVersion = computeCodePeriodVersion(metadata.getCodePeriodVersion(), projectVersion, baseAnalysis); | |||
return new ProjectAttributes(projectVersion, codePeriodVersion); | |||
} | |||
private static String computeCodePeriodVersion(String rawCodePeriodVersion, @Nullable String projectVersion, @Nullable SnapshotDto baseAnalysis) { | |||
String codePeriodVersion = trimToNull(rawCodePeriodVersion); | |||
if (codePeriodVersion != null) { | |||
return codePeriodVersion; | |||
} | |||
// support case (legacy but not forbidden) where only projectVersion is set | |||
if (projectVersion != null) { | |||
return projectVersion; | |||
} | |||
if (baseAnalysis != null) { | |||
return firstNonNull(baseAnalysis.getCodePeriodVersion(), DEFAULT_PROJECT_VERSION); | |||
} | |||
return DEFAULT_PROJECT_VERSION; | |||
} | |||
private ComponentKeyGenerator loadKeyGenerator() { | |||
return analysisMetadataHolder.getBranch(); | |||
} |
@@ -105,7 +105,7 @@ public class LoadPeriodsStep implements ComputationStep { | |||
} | |||
private Optional<Period> resolvePeriod(Component projectOrView) { | |||
String currentVersion = projectOrView.getProjectAttributes().getVersion(); | |||
String currentVersion = projectOrView.getProjectAttributes().getCodePeriodVersion(); | |||
Optional<String> propertyValue = configRepository.getConfiguration().get(LEAK_PERIOD) | |||
.filter(t -> !t.isEmpty()); | |||
checkPeriodProperty(propertyValue.isPresent(), "", "property is undefined or value is empty"); |
@@ -33,6 +33,8 @@ import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.component.SnapshotDto; | |||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | |||
/** | |||
* Persist analysis | |||
*/ | |||
@@ -100,11 +102,12 @@ public class PersistAnalysisStep implements ComputationStep { | |||
private SnapshotDto createAnalysis(String snapshotUuid, Component component) { | |||
String componentUuid = component.getUuid(); | |||
String version = component.getType() == Component.Type.PROJECT ? component.getProjectAttributes().getVersion() : null; | |||
String codePeriodVersion = component.getType() == PROJECT ? component.getProjectAttributes().getCodePeriodVersion() : null; | |||
String projectVersion = component.getType() == PROJECT ? component.getProjectAttributes().getProjectVersion().orElse(null) : null; | |||
return new SnapshotDto() | |||
.setUuid(snapshotUuid) | |||
.setCodePeriodVersion(version) | |||
.setProjectVersion(version) | |||
.setCodePeriodVersion(codePeriodVersion) | |||
.setProjectVersion(projectVersion) | |||
.setComponentUuid(componentUuid) | |||
.setLast(false) | |||
.setStatus(SnapshotDto.STATUS_UNPROCESSED) |
@@ -101,10 +101,10 @@ public class PersistEventsStep implements ComputationStep { | |||
} | |||
private void saveVersionEvent(DbSession session, Component component, Long analysisDate) { | |||
String version = component.getProjectAttributes().getVersion(); | |||
deletePreviousEventsHavingSameVersion(session, version, component); | |||
String codePeriodVersion = component.getProjectAttributes().getCodePeriodVersion(); | |||
deletePreviousEventsHavingSameVersion(session, codePeriodVersion, component); | |||
dbClient.eventDao().insert(session, newBaseEvent(component, analysisDate) | |||
.setName(version) | |||
.setName(codePeriodVersion) | |||
.setCategory(EventDto.CATEGORY_VERSION)); | |||
} | |||
@@ -132,7 +132,7 @@ public class QualityGateEventsStep implements ComputationStep { | |||
.setDefaultMessage(String.format("Alert on %s: %s", project.getName(), label)) | |||
.setFieldValue("projectName", project.getName()) | |||
.setFieldValue("projectKey", project.getKey()) | |||
.setFieldValue("projectVersion", project.getProjectAttributes().getVersion()) | |||
.setFieldValue("projectVersion", project.getProjectAttributes().getCodePeriodVersion()) | |||
.setFieldValue("alertName", label) | |||
.setFieldValue("alertText", rawStatus.getText()) | |||
.setFieldValue("alertLevel", rawStatus.getStatus().toString()) |
@@ -165,7 +165,7 @@ public class SendIssueNotificationsStep implements ComputationStep { | |||
NewIssuesNotification notification = newIssuesNotificationFactory | |||
.newNewIssuesNotification() | |||
.setProject(project.getKey(), project.getName(), getBranchName(), getPullRequest()) | |||
.setProjectVersion(project.getProjectAttributes().getVersion()) | |||
.setProjectVersion(project.getProjectAttributes().getCodePeriodVersion()) | |||
.setAnalysisDate(new Date(analysisDate)) | |||
.setStatistics(project.getName(), globalStatistics) | |||
.setDebt(Duration.create(globalStatistics.effort().getOnLeak())); | |||
@@ -186,7 +186,7 @@ public class SendIssueNotificationsStep implements ComputationStep { | |||
.setAssignee(userDtoByUuid.get(assigneeUuid)); | |||
myNewIssuesNotification | |||
.setProject(project.getKey(), project.getName(), getBranchName(), getPullRequest()) | |||
.setProjectVersion(project.getProjectAttributes().getVersion()) | |||
.setProjectVersion(project.getProjectAttributes().getCodePeriodVersion()) | |||
.setAnalysisDate(new Date(analysisDate)) | |||
.setStatistics(project.getName(), assigneeStatistics) | |||
.setDebt(Duration.create(assigneeStatistics.effort().getOnLeak())); |
@@ -246,7 +246,7 @@ public class ComponentImplTest { | |||
.setUuid("uuid_" + dbKey) | |||
.setReportAttributes(ReportAttributes.newBuilder(dbKey.hashCode()).build()); | |||
if (type == PROJECT) { | |||
builder.setProjectAttributes(new ProjectAttributes("version_1")); | |||
builder.setProjectAttributes(new ProjectAttributes(null, "version_1")); | |||
} | |||
return builder; | |||
} |
@@ -19,6 +19,9 @@ | |||
*/ | |||
package org.sonar.ce.task.projectanalysis.component; | |||
import com.tngtech.java.junit.dataprovider.DataProvider; | |||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | |||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | |||
import java.util.Arrays; | |||
import java.util.EnumSet; | |||
import java.util.HashMap; | |||
@@ -32,10 +35,10 @@ import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.junit.rules.ExternalResource; | |||
import org.junit.runner.RunWith; | |||
import org.sonar.ce.task.projectanalysis.analysis.Branch; | |||
import org.sonar.ce.task.projectanalysis.issue.IssueRelocationToRoot; | |||
import org.sonar.core.component.ComponentKeys; | |||
import org.sonar.db.component.SnapshotDto; | |||
import org.sonar.scanner.protocol.output.ScannerReport; | |||
import org.sonar.server.project.Project; | |||
@@ -50,13 +53,14 @@ import static org.mockito.Mockito.when; | |||
import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; | |||
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; | |||
import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto; | |||
import static org.sonar.scanner.protocol.output.ScannerReport.Component.newBuilder; | |||
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; | |||
@RunWith(DataProviderRunner.class) | |||
public class ComponentTreeBuilderTest { | |||
private static final ComponentKeyGenerator KEY_GENERATOR = (projectKey, path) -> "generated_" | |||
@@ -67,7 +71,9 @@ public class ComponentTreeBuilderTest { | |||
private static final EnumSet<ScannerReport.Component.ComponentType> REPORT_TYPES = EnumSet.of(PROJECT, MODULE, DIRECTORY, FILE); | |||
private static final String NO_SCM_BASE_PATH = ""; | |||
// both no project as "" or null should be supported | |||
private static final String NO_PROJECT_VERSION = new Random().nextBoolean() ? "" : null; | |||
private static final ProjectAttributes SOME_PROJECT_ATTRIBUTES = new ProjectAttributes( | |||
new Random().nextBoolean() ? null : randomAlphabetic(12), | |||
randomAlphabetic(20)); | |||
private IssueRelocationToRoot issueRelocationToRoot = mock(IssueRelocationToRoot.class); | |||
@Rule | |||
@@ -98,7 +104,7 @@ public class ComponentTreeBuilderTest { | |||
.setProjectRelativePath("src") | |||
.setLines(1)); | |||
try { | |||
call(project, NO_SCM_BASE_PATH, null); | |||
call(project, NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
fail("Should have thrown a IllegalArgumentException"); | |||
} catch (IllegalArgumentException e) { | |||
assertThat(e).hasMessage("Unsupported component type '" + type + "'"); | |||
@@ -124,7 +130,8 @@ public class ComponentTreeBuilderTest { | |||
} | |||
@Test | |||
public void by_default_project_fields_are_loaded_from_report() { | |||
@UseDataProvider("projectVersionOrNull") | |||
public void by_default_project_fields_are_loaded_from_report(@Nullable String projectVersion) { | |||
String nameInReport = "the name"; | |||
String descriptionInReport = "the desc"; | |||
Component root = call(newBuilder() | |||
@@ -133,7 +140,7 @@ public class ComponentTreeBuilderTest { | |||
.setRef(42) | |||
.setName(nameInReport) | |||
.setDescription(descriptionInReport) | |||
.build(), NO_SCM_BASE_PATH, "6.5"); | |||
.build(), NO_SCM_BASE_PATH, new ProjectAttributes(projectVersion, "6.5")); | |||
assertThat(root.getUuid()).isEqualTo("generated_K1_uuid"); | |||
assertThat(root.getDbKey()).isEqualTo("generated_K1"); | |||
@@ -143,15 +150,28 @@ public class ComponentTreeBuilderTest { | |||
assertThat(root.getShortName()).isEqualTo(nameInReport); | |||
assertThat(root.getDescription()).isEqualTo(descriptionInReport); | |||
assertThat(root.getReportAttributes().getRef()).isEqualTo(42); | |||
assertThat(root.getProjectAttributes().getVersion()).isEqualTo("6.5"); | |||
if (projectVersion == null) { | |||
assertThat(root.getProjectAttributes().getProjectVersion()).isEmpty(); | |||
} else { | |||
assertThat(root.getProjectAttributes().getProjectVersion()).contains(projectVersion); | |||
} | |||
assertThat(root.getProjectAttributes().getCodePeriodVersion()).isEqualTo("6.5"); | |||
assertThatFileAttributesAreNotSet(root); | |||
} | |||
@DataProvider | |||
public static Object[][] projectVersionOrNull() { | |||
return new Object[][] { | |||
{null}, | |||
{randomAlphabetic(15)} | |||
}; | |||
} | |||
@Test | |||
public void project_name_is_loaded_from_db_if_absent_from_report() { | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
.build(), NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
assertThat(root.getName()).isEqualTo(projectInDb.getName()); | |||
} | |||
@@ -164,7 +184,7 @@ public class ComponentTreeBuilderTest { | |||
.setName(reportName) | |||
.build(); | |||
Component root = newUnderTest(null, true, NO_PROJECT_VERSION).buildProject(reportProject, NO_SCM_BASE_PATH); | |||
Component root = newUnderTest(SOME_PROJECT_ATTRIBUTES, true).buildProject(reportProject, NO_SCM_BASE_PATH); | |||
assertThat(root.getName()).isEqualTo(reportName); | |||
} | |||
@@ -177,46 +197,17 @@ public class ComponentTreeBuilderTest { | |||
.setName(reportName) | |||
.build(); | |||
Component root = newUnderTest(null, false, null) | |||
Component root = newUnderTest(SOME_PROJECT_ATTRIBUTES, 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().setCodePeriodVersion("6.5"); | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), baseAnalysis, NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
assertThat(root.getProjectAttributes().getVersion()).isEqualTo("6.5"); | |||
} | |||
@Test | |||
public void project_version_is_loaded_from_db_if_empty_report() { | |||
SnapshotDto baseAnalysis = new SnapshotDto().setCodePeriodVersion("6.5"); | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), baseAnalysis, NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
assertThat(root.getProjectAttributes().getVersion()).isEqualTo("6.5"); | |||
} | |||
@Test | |||
public void project_version_is_hardcoded_if_absent_from_report_and_db() { | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
assertThat(root.getProjectAttributes().getVersion()).isEqualTo("not provided"); | |||
} | |||
@Test | |||
public void project_description_is_loaded_from_db_if_absent_from_report() { | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
.build(), NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
assertThat(root.getDescription()).isEqualTo(projectInDb.getDescription()); | |||
} | |||
@@ -229,7 +220,7 @@ public class ComponentTreeBuilderTest { | |||
.setDescription(reportDescription) | |||
.build(); | |||
Component root = newUnderTest(null, true, null).buildProject(reportProject, NO_SCM_BASE_PATH); | |||
Component root = newUnderTest(SOME_PROJECT_ATTRIBUTES, true).buildProject(reportProject, NO_SCM_BASE_PATH); | |||
assertThat(root.getDescription()).isEqualTo(reportDescription); | |||
} | |||
@@ -242,7 +233,7 @@ public class ComponentTreeBuilderTest { | |||
.setDescription(reportDescription) | |||
.build(); | |||
Component root = newUnderTest(null, false, null).buildProject(reportProject, NO_SCM_BASE_PATH); | |||
Component root = newUnderTest(SOME_PROJECT_ATTRIBUTES, false).buildProject(reportProject, NO_SCM_BASE_PATH); | |||
assertThat(root.getDescription()).isEqualTo(projectInDb.getDescription()); | |||
} | |||
@@ -251,11 +242,20 @@ public class ComponentTreeBuilderTest { | |||
public void project_scmPath_is_empty_if_scmBasePath_is_empty() { | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
.build(), NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
assertThat(root.getReportAttributes().getScmPath()).isEmpty(); | |||
} | |||
@Test | |||
public void projectAttributes_is_constructor_argument() { | |||
Component root = call(newBuilder() | |||
.setType(PROJECT) | |||
.build(), NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
assertThat(root.getProjectAttributes()).isSameAs(SOME_PROJECT_ATTRIBUTES); | |||
} | |||
@Test | |||
public void any_component_with_projectRelativePath_has_this_value_as_scmPath_if_scmBasePath_is_empty() { | |||
ScannerReport.Component project = newBuilder() | |||
@@ -271,7 +271,7 @@ public class ComponentTreeBuilderTest { | |||
.setProjectRelativePath("src/js/Foo.js") | |||
.setLines(1)); | |||
Component root = call(project, NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
Component root = call(project, NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
assertThat(root.getReportAttributes().getScmPath()) | |||
.contains("root"); | |||
@@ -288,7 +288,7 @@ public class ComponentTreeBuilderTest { | |||
ScannerReport.Component project = createProject(); | |||
String scmBasePath = randomAlphabetic(10); | |||
Component root = call(project, scmBasePath, NO_PROJECT_VERSION); | |||
Component root = call(project, scmBasePath, SOME_PROJECT_ATTRIBUTES); | |||
assertThat(root.getReportAttributes().getScmPath()) | |||
.contains(scmBasePath); | |||
Component directory = root.getChildren().iterator().next(); | |||
@@ -903,22 +903,18 @@ public class ComponentTreeBuilderTest { | |||
} | |||
private Component call(ScannerReport.Component project) { | |||
return call(project, NO_SCM_BASE_PATH, NO_PROJECT_VERSION); | |||
} | |||
private Component call(ScannerReport.Component project, String scmBasePath, @Nullable String projectVersion) { | |||
return call(project, null, scmBasePath, projectVersion); | |||
return call(project, NO_SCM_BASE_PATH, SOME_PROJECT_ATTRIBUTES); | |||
} | |||
private Component call(ScannerReport.Component project, @Nullable SnapshotDto baseAnalysis, String scmBasePath, @Nullable String projectVersion) { | |||
return newUnderTest(baseAnalysis, true, projectVersion).buildProject(project, scmBasePath); | |||
private Component call(ScannerReport.Component project, String scmBasePath, ProjectAttributes projectAttributes) { | |||
return newUnderTest(projectAttributes, true).buildProject(project, scmBasePath); | |||
} | |||
private ComponentTreeBuilder newUnderTest(@Nullable SnapshotDto baseAnalysis, boolean mainBranch, @Nullable String projectVersion) { | |||
private ComponentTreeBuilder newUnderTest(ProjectAttributes projectAttributes, boolean mainBranch) { | |||
Branch branch = mock(Branch.class); | |||
when(branch.isMain()).thenReturn(mainBranch); | |||
return new ComponentTreeBuilder(KEY_GENERATOR, PUBLIC_KEY_GENERATOR, UUID_SUPPLIER, scannerComponentProvider, | |||
projectInDb, branch, baseAnalysis, projectVersion, | |||
projectInDb, branch, projectAttributes, | |||
issueRelocationToRoot); | |||
} | |||
@@ -43,7 +43,7 @@ public class ReportComponent implements Component { | |||
.setPublicKey("PUBLIC_PROJECT_KEY") | |||
.setUuid("PROJECT_UUID") | |||
.setName("Project Name") | |||
.setProjectVersion("1.0-SNAPSHOT") | |||
.setCodePeriodVersion("1.0-SNAPSHOT") | |||
.build(); | |||
private final Type type; | |||
@@ -69,7 +69,9 @@ public class ReportComponent implements Component { | |||
this.shortName = builder.shortName == null ? this.name : builder.shortName; | |||
this.description = builder.description; | |||
this.uuid = builder.uuid; | |||
this.projectAttributes = Optional.ofNullable(builder.projectVersion).map(ProjectAttributes::new).orElse(null); | |||
this.projectAttributes = Optional.ofNullable(builder.codePeriodVersion) | |||
.map(t -> new ProjectAttributes(builder.projectVersion, t)) | |||
.orElse(null); | |||
this.reportAttributes = ReportAttributes.newBuilder(builder.ref) | |||
.build(); | |||
this.fileAttributes = builder.fileAttributes == null ? DEFAULT_FILE_ATTRIBUTES : builder.fileAttributes; | |||
@@ -203,6 +205,7 @@ public class ReportComponent implements Component { | |||
private String publicKey; | |||
private String name; | |||
private String shortName; | |||
private String codePeriodVersion; | |||
private String projectVersion; | |||
private String description; | |||
private FileAttributes fileAttributes; | |||
@@ -213,7 +216,7 @@ public class ReportComponent implements Component { | |||
this.type = type; | |||
this.ref = ref; | |||
if (type == Type.PROJECT) { | |||
this.projectVersion = "toBeDefined"; | |||
this.codePeriodVersion = "toBeDefined"; | |||
} | |||
} | |||
@@ -247,9 +250,14 @@ public class ReportComponent implements Component { | |||
return this; | |||
} | |||
public Builder setProjectVersion(String s) { | |||
checkProjectVersion(s); | |||
this.projectVersion = s; | |||
public Builder setCodePeriodVersion(String s) { | |||
checkCodePeriodVersion(s); | |||
this.codePeriodVersion = s; | |||
return this; | |||
} | |||
public Builder setProjectVersion(@Nullable String projectVersion) { | |||
this.projectVersion = projectVersion; | |||
return this; | |||
} | |||
@@ -273,12 +281,12 @@ public class ReportComponent implements Component { | |||
} | |||
public ReportComponent build() { | |||
checkProjectVersion(this.projectVersion); | |||
checkCodePeriodVersion(this.codePeriodVersion); | |||
return new ReportComponent(this); | |||
} | |||
private void checkProjectVersion(@Nullable String s) { | |||
checkArgument(type != Type.PROJECT ^ s != null, "Project version must and can only be set on Project"); | |||
private void checkCodePeriodVersion(@Nullable String s) { | |||
checkArgument(type != Type.PROJECT ^ s != null, "CodePeriod version must and can only be set on Project"); | |||
} | |||
} | |||
@@ -49,6 +49,8 @@ import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType; | |||
import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus; | |||
import org.sonar.server.project.Project; | |||
import static java.util.Optional.ofNullable; | |||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.any; | |||
import static org.mockito.Mockito.mock; | |||
@@ -66,6 +68,8 @@ import static org.sonar.scanner.protocol.output.ScannerReport.Component.Componen | |||
@RunWith(DataProviderRunner.class) | |||
public class BuildComponentTreeStepTest { | |||
private static final String NO_SCANNER_PROJECT_VERSION = null; | |||
private static final String NO_SCANNER_CODE_PERIOD_VERSION = null; | |||
private static final int ROOT_REF = 1; | |||
private static final int MODULE_REF = 2; | |||
@@ -95,11 +99,7 @@ public class BuildComponentTreeStepTest { | |||
@Rule | |||
public DbTester dbTester = DbTester.create(System2.INSTANCE); | |||
@Rule | |||
public BatchReportReaderRule reportReader = new BatchReportReaderRule() | |||
.setMetadata(ScannerReport.Metadata.newBuilder() | |||
.setProjectKey(REPORT_PROJECT_KEY) | |||
.setRootComponentRef(ROOT_REF) | |||
.build()); | |||
public BatchReportReaderRule reportReader = new BatchReportReaderRule().setMetadata(createReportMetadata(NO_SCANNER_PROJECT_VERSION, NO_SCANNER_CODE_PERIOD_VERSION)); | |||
@Rule | |||
public MutableTreeRootHolderRule treeRootHolder = new MutableTreeRootHolderRule(); | |||
@Rule | |||
@@ -275,7 +275,7 @@ public class BuildComponentTreeStepTest { | |||
verifyComponentByRef(ROOT_REF, "generated", REPORT_PROJECT_KEY, analysisMetadataHolder.getProject().getName(), null); | |||
verifyComponentByKey(REPORT_PROJECT_KEY + ":" + REPORT_DIR_PATH_1, "generated", "dir1"); | |||
verifyComponentByRef(FILE_1_REF, "generated", REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1, REPORT_FILE_NAME_1,null); | |||
verifyComponentByRef(FILE_1_REF, "generated", REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1, REPORT_FILE_NAME_1, null); | |||
verifyComponentMissingByRef(LEAFLESS_MODULE_REF); | |||
verifyComponentMissingByRef(LEAFLESS_DIR_REF); | |||
@@ -469,6 +469,172 @@ public class BuildComponentTreeStepTest { | |||
assertThat(analysisMetadataHolder.isFirstAnalysis()).isFalse(); | |||
} | |||
@Test | |||
public void set_codePeriodVersion_to_not_provided_when_both_codePeriod_and_project_version_are_not_set_on_first_analysis() { | |||
setAnalysisMetadataHolder(); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getCodePeriodVersion()).isEqualTo("not provided"); | |||
} | |||
@Test | |||
@UseDataProvider("oneParameterNullNonNullCombinations") | |||
public void set_codePeriodVersion_to_previous_analysis_codePeriodVersion_when_both_codePeriod_and_project_version_are_not_set( | |||
@Nullable String previousAnalysisCodePeriodVersion) { | |||
setAnalysisMetadataHolder(); | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); | |||
ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD").setDbKey(REPORT_PROJECT_KEY)); | |||
insertSnapshot(newAnalysis(project).setCodePeriodVersion(previousAnalysisCodePeriodVersion).setLast(true)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
String codePeriodVersion = treeRootHolder.getReportTreeRoot().getProjectAttributes().getCodePeriodVersion(); | |||
if (previousAnalysisCodePeriodVersion == null) { | |||
assertThat(codePeriodVersion).isEqualTo("not provided"); | |||
} else { | |||
assertThat(codePeriodVersion).isEqualTo(previousAnalysisCodePeriodVersion); | |||
} | |||
} | |||
@Test | |||
public void set_codePeriodVersion_to_projectVersion_when_codePeriodVersion_is_unset_and_projectVersion_is_set_on_first_analysis() { | |||
String scannerProjectVersion = randomAlphabetic(12); | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(scannerProjectVersion, NO_SCANNER_CODE_PERIOD_VERSION)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getCodePeriodVersion()) | |||
.isEqualTo(scannerProjectVersion); | |||
} | |||
@Test | |||
@UseDataProvider("oneParameterNullNonNullCombinations") | |||
public void set_codePeriodVersion_to_projectVersion_when_codePeriodVersion_is_unset_and_projectVersion_is_set_on_later_analysis( | |||
@Nullable String previousAnalysisCodePeriodVersion) { | |||
String scannerProjectVersion = randomAlphabetic(12); | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(scannerProjectVersion, NO_SCANNER_CODE_PERIOD_VERSION)); | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); | |||
ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD").setDbKey(REPORT_PROJECT_KEY)); | |||
insertSnapshot(newAnalysis(project).setCodePeriodVersion(previousAnalysisCodePeriodVersion).setLast(true)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getCodePeriodVersion()) | |||
.isEqualTo(scannerProjectVersion); | |||
} | |||
@Test | |||
@UseDataProvider("oneParameterNullNonNullCombinations") | |||
public void set_codePeriodVersion_to_codePeriodVersion_when_it_is_set_on_first_analysis(@Nullable String projectVersion) { | |||
String scannerCodePeriodVersion = randomAlphabetic(12); | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(projectVersion, scannerCodePeriodVersion)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getCodePeriodVersion()) | |||
.isEqualTo(scannerCodePeriodVersion); | |||
} | |||
@Test | |||
@UseDataProvider("twoParametersNullNonNullCombinations") | |||
public void set_codePeriodVersion_to_codePeriodVersion_when_it_is_set_on_later_analysis(@Nullable String projectVersion, @Nullable String previousAnalysisCodePeriodVersion) { | |||
String scannerCodePeriodVersion = randomAlphabetic(12); | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(projectVersion, scannerCodePeriodVersion)); | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); | |||
ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD").setDbKey(REPORT_PROJECT_KEY)); | |||
insertSnapshot(newAnalysis(project).setCodePeriodVersion(previousAnalysisCodePeriodVersion).setLast(true)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getCodePeriodVersion()) | |||
.isEqualTo(scannerCodePeriodVersion); | |||
} | |||
@Test | |||
@UseDataProvider("oneParameterNullNonNullCombinations") | |||
public void set_projectVersion_to_null_when_projectVersion_is_unset_on_first_analysis(@Nullable String codePeriodVersion) { | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(NO_SCANNER_PROJECT_VERSION, codePeriodVersion)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getProjectVersion()).isEmpty(); | |||
} | |||
@Test | |||
@UseDataProvider("twoParametersNullNonNullCombinations") | |||
public void set_projectVersion_to_null_when_projectVersion_is_unset_on_later_analysis(@Nullable String codePeriodVersion, @Nullable String previousAnalysisCodePeriodVersion) { | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(NO_SCANNER_PROJECT_VERSION, codePeriodVersion)); | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); | |||
ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD").setDbKey(REPORT_PROJECT_KEY)); | |||
insertSnapshot(newAnalysis(project).setCodePeriodVersion(previousAnalysisCodePeriodVersion).setLast(true)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getProjectVersion()).isEmpty(); | |||
} | |||
@Test | |||
@UseDataProvider("oneParameterNullNonNullCombinations") | |||
public void set_projectVersion_to_projectVersion_when_projectVersion_is_set_on_first_analysis(@Nullable String codePeriodVersion) { | |||
String projectVersion = randomAlphabetic(7); | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(projectVersion, codePeriodVersion)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getProjectVersion()).contains(projectVersion); | |||
} | |||
@Test | |||
@UseDataProvider("twoParametersNullNonNullCombinations") | |||
public void set_projectVersion_to_projectVersion_when_projectVersion_is_set_on_later_analysis(@Nullable String codePeriodVersion, @Nullable String previousAnalysisCodePeriodVersion) { | |||
String projectVersion = randomAlphabetic(7); | |||
setAnalysisMetadataHolder(); | |||
reportReader.setMetadata(createReportMetadata(projectVersion, codePeriodVersion)); | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); | |||
ComponentDto project = insertComponent(newPrivateProjectDto(organizationDto, "ABCD").setDbKey(REPORT_PROJECT_KEY)); | |||
insertSnapshot(newAnalysis(project).setCodePeriodVersion(previousAnalysisCodePeriodVersion).setLast(true)); | |||
reportReader.putComponent(component(ROOT_REF, PROJECT, REPORT_PROJECT_KEY)); | |||
underTest.execute(new TestComputationStepContext()); | |||
assertThat(treeRootHolder.getReportTreeRoot().getProjectAttributes().getProjectVersion()).contains(projectVersion); | |||
} | |||
@DataProvider | |||
public static Object[][] oneParameterNullNonNullCombinations() { | |||
return new Object[][] { | |||
{null}, | |||
{randomAlphabetic(7)} | |||
}; | |||
} | |||
@DataProvider | |||
public static Object[][] twoParametersNullNonNullCombinations() { | |||
return new Object[][] { | |||
{null, null}, | |||
{randomAlphabetic(7), null}, | |||
{null, randomAlphabetic(8)}, | |||
{randomAlphabetic(9), randomAlphabetic(10)} | |||
}; | |||
} | |||
private void verifyComponent(Component component, Component.Type type, @Nullable Integer componentRef, int size) { | |||
assertThat(component.getType()).isEqualTo(type); | |||
assertThat(component.getReportAttributes().getRef()).isEqualTo(componentRef); | |||
@@ -599,4 +765,13 @@ public class BuildComponentTreeStepTest { | |||
.setProject(Project.from(newPrivateProjectDto(newOrganizationDto()).setDbKey(REPORT_PROJECT_KEY))); | |||
} | |||
public static ScannerReport.Metadata createReportMetadata(@Nullable String projectVersion, @Nullable String scannerCodePeriodVersion) { | |||
ScannerReport.Metadata.Builder builder = ScannerReport.Metadata.newBuilder() | |||
.setProjectKey(REPORT_PROJECT_KEY) | |||
.setRootComponentRef(ROOT_REF); | |||
ofNullable(scannerCodePeriodVersion).ifPresent(builder::setCodePeriodVersion); | |||
ofNullable(projectVersion).ifPresent(builder::setProjectVersion); | |||
return builder.build(); | |||
} | |||
} |
@@ -551,7 +551,7 @@ public class LoadPeriodsStepTest extends BaseStepTest { | |||
} | |||
private void setupRoot(ComponentDto project, String version) { | |||
treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey()).setProjectVersion(version).build()); | |||
treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey()).setCodePeriodVersion(version).build()); | |||
when(configurationRepository.getConfiguration()).thenReturn(settings.asConfig()); | |||
} | |||
@@ -58,7 +58,7 @@ public class PersistEventsStepTest extends BaseStepTest { | |||
private static final long NOW = 1225630680000L; | |||
private static final ReportComponent ROOT = builder(PROJECT, 1) | |||
.setUuid("ABCD") | |||
.setProjectVersion("version_1") | |||
.setCodePeriodVersion("version_1") | |||
.addChildren( | |||
builder(DIRECTORY, 2) | |||
.setUuid("BCDE") | |||
@@ -107,7 +107,7 @@ public class PersistEventsStepTest extends BaseStepTest { | |||
when(system2.now()).thenReturn(NOW); | |||
Component project = builder(PROJECT, 1) | |||
.setUuid("ABCD") | |||
.setProjectVersion("1.0") | |||
.setCodePeriodVersion("1.0") | |||
.addChildren( | |||
builder(DIRECTORY, 2) | |||
.setUuid("BCDE") | |||
@@ -197,7 +197,7 @@ public class PersistEventsStepTest extends BaseStepTest { | |||
Component project = builder(PROJECT, 1) | |||
.setUuid(projectDto.uuid()) | |||
.setProjectVersion("1.5-SNAPSHOT") | |||
.setCodePeriodVersion("1.5-SNAPSHOT") | |||
.addChildren( | |||
builder(DIRECTORY, 2) | |||
.setUuid("BCDE") |
@@ -20,6 +20,7 @@ | |||
package org.sonar.ce.task.projectanalysis.step; | |||
import java.util.Optional; | |||
import java.util.Random; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
@@ -45,6 +46,7 @@ import org.sonar.server.notification.NotificationService; | |||
import org.sonar.server.project.Project; | |||
import static java.util.Collections.emptyList; | |||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
@@ -58,10 +60,12 @@ import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR; | |||
import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.OK; | |||
public class QualityGateEventsStepTest { | |||
private static final String PROJECT_VERSION = new Random().nextBoolean() ? null : randomAlphabetic(19); | |||
private static final ReportComponent PROJECT_COMPONENT = ReportComponent.builder(Component.Type.PROJECT, 1) | |||
.setUuid("uuid 1") | |||
.setKey("key 1") | |||
.setProjectVersion("V1.9") | |||
.setCodePeriodVersion("V1.9") | |||
.setProjectVersion(PROJECT_VERSION) | |||
.addChildren(ReportComponent.builder(Component.Type.DIRECTORY, 2).build()) | |||
.build(); | |||
private static final String INVALID_ALERT_STATUS = "trololo"; | |||
@@ -178,7 +182,7 @@ public class QualityGateEventsStepTest { | |||
assertThat(notification.getType()).isEqualTo("alerts"); | |||
assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey()); | |||
assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getVersion()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getCodePeriodVersion()); | |||
assertThat(notification.getFieldValue("branch")).isNull(); | |||
assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name()); | |||
assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel); | |||
@@ -229,7 +233,7 @@ public class QualityGateEventsStepTest { | |||
assertThat(notification.getType()).isEqualTo("alerts"); | |||
assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey()); | |||
assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getVersion()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getCodePeriodVersion()); | |||
assertThat(notification.getFieldValue("branch")).isNull(); | |||
assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name()); | |||
assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel); | |||
@@ -259,7 +263,7 @@ public class QualityGateEventsStepTest { | |||
assertThat(notification.getType()).isEqualTo("alerts"); | |||
assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey()); | |||
assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getVersion()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getCodePeriodVersion()); | |||
assertThat(notification.getFieldValue("branch")).isEqualTo(branchName); | |||
reset(measureRepository, eventRepository, notificationService); | |||
@@ -281,7 +285,7 @@ public class QualityGateEventsStepTest { | |||
assertThat(notification.getType()).isEqualTo("alerts"); | |||
assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey()); | |||
assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getVersion()); | |||
assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getCodePeriodVersion()); | |||
assertThat(notification.getFieldValue("branch")).isEqualTo(null); | |||
reset(measureRepository, eventRepository, notificationService); |
@@ -19,10 +19,14 @@ | |||
*/ | |||
package org.sonar.ce.task.projectanalysis.step; | |||
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 org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.sonar.api.utils.DateUtils; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | |||
@@ -43,12 +47,14 @@ import org.sonar.db.component.SnapshotQuery; | |||
import org.sonar.db.component.SnapshotTesting; | |||
import org.sonar.db.organization.OrganizationDto; | |||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE; | |||
import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION; | |||
@RunWith(DataProviderRunner.class) | |||
public class ReportPersistAnalysisStepTest extends BaseStepTest { | |||
private static final String PROJECT_KEY = "PROJECT_KEY"; | |||
@@ -93,7 +99,8 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { | |||
} | |||
@Test | |||
public void persist_analysis() { | |||
@UseDataProvider("projectVersionOrNull") | |||
public void persist_analysis(String projectVersion) { | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); | |||
ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project"); | |||
dbClient.componentDao().insert(dbTester.getSession(), projectDto); | |||
@@ -107,7 +114,13 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { | |||
Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build(); | |||
Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build(); | |||
Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setProjectVersion("1.0").addChildren(directory).build(); | |||
Component project = ReportComponent.builder(Component.Type.PROJECT, 1) | |||
.setUuid("ABCD") | |||
.setKey(PROJECT_KEY) | |||
.setCodePeriodVersion("1.0") | |||
.setProjectVersion(projectVersion) | |||
.addChildren(directory) | |||
.build(); | |||
treeRootHolder.setRoot(project); | |||
dbIdsRepository.setComponentId(project, projectDto.getId()); | |||
@@ -122,7 +135,7 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { | |||
assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID); | |||
assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid()); | |||
assertThat(projectSnapshot.getCodePeriodVersion()).isEqualTo("1.0"); | |||
assertThat(projectSnapshot.getProjectVersion()).isEqualTo("1.0"); | |||
assertThat(projectSnapshot.getProjectVersion()).isEqualTo(projectVersion); | |||
assertThat(projectSnapshot.getLast()).isFalse(); | |||
assertThat(projectSnapshot.getStatus()).isEqualTo("U"); | |||
assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate); | |||
@@ -132,6 +145,14 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { | |||
assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId()); | |||
} | |||
@DataProvider | |||
public static Object[][] projectVersionOrNull() { | |||
return new Object[][] { | |||
{null}, | |||
{randomAlphabetic(17)} | |||
}; | |||
} | |||
@Test | |||
public void persist_snapshots_with_leak_period() { | |||
OrganizationDto organizationDto = dbTester.organizations().insert(); |
@@ -96,7 +96,7 @@ public class SendIssueNotificationsStepTest extends BaseStepTest { | |||
private static final Component FILE = builder(Type.FILE, 11).build(); | |||
private static final Component PROJECT = builder(Type.PROJECT, 1) | |||
.setProjectVersion(randomAlphanumeric(10)) | |||
.setCodePeriodVersion(randomAlphanumeric(10)) | |||
.addChildren(FILE).build(); | |||
@Rule |