]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5097 Convert existing alerts to quality gates
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 17 Mar 2014 08:20:07 +0000 (09:20 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 17 Mar 2014 13:43:42 +0000 (14:43 +0100)
sonar-server/src/main/webapp/WEB-INF/db/migrate/512_convert_alerts_to_quality_gates.rb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/db/migrate/518_update_alerts_on_debt_to_minutes.rb [deleted file]
sonar-server/src/main/webapp/WEB-INF/db/migrate/518_update_conditions_on_debt_to_minutes.rb [new file with mode: 0644]

diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/512_convert_alerts_to_quality_gates.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/512_convert_alerts_to_quality_gates.rb
new file mode 100644 (file)
index 0000000..408a8eb
--- /dev/null
@@ -0,0 +1,78 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 4.3
+#
+class ConvertAlertsToQualityGates < ActiveRecord::Migration
+
+  class RulesProfile < ActiveRecord::Base
+  end
+
+  class Alert < ActiveRecord::Base
+  end
+
+  class QualityGate < ActiveRecord::Base
+  end
+
+  class QualityGateCondition < ActiveRecord::Base
+  end
+
+  def self.up
+
+    RulesProfile.reset_column_information
+    Alert.reset_column_information
+    QualityGate.reset_column_information
+    QualityGateCondition.reset_column_information
+
+    alerts = {}
+    Alert.all.each do |alert|
+      alerts[alert.profile_id] ||= []
+      alerts[alert.profile_id].push alert
+    end
+
+    new_operators = {
+      '='  => 'EQ',
+      '!=' => 'NE',
+      '>'  => 'GT',
+      '<'  => 'LT'
+    }
+
+    RulesProfile.all.each do |profile|
+      if alerts.has_key?(profile.id)
+        qgate_name = profile.name + ' - ' + profile.language
+
+        # This block allows re-execution in case of failure
+        old_qgate = QualityGate.find_by_name(qgate_name)
+        unless old_qgate.nil?
+          QualityGateCondition.destroy_all(:qgate_id => old_qgate.id)
+          old_qgate.destroy
+        end
+
+        qgate = QualityGate.create(:name => qgate_name)
+        alerts[profile.id].each do |alert|
+          QualityGateCondition.create(:qgate_id => qgate.id, :metric_id => alert.metric_id, :operator => new_operators[alert.operator],
+            :value_warning => alert.value_warning, :value_error => alert.value_error, :period => alert.period)
+        end
+      end
+    end
+  end
+
+end
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/518_update_alerts_on_debt_to_minutes.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/518_update_alerts_on_debt_to_minutes.rb
deleted file mode 100644 (file)
index f0f33ea..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-#
-# SonarQube, open source software quality management tool.
-# Copyright (C) 2008-2014 SonarSource
-# mailto:contact AT sonarsource DOT com
-#
-# SonarQube 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.
-#
-# SonarQube 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.
-#
-
-#
-# SonarQube 4.3
-# SONAR-4996
-#
-class UpdateAlertsOnDebtToMinutes < ActiveRecord::Migration
-
-  class Property < ActiveRecord::Base
-
-  end
-
-  class Alert < ActiveRecord::Base
-
-  end
-
-  def self.up
-    hours_in_day_prop = Property.find_by_prop_key('sonar.technicalDebt.hoursInDay')
-    hours_in_day = hours_in_day_prop ? hours_in_day_prop : 8
-
-    metrics = Metric.find(:all, :conditions => ['name in (?)', ['sqale_index', 'new_technical_debt',
-                                                                'sqale_effort_to_grade_a', 'sqale_effort_to_grade_b', 'sqale_effort_to_grade_c', 'sqale_effort_to_grade_d',
-                                                                'blocker_remediation_cost', 'critical_remediation_cost', 'major_remediation_cost', 'minor_remediation_cost',
-                                                                'info_remediation_cost']])
-    alerts = Alert.all(:conditions => ['metric_id in (?)', metrics.map { |m| m.id } ])
-
-    alerts.each do |alert|
-      alert.value_error = convert_days_to_minutes(alert.value_error.to_f, hours_in_day) unless alert.value_error.blank?
-      alert.value_warning = convert_days_to_minutes(alert.value_warning.to_f, hours_in_day) unless alert.value_warning.blank?
-      alert.save!
-    end
-  end
-
-  def self.convert_days_to_minutes(hours, hours_in_day)
-    result = hours * hours_in_day * 60
-    # Round value
-    result.ceil
-  end
-end
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/518_update_conditions_on_debt_to_minutes.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/518_update_conditions_on_debt_to_minutes.rb
new file mode 100644 (file)
index 0000000..8f377ef
--- /dev/null
@@ -0,0 +1,57 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 4.3
+# SONAR-4996
+#
+class UpdateConditionsOnDebtToMinutes < ActiveRecord::Migration
+
+  class Property < ActiveRecord::Base
+
+  end
+
+  class QualityGateCondition < ActiveRecord::Base
+
+  end
+
+  def self.up
+    hours_in_day_prop = Property.find_by_prop_key('sonar.technicalDebt.hoursInDay')
+    hours_in_day = hours_in_day_prop ? hours_in_day_prop : 8
+
+    metrics = Metric.find(:all, :conditions => ['name in (?)', ['sqale_index', 'new_technical_debt',
+                                                                'sqale_effort_to_grade_a', 'sqale_effort_to_grade_b', 'sqale_effort_to_grade_c', 'sqale_effort_to_grade_d',
+                                                                'blocker_remediation_cost', 'critical_remediation_cost', 'major_remediation_cost', 'minor_remediation_cost',
+                                                                'info_remediation_cost']])
+    conditions = QualityGateCondition.all(:conditions => ['metric_id in (?)', metrics.map { |m| m.id } ])
+
+    conditions.each do |condition|
+      condition.value_error = convert_days_to_minutes(condition.value_error.to_f, hours_in_day) unless condition.value_error.blank?
+      condition.value_warning = convert_days_to_minutes(condition.value_warning.to_f, hours_in_day) unless condition.value_warning.blank?
+      condition.save!
+    end
+  end
+
+  def self.convert_days_to_minutes(hours, hours_in_day)
+    result = hours * hours_in_day * 60
+    # Round value
+    result.ceil
+  end
+end