]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4083 name is required when creating a metric and description return nothing...
authorJulien Lancelot <julien.lancelot@gmail.com>
Mon, 25 Mar 2013 11:06:19 +0000 (12:06 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Mon, 25 Mar 2013 11:06:19 +0000 (12:06 +0100)
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/metrics_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/models/metric.rb

index 36ac182ee75ed7f325a3db64b4a290ac5f8edd5b..3f849a5e4499d7438072639dca4eaa6e6b2b8a11 100644 (file)
@@ -44,6 +44,8 @@ class Api::MetricsController < Api::RestController
   end
 
   def create
+    bad_request('Name is required') unless params[:name].present?
+
     metric_test = Metric.first(:conditions => ['name=? OR id=?', params[:id], params[:id].to_i])
 
     exist_and_is_disable = !metric_test.nil? && !metric_test.enabled?
@@ -54,9 +56,13 @@ class Api::MetricsController < Api::RestController
     end
 
     begin
-      metric.attributes = params.merge({:name => params[:id], :short_name => params[:name], :enabled => true})
+      metric.attributes = params.merge({:name => params[:id], :short_name => params[:name]})
+      if metric.short_name(false)
+        metric.name = metric.short_name(false).downcase.gsub(/\s/, '_')[0..59] unless params[:id]
+      end
       metric.origin = Metric::ORIGIN_WS
       metric.user_managed = true
+      metric.enabled = true
       metric.save!
       Metric.clear_cache
       rest_status_ok
@@ -69,7 +75,7 @@ class Api::MetricsController < Api::RestController
     metric = Metric.first(:conditions => ['(name=? OR id=?) AND enabled=? AND user_managed=?', params[:id], params[:id].to_i, true, true])
     if metric
       begin
-        metric.attributes = params.merge({:name => params[:id], :short_name => params[:name], :enabled => true})
+        metric.attributes = params.merge({:name => params[:id], :short_name => params[:name]})
         metric.save!
         Metric.clear_cache
         rest_status_ok
@@ -77,14 +83,14 @@ class Api::MetricsController < Api::RestController
         rest_status_ko(metric.errors.full_messages.join("."), 400)
       end
     else
-      rest_status_ko('Unable to update : Metric [' + params[:id] + '] does not exist', 404)
+      rest_status_ko('Unable to update manual metric: '+ params[:id], 404)
     end
   end
 
   def destroy
     metric = Metric.first(:conditions => ['(name=? OR id=?) AND enabled=? AND user_managed=?', params[:id], params[:id].to_i, true, true])
     if !metric
-      rest_status_ko('Unable to delete : Metric [' + params[:id] + '] does not exist', 404)
+      rest_status_ko('Unable to delete manual metric which does not exist: ' + params[:id], 404)
     else
       metric.enabled = false
       begin
index fa3825630e90fefbdafe0e341bf0fa3a8171fef1..818799efbf6954bc0e4e26998bbb023eb8e85f12 100644 (file)
@@ -43,7 +43,7 @@ class Metric < ActiveRecord::Base
   CACHE_KEY='metrics'
   I18N_DOMAIN_CACHE_KEY='i18n_domains'
   I18N_SHORT_NAME_CACHE_KEY='i18n_metric_short_names'
-  
+
   validates_length_of       :name, :within => 1..64
   validates_uniqueness_of   :name
   validates_length_of       :short_name, :within => 1..64
@@ -132,7 +132,7 @@ class Metric < ActiveRecord::Base
   end
  
   def description(translate=true)
-    default_string = read_attribute(:description)
+    default_string = read_attribute(:description) || ''
     return default_string unless translate
 
     metric_name = read_attribute(:name)