From 1a0d00659736c083afa6eae1f08c6ee3ea6f6752 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 4 Oct 2012 18:54:51 +0200 Subject: [PATCH] SONAR-3819 Use the "options" parameter on @Property to filter metrics --- .../org/sonar/plugins/core/CorePlugin.java | 4 +- .../src/main/java/org/sonar/api/Property.java | 10 ++++- .../WEB-INF/app/helpers/settings_helper.rb | 19 ++++++++++ .../app/views/settings/_type_METRIC.html.erb | 38 ++++++++----------- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index e1c7f2eab74..c059833d500 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -177,7 +177,9 @@ import java.util.List; @PropertyField( key = "metric", name = "metric", - type = PropertyType.METRIC), + type = PropertyType.METRIC, + options = {"regexp_on_key: new_.*"} + ), @PropertyField( key = "password", name = "password", diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java index 3d85cfb46f0..b0179ceedf3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java @@ -88,7 +88,15 @@ public @interface Property { /** * Options for *_LIST types * - * @since 3.0 + * @since 3.0 Options for property of type PropertyType.SINGLE_SELECT_LIST + * + * @since 3.3 Options for property of type PropertyType.METRIC. + * If no option is specified, any metric will match. + * If options are specified, all must match for the metric to be displayed. + * Three types of filter are supported key:REGEXP, domain:REGEXP and type:comma_separated__list_of_types. + * For example key:new_.* will match any metric which key starts by new_. + * For example type:INT,FLOAT will match any metric of type INT or FLOAT. + * For example type:NUMERIC will match any metric of numerictype. */ String[] options() default {}; diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb index 13603e2af35..0272059d815 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb @@ -86,4 +86,23 @@ module SettingsHelper end name end + + def metrics_for_property(property) + Metric.all.select(&:display?).sort_by(&:short_name).select do |metric| + property.options.blank? || property.options.any? { |option| metric_matches(metric, option) } + end + end + + def metric_matches(metric, option) + if /key:(.*)/.match(option) + Regexp.new(Regexp.last_match(1).strip).match(metric.key) + elsif /domain:(.*)/.match(option) + Regexp.new(Regexp.last_match(1)).match(metric.domain) + elsif /type:(.*)/.match(option) + false + Regexp.last_match(1).split(',').any? { |type| (type == metric.value_type) || ((type == 'NUMERIC') && metric.numeric?) } + else + false + end + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb index c4329684079..61840cb7fa4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb @@ -1,27 +1,21 @@ \ No newline at end of file -- 2.39.5