aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/rule.rb
blob: cfaa06e534f074f3a04a828d835252d8843ccbcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2008-2012 SonarSource
# mailto:contact AT sonarsource DOT com
#
# Sonar 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.
#
# Sonar 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 Sonar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
#
class Rule < ActiveRecord::Base

  MANUAL_REPOSITORY_KEY = 'manual'

  validates_presence_of :name, :plugin_name
  validates_presence_of :plugin_rule_key, :if => 'name.present?'

  has_many :rules_parameters
  has_many :rule_failures
  has_many :active_rules
  belongs_to :parent, :class_name => 'Rule', :foreign_key => 'parent_id'

  def repository_key
    plugin_name
  end

  def parameters
    rules_parameters
  end

  def parameter(name)
    result=nil
    parameters.each do |param|
      result = param if param.name==name
    end
    result
  end

  def priority_text
    Sonar::RulePriority.to_s(priority)
  end

  def key
    "#{plugin_name}:#{plugin_rule_key}"
  end

  def template?
    cardinality=='MULTIPLE'
  end

  def editable?
    !parent_id.nil?
  end

  def <=>(other)
    return -1 if other.nil?
    return 1 if other.name.nil?
    name.downcase<=>other.name.downcase
  end

  def name
    @l10n_name ||=
        begin
          result = Java::OrgSonarServerUi::JRubyFacade.getInstance().getRuleName(I18n.locale, repository_key, plugin_rule_key)
          result = read_attribute(:name) unless result
          result
        end
  end

  def name=(value)
    write_attribute(:name, value)
  end

  def description
    @l10n_description ||=
        begin
          result = Java::OrgSonarServerUi::JRubyFacade.getInstance().getRuleDescription(I18n.locale, repository_key, plugin_rule_key)
          result = read_attribute(:description) unless result
          result
        end
  end

  def description=(value)
    write_attribute(:description, value)
  end

  def config_key
    plugin_config_key
  end

  def self.to_i(key_or_id)
    id=key_or_id.to_i
    if id<=0 && key_or_id
      parts=key_or_id.split(':')
      if parts.size==2
        rule=Rule.find(:first, :conditions => {:plugin_name => parts[0], :plugin_rule_key => parts[1]})
        id=rule.id if rule
      end
    end
    id>0 ? id : nil
  end

  def self.by_key_or_id(key_or_id)
    rule=nil
    if key_or_id.present?
      id=key_or_id.to_i
      if id<=0
        parts=key_or_id.split(':')
        if parts.size==2
          rule=Rule.find(:first, :conditions => {:plugin_name => parts[0], :plugin_rule_key => parts[1]})
        end
      else
        rule=Rule.find(id)
      end
    end
    rule
  end

  def self.manual_rules
    Rule.find(:all, :conditions => ['enabled=? and plugin_name=?', true, MANUAL_REPOSITORY_KEY], :order => 'name')
  end

  def self.manual_rule(id)
    Rule.find(:first, :conditions => ['enabled=? and plugin_name=? and id=?', true, MANUAL_REPOSITORY_KEY, id])
  end

  def self.find_or_create_manual_rule(rule_id_or_name, create_if_not_found=false)
    if Api::Utils.is_integer?(rule_id_or_name)
      rule = Rule.find(:first, :conditions => {:enabled => true, :plugin_name => MANUAL_REPOSITORY_KEY, :id => rule_id_or_name.to_i})
    else
      key = rule_id_or_name.strip.downcase.sub(/\s+/, '_')
      rule = Rule.find(:first, :conditions => {:enabled => true, :plugin_name => MANUAL_REPOSITORY_KEY, :plugin_rule_key => key})
      if rule==nil && create_if_not_found
        rule = Rule.create!(:enabled => true, :plugin_name => MANUAL_REPOSITORY_KEY, :plugin_rule_key => key, :name => rule_id_or_name)
      end
    end
    rule
  end

  def create_violation!(resource, options={})
    line = options['line']
    checksum = nil
    level = Sonar::RulePriority.id(options['severity']||Severity::MAJOR)
    RuleFailure.create!(
        :snapshot => resource.last_snapshot,
        :rule => self,
        :failure_level => level,
        :message => options['message'],
        :cost => (options['cost'] ? options['cost'].to_f : nil),
        :switched_off => false,
        :line => line,
        :checksum => checksum)
  end


  def to_hash_json(profile)
    json = {'title' => name, 'key' => key, 'plugin' => plugin_name, 'config_key' => config_key}
    json['description'] = description if description
    active_rule = nil
    if profile
      active_rule = profile.active_by_rule_id(id)
      if active_rule
        json['priority'] = active_rule.priority_text
        json['status'] = 'ACTIVE'
      else
        json['priority'] = priority_text
        json['status'] = 'INACTIVE'
      end
    else
      json['priority'] = priority_text
    end
    json['params'] = parameters.collect { |parameter| parameter.to_hash_json(active_rule) } unless parameters.empty?
    json
  end

  def to_xml(profile, xml)
    xml.rule do
      xml.title(name)
      xml.key(key)
      xml.config_key(config_key)
      xml.plugin(plugin_name)
      xml.description { xml.cdata!(description) } if description
      active_rule = nil
      if profile
        active_rule = profile.active_by_rule_id(id)
        if active_rule
          xml.priority(active_rule.priority_text)
          xml.status('ACTIVE')
        else
          xml.priority(priority_text)
          xml.status("INACTIVE")
        end
      else
        xml.priority(priority_text)
      end
      parameters.each do |parameter|
        parameter.to_xml(active_rule, xml)
      end
    end
  end

  def to_csv(profile)
    csv = [name.strip, plugin_rule_key, plugin_name]
    if profile
      active_rule = profile.active_by_rule_id(id)
      if active_rule
        csv << active_rule.priority_text
        csv << 'ACTIVE'
      else
        csv << priority_text
        csv << 'INACTIVE'
      end
    end
    csv
  end


  # options :language => nil, :plugins => [], :searchtext => '', :profile => nil, :priorities => [], :status =>
  def self.search(java_facade, options={})
    conditions = ['enabled=:enabled']
    values = {:enabled => true}

    plugins=nil
    if remove_blank(options[:plugins])
      plugins = options[:plugins]
      unless options[:language].blank?
        plugins = plugins & java_facade.getRuleRepositoriesByLanguage(options[:language]).collect { |repo| repo.getKey() }
      end
    elsif !options[:language].blank?
      plugins = java_facade.getRuleRepositoriesByLanguage(options[:language]).collect { |repo| repo.getKey() }
    end

    if plugins
      if plugins.empty?
        conditions << "plugin_name IS NULL"
      else
        conditions << "plugin_name IN (:plugin_names)"
        values[:plugin_names] = plugins
      end
    end

    unless options[:searchtext].blank?
      searchtext = options[:searchtext].to_s.strip
      search_text_conditions='(UPPER(rules.name) LIKE :searchtext OR plugin_rule_key = :key'

      additional_keys=java_facade.searchRuleName(I18n.locale, searchtext)
      additional_keys.each do |java_rule_key|
        search_text_conditions<<" OR (plugin_name='#{java_rule_key.getRepositoryKey()}' AND plugin_rule_key='#{java_rule_key.getKey()}')"
      end

      search_text_conditions<<')'
      conditions << search_text_conditions
      values[:searchtext] = "%" << searchtext.clone.upcase << "%"
      values[:key] = searchtext
    end

    includes=(options[:include_parameters] ? :rules_parameters : nil)
    rules = Rule.find(:all, :include => includes, :conditions => [conditions.join(" AND "), values]).sort
    filter(rules, options)
  end

  def self.remove_blank(array)
    if array
      array = array - ['']
      array.empty? ? nil : array
    else
      nil
    end
  end

  def self.filter(rules, options)
    priorities = remove_blank(options[:priorities])
    profile = options[:profile]
    inheritance = options[:inheritance]

    if profile
      inactive = (options[:status]=='INACTIVE')
      active = (options[:status]=='ACTIVE')

      rules = rules.reject do |rule|
        active_rule = profile.active_by_rule_id(rule.id)
        ((inactive && active_rule) || (active && active_rule.nil?))
      end

      if priorities
        rules = rules.select do |rule|
          active_rule = profile.active_by_rule_id(rule.id)
          (active_rule && priorities.include?(active_rule.priority_text)) || (active_rule.nil? && priorities.include?(rule.priority_text))
        end
      end

      if inheritance=='NOT'
        rules = rules.select do |rule|
          active_rule = profile.active_by_rule_id(rule.id)
          (active_rule.nil? || active_rule.inheritance.blank?)
        end
      elsif inheritance.present?
        rules = rules.select do |rule|
          active_rule = profile.active_by_rule_id(rule.id)
          (active_rule && active_rule.inheritance==inheritance)
        end
      end

    elsif priorities
      rules = rules.select do |rule|
        priorities.include?(rule.priority_text)
      end
    end
    rules
  end
end