aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb
blob: e7dcdae8766fe4f3088930c013467a06ffa8e08d (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2008-2011 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 Review < ActiveRecord::Base
  belongs_to :user
  belongs_to :assignee, :class_name => "User", :foreign_key => "assignee_id"
  belongs_to :resource, :class_name => "Project", :foreign_key => "resource_id"
  belongs_to :project, :class_name => "Project", :foreign_key => "project_id"
  belongs_to :rule
  has_many :review_comments, :order => "created_at", :dependent => :destroy
  alias_attribute :comments, :review_comments
  has_and_belongs_to_many :action_plans

  validates_presence_of :user, :message => "can't be empty"
  validates_presence_of :status, :message => "can't be empty"
  validates_inclusion_of :severity, :in => Severity::KEYS

  before_save :assign_project

  STATUS_OPEN = 'OPEN'
  STATUS_RESOLVED = 'RESOLVED'
  STATUS_REOPENED = 'REOPENED'
  STATUS_CLOSED = 'CLOSED'

  RESOLUTION_FALSE_POSITIVE = 'FALSE-POSITIVE'
  RESOLUTION_FIXED = 'FIXED'

  def on_project?
    resource_id==project_id
  end

  def rule
    @rule ||=
      begin
        rule_failure ? rule_failure.rule : nil
      end
  end

  def violation
    rule_failure
  end

  def rule_failure
    @rule_failure ||=
      begin
        # We need to manually run this DB request as the real relation Reviews-RuleFailures is 1:n but we want only 1 violation
        # (more than 1 violation can have the same "permanent_id" when several analyses are run in a small time frame)
        RuleFailure.find(:first, :conditions => {:permanent_id => rule_failure_permanent_id}, :order => 'id desc')
      end
  end


  #
  #
  # REVIEW CORE METHODS
  #
  #

  # params are mandatory:
  # - :user
  # - :text
  def create_comment(options={})
    comment = comments.create!(options)
    touch
    notification_manager.notifyChanged(id.to_i, comment.user.login.to_java, to_java_map, to_java_map("comment" => comment.text))
  end

  def edit_comment(current_user, comment_id, comment_text)
    comment=comments.find(comment_id)
    if comment
      old_comment_text=comment.text
      comment.text=comment_text
      comment.save!
      touch
      notification_manager.notifyChanged(id.to_i, current_user.login.to_java, to_java_map("comment" => old_comment_text), to_java_map("comment" => comment.text))
    end
  end

  # TODO Godin: seems that this method not used anymore
  def edit_last_comment(current_user, comment_text)
    comment=comments.last
    old_comment_text=comment.text
    comment.text=comment_text
    comment.save!
    touch
    notification_manager.notifyChanged(id.to_i, current_user.login.to_java, to_java_map("comment" => old_comment_text), to_java_map("comment" => comment.text))
  end

  def delete_comment(current_user, comment_id)
    comment=comments.find(comment_id)
    comments.pop
    if comment
      old_comment_text=comment.text
      comment.delete
      touch
      notification_manager.notifyChanged(id.to_i, current_user.login.to_java, to_java_map("comment" => old_comment_text), to_java_map)
    end
  end

  def notification_manager
    Java::OrgSonarServerUi::JRubyFacade.getInstance().getReviewsNotificationManager()
  end

  def to_java_map(options = {})
    java.util.HashMap.new(
      {
        "project" => project.long_name.to_java,
        "resource" => resource.long_name.to_java,
        "title" => title.to_java,
        "creator" => user == nil ? nil : user.login.to_java,
        "assignee" => assignee == nil ? nil : assignee.login.to_java,
        "status" => status.to_java,
        "resolution" => resolution.to_java,
        "severity" => severity.to_java
      }.merge(options))
  end

  def reassign(current_user, assignee, options={})
    if options[:text].present?
      comments.create!(:user => current_user, :text => options[:text])
    end
    old = self.to_java_map
    self.assignee = assignee
    self.save!
    notification_manager.notifyChanged(id.to_i, current_user.login.to_java, old, to_java_map)
  end

  def reopen(current_user, options={})
    old = self.to_java_map
    if options[:text].present?
      comments.create!(:user => current_user, :text => options[:text])
    end
    self.status = STATUS_REOPENED
    self.resolution = nil
    self.save!
    notification_manager.notifyChanged(id.to_i, current_user.login.to_java, old, to_java_map)
  end

  def resolve(current_user, options={})
    old = self.to_java_map
    if options[:text].present?
      comments.create!(:user => current_user, :text => options[:text])
    end
    self.status = STATUS_RESOLVED
    self.resolution = RESOLUTION_FIXED
    self.save!
    notification_manager.notifyChanged(id.to_i, current_user.login.to_java, old, to_java_map)
  end

  # Parameters:
  # - :text
  def set_false_positive(is_false_positive, user, options={})
    if violation.nil?
      bad_request('This review does not relate to a violation')
    end
    violation.switched_off=is_false_positive
    violation.save!
    if options[:text].present?
      comments.create!(:user => user, :text => options[:text])
    end
    old = self.to_java_map
    self.assignee = nil
    self.status = is_false_positive ? STATUS_RESOLVED : STATUS_REOPENED
    self.resolution = is_false_positive ? RESOLUTION_FALSE_POSITIVE : nil
    self.save!
    notification_manager.notifyChanged(id.to_i, user.login.to_java, old, to_java_map("comment" => options[:text]))
  end

  def false_positive
    resolution == RESOLUTION_FALSE_POSITIVE
  end

  def can_change_false_positive_flag?
    (status == STATUS_RESOLVED && resolution == RESOLUTION_FALSE_POSITIVE) || status == STATUS_OPEN || status == STATUS_REOPENED
  end

  def set_severity(new_severity, user, options={})
    if options[:text].present?
      comments.create!(:user => user, :text => options[:text])
    end
    old = self.to_java_map
    self.severity=new_severity
    self.manual_severity=(new_severity!=violation.severity)
    self.save!
    notification_manager.notifyChanged(id.to_i, user.login.to_java, old, to_java_map("comment" => options[:text]))
  end

  def resolved?
    status == STATUS_RESOLVED
  end

  def closed?
    status == STATUS_CLOSED
  end

  def reopened?
    status == STATUS_REOPENED
  end

  def open?
    status == STATUS_OPEN
  end

  #
  #
  # SEARCH METHODS
  #
  #  

  def self.search(options={})
    conditions=[]
    values={}

    if options['id'].present?
      conditions << 'id=:id'
      values[:id]=options['id'].to_i
    elsif options['ids'].present?
      ids=options['ids'].split(',')
      conditions << 'id in (:ids)'
      values[:ids]=ids.map { |id| id.to_i }
    else
      
      # --- 'review_type' is deprecated since 2.9 ---
      # Following code just for backward compatibility
      review_type = options['review_type']
      if review_type
        if review_type == RESOLUTION_FALSE_POSITIVE
          conditions << "resolution='#{RESOLUTION_FALSE_POSITIVE}'"
        else
          conditions << "(resolution<>'#{RESOLUTION_FALSE_POSITIVE}' OR resolution IS NULL)"
        end
      end
      # --- End of code for backward compatibility code ---

      # --- For UI
      false_positives = options['false_positives']
      if false_positives == "only"
        conditions << "resolution='#{RESOLUTION_FALSE_POSITIVE}'"
      elsif false_positives == "without"
        conditions << "(resolution<>'#{RESOLUTION_FALSE_POSITIVE}' OR resolution IS NULL)"
      end
      # --- End

      # --- For web-service
      resolutions = options['resolutions'].split(',') if options['resolutions']
      if resolutions && resolutions.size>0 && !resolutions[0].blank?
        conditions << 'resolution in (:resolutions)'
        values[:resolutions] = resolutions
      end
      # --- End


      projects=options['projects'].split(',') if options['projects']
      if projects && projects.size>0 && !projects[0].blank?
        conditions << 'project_id in (:projects)'
        projectIds = []
        projects.each do |project|
          foundProject = Project.by_key(project)
          projectIds << foundProject.id if foundProject
        end
        values[:projects]=projectIds
      end

      resources=options['resources'].split(',') if options['resources']
      if resources && resources.size>0 && !resources[0].blank?
        conditions << 'resource_id in (:resources)'
        resourceIds = []
        resources.each do |resource|
          foundResource = Project.by_key(resource)
          resourceIds << foundResource.id if foundResource
        end
        values[:resources]=resourceIds
      end

      statuses=options['statuses'].split(',') if options['statuses']
      if statuses && statuses.size>0 && !statuses[0].blank?
        conditions << 'status in (:statuses)'
        values[:statuses]=statuses
      end

      severities=options['severities'].split(',') if options['severities']
      if severities && severities.size>0 && !severities[0].blank?
        conditions << 'severity in (:severities)'
        values[:severities]=severities
      end

      authors=options['authors'].split(',') if options['authors']
      if authors && authors.size>0 && !authors[0].blank?
        conditions << 'user_id in (:authors)'
        unless Api::Utils.is_number?(authors[0])
          values[:authors]=User.logins_to_ids(authors)
        else
          values[:authors]=authors.map { |user_id| user_id.to_i }
        end
      end

      assignees=options['assignees'].split(',') if options['assignees']
      if assignees && assignees.size>0 && !assignees[0].blank?
        conditions << 'assignee_id in (:assignees)'
        unless Api::Utils.is_number?(assignees[0])
          values[:assignees]=User.logins_to_ids(assignees)
        else
          values[:assignees]=assignees.map { |user_id| user_id.to_i }
        end
      end
      
      action_plan_id = options['action_plan_id']
      if action_plan_id
        action_plan = ActionPlan.find action_plan_id.to_i, :include => 'reviews'
        if action_plan
          conditions << 'id in (:ids)'
          values[:ids]=action_plan.reviews.map { |r| r.id }
        end
      end

      from=options['from']
      if from
        conditions << 'created_at >= :from'
        values[:from] = from
      end

      to=options['to']
      if from
        conditions << 'created_at <= :to'
        values[:to] = to
      end
    end

    sort=options['sort']
    asc=options['asc']
    if sort
      if asc
        sort += ' ASC, reviews.updated_at DESC'
      else
        sort += ' DESC, reviews.updated_at DESC'
      end
    else
      sort = 'reviews.updated_at DESC'
    end

    Review.find(:all, :include => ['review_comments', 'project', 'assignee', 'resource', 'user'], :conditions => [conditions.join(' AND '), values], :order => sort, :limit => 200)
  end


  #
  #
  # XML AND JSON UTILITY METHODS
  #
  #

  def self.reviews_to_xml(reviews, convert_markdown=false)
    xml = Builder::XmlMarkup.new(:indent => 0)
    xml.instruct!
    xml.reviews do
      reviews.each do |review|
        review.to_xml(xml, convert_markdown)
      end
    end
  end

  def to_xml(xml, convert_markdown=false)
    xml.review do
      xml.id(id.to_i)
      xml.createdAt(Api::Utils.format_datetime(created_at))
      xml.updatedAt(Api::Utils.format_datetime(updated_at))
      xml.author(user.login)
      xml.assignee(assignee.login) if assignee
      xml.title(title)
      xml.status(status)
      xml.resolution(resolution) if resolution
      xml.severity(severity)
      xml.resource(resource.kee) if resource
      xml.line(resource_line) if resource_line && resource_line>0
      xml.violationId(rule_failure_permanent_id) if rule_failure_permanent_id
      xml.comments do
        review_comments.each do |comment|
          xml.comment do
            xml.id(comment.id.to_i)
            xml.author(comment.user.login)
            xml.updatedAt(Api::Utils.format_datetime(comment.updated_at))
            if convert_markdown
              xml.text(comment.html_text)
            else
              xml.text(comment.plain_text)
            end
          end
        end
      end
    end
  end

  def self.reviews_to_json(reviews, convert_markdown=false)
    JSON(reviews.collect { |review| review.to_json(convert_markdown) })
  end

  def to_json(convert_markdown=false)
    json = {}
    json['id'] = id.to_i
    json['createdAt'] = Api::Utils.format_datetime(created_at)
    json['updatedAt'] = Api::Utils.format_datetime(updated_at)
    json['author'] = user.login
    json['assignee'] = assignee.login if assignee
    json['title'] = title if title
    json['status'] = status
    json['resolution'] = resolution if resolution
    json['severity'] = severity
    json['resource'] = resource.kee if resource
    json['line'] = resource_line if resource_line && resource_line>0
    json['violationId'] = rule_failure_permanent_id if rule_failure_permanent_id
    comments = []
    review_comments.each do |comment|
      comments << {
        'id' => comment.id.to_i,
        'author' => comment.user.login,
        'updatedAt' => Api::Utils.format_datetime(comment.updated_at),
        'text' => convert_markdown ? comment.html_text : comment.plain_text
      }
    end
    json['comments'] = comments
    json
  end

  #
  #
  # PRIVATE METHODS
  #
  #
  private

  def assign_project
    if self.project.nil? && self.resource
      self.project=self.resource.root_project
    end
  end


end