]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2381, SONAR-2382 Web services return comment in HTML by default
authorFabrice Bellingard <bellingard@gmail.com>
Wed, 4 May 2011 14:53:02 +0000 (16:53 +0200)
committerFabrice Bellingard <bellingard@gmail.com>
Wed, 4 May 2011 14:53:02 +0000 (16:53 +0200)
Available for the Violation and Review WS:
- parameter 'output' on the request
- if set to 'html', comments are returned in HTML
- otherwise comments are returned as raw text
- Java WS client updated consequently

sonar-server/src/main/webapp/WEB-INF/app/controllers/api/reviews_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/violations_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/models/review.rb
sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ReviewQuery.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ViolationQuery.java
sonar-ws-client/src/test/java/org/sonar/wsclient/services/ReviewQueryTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/services/ViolationQueryTest.java

index ad85564b11b3e865ea51ff2b157191f34ebb8af8..b178a345f5c21fc3af67ee2c9842c500a06ecb08 100644 (file)
@@ -23,11 +23,12 @@ require 'json'
 class Api::ReviewsController < Api::ApiController
 
   def index
+    convert_markdown=(params[:output]=='html')
     reviews=select_authorized(:user, Review.search(params), :project)
     
     respond_to do |format|
-      format.json { render :json => jsonp(Review.reviews_to_json(reviews)) }
-      format.xml {render :xml => Review.reviews_to_xml(reviews)}
+      format.json { render :json => jsonp(Review.reviews_to_json(reviews, convert_markdown)) }
+      format.xml {render :xml => Review.reviews_to_xml(reviews, convert_markdown)}
       format.text { render :text => text_not_supported }
     end
   end
index 55a8198579ac520882f76820fbbaf84016538bd3..a22223d549d774cc7f2ae8d44809e9c30aec76a0 100644 (file)
@@ -93,16 +93,18 @@ class Api::ViolationsController < Api::ResourceRestController
 
   def rest_to_json(rule_failures)
     include_review=(params['include_review']=='true')
-    JSON(rule_failures.collect{|rule_failure| rule_failure.to_json(include_review)})
+    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')
     xml = Builder::XmlMarkup.new(:indent => 0)
     xml.instruct!
     xml.violations do
       rule_failures.each do |rule_failure|
-        rule_failure.to_xml(xml, include_review)
+        rule_failure.to_xml(xml, include_review, convert_markdown)
       end
     end
   end
index 3a45ff5b0edfe8f5afd3d96b44dca5fde3910f33..6d5af84149259a475dc6c569c3dca8d954e8e408 100644 (file)
@@ -128,17 +128,17 @@ class Review < ActiveRecord::Base
     Review.find(:all, :include => [ 'review_comments' ], :order => 'created_at DESC', :conditions => [conditions.join(' AND '), values], :limit => 200)
   end
 
-  def self.reviews_to_xml(reviews)
+  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)
+        review.to_xml(xml, convert_markdown)
       end
     end
   end
 
-  def to_xml(xml)
+  def to_xml(xml, convert_markdown=false)
     xml.review do
       xml.id(id.to_i)
       xml.createdAt(Api::Utils.format_datetime(created_at))
@@ -156,18 +156,22 @@ class Review < ActiveRecord::Base
           xml.comment do
             xml.author(comment.user.login)
             xml.updatedAt(Api::Utils.format_datetime(comment.updated_at))
-            xml.text(comment.html_text)
+            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)
-    JSON(reviews.collect{|review| review.to_json()})
+  def self.reviews_to_json(reviews, convert_markdown=false)
+    JSON(reviews.collect{|review| review.to_json(convert_markdown)})
   end
 
-  def to_json
+  def to_json(convert_markdown=false)
     json = {}
     json['id'] = id.to_i
     json['createdAt'] = Api::Utils.format_datetime(created_at)
@@ -185,7 +189,7 @@ class Review < ActiveRecord::Base
       comments << {
         'author' => comment.user.login,
         'updatedAt' => Api::Utils.format_datetime(comment.updated_at),
-        'text' => comment.html_text
+        'text' => convert_markdown ? comment.html_text : comment.review_text
       }
     end
     json['comments'] = comments
index 30615035a465d31bb7e312a5b26eeb0d1d49f7b5..9842e8dfbb74fcf806b6bc21d3ae4f2d9fb32ffc 100644 (file)
@@ -44,7 +44,7 @@ class RuleFailure < ActiveRecord::Base
       end
   end
 
-  def to_json(include_review=false)
+  def to_json(include_review=false, convert_markdown=false)
     json = {}
     json['message'] = message
     json['line'] = line if line && line>=1
@@ -64,11 +64,11 @@ class RuleFailure < ActiveRecord::Base
       :qualifier => snapshot.project.qualifier,
       :language => snapshot.project.language
     }
-    json['review'] = review.to_json if include_review && review
+    json['review'] = review.to_json(convert_markdown) if include_review && review
     json
   end
 
-  def to_xml(xml=Builder::XmlMarkup.new(:indent => 0), include_review=false)
+  def to_xml(xml=Builder::XmlMarkup.new(:indent => 0), include_review=false, convert_markdown=false)
     xml.violation do
       xml.message(message)
       xml.line(line) if line && line>=1
@@ -88,7 +88,7 @@ class RuleFailure < ActiveRecord::Base
         xml.qualifier(snapshot.project.qualifier)
         xml.language(snapshot.project.language)
       end
-      review.to_xml(xml) if include_review && review
+      review.to_xml(xml, convert_markdown) if include_review && review
     end
   end
 
index 21dc8d0d0d5d02bbf3fab8afb04b0ef7bc976098..38c3780ce2d2aaa26b472d1b167ebdba6aa319fa 100644 (file)
@@ -35,6 +35,7 @@ public class ReviewQuery extends Query<Review> {
   private String[] resourceKeysOrIds;
   private String[] authorLoginsOrIds;
   private String[] assigneeLoginsOrIds;
+  private String output;
 
   public ReviewQuery() {
   }
@@ -187,6 +188,21 @@ public class ReviewQuery extends Query<Review> {
     return this;
   }
 
+  /**
+   * @return the output
+   */
+  public String getOutput() {
+    return output;
+  }
+
+  /**
+   * @param output the output to set
+   */
+  public ReviewQuery setOutput(String output) {
+    this.output = output;
+    return this;
+  }
+
   @Override
   public String getUrl() {
     StringBuilder url = new StringBuilder(BASE_URL);
@@ -203,6 +219,7 @@ public class ReviewQuery extends Query<Review> {
     appendUrlParameter(url, "resources", resourceKeysOrIds);
     appendUrlParameter(url, "authors", authorLoginsOrIds);
     appendUrlParameter(url, "assignees", assigneeLoginsOrIds);
+    appendUrlParameter(url, "output", output);
 
     return url.toString();
   }
index e3eb5dd60dea9ea4af7d01561e5de005e74eb462..42408d98c037240a3b0a1f5eea32431658127f36 100644 (file)
@@ -32,6 +32,7 @@ public class ViolationQuery extends Query<Violation> {
   private String[] severities;
   private Integer limit;
   private Boolean includeReview;
+  private String output;
 
   public ViolationQuery(String resourceKeyOrId) {
     this.resourceKeyOrId = resourceKeyOrId;
@@ -144,6 +145,21 @@ public class ViolationQuery extends Query<Violation> {
     return this;
   }
 
+  /**
+   * @since 2.8
+   */
+  public String getOutput() {
+    return output;
+  }
+
+  /**
+   * @since 2.8
+   */
+  public ViolationQuery setOutput(String output) {
+    this.output = output;
+    return this;
+  }
+
   @Override
   public String getUrl() {
     StringBuilder url = new StringBuilder(BASE_URL);
@@ -159,6 +175,7 @@ public class ViolationQuery extends Query<Violation> {
     appendUrlParameter(url, "categories", categories);
     appendUrlParameter(url, "priorities", severities);
     appendUrlParameter(url, "include_review", includeReview);
+    appendUrlParameter(url, "output", output);
 
     return url.toString();
   }
index 0dc688784c1b3494e282aa5128601bece434f108..bf692cbbcb51bd79a7096452611faf163946f361 100644 (file)
@@ -40,16 +40,11 @@ public class ReviewQueryTest extends QueryTestCase {
   @Test
   public void resourceTreeViolations() {
     ReviewQuery query = new ReviewQuery();
-    query.setIds(10L, 11L);
-    query.setReviewType("FALSE_POSITIVE");
-    query.setStatuses("OPEN");
-    query.setSeverities("MINOR", "INFO");
-    query.setProjectKeysOrIds("com.sonar.foo:bar");
-    query.setResourceKeysOrIds("2", "3");
-    query.setAuthorLoginsOrIds("20");
-    query.setAssigneeLoginsOrIds("admin");
+    query.setIds(10L, 11L).setReviewType("FALSE_POSITIVE").setStatuses("OPEN").setSeverities("MINOR", "INFO")
+        .setProjectKeysOrIds("com.sonar.foo:bar").setResourceKeysOrIds("2", "3").setAuthorLoginsOrIds("20").setAssigneeLoginsOrIds("admin")
+        .setOutput("html");
     assertThat(
         query.getUrl(),
-        is("/api/reviews?ids=10,11&review_type=FALSE_POSITIVE&statuses=OPEN&severities=MINOR,INFO&projects=com.sonar.foo%3Abar&resources=2,3&authors=20&assignees=admin&"));
+        is("/api/reviews?ids=10,11&review_type=FALSE_POSITIVE&statuses=OPEN&severities=MINOR,INFO&projects=com.sonar.foo%3Abar&resources=2,3&authors=20&assignees=admin&output=html&"));
   }
 }
index f7f9279906d5d6699523d86b498e0a111217ee55..9a7140db3ee9d2ed8d2fb752b8cedda8f126e210 100644 (file)
@@ -40,9 +40,10 @@ public class ViolationQueryTest extends QueryTestCase {
         .setLimit(20)
         .setSeverities("MAJOR", "BLOCKER")
         .setQualifiers("FIL")
-        .setRuleKeys("checkstyle:foo", "pmd:bar");
+        .setRuleKeys("checkstyle:foo", "pmd:bar")
+        .setOutput("html");
     assertThat(
         query.getUrl(),
-        is("/api/violations?resource=myproject&depth=-1&limit=20&qualifiers=FIL&rules=checkstyle%3Afoo,pmd%3Abar&priorities=MAJOR,BLOCKER&"));
+        is("/api/violations?resource=myproject&depth=-1&limit=20&qualifiers=FIL&rules=checkstyle%3Afoo,pmd%3Abar&priorities=MAJOR,BLOCKER&output=html&"));
   }
 }