From c71a64449277c2bb6bceb7cc662592f68c852897 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 8 Nov 2019 13:39:52 +0100 Subject: [PATCH] SONAR-12663 Fix migration when 2 conditions on same metric --- ...igrateNoMoreUsedQualityGateConditions.java | 30 +++++++++++++++++++ ...teNoMoreUsedQualityGateConditionsTest.java | 17 +++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditions.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditions.java index 7bd9a7636f6..b7400e07f10 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditions.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditions.java @@ -22,7 +22,9 @@ package org.sonar.server.platform.db.migration.version.v76; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -38,6 +40,7 @@ import org.sonar.server.platform.db.migration.step.DataChange; import org.sonar.server.platform.db.migration.step.Upsert; import static com.google.common.base.Strings.isNullOrEmpty; +import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toSet; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; @@ -106,6 +109,7 @@ public class MigrateNoMoreUsedQualityGateConditions extends DataChange { markNoMoreSupportedConditionsAsToBeDeleted(migrationContext, conditions); markConditionsHavingOnlyWarningAsToBeDeleted(conditions); markConditionsUsingLeakPeriodHavingNoRelatedLeakMetricAsToBeDeleted(migrationContext, conditions); + markConditionsUsingLeakPeriodUsingSameMetricAsOtherConditionOnOverallAsToBeDeleted(conditions); markConditionsUsingLeakPeriodHavingAlreadyRelatedConditionAsToBeDeleted(migrationContext, conditions); updateConditionsUsingLeakPeriod(migrationContext, conditions); updateConditionsHavingErrorAndWarningByRemovingWarning(migrationContext, conditions); @@ -164,6 +168,32 @@ public class MigrateNoMoreUsedQualityGateConditions extends DataChange { }); } + private static void markConditionsUsingLeakPeriodUsingSameMetricAsOtherConditionOnOverallAsToBeDeleted(List allConditions) { + Map> conditionsByMetricId = new HashMap<>(); + for (QualityGateCondition c : allConditions) { + if (c.isToBeDeleted()) { + return; + } + int metricId = c.getMetricId(); + if (!conditionsByMetricId.containsKey(metricId)) { + conditionsByMetricId.put(metricId, new ArrayList<>(singletonList(c))); + } else { + List qualityGateConditions = conditionsByMetricId.get(metricId); + qualityGateConditions.add(c); + conditionsByMetricId.put(metricId, qualityGateConditions); + } + } + + for (Map.Entry> entry : conditionsByMetricId.entrySet()) { + List conditions = entry.getValue(); + if (conditions.size() > 1) { + conditions.stream() + .filter(QualityGateCondition::hasLeakPeriod) + .forEach(QualityGateCondition::setToBeDeleted); + } + } + } + private static void markConditionsUsingLeakPeriodHavingAlreadyRelatedConditionAsToBeDeleted(MigrationContext migrationContext, List conditions) { Map conditionsByMetricKey = conditions.stream() .filter(c -> !c.isToBeDeleted()) diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditionsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditionsTest.java index c725577ec8a..c900f6f18ac 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditionsTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v76/MigrateNoMoreUsedQualityGateConditionsTest.java @@ -89,8 +89,21 @@ public class MigrateNoMoreUsedQualityGateConditionsTest { assertConditions( tuple(conditionWithError, issues, "5", null, null), tuple(conditionWithErrorAndWarning1, coverage, "5", null, null), - tuple(conditionWithErrorAndWarning2, newLines, "7", null, 1L) - ); + tuple(conditionWithErrorAndWarning2, newLines, "7", null, 1L)); + } + + @Test + public void delete_conditions_on_leak_using_same_metric_as_other_condition_on_overall() throws SQLException { + long qualityGate = insertQualityGate(false); + long issues = insertMetric("violations", DIRECTION_WORST, "INT"); + long conditionOnOverallValue = insertCondition(qualityGate, issues, "GT", "5", null, null); + // Same condition but on leak period => will be deleted + long conditionOnLeakValue = insertCondition(qualityGate, issues, "GT", "15", null, 1); + + underTest.execute(); + + assertConditions( + tuple(conditionOnOverallValue, issues, "5", null, null)); } @Test -- 2.39.5