From d6ca1d42ff5ad857ac04b4479732bd91d84e5076 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 5 Oct 2012 16:25:01 +0200 Subject: [PATCH] SONAR-3819 Use the "options" parameter on @WidgetProperty to filter metrics --- .../org/sonar/api/web/WidgetProperty.java | 14 +++++++ .../WEB-INF/app/helpers/properties_helper.rb | 41 ++++++++++++++----- .../app/helpers/rules_configuration_helper.rb | 2 +- .../WEB-INF/app/helpers/settings_helper.rb | 21 +--------- .../app/helpers/widget_properties_helper.rb | 2 +- .../app/views/settings/_type_METRIC.html.erb | 2 +- 6 files changed, 49 insertions(+), 33 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java index 4fd661a7dfb..90fe3ea39fe 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java @@ -37,4 +37,18 @@ public @interface WidgetProperty { String description() default ""; boolean optional() default true; + + /** + * Options for property of type WidgetPropertyType.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. + * + * @since 3.3 + */ + String[] options() default {}; } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb index 3aef3b645bd..f2809b086bf 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb @@ -19,42 +19,42 @@ # module PropertiesHelper - def property_value(key, type, value, options = {}) + def property_input_field(key, type, value, options, additionalOptions = {}) if type==PropertyType::TYPE_STRING - text_field_tag key, value, {:size => 25}.update(options) + text_field_tag key, value, {:size => 25}.update(additionalOptions) elsif type==PropertyType::TYPE_TEXT - text_area_tag key, value, {:rows => '6', :style => 'width: 100%'}.update(options) + text_area_tag key, value, {:rows => '6', :style => 'width: 100%'}.update(additionalOptions) elsif type==PropertyType::TYPE_PASSWORD - password_field_tag key, value, {:size => 25}.update(options) + password_field_tag key, value, {:size => 25}.update(additionalOptions) elsif type==PropertyType::TYPE_BOOLEAN - (hidden_field_tag key, 'false', options) + (check_box_tag key, 'true', value=='true', options) + (hidden_field_tag key, 'false', additionalOptions) + (check_box_tag key, 'true', value=='true', additionalOptions) elsif type==PropertyType::TYPE_INTEGER - text_field_tag key, value, {:size => 10}.update(options) + text_field_tag key, value, {:size => 10}.update(additionalOptions) elsif type==PropertyType::TYPE_FLOAT - text_field_tag key, value, {:size => 10}.update(options) + text_field_tag key, value, {:size => 10}.update(additionalOptions) elsif type==PropertyType::TYPE_METRIC - metric_select_tag key, Metric.all.select{|m| m.display?}, :selected_key => value, :allow_empty => true + metric_select_tag key, metrics_filtered_by(options), :selected_key => value, :allow_empty => true elsif type==PropertyType::TYPE_REGULAR_EXPRESSION - text_field_tag key, value, {:size => 25}.update(options) + text_field_tag key, value, {:size => 25}.update(additionalOptions) elsif type==PropertyType::TYPE_FILTER user_filters = options_key(value, ::Filter.find(:all, :conditions => ['user_id=?', current_user.id]).sort_by(&:id)) shared_filters = options_key(value, ::Filter.find(:all, :conditions => ['(user_id<>? or user_id is null) and shared=?', current_user.id, true]).sort_by(&:id)) - filters_combo = select_tag key, option_group('My Filters', user_filters) + option_group('Shared Filters', shared_filters), options + filters_combo = select_tag key, option_group('My Filters', user_filters) + option_group('Shared Filters', shared_filters), additionalOptions filter_link = link_to message('widget.filter.edit'), {:controller => :filters, :action => :manage}, :class => 'link-action' "#{filters_combo} #{filter_link}" else - hidden_field_tag key, options + hidden_field_tag key, additionalOptions end end @@ -70,4 +70,23 @@ module PropertiesHelper options.empty? ? '' : "" + options + "" end + def metrics_filtered_by(options) + Metric.all.select(&:display?).sort_by(&:short_name).select do |metric| + options.blank? || 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/helpers/rules_configuration_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb index aa4beee3dbf..8f797bcd9fd 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb @@ -50,7 +50,7 @@ module RulesConfigurationHelper def param_value_input(parameter, value, options = {}) type=type_with_compatibility(parameter.param_type) - property_value 'value', type, value, {:id => parameter.id}.update(options) + property_input_field 'value', type, value, nil, {:id => parameter.id}.update(options) end def is_set(type) 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 0272059d815..857026a8042 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 @@ -18,6 +18,8 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 # module SettingsHelper + include PropertiesHelper + def category_name(category) message("property.category.#{category}", :default => category) end @@ -86,23 +88,4 @@ 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/helpers/widget_properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb index c6a3dda5149..00de6bec37c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb @@ -21,7 +21,7 @@ module WidgetPropertiesHelper include PropertiesHelper def property_value_field(definition, value) - property_value definition.key(), definition.type.name(), value.nil? ? definition.defaultValue() : value + property_input_field definition.key, definition.type.name, value.nil? ? definition.defaultValue : value, definition.options 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 61840cb7fa4..d06c87e3272 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 @@ -2,7 +2,7 @@ <% metrics_per_domain={} - metrics_for_property(property).each do |metric| + metrics_filtered_by(property.options).each do |metric| domain=metric.domain || '' metrics_per_domain[domain]||=[] metrics_per_domain[domain]<