From 8ee5ce3071082e0fb26f8984bcaa8f5bdb7877d7 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Thu, 5 May 2011 17:19:26 +0200 Subject: [PATCH] Improve the web services 'violations' and 'reviews' SONAR-2249 always use unix newline (LF) in violation messages SONAR-2382 always use unix newlines (LF) in review comments (plain output) SONAR-2382 use upper-case constants for output parameter SONAR-2382 replace the XML node by --- .../app/controllers/api/reviews_controller.rb | 2 +- .../app/controllers/api/violations_controller.rb | 4 ++-- .../src/main/webapp/WEB-INF/app/models/api/utils.rb | 4 ++++ .../src/main/webapp/WEB-INF/app/models/review.rb | 6 +++--- .../webapp/WEB-INF/app/models/review_comment.rb | 4 ++++ .../main/webapp/WEB-INF/app/models/rule_failure.rb | 13 ++++++++++--- .../org/sonar/wsclient/services/ReviewQuery.java | 6 +++--- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/reviews_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/reviews_controller.rb index b178a345f5c..4638eafc9c3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/reviews_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/reviews_controller.rb @@ -23,7 +23,7 @@ require 'json' class Api::ReviewsController < Api::ApiController def index - convert_markdown=(params[:output]=='html') + convert_markdown=(params[:output]=='HTML') reviews=select_authorized(:user, Review.search(params), :project) respond_to do |format| diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/violations_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/violations_controller.rb index a22223d549d..02be1887f62 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/violations_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/violations_controller.rb @@ -93,13 +93,13 @@ class Api::ViolationsController < Api::ResourceRestController def rest_to_json(rule_failures) include_review=(params['include_review']=='true') - convert_markdown=(params[:output]=='html') + convert_markdown=(params[:output]=='HTML') JSON(rule_failures.collect{|rule_failure| rule_failure.to_json(include_review, convert_markdown)}) end def rest_to_xml(rule_failures) include_review=(params['include_review']=='true') - convert_markdown=(params[:output]=='html') + convert_markdown=(params[:output]=='HTML') xml = Builder::XmlMarkup.new(:indent => 0) xml.instruct! xml.violations do diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb index 5aeb88e1d02..a007048931b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb @@ -47,4 +47,8 @@ class Api::Utils # See http://jira.codehaus.org/browse/SONAR-2282 input.split(/\r?\n|\r/, -1) end + + def self.convert_string_to_unix_newlines(input) + split_newlines(input).join('\n') + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb index 6d5af841492..dbfa303a2db 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/review.rb @@ -143,7 +143,7 @@ class Review < ActiveRecord::Base 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.author(user.login) xml.assignee(assignee.login) if assignee xml.title(title) xml.type(review_type) @@ -159,7 +159,7 @@ class Review < ActiveRecord::Base if convert_markdown xml.text(comment.html_text) else - xml.text(comment.review_text) + xml.text(comment.plain_text) end end end @@ -189,7 +189,7 @@ class Review < ActiveRecord::Base comments << { 'author' => comment.user.login, 'updatedAt' => Api::Utils.format_datetime(comment.updated_at), - 'text' => convert_markdown ? comment.html_text : comment.review_text + 'text' => convert_markdown ? comment.html_text : comment.plain_text } end json['comments'] = comments diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb index 214f1afa4f9..b8059c19b07 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/review_comment.rb @@ -29,6 +29,10 @@ class ReviewComment < ActiveRecord::Base Api::Utils.markdown_to_html(review_text) end + def plain_text + Api::Utils.convert_string_to_unix_newlines(review_text) + end + private def comment_should_not_be_blank diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb index 9842e8dfbb7..eda6220717f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb @@ -36,7 +36,14 @@ class RuleFailure < ActiveRecord::Base end end end - + + def plain_message + @plain_message ||= + begin + Api::Utils.convert_string_to_unix_newlines(message) + end + end + def html_message @html_message ||= begin @@ -46,7 +53,7 @@ class RuleFailure < ActiveRecord::Base def to_json(include_review=false, convert_markdown=false) json = {} - json['message'] = message + json['message'] = plain_message if plain_message json['line'] = line if line && line>=1 json['priority'] = Sonar::RulePriority.to_s(failure_level).upcase json['switchedOff']=true if switched_off? @@ -70,7 +77,7 @@ class RuleFailure < ActiveRecord::Base def to_xml(xml=Builder::XmlMarkup.new(:indent => 0), include_review=false, convert_markdown=false) xml.violation do - xml.message(message) + xml.message(plain_message) if plain_message xml.line(line) if line && line>=1 xml.priority(Sonar::RulePriority.to_s(failure_level)) xml.switchedOff(true) if switched_off? diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ReviewQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ReviewQuery.java index 5914e4d3de1..dda32f50a08 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ReviewQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ReviewQuery.java @@ -26,6 +26,9 @@ public class ReviewQuery extends Query { public static final String BASE_URL = "/api/reviews"; + public static final String OUTPUT_PLAIN = "PLAIN"; + public static final String OUTPUT_HTML = "HTML"; + private String reviewType; private Long id; private Long[] ids; @@ -195,9 +198,6 @@ public class ReviewQuery extends Query { return output; } - /** - * @param output the output to set - */ public ReviewQuery setOutput(String output) { this.output = output; return this; -- 2.39.5