]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12233 Remove quality gate conditions on Security Review Rating
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 25 Jun 2019 12:42:14 +0000 (14:42 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 28 Jun 2019 06:45:54 +0000 (08:45 +0200)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v79/DbVersion79.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRating.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v79/DbVersion79Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRatingTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRatingTest/quality_gate_conditions.sql [new file with mode: 0644]

index 963cc0ffb9686c6c83514211a2f9213acc509ae3..94eff8f6dab11fb0bbecee7fe15075a246dd5c8f 100644 (file)
@@ -29,6 +29,7 @@ public class DbVersion79 implements DbVersion {
       .add(2800, "Truncate environment variables and system properties from existing scanner reports",
         TruncateEnvAndSystemVarsFromScannerContext.class)
       .add(2801, "populate install version and install date internal properties", PopulateInstallDateAndVersion.class)
-      .add(2802, "Migrate property 'sonar.pullrequest.provider' value from VSTS to Azure DevOps", MigrateVstsProviderToAzureDevOps.class);
+      .add(2802, "Migrate property 'sonar.pullrequest.provider' value from VSTS to Azure DevOps", MigrateVstsProviderToAzureDevOps.class)
+      .add(2803, "Remove quality gate conditions on Security Review Rating", RemoveQGConditionsOnSecurityReviewRating.class);
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRating.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRating.java
new file mode 100644 (file)
index 0000000..53ca061
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v79;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.SupportsBlueGreen;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+
+@SupportsBlueGreen
+public class RemoveQGConditionsOnSecurityReviewRating extends DataChange {
+
+  public RemoveQGConditionsOnSecurityReviewRating(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate().rowPluralName("quality gate conditions on security review rating");
+    massUpdate.select("SELECT qgc.id FROM quality_gate_conditions qgc " +
+      "INNER JOIN metrics m on m.id=qgc.metric_id " +
+      "WHERE m.name='security_review_rating'");
+    massUpdate.update("DELETE FROM quality_gate_conditions WHERE id=?");
+    massUpdate.execute((row, update) -> {
+      update.setLong(1, row.getLong(1));
+      return true;
+    });
+  }
+
+}
index 760504bc02b3b957a39e6cc9e2b2eb3eb201d55d..5bb20f4ee3c0e325521c4fa722b8135b649ae151 100644 (file)
@@ -35,7 +35,7 @@ public class DbVersion79Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 3);
+    verifyMigrationCount(underTest, 4);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRatingTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRatingTest.java
new file mode 100644 (file)
index 0000000..b06c493
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v79;
+
+import com.google.common.collect.ImmutableMap;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.lang.math.RandomUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static com.google.common.primitives.Longs.asList;
+import static java.lang.String.format;
+import static java.util.stream.Collectors.toList;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RemoveQGConditionsOnSecurityReviewRatingTest {
+
+  private static final String TABLE_QUALITY_GATE_CONDITIONS = "quality_gate_conditions";
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(RemoveQGConditionsOnSecurityReviewRatingTest.class, "quality_gate_conditions.sql");
+
+  private RemoveQGConditionsOnSecurityReviewRating underTest = new RemoveQGConditionsOnSecurityReviewRating(db.database());
+
+  @Test
+  public void migrate() throws SQLException {
+    long securityReviewRatingMetric = insertMetric("security_review_rating");
+    long otherMetric = insertMetric("other");
+    insertQualityGateCondition(securityReviewRatingMetric);
+    insertQualityGateCondition(securityReviewRatingMetric);
+    long conditionOnOtherMetric = insertQualityGateCondition(otherMetric);
+
+    underTest.execute();
+
+    verifyConditionIds(asList(conditionOnOtherMetric));
+  }
+
+  @Test
+  public void do_nothing_when_no_condition_on_security_review_rating() throws SQLException {
+    long otherMetric = insertMetric("other");
+    long condition1 = insertQualityGateCondition(otherMetric);
+    long condition2 = insertQualityGateCondition(otherMetric);
+
+    underTest.execute();
+
+    verifyConditionIds(asList(condition1, condition2));
+  }
+
+  @Test
+  public void do_nothing_when_no_condition() throws SQLException {
+    insertMetric("other");
+
+    underTest.execute();
+
+    assertThat(db.countRowsOfTable(TABLE_QUALITY_GATE_CONDITIONS)).isZero();
+  }
+
+  @Test
+  public void migration_is_reentrant() throws SQLException {
+    long securityReviewRatingMetric = insertMetric("security_review_rating");
+    long otherMetric = insertMetric("other");
+    insertQualityGateCondition(securityReviewRatingMetric);
+    insertQualityGateCondition(securityReviewRatingMetric);
+    long conditionOnOtherMetric = insertQualityGateCondition(otherMetric);
+
+    underTest.execute();
+    underTest.execute();
+
+    verifyConditionIds(asList(conditionOnOtherMetric));
+  }
+
+  private void verifyConditionIds(List<Long> expectedConditionIds) {
+    List<Map<String, Object>> results = db.select("select ID from  " + TABLE_QUALITY_GATE_CONDITIONS);
+    assertThat(results.stream()
+      .map(map -> (long) map.get("ID"))
+      .collect(toList()))
+        .containsExactlyInAnyOrderElementsOf(expectedConditionIds);
+  }
+
+  private long insertQualityGateCondition(long metricId) {
+    long qualityGateId = RandomUtils.nextInt();
+    Map<String, Object> values = new HashMap<>(ImmutableMap.of("QGATE_ID", qualityGateId, "METRIC_ID", metricId, "OPERATOR", "GT"));
+    values.put("VALUE_ERROR", RandomUtils.nextInt());
+    db.executeInsert(TABLE_QUALITY_GATE_CONDITIONS, values);
+    String sql = format("select id as \"id\" from %s where qgate_id='%s' and metric_id='%s'", TABLE_QUALITY_GATE_CONDITIONS, qualityGateId, metricId);
+    return (Long) db
+      .selectFirst(sql)
+      .get("id");
+  }
+
+  private long insertMetric(String key) {
+    db.executeInsert("metrics", "NAME", key);
+    return (Long) db.selectFirst(format("select id as \"id\" from metrics where name='%s'", key)).get("id");
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRatingTest/quality_gate_conditions.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v79/RemoveQGConditionsOnSecurityReviewRatingTest/quality_gate_conditions.sql
new file mode 100644 (file)
index 0000000..163b1e7
--- /dev/null
@@ -0,0 +1,31 @@
+CREATE TABLE "QUALITY_GATE_CONDITIONS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "QGATE_ID" INTEGER,
+  "METRIC_ID" INTEGER,
+  "OPERATOR" VARCHAR(3),
+  "VALUE_ERROR" VARCHAR(64),
+  "VALUE_WARNING" VARCHAR(64),
+  "PERIOD" INTEGER,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP,
+);
+
+CREATE TABLE "METRICS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "NAME" VARCHAR(64) NOT NULL,
+  "DESCRIPTION" VARCHAR(255),
+  "DIRECTION" INTEGER NOT NULL DEFAULT 0,
+  "DOMAIN" VARCHAR(64),
+  "SHORT_NAME" VARCHAR(64),
+  "QUALITATIVE" BOOLEAN NOT NULL DEFAULT FALSE,
+  "VAL_TYPE" VARCHAR(8),
+  "USER_MANAGED" BOOLEAN DEFAULT FALSE,
+  "ENABLED" BOOLEAN DEFAULT TRUE,
+  "WORST_VALUE" DOUBLE,
+  "BEST_VALUE" DOUBLE,
+  "OPTIMIZED_BEST_VALUE" BOOLEAN,
+  "HIDDEN" BOOLEAN,
+  "DELETE_HISTORICAL_DATA" BOOLEAN,
+  "DECIMAL_SCALE" INTEGER
+);
+CREATE UNIQUE INDEX "METRICS_UNIQUE_NAME" ON "METRICS" ("NAME");