aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb
blob: 6d5af84149259a475dc6c569c3dca8d954e8e408 (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
#
# 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"
  has_many :review_comments, :order => "created_at", :dependent => :destroy
  alias_attribute :comments, :review_comments
  belongs_to :rule_failure, :foreign_key => 'rule_failure_permanent_id'
  
  validates_presence_of :user, :message => "can't be empty"
  validates_presence_of :review_type, :message => "can't be empty"
  validates_presence_of :status, :message => "can't be empty"
  
  before_save :assign_project
  
  TYPE_VIOLATION = 'VIOLATION'
  TYPE_FALSE_POSITIVE = 'FALSE_POSITIVE'
  
  STATUS_OPEN = 'OPEN'
  STATUS_CLOSED = 'CLOSED'
    
  def on_project?
    resource_id==project_id
  end
  
  def rule
    @rule ||= 
      begin
        rule_failure ? rule_failure.rule : nil
      end
  end

  def self.search(options={})
    conditions=[]
    values={}
      
    review_type = options['review_type']
    if review_type
      conditions << 'review_type=:type'
      values[:type] = review_type.upcase
    else
      conditions=['review_type<>:not_type']
      values={:not_type => Review::TYPE_FALSE_POSITIVE}
    end
    
    ids=options['ids'].split(',') if options['ids']
    if options['id']
      conditions << 'id=:id'
      values[:id]=options['id'].to_i
    elsif ids && ids.size>0 && !ids[0].blank?
      conditions << 'id in (:ids)'
      values[:ids]=ids.map{|id| id.to_i}
    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

    Review.find(:all, :include => [ 'review_comments' ], :order => 'created_at DESC', :conditions => [conditions.join(' AND '), values], :limit => 200)
  end

  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.user(user.login)
      xml.assignee(assignee.login) if assignee
      xml.title(title)
      xml.type(review_type)
      xml.status(status)
      xml.severity(severity)
      xml.resource(resource.kee)  if resource
      xml.line(resource_line) if resource_line && resource_line>0
      xml.comments do
        review_comments.each do |comment|
          xml.comment do
            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.review_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['type'] = review_type
    json['status'] = status
    json['severity'] = severity
    json['resource'] = resource.kee if resource
    json['line'] = resource_line if resource_line && resource_line>0
    comments = []
    review_comments.each do |comment|
      comments << {
        'author' => comment.user.login,
        'updatedAt' => Api::Utils.format_datetime(comment.updated_at),
        'text' => convert_markdown ? comment.html_text : comment.review_text
      }
    end
    json['comments'] = comments
    json
  end

  

  
  private
  

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