aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-10-04 18:54:51 +0200
committerDavid Gageot <david@gageot.net>2012-10-04 19:16:34 +0200
commit1a0d00659736c083afa6eae1f08c6ee3ea6f6752 (patch)
tree56dd84e9702c513e1579d56db18ce6dca957f1dc
parent2b3d944acfbfa405ead9b7420447730f40f65393 (diff)
downloadsonarqube-1a0d00659736c083afa6eae1f08c6ee3ea6f6752.tar.gz
sonarqube-1a0d00659736c083afa6eae1f08c6ee3ea6f6752.zip
SONAR-3819 Use the "options" parameter on @Property to filter metrics
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java4
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/Property.java10
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb19
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb38
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</code>
+ *
+ * @since 3.3 Options for property of type PropertyType.METRIC</code>.
+ * 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 <code>key:REGEXP</code>, <code>domain:REGEXP</code> and <code>type:comma_separated__list_of_types</code>.
+ * For example <code>key:new_.*</code> will match any metric which key starts by <code>new_</code>.
+ * For example <code>type:INT,FLOAT</code> will match any metric of type <code>INT</code> or <code>FLOAT</code>.
+ * For example <code>type:NUMERIC</code> 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 @@
<select name="<%= name -%>" id="<%= id -%>">
<option value=""><%= message('default') -%></option>
<%
- metrics_per_domain={}
- Metric.all.select { |m| m.display? }.sort_by { |m| m.short_name }.each do |metric|
- domain=metric.domain || ''
- metrics_per_domain[domain]||=[]
- metrics_per_domain[domain]<<metric
- end
-
- metrics_per_domain.keys.sort.each do |domain|
-%>
-
- <optgroup label="<%= h domain -%>">
-<%
- metrics_per_domain[domain].each do |m|
- selected_attr = (m.key==value || m.id==value) ? " selected='selected'" : ''
-%>
- <option value="<%= m.key -%>" <%= selected_attr -%>><%= m.short_name -%></option>
-<%
+ metrics_per_domain={}
+ metrics_for_property(property).each do |metric|
+ domain=metric.domain || ''
+ metrics_per_domain[domain]||=[]
+ metrics_per_domain[domain]<<metric
end
-%>
- </optgroup>
-<%
- end
-%>
+
+ metrics_per_domain.keys.sort.each do |domain|
+ %>
+ <optgroup label="<%= h domain -%>">
+ <% metrics_per_domain[domain].each do |m|
+ selected_attr = (m.key==value || m.id==value) ? " selected='selected'" : ''
+ %>
+ <option value="<%= m.key -%>" <%= selected_attr -%>><%= m.short_name -%></option>
+ <% end -%>
+ </optgroup>
+ <% end -%>
</select> \ No newline at end of file