- New class Api::Utils to define some shared methods (around ISO datetime, markdown, ...)
- Move to_json and to_xml methods to RuleFailure and Review models
- Delete the class MarkdownHelper. Method to convert to html is moved to Api::Utils
hash[:rk]=event.resource.key if event.resource
hash[:n]=event.name if event.name
hash[:c]=event.category
- hash[:dt]=format_datetime(event.event_date) if event.event_date
+ hash[:dt]=Api::Utils.format_datetime(event.event_date) if event.event_date
hash[:ds]=event.description if event.description
hash[:data]=event.data if event.data
hash
xml.name(event.name) if event.name
xml.resourceKey(event.resource.key) if event.resource
xml.category(event.category)
- xml.date(format_datetime(event.event_date)) if event.event_date
+ xml.date(Api::Utils.format_datetime(event.event_date)) if event.event_date
xml.description(event.description) if event.description
xml.data(event.data) if event.data
end
versions={}
@snapshots_by_pid[project.id].sort{|s1,s2| s2.version <=> s1.version}.each do |snapshot|
version={:sid => snapshot.id.to_s}
- version[:d]=format_datetime(snapshot.created_at) if snapshot.created_at
+ version[:d]=Api::Utils.format_datetime(snapshot.created_at) if snapshot.created_at
if snapshot.last?
hash[:lv]=snapshot.version
end
if @snapshots_by_pid && @snapshots_by_pid[project.id]
@snapshots_by_pid[project.id].sort{|s1,s2| s2.version <=> s1.version}.each do |snapshot|
attributes={:sid => snapshot.id.to_s, :last => snapshot.last?}
- attributes[:date]=format_datetime(snapshot.created_at) if snapshot.created_at
+ attributes[:date]=Api::Utils.format_datetime(snapshot.created_at) if snapshot.created_at
xml.version(snapshot.version, attributes)
end
end
'name' => resource.name,
'scope' => resource.scope,
'qualifier' => resource.qualifier,
- 'date' => format_datetime(snapshot.created_at)}
+ 'date' => Api::Utils.format_datetime(snapshot.created_at)}
json['lname']=resource.long_name if resource.long_name
json['lang']=resource.language if resource.language
json['version']=snapshot.version if snapshot.version
if include_trends
json[:p1]=snapshot.period1_mode if snapshot.period1_mode
json[:p1p]=snapshot.period1_param if snapshot.period1_param
- json[:p1d]=format_datetime(snapshot.period1_date) if snapshot.period1_date
+ json[:p1d]=Api::Utils.format_datetime(snapshot.period1_date) if snapshot.period1_date
json[:p2]=snapshot.period2_mode if snapshot.period2_mode
json[:p2p]=snapshot.period2_param if snapshot.period2_param
- json[:p2d]=format_datetime(snapshot.period2_date) if snapshot.period2_date
+ json[:p2d]=Api::Utils.format_datetime(snapshot.period2_date) if snapshot.period2_date
json[:p3]=snapshot.period3_mode if snapshot.period3_mode
json[:p3p]=snapshot.period3_param if snapshot.period3_param
- json[:p3d]=format_datetime(snapshot.period3_date) if snapshot.period3_date
+ json[:p3d]=Api::Utils.format_datetime(snapshot.period3_date) if snapshot.period3_date
json[:p4]=snapshot.period4_mode if snapshot.period4_mode
json[:p4p]=snapshot.period4_param if snapshot.period4_param
- json[:p4d]=format_datetime(snapshot.period4_date) if snapshot.period4_date
+ json[:p4d]=Api::Utils.format_datetime(snapshot.period4_date) if snapshot.period4_date
json[:p5]=snapshot.period5_mode if snapshot.period5_mode
json[:p5p]=snapshot.period5_param if snapshot.period5_param
- json[:p5d]=format_datetime(snapshot.period5_date) if snapshot.period5_date
+ json[:p5d]=Api::Utils.format_datetime(snapshot.period5_date) if snapshot.period5_date
end
if measures
json_measures=[]
xml.qualifier(resource.qualifier)
xml.lang(resource.language) if resource.language
xml.version(snapshot.version) if snapshot.version
- xml.date(format_datetime(snapshot.created_at))
+ xml.date(Api::Utils.format_datetime(snapshot.created_at))
xml.description(resource.description) if include_descriptions && resource.description
xml.copy(resource.copy_resource_id) if resource.copy_resource_id
if include_trends
xml.period1(snapshot.period1_mode) if snapshot.period1_mode
xml.period1_param(snapshot.period1_param) if snapshot.period1_param
- xml.period1_date(format_datetime(snapshot.period1_date)) if snapshot.period1_date
+ xml.period1_date(Api::Utils.format_datetime(snapshot.period1_date)) if snapshot.period1_date
xml.period2(snapshot.period2_mode) if snapshot.period2_mode
xml.period2_param(snapshot.period2_param) if snapshot.period2_param
- xml.period2_date(format_datetime(snapshot.period2_date)) if snapshot.period2_date
+ xml.period2_date(Api::Utils.format_datetime(snapshot.period2_date)) if snapshot.period2_date
xml.period3(snapshot.period3_mode) if snapshot.period3_mode
xml.period3_param(snapshot.period3_param) if snapshot.period3_param
- xml.period3_date(format_datetime(snapshot.period3_date)) if snapshot.period3_date
+ xml.period3_date(Api::Utils.format_datetime(snapshot.period3_date)) if snapshot.period3_date
xml.period4(snapshot.period4_mode) if snapshot.period4_mode
xml.period4_param(snapshot.period4_param) if snapshot.period4_param
- xml.period4_date(format_datetime(snapshot.period4_date)) if snapshot.period4_date
+ xml.period4_date(Api::Utils.format_datetime(snapshot.period4_date)) if snapshot.period4_date
xml.period5(snapshot.period5_mode) if snapshot.period5_mode
xml.period5_param(snapshot.period5_param) if snapshot.period5_param
- xml.period5_date(format_datetime(snapshot.period5_date)) if snapshot.period5_date
+ xml.period5_date(Api::Utils.format_datetime(snapshot.period5_date)) if snapshot.period5_date
end
if measures
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
#
-require "json"
+require 'json'
class Api::ReviewsController < Api::ApiController
- include MarkdownHelper, ReviewsHelper
-
def index
- convert_markdown=(params[:html]=='true')
reviews=Review.search(params)
respond_to do |format|
- format.json { render :json => jsonp(to_json(reviews, convert_markdown)) }
- format.xml {render :xml => to_xml(reviews, convert_markdown)}
+ format.json { render :json => jsonp(Review.reviews_to_json(reviews)) }
+ format.xml {render :xml => Review.reviews_to_xml(reviews)}
format.text { render :text => text_not_supported }
end
end
end
@sids.each do |snapshot_id|
- cell={:d => format_datetime(@dates_by_sid[snapshot_id])}
+ cell={:d => Api::Utils.format_datetime(@dates_by_sid[snapshot_id])}
cell_values=[]
cell[:v]=cell_values
end
csv << header
@sids.each do |snapshot_id|
- row=[format_datetime(@dates_by_sid[snapshot_id])]
+ row=[Api::Utils.format_datetime(@dates_by_sid[snapshot_id])]
@metadata.each do |metadata|
measure=@measures_by_sid[snapshot_id][metadata.to_id]
if measure
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
#
-require "json"
+require 'json'
class Api::ViolationsController < Api::ResourceRestController
end
def rest_to_json(rule_failures)
- JSON(rule_failures.collect{|rule_failure| rule_failure.to_hash_json(params['include_review']=="true")})
+ include_review=(params['include_review']=='true')
+ JSON(rule_failures.collect{|rule_failure| rule_failure.to_json(include_review)})
end
def rest_to_xml(rule_failures)
+ include_review=(params['include_review']=='true')
xml = Builder::XmlMarkup.new(:indent => 0)
xml.instruct!
xml.violations do
rule_failures.each do |rule_failure|
- rule_failure.to_xml(xml, params['include_review']=="true")
+ rule_failure.to_xml(xml, include_review)
end
end
end
class ResourceController < ApplicationController
SECTION=Navigation::SECTION_RESOURCE
- helper :dashboard, :markdown
+ helper :dashboard
def index
@resource = Project.by_key(params[:id])
:only => [:assign, :comment_form, :flag_as_false_positive,
:violation_assign, :violation_flag_as_false_positive,:violation_save_comment, :violation_delete_comment],
:redirect_to => {:action => :error_not_post}
- helper ReviewsHelper, MarkdownHelper, SourceHelper
+ helper SourceHelper
def index
init_params()
+++ /dev/null
- #
- # 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
- #
-module MarkdownHelper
-
- def markdown_to_html(input)
- input ? Java::OrgSonarServerUi::JRubyFacade.markdownToHtml(ERB::Util.html_escape(input)) : ''
- end
-
-end
\ No newline at end of file
Project.find(:all, :select => 'id,name,long_name', :conditions => ['enabled=? AND scope=? AND qualifier IN (?)', true, 'PRJ', ['TRK', 'VW','SVW']], :order => 'name ASC')
end
- def to_xml(reviews, convert_markdown)
- xml = Builder::XmlMarkup.new(:indent => 0)
- xml.instruct!
-
- xml.reviews do
- reviews.each do |review|
- review_to_xml(xml, review, convert_markdown)
- end
- end
- end
-
- def review_to_xml(xml, review, html=false)
- xml.review do
- xml.id(review.id.to_i)
- xml.createdAt(format_datetime(review.created_at))
- xml.updatedAt(format_datetime(review.updated_at))
- xml.user(review.user.login)
- xml.assignee(review.assignee.login) if review.assignee
- xml.title(review.title)
- xml.type(review.review_type)
- xml.status(review.status)
- xml.severity(review.severity)
- xml.resource(review.resource.kee) if review.resource
- xml.line(review.resource_line) if review.resource_line > 0
- xml.comments do
- review.review_comments.each do |comment|
- xml.comment do
- xml.author(comment.user.login)
- xml.updatedAt(format_datetime(comment.updated_at))
- xml.text(html ? markdown_to_html(comment.review_text): comment.review_text)
- end
- end
- end
- end
- end
-
- def to_json(reviews, convert_markdown=false)
- JSON(reviews.collect{|review| review_to_json(review, convert_markdown)})
- end
-
- def review_to_json(review, html=false)
- json = {}
- json['id'] = review.id.to_i
- json['createdAt'] = format_datetime(review.created_at)
- json['updatedAt'] = format_datetime(review.updated_at)
- json['author'] = review.user.login
- json['assignee'] = review.assignee.login if review.assignee
- json['title'] = review.title if review.title
- json['type'] = review.review_type
- json['status'] = review.status
- json['severity'] = review.severity
- json['resource'] = review.resource.kee if review.resource
- json['line'] = review.resource_line if review.resource_line > 0
- comments = []
- review.review_comments.each do |comment|
- comments << {
- 'author' => comment.user.login,
- 'updatedAt' => format_datetime(comment.updated_at),
- 'text' => (html ? markdown_to_html(comment.review_text): comment.review_text)
- }
- end
- json['comments'] = comments
- json
- end
-
end
--- /dev/null
+#
+# 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
+#
+require 'time'
+
+class Api::Utils
+
+ # Format dateTime to ISO format
+ def self.format_datetime(datetime)
+ datetime.strftime("%Y-%m-%dT%H:%M:%S%z")
+ end
+
+ def self.parse_datetime(datetime_string, default_is_now=true)
+ if datetime_string.blank?
+ return (default_is_now ? Time.now : nil)
+ end
+ Time.parse(datetime_string)
+ end
+
+ def self.is_number?(s)
+ true if Float(s) rescue false
+ end
+
+ def self.markdown_to_html(markdown)
+ markdown ? Java::OrgSonarServerUi::JRubyFacade.markdownToHtml(ERB::Util.html_escape(markdown)) : ''
+ end
+
+end
authors=options['authors'].split(',') if options['authors']
if authors && authors.size>0 && !authors[0].blank?
conditions << 'user_id in (:authors)'
- unless is_number?(authors[0])
+ unless Api::Utils.is_number?(authors[0])
authors=User.logins_to_ids(authors)
end
values[:authors]=authors
assignees=options['assignees'].split(',') if options['assignees']
if assignees && assignees.size>0 && !assignees[0].blank?
conditions << 'assignee_id in (:assignees)'
- unless is_number?(assignees[0])
+ unless Api::Utils.is_number?(assignees[0])
assignees=User.logins_to_ids(assignees)
end
values[:assignees]=assignees
Review.find(:all, :include => [ 'review_comments' ], :order => 'created_at DESC', :conditions => [conditions.join(' AND '), values], :limit => 200)
end
+
+ def self.reviews_to_xml(reviews)
+ xml = Builder::XmlMarkup.new(:indent => 0)
+ xml.instruct!
+ xml.reviews do
+ reviews.each do |review|
+ review.to_xml(xml)
+ end
+ end
+ end
+
+ def to_xml(xml)
+ 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))
+ xml.text(comment.html_text)
+ end
+ end
+ end
+ end
+ end
+
+ def self.reviews_to_json(reviews)
+ JSON(reviews.collect{|review| review.to_json()})
+ end
+
+ def to_json
+ 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' => comment.html_text
+ }
+ end
+ json['comments'] = comments
+ json
+ end
+
+
+
private
- def self.is_number?(s)
- true if Float(s) rescue false
- end
+
def assign_project
if self.project.nil? && self.resource
belongs_to :user
belongs_to :review
validates_presence_of :user => "can't be empty"
- validate :comment_should_not_be_empty
+ validate :comment_should_not_be_blank
alias_attribute :text, :review_text
-
+
+ def html_text
+ Api::Utils.markdown_to_html(review_text)
+ end
+
private
-
- def comment_should_not_be_empty
- errors.add("Comment", " cannot be empty") if review_text.blank?
+
+ def comment_should_not_be_blank
+ errors.add("Comment", " cannot be blank") if review_text.blank?
end
end
class RuleFailure < ActiveRecord::Base
- include MarkdownHelper, ReviewsHelper
-
belongs_to :rule
belongs_to :snapshot
has_one :review, :primary_key => "permanent_id", :foreign_key => "rule_failure_permanent_id", :order => "created_at"
- def false_positive?
- switched_off==true
- end
-
# first line of message
def title
@title||=
end
end
- # in case to_has_json was used somewhere else before the "include_review" param was introduced
- def to_hash_json
- to_hash_json(false)
- end
-
- def to_hash_json(include_review=false)
+ def to_json(include_review=false)
json = {}
json['message'] = message
json['line'] = line if line && line>=1
json['priority'] = Sonar::RulePriority.to_s(failure_level).upcase
json['switchedOff']=true if switched_off?
if created_at
- json['createdAt'] = format_datetime(created_at)
+ json['createdAt'] = Api::Utils.format_datetime(created_at)
end
json['rule'] = {
:key => rule.key,
:qualifier => snapshot.project.qualifier,
:language => snapshot.project.language
}
- json['review'] = review_to_json(review, true) if review && include_review
+ json['review'] = review.to_json if include_review && review
json
end
- # in case to_xml was used somewhere else before the "include_review" param was introduced
- def to_xml(xml)
- to_xml(xml, false)
- end
-
def to_xml(xml=Builder::XmlMarkup.new(:indent => 0), include_review=false)
xml.violation do
xml.message(message)
xml.priority(Sonar::RulePriority.to_s(failure_level))
xml.switchedOff(true) if switched_off?
if created_at
- xml.createdAt(format_datetime(created_at))
+ xml.createdAt(Api::Utils.format_datetime(created_at))
end
xml.rule do
xml.key(rule.key)
xml.qualifier(snapshot.project.qualifier)
xml.language(snapshot.project.language)
end
- review_to_xml(xml, review, true) if review && include_review
+ review.to_xml(xml) if include_review && review
end
end
- def format_datetime(datetime)
- datetime.strftime("%Y-%m-%dT%H:%M:%S%z")
- end
-
def build_review(options={})
if review.nil?
self.review=Review.new(
<span class="violation_date"><%= distance_of_time_in_words_to_now(violation.created_at) -%></span>
<% end %>
- <% if violation.false_positive? %>
+ <% if violation.switched_off? %>
<%= image_tag("sep12.png") -%>
<span class="falsePositive">False-Positive</span>
<%= image_tag("sep12.png") -%>
<%
if violation.review
- unless violation.false_positive?
+ unless violation.switched_off?
%>
<%= link_to_remote (violation.review && violation.review.assignee_id ? "Reassign" : "Assign"),
<% end %>
- <%= link_to_remote (violation.false_positive? ? "Unflag as false-positive" : "Flag as false-positive"),
- :url => { :controller => "reviews", :action => "violation_false_positive_form", :id => violation.id, :false_positive => !violation.false_positive? },
+ <%= link_to_remote (violation.switched_off? ? "Unflag as false-positive" : "Flag as false-positive"),
+ :url => { :controller => "reviews", :action => "violation_false_positive_form", :id => violation.id, :false_positive => !violation.switched_off? },
:update => "reviewForm" + violation.id.to_s,
:complete => "$('vActions" + violation.id.to_s + "').hide();$('reviewForm" + violation.id.to_s + "').show();$('commentText" + violation.id.to_s + "').focus();" -%>
</span>
</h4>
<% if is_last_comment %>
<div id="lastComment<%= violation.id -%>">
- <%= markdown_to_html(review_comment.text) -%>
+ <%= review_comment.html_text -%>
</div>
<% else %>
- <%= markdown_to_html(review_comment.text) -%>
+ <%= review_comment.html_text -%>
<% end %>
</div>
<%
</h4>
<% if is_last_comment %>
<div id="lastComment">
- <%= markdown_to_html(comment.text) -%>
+ <%= comment.html_text -%>
</div>
<% else %>
- <%= markdown_to_html(comment.text) -%>
+ <%= comment.html_text -%>
<% end %>
</div>
<% end %>