]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2347 Close a review when its corresponding violation is fixed
authorFabrice Bellingard <bellingard@gmail.com>
Tue, 17 May 2011 09:02:32 +0000 (11:02 +0200)
committerFabrice Bellingard <bellingard@gmail.com>
Tue, 17 May 2011 09:02:32 +0000 (11:02 +0200)
Update the DB Cleaner plugin to delete reviews and review comments
related to deleted projects.

plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviews.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest/purgeOrphanReviews-result.xml [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest/purgeOrphanReviews.xml [new file with mode: 0644]
sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl

index f38a6c39b20b83e092b582f1c25d2ba13a590fb2..a02d8d0a3087bd4320ec247eaee56b9823f12a3a 100644 (file)
  */
 package org.sonar.plugins.dbcleaner;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
 import org.sonar.api.SonarPlugin;
 import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
 import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
 import org.sonar.plugins.dbcleaner.period.PeriodPurge;
-import org.sonar.plugins.dbcleaner.purges.*;
+import org.sonar.plugins.dbcleaner.purges.PurgeDeletedResources;
+import org.sonar.plugins.dbcleaner.purges.PurgeDependencies;
+import org.sonar.plugins.dbcleaner.purges.PurgeDeprecatedLast;
+import org.sonar.plugins.dbcleaner.purges.PurgeDisabledResources;
+import org.sonar.plugins.dbcleaner.purges.PurgeEntities;
+import org.sonar.plugins.dbcleaner.purges.PurgeEventOrphans;
+import org.sonar.plugins.dbcleaner.purges.PurgeOrphanResources;
+import org.sonar.plugins.dbcleaner.purges.PurgeOrphanReviews;
+import org.sonar.plugins.dbcleaner.purges.PurgePropertyOrphans;
+import org.sonar.plugins.dbcleaner.purges.PurgeResourceRoles;
+import org.sonar.plugins.dbcleaner.purges.PurgeRuleMeasures;
+import org.sonar.plugins.dbcleaner.purges.PurgeUnprocessed;
 import org.sonar.plugins.dbcleaner.runner.PurgeRunner;
 
-import java.util.Arrays;
-import java.util.List;
-
 @Properties({
-    @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK,
-        defaultValue = DbCleanerConstants.ONE_MONTH, name = "Number of months before starting to keep only one snapshot by week",
+    @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, defaultValue = DbCleanerConstants.ONE_MONTH,
+        name = "Number of months before starting to keep only one snapshot by week",
         description = "After this number of months, if there are several snapshots during the same week, "
             + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true),
-    @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH,
-        defaultValue = DbCleanerConstants.ONE_YEAR, name = "Number of months before starting to keep only one snapshot by month",
+    @Property(key = DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, defaultValue = DbCleanerConstants.ONE_YEAR,
+        name = "Number of months before starting to keep only one snapshot by month",
         description = "After this number of months, if there are several snapshots during the same month, "
             + "the DbCleaner keeps the first one and fully delete the other ones.", global = true, project = true),
     @Property(key = DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS, defaultValue = DbCleanerConstants.FIVE_YEARS,
         name = "Number of months before starting to delete all remaining snapshots",
-        description = "After this number of months, all snapshots are fully deleted.", global = true, project = true)})
+        description = "After this number of months, all snapshots are fully deleted.", global = true, project = true) })
 public final class DbCleanerPlugin extends SonarPlugin {
 
   public List getExtensions() {
@@ -51,9 +62,9 @@ public final class DbCleanerPlugin extends SonarPlugin {
         DefaultPeriodCleaner.class,
 
         // purges
-        PurgeOrphanResources.class, PurgeEntities.class, PurgeRuleMeasures.class, PurgeUnprocessed.class,
-        PurgeDeletedResources.class, PurgeDeprecatedLast.class, PurgeDisabledResources.class,
-        PurgeResourceRoles.class, PurgeEventOrphans.class, PurgePropertyOrphans.class, PeriodPurge.class, PurgeDependencies.class,
+        PurgeOrphanResources.class, PurgeEntities.class, PurgeRuleMeasures.class, PurgeUnprocessed.class, PurgeDeletedResources.class,
+        PurgeDeprecatedLast.class, PurgeDisabledResources.class, PurgeResourceRoles.class, PurgeEventOrphans.class,
+        PurgePropertyOrphans.class, PeriodPurge.class, PurgeDependencies.class, PurgeOrphanReviews.class,
 
         // post-job
         PurgeRunner.class);
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviews.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviews.java
new file mode 100644 (file)
index 0000000..8e52416
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.dbcleaner.purges;
+
+import javax.persistence.Query;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.plugins.dbcleaner.api.Purge;
+import org.sonar.plugins.dbcleaner.api.PurgeContext;
+
+/**
+ * Purge Review that are attached to projects that have been deleted.
+ * 
+ * @since 2.8
+ */
+public final class PurgeOrphanReviews extends Purge {
+
+  private static final Logger LOG = LoggerFactory.getLogger(PurgeOrphanReviews.class);
+
+  public PurgeOrphanReviews(DatabaseSession session) {
+    super(session);
+  }
+
+  public void purge(PurgeContext context) {
+    DatabaseSession session = getSession();
+    
+    // delete reviews
+    Query query = session.createNativeQuery(getDeleteReviewsSqlRequest());
+    int rowDeleted = query.executeUpdate();
+    LOG.debug("- {} reviews deleted.", rowDeleted);
+    
+    // and delete review comments
+    query = session.createNativeQuery(getDeleteReviewCommentsSqlRequest());
+    rowDeleted = query.executeUpdate();
+    LOG.debug("- {} review comments deleted.", rowDeleted);
+
+    session.commit();
+  }
+
+  protected String getDeleteReviewsSqlRequest() {
+    return "DELETE FROM reviews WHERE project_id not in (SELECT id FROM projects WHERE scope = 'PRJ' and qualifier = 'TRK')";
+  }
+
+  protected String getDeleteReviewCommentsSqlRequest() {
+    return "DELETE FROM review_comments WHERE review_id not in (SELECT id FROM reviews)";
+  }
+}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest.java
new file mode 100644 (file)
index 0000000..de1632a
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.dbcleaner.purges;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.sql.Statement;
+
+import org.junit.Test;
+import org.sonar.test.persistence.DatabaseTestCase;
+
+public class PurgeOrphanReviewsTest extends DatabaseTestCase {
+
+  @Test
+  public void shouldCloseReviewWithoutCorrespondingViolation() throws Exception {
+    setupData("purgeOrphanReviews");
+
+    Statement stmt = getConnection().createStatement();
+    int count = stmt.executeUpdate(new PurgeOrphanReviews(null).getDeleteReviewsSqlRequest());
+    assertThat(count, is(1));
+
+    count = stmt.executeUpdate(new PurgeOrphanReviews(null).getDeleteReviewCommentsSqlRequest());
+    assertThat(count, is(1));
+
+    assertTables("purgeOrphanReviews", "reviews");
+  }
+
+}
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest/purgeOrphanReviews-result.xml b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest/purgeOrphanReviews-result.xml
new file mode 100644 (file)
index 0000000..7680bdd
--- /dev/null
@@ -0,0 +1,43 @@
+<dataset>
+  <projects long_name="[null]" id="1" scope="PRJ" qualifier="TRK" kee="other-project" name="other-project"
+            root_id="[null]" profile_id="[null]"
+            description="[null]"
+            enabled="true" language="java" copy_resource_id="[null]"/>
+
+  <!-- project1 has been removed from UI -->
+  <!--<projects long_name="[null]" id="2" scope="PRJ" qualifier="TRK" kee="project1" name="project1"-->
+            <!--root_id="[null]"-->
+            <!--description="[null]"-->
+            <!--enabled="true" language="java" copy_resource_id="[null]"/>-->
+
+  <reviews
+                       id="1"
+                       status="OPEN"
+                       rule_failure_permanent_id="1"
+                       resource_id="555"
+                       project_id="1"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
+                       
+  <!--  Following must have been deleted
+  <reviews
+                       id="2"
+                       status="CLOSED"
+                       rule_failure_permanent_id="2"
+                       resource_id="666"
+                       project_id="2"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
+  -->
+                       
+  <review_comments
+                       id="1"
+                       review_id="1"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" review_text=""/>
+                       
+  <!--  Following must have been deleted
+  <review_comments
+                       id="2"
+                       review_id="2"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" review_text=""/>
+  -->
+
+</dataset>
\ No newline at end of file
diff --git a/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest/purgeOrphanReviews.xml b/plugins/sonar-dbcleaner-plugin/src/test/resources/org/sonar/plugins/dbcleaner/purges/PurgeOrphanReviewsTest/purgeOrphanReviews.xml
new file mode 100644 (file)
index 0000000..01da166
--- /dev/null
@@ -0,0 +1,39 @@
+<dataset>
+  <projects long_name="[null]" id="1" scope="PRJ" qualifier="TRK" kee="other-project" name="other-project"
+            root_id="[null]" profile_id="[null]"
+            description="[null]"
+            enabled="true" language="java" copy_resource_id="[null]"/>
+
+  <!-- project1 has been removed from UI -->
+  <!--<projects long_name="[null]" id="2" scope="PRJ" qualifier="TRK" kee="project1" name="project1"-->
+            <!--root_id="[null]"-->
+            <!--description="[null]"-->
+            <!--enabled="true" language="java" copy_resource_id="[null]"/>-->
+
+  <reviews
+                       id="1"
+                       status="OPEN"
+                       rule_failure_permanent_id="1"
+                       resource_id="555"
+                       project_id="1"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
+  <reviews
+                       id="2"
+                       status="CLOSED"
+                       rule_failure_permanent_id="2"
+                       resource_id="666"
+                       project_id="2"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" assignee_id="[null]" title="[null]" review_type="[null]" severity="[null]" resource_line="[null]"/>
+                       
+  <review_comments
+                       id="1"
+                       review_id="1"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" review_text=""/>
+                       
+  <review_comments
+                       id="2"
+                       review_id="2"
+                       created_at="[null]" updated_at="[null]" user_id="[null]" review_text=""/>
+
+
+</dataset>
\ No newline at end of file
index c5125179cbfdf82682afc1e255bb6f760e2183ea..54f49a0953becba0732573585abe691ea8a5a565 100644 (file)
@@ -492,6 +492,7 @@ CREATE TABLE REVIEWS (
        STATUS VARCHAR(10),
        SEVERITY VARCHAR(10),
        RULE_FAILURE_PERMANENT_ID INTEGER,
+       PROJECT_ID INTEGER,
        RESOURCE_ID INTEGER,
        RESOURCE_LINE INTEGER,
     primary key (id)