Browse Source

SONAR-12364 - remove manual baseline column from usage

tags/8.0
Jacek 4 years ago
parent
commit
545d742d5d

+ 8
- 1
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java View File

@@ -57,6 +57,8 @@ import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.event.EventTesting;
import org.sonar.db.newcodeperiod.NewCodePeriodDto;
import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.organization.OrganizationDto;

import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
@@ -206,7 +208,12 @@ public class LoadPeriodsStepTest extends BaseStepTest {
SnapshotDto analysis3 = dbTester.components().insertSnapshot(branch, snapshot -> snapshot.setCreatedAt(1227157200000L).setLast(false)); // 2008-11-20
SnapshotDto analysis4 = dbTester.components().insertSnapshot(branch, snapshot -> snapshot.setCreatedAt(1227358680000L).setLast(false)); // 2008-11-22
SnapshotDto analysis5 = dbTester.components().insertSnapshot(branch, snapshot -> snapshot.setCreatedAt(1227934800000L).setLast(true)); // 2008-11-29
dbTester.getDbClient().branchDao().updateManualBaseline(dbTester.getSession(), branch.uuid(), analysis1.getUuid());
dbTester.newCodePeriods().insert(new NewCodePeriodDto()
.setProjectUuid(project.uuid())
.setBranchUuid(branch.uuid())
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue(analysis1.getUuid()));

dbTester.commit();
when(system2Mock.now()).thenReturn(november30th2008.getTime());
when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);

+ 0
- 15
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java View File

@@ -22,7 +22,6 @@ package org.sonar.db.component;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
@@ -64,20 +63,6 @@ public class BranchDao implements Dao {
return mapper(dbSession).updateMainBranchName(projectUuid, newBranchKey, now);
}

/**
* Set or unset the uuid of the manual baseline analysis by updating the manual_baseline_analysis_uuid column, if:
*
* - the specified uuid exists
* - and the specified uuid corresponds to a long-living branch (including the main branch)
*
* @return the number of rows that were updated
*/
@Deprecated
public int updateManualBaseline(DbSession dbSession, String uuid, @Nullable String analysisUuid) {
long now = system2.now();
return mapper(dbSession).updateManualBaseline(uuid, analysisUuid == null || analysisUuid.isEmpty() ? null : analysisUuid, now);
}

public Optional<BranchDto> selectByBranchKey(DbSession dbSession, String projectUuid, String key) {
return selectByKey(dbSession, projectUuid, key, KeyType.BRANCH);
}

+ 14
- 34
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java View File

@@ -76,7 +76,7 @@ public class BranchDto {
* UUID of the branch:
* - in which the short-lived branch or pull request will be merged into
* - that is the base of long-lived branch.
*
* <p>
* Can be null if information is not known.
*/
@Nullable
@@ -88,12 +88,6 @@ public class BranchDto {
@Nullable
private byte[] pullRequestBinary;

/**
* The UUID of the analysis set by user as manual baseline for computation of the New Code Period, if any.
*/
@Nullable
private String manualBaseline;

public String getUuid() {
return uuid;
}
@@ -195,18 +189,6 @@ public class BranchDto {
}
}

@CheckForNull
@Deprecated
public String getManualBaseline() {
return manualBaseline;
}

@Deprecated
public BranchDto setManualBaseline(@Nullable String manualBaseline) {
this.manualBaseline = manualBaseline == null || manualBaseline.isEmpty() ? null : manualBaseline;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
@@ -217,11 +199,10 @@ public class BranchDto {
}
BranchDto branchDto = (BranchDto) o;
return Objects.equals(uuid, branchDto.uuid) &&
Objects.equals(projectUuid, branchDto.projectUuid) &&
Objects.equals(kee, branchDto.kee) &&
branchType == branchDto.branchType &&
Objects.equals(mergeBranchUuid, branchDto.mergeBranchUuid) &&
Objects.equals(manualBaseline, branchDto.manualBaseline);
Objects.equals(projectUuid, branchDto.projectUuid) &&
Objects.equals(kee, branchDto.kee) &&
branchType == branchDto.branchType &&
Objects.equals(mergeBranchUuid, branchDto.mergeBranchUuid);
}

@Override
@@ -231,15 +212,14 @@ public class BranchDto {

@Override
public String toString() {
StringBuilder sb = new StringBuilder("BranchDto{");
sb.append("uuid='").append(uuid).append('\'');
sb.append(", projectUuid='").append(projectUuid).append('\'');
sb.append(", kee='").append(kee).append('\'');
sb.append(", keyType=").append(keyType);
sb.append(", branchType=").append(branchType);
sb.append(", mergeBranchUuid='").append(mergeBranchUuid).append('\'');
sb.append(", manualBaseline='").append(manualBaseline).append('\'');
sb.append('}');
return sb.toString();
return new StringBuilder("BranchDto{")
.append("uuid='").append(uuid).append('\'')
.append(", projectUuid='").append(projectUuid).append('\'')
.append(", kee='").append(kee).append('\'')
.append(", keyType=").append(keyType)
.append(", branchType=").append(branchType)
.append(", mergeBranchUuid='").append(mergeBranchUuid).append('\'')
.append('}')
.toString();
}
}

+ 1
- 14
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml View File

@@ -9,8 +9,7 @@
pb.key_type as keyType,
pb.branch_type as branchType,
pb.merge_branch_uuid as mergeBranchUuid,
pb.pull_request_binary as pullRequestBinary,
pb.manual_baseline_analysis_uuid as manualBaseline
pb.pull_request_binary as pullRequestBinary
</sql>

<insert id="insert" parameterType="map" useGeneratedKeys="false">
@@ -22,7 +21,6 @@
branch_type,
merge_branch_uuid,
pull_request_binary,
manual_baseline_analysis_uuid,
created_at,
updated_at
) values (
@@ -33,7 +31,6 @@
#{dto.branchType, jdbcType=VARCHAR},
#{dto.mergeBranchUuid, jdbcType=VARCHAR},
#{dto.pullRequestBinary, jdbcType=BINARY},
#{dto.manualBaseline, jdbcType=VARCHAR},
#{now, jdbcType=BIGINT},
#{now, jdbcType=BIGINT}
)
@@ -48,16 +45,6 @@
uuid = #{projectUuid, jdbcType=VARCHAR}
</update>

<update id="updateManualBaseline" parameterType="map" useGeneratedKeys="false">
update project_branches
set
manual_baseline_analysis_uuid = #{analysisUuid, jdbcType=VARCHAR},
updated_at = #{now, jdbcType=BIGINT}
where
uuid = #{uuid, jdbcType=VARCHAR}
and branch_type = 'LONG'
</update>

<update id="update" parameterType="map" useGeneratedKeys="false">
update project_branches
set

+ 5
- 3
server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml View File

@@ -27,6 +27,7 @@
s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast", ve.name as "version"
from snapshots s
left outer join events ve on ve.analysis_uuid=s.uuid and ve.category='Version'
left outer join new_code_periods npc on ve.analysis_uuid=s.uuid and ve.category='Version'
where
s.component_uuid=#{componentUuid,jdbcType=VARCHAR}
and s.status='P'
@@ -45,11 +46,12 @@

<select id="selectManualBaseline" parameterType="String" resultType="String">
select
manual_baseline_analysis_uuid
value
from
project_branches pb
new_code_periods ncp
where
pb.uuid=#{projectUuid,jdbcType=VARCHAR}
ncp.type='SPECIFIC_ANALYSIS'
AND ncp.branch_uuid=#{projectUuid,jdbcType=VARCHAR}
</select>

<select id="selectStaleShortLivingBranchesAndPullRequests" parameterType="map" resultType="String">

+ 4
- 83
server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java View File

@@ -21,14 +21,13 @@ package org.sonar.db.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.Map;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.api.utils.System2;
import org.sonar.api.impl.utils.TestSystem2;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.protobuf.DbProjectBranches;
@@ -100,76 +99,19 @@ public class BranchDaoTest {
assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG);
}

@Test
@UseDataProvider("nullOrEmpty")
public void insert_null_or_empty_manual_baseline_analysis_uuid_is_null(@Nullable String nullOrEmpty) {
BranchDto dto = new BranchDto();
dto.setProjectUuid("U1");
dto.setUuid("U1");
dto.setBranchType(BranchType.LONG);
dto.setKey("foo");
dto.setManualBaseline(nullOrEmpty);

underTest.insert(dbSession, dto);

assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline()).isNull();
}

@DataProvider
public static Object[][] nullOrEmpty() {
return new Object[][] {
return new Object[][]{
{null},
{""}
};
}

@Test
public void insert_manual_baseline_analysis_uuid() {
String manualBaselineAnalysisUuid = randomAlphabetic(12);
BranchDto dto = new BranchDto();
dto.setProjectUuid("U1");
dto.setUuid("U1");
dto.setBranchType(BranchType.LONG);
dto.setKey("foo");
dto.setManualBaseline(manualBaselineAnalysisUuid);

underTest.insert(dbSession, dto);

assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline()).isEqualTo(manualBaselineAnalysisUuid);
}

@Test
@UseDataProvider("oldAndNewValuesCombinations")
public void update_manualBaselineAnalysisUuid_if_new_value_is_different(@Nullable String oldValue, @Nullable String newValue) {
BranchDto dto = new BranchDto();
dto.setProjectUuid("U1");
dto.setUuid("U1");
dto.setBranchType(BranchType.LONG);
dto.setKey("foo");
dto.setManualBaseline(oldValue);
underTest.insert(dbSession, dto);

BranchDto otherDtoThatShouldNotChange = new BranchDto();
otherDtoThatShouldNotChange.setProjectUuid("U2");
otherDtoThatShouldNotChange.setUuid("U2");
otherDtoThatShouldNotChange.setBranchType(BranchType.LONG);
otherDtoThatShouldNotChange.setKey("branch");
otherDtoThatShouldNotChange.setManualBaseline(oldValue);
underTest.insert(dbSession, otherDtoThatShouldNotChange);

assertThat(underTest.updateManualBaseline(dbSession, dto.getUuid(), newValue)).isEqualTo(1);

assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline())
.isEqualTo(emptyToNull(newValue));
assertThat(underTest.selectByUuid(dbSession, otherDtoThatShouldNotChange.getUuid()).get().getManualBaseline())
.isEqualTo(emptyToNull(oldValue));
}

@DataProvider
public static Object[][] oldAndNewValuesCombinations() {
String value1 = randomAlphabetic(10);
String value2 = randomAlphabetic(20);
return new Object[][] {
return new Object[][]{
{null, value1},
{"", value1},
{value1, null},
@@ -181,26 +123,9 @@ public class BranchDaoTest {
};
}

@Test
@UseDataProvider("nonLongBranchType")
public void do_not_update_manual_baseline_of_non_long_branches(BranchType branchType) {
String analysisUuid = randomAlphabetic(12);
String otherAnalysisUuid = randomAlphabetic(12);
BranchDto noManualBaselineDto = ComponentTesting.newBranchDto(db.components().insertPrivateProject(), branchType);
BranchDto withManualBaselineDto = ComponentTesting.newBranchDto(db.components().insertPrivateProject(), branchType).setManualBaseline(analysisUuid);
underTest.insert(dbSession, noManualBaselineDto);
underTest.insert(dbSession, withManualBaselineDto);
dbSession.commit();

assertThat(underTest.updateManualBaseline(dbSession, noManualBaselineDto.getUuid(), null)).isZero();
assertThat(underTest.updateManualBaseline(dbSession, noManualBaselineDto.getUuid(), otherAnalysisUuid)).isZero();
assertThat(underTest.updateManualBaseline(dbSession, withManualBaselineDto.getUuid(), null)).isZero();
assertThat(underTest.updateManualBaseline(dbSession, withManualBaselineDto.getUuid(), otherAnalysisUuid)).isZero();
}

@DataProvider
public static Object[][] nonLongBranchType() {
return new Object[][] {
return new Object[][]{
{BranchType.SHORT},
{BranchType.PULL_REQUEST}
};
@@ -427,7 +352,6 @@ public class BranchDaoTest {
featureBranch.setBranchType(BranchType.SHORT);
featureBranch.setKey("feature/foo");
featureBranch.setMergeBranchUuid("U3");
featureBranch.setManualBaseline("analysisUUID");
underTest.insert(dbSession, featureBranch);

// select the feature branch
@@ -437,7 +361,6 @@ public class BranchDaoTest {
assertThat(loaded.getProjectUuid()).isEqualTo(featureBranch.getProjectUuid());
assertThat(loaded.getBranchType()).isEqualTo(featureBranch.getBranchType());
assertThat(loaded.getMergeBranchUuid()).isEqualTo(featureBranch.getMergeBranchUuid());
assertThat(loaded.getManualBaseline()).isEqualTo(featureBranch.getManualBaseline());

// select a branch on another project with same branch name
assertThat(underTest.selectByBranchKey(dbSession, "U3", "feature/foo")).isEmpty();
@@ -459,7 +382,6 @@ public class BranchDaoTest {
pullRequest.setBranchType(BranchType.PULL_REQUEST);
pullRequest.setKey(pullRequestId);
pullRequest.setMergeBranchUuid("U3");
pullRequest.setManualBaseline("analysisUUID");
underTest.insert(dbSession, pullRequest);

// select the feature branch
@@ -469,7 +391,6 @@ public class BranchDaoTest {
assertThat(loaded.getProjectUuid()).isEqualTo(pullRequest.getProjectUuid());
assertThat(loaded.getBranchType()).isEqualTo(pullRequest.getBranchType());
assertThat(loaded.getMergeBranchUuid()).isEqualTo(pullRequest.getMergeBranchUuid());
assertThat(loaded.getManualBaseline()).isEqualTo(pullRequest.getManualBaseline());

// select a branch on another project with same branch name
assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty();

+ 25
- 5
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java View File

@@ -72,6 +72,8 @@ import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.newcodeperiod.NewCodePeriodDto;
import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.rule.RuleDefinitionDto;
@@ -418,7 +420,7 @@ public class PurgeDaoTest {

@Test
public void selectPurgeableAnalyses() {
SnapshotDto[] analyses = new SnapshotDto[] {
SnapshotDto[] analyses = new SnapshotDto[]{
newSnapshot()
.setUuid("u1")
.setComponentUuid(PROJECT_UUID)
@@ -476,7 +478,13 @@ public class PurgeDaoTest {
.setComponentUuid(project1.uuid())
.setStatus(STATUS_PROCESSED)
.setLast(false));
dbClient.branchDao().updateManualBaseline(dbSession, project1.uuid(), analysis1.getUuid());
dbClient.newCodePeriodDao().insert(dbSession,
new NewCodePeriodDto()
.setProjectUuid(project1.uuid())
.setBranchUuid(project1.uuid())
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue(analysis1.getUuid())
);
ComponentDto project2 = db.components().insertPrivateProject();
SnapshotDto analysis2 = db.components().insertSnapshot(newSnapshot()
.setComponentUuid(project2.uuid())
@@ -497,7 +505,13 @@ public class PurgeDaoTest {
.setComponentUuid(project.uuid())
.setStatus(STATUS_PROCESSED)
.setLast(false));
dbClient.branchDao().updateManualBaseline(dbSession, project.uuid(), analysisProject.getUuid());
dbClient.newCodePeriodDao().insert(dbSession,
new NewCodePeriodDto()
.setProjectUuid(project.uuid())
.setBranchUuid(project.uuid())
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue(analysisProject.getUuid())
);
ComponentDto branch1 = db.components().insertProjectBranch(project);
SnapshotDto analysisBranch1 = db.components().insertSnapshot(newSnapshot()
.setComponentUuid(branch1.uuid())
@@ -508,7 +522,13 @@ public class PurgeDaoTest {
.setComponentUuid(branch2.uuid())
.setStatus(STATUS_PROCESSED)
.setLast(false));
dbClient.branchDao().updateManualBaseline(dbSession, branch2.uuid(), analysisBranch2.getUuid());
dbClient.newCodePeriodDao().insert(dbSession,
new NewCodePeriodDto()
.setProjectUuid(project.uuid())
.setBranchUuid(branch2.uuid())
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue(analysisBranch2.getUuid())
);
dbSession.commit();

assertThat(underTest.selectPurgeableAnalyses(project.uuid(), dbSession))
@@ -1577,7 +1597,7 @@ public class PurgeDaoTest {

@SafeVarargs
private final void insertCeActivityAndChildDataWithDate(String ceActivityUuid, LocalDateTime dateTime,
Consumer<CeQueueDto>... queueDtoConsumers) {
Consumer<CeQueueDto>... queueDtoConsumers) {
long date = dateTime.toInstant(UTC).toEpochMilli();
CeQueueDto queueDto = new CeQueueDto();
queueDto.setUuid(ceActivityUuid);

+ 1
- 1
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/UnsetBaselineAction.java View File

@@ -91,7 +91,7 @@ public class UnsetBaselineAction implements ProjectAnalysesWsAction {
userSession.checkComponentPermission(UserRole.ADMIN, projectBranch);

String projectUuid = projectBranch.getMainBranchProjectUuid() != null ? projectBranch.getMainBranchProjectUuid() : projectBranch.uuid();
String branchUuid = branchKey != null ? projectBranch.uuid() : null;
String branchUuid = projectBranch.uuid();
dbClient.newCodePeriodDao().deleteByProjectUuidAndBranchUuid(dbSession, projectUuid, branchUuid);
dbSession.commit();
}

+ 11
- 5
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java View File

@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
@@ -38,6 +39,7 @@ import org.sonar.db.component.BranchDto;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.newcodeperiod.NewCodePeriodDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
@@ -174,7 +176,7 @@ public class UnsetBaselineActionTest {

@DataProvider
public static Object[][] nullOrEmptyOrValue() {
return new Object[][] {
return new Object[][]{
{null},
{""},
{randomAlphabetic(10)},
@@ -193,7 +195,7 @@ public class UnsetBaselineActionTest {

@DataProvider
public static Object[][] nullOrEmpty() {
return new Object[][] {
return new Object[][]{
{null},
{""},
};
@@ -201,7 +203,7 @@ public class UnsetBaselineActionTest {

@DataProvider
public static Object[][] nonexistentParamsAndFailureMessage() {
return new Object[][] {
return new Object[][]{
{ImmutableMap.of(PARAM_PROJECT, "nonexistent"), "Component 'nonexistent' on branch .* not found"},
{ImmutableMap.of(PARAM_BRANCH, "nonexistent"), "Component .* on branch 'nonexistent' not found"}
};
@@ -273,10 +275,14 @@ public class UnsetBaselineActionTest {

private void verifyManualBaseline(ComponentDto project, @Nullable SnapshotDto projectAnalysis) {
BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(dbSession, project.uuid()).get();
Optional<NewCodePeriodDto> newCodePeriod = db.getDbClient().newCodePeriodDao().selectByBranch(dbSession, branchDto.getProjectUuid(), branchDto.getUuid());
if (projectAnalysis == null) {
assertThat(branchDto.getManualBaseline()).isNull();
assertThat(newCodePeriod).isNotNull();
assertThat(newCodePeriod).isEmpty();
} else {
assertThat(branchDto.getManualBaseline()).isEqualTo(projectAnalysis.getUuid());
assertThat(newCodePeriod).isNotNull();
assertThat(newCodePeriod).isNotEmpty();
assertThat(newCodePeriod.get().getValue()).isEqualTo(projectAnalysis.getUuid());
}
}


Loading…
Cancel
Save