class Api::ReviewsController < Api::ApiController
- include MarkdownHelper
+ include MarkdownHelper, ReviewsHelper
def index
convert_markdown=(params[:html]=='true')
end
end
-
- private
-
- def to_xml(reviews, convert_markdown)
- xml = Builder::XmlMarkup.new(:indent => 0)
- xml.instruct!
-
- xml.reviews do
- reviews.each do |review|
- 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(convert_markdown ? markdown_to_html(comment.review_text): comment.review_text)
- end
- end
- 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
\ No newline at end of file
end
def rest_to_json(rule_failures)
- JSON(rule_failures.collect{|rule_failure| rule_failure.to_hash_json})
+ JSON(rule_failures.collect{|rule_failure| rule_failure.to_hash_json(params['include_review']=="true"?true:false)})
end
def rest_to_xml(rule_failures)
xml.instruct!
xml.violations do
rule_failures.each do |rule_failure|
- rule_failure.to_xml(xml)
+ rule_failure.to_xml(xml, params['include_review']=="true"?true:false)
end
end
end
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
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"
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)
json = {}
json['message'] = message
json['line'] = line if line && line>=1
:qualifier => snapshot.project.qualifier,
:language => snapshot.project.language
}
- json['review']=review.id.to_i if review
+ json['review'] = review_to_json(review, true) if review && include_review
json
end
- def to_xml(xml=Builder::XmlMarkup.new(:indent => 0))
+ # 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.line(line) if line && line>=1
xml.qualifier(snapshot.project.qualifier)
xml.language(snapshot.project.language)
end
- xml.review(review.id.to_i) if review
+ review_to_xml(xml, review, true) if review && include_review
end
end
private String resourceQualifier = null;
private Date createdAt = null;
private boolean switchedOff;
- private Long reviewId = null;
+ private Review review = null;
public String getMessage() {
return message;
/**
* @since 2.8
*/
- public Long getReviewId() {
- return reviewId;
+ public Review getReview() {
+ return review;
}
/**
* @since 2.8
*/
- public Violation setReviewId(Long l) {
- this.reviewId = l;
+ public Violation setReview(Review review) {
+ this.review = review;
return this;
}
}
private String[] categories;
private String[] severities;
private Integer limit;
+ private Boolean includeReview;
public ViolationQuery(String resourceKeyOrId) {
this.resourceKeyOrId = resourceKeyOrId;
return this;
}
+ /**
+ * @since 2.8
+ */
+ public Boolean getIncludeReview() {
+ return includeReview;
+ }
+
+ /**
+ * @since 2.8
+ */
+ public ViolationQuery setIncludeReview(Boolean includeReview) {
+ this.includeReview = includeReview;
+ return this;
+ }
+
@Override
public String getUrl() {
StringBuilder url = new StringBuilder(BASE_URL);
appendUrlParameter(url, "rules", ruleKeys);
appendUrlParameter(url, "categories", categories);
appendUrlParameter(url, "priorities", severities);
+ appendUrlParameter(url, "include_review", includeReview);
return url.toString();
}
violation.setSeverity(utils.getString(json, "priority"));
violation.setCreatedAt(utils.getDateTime(json, "createdAt"));
violation.setSwitchedOff(utils.getBoolean(json, "switchedOff"));
- violation.setReviewId(utils.getLong(json, "review"));
+
+ Object review = utils.getField(json, "review");
+ if (review != null) {
+ ReviewUnmarshaller reviewUnmarshaller = new ReviewUnmarshaller();
+ violation.setReview(reviewUnmarshaller.parse(review));
+ }
Object rule = utils.getField(json, "rule");
if (rule != null) {
package org.sonar.wsclient.unmarshallers;
import org.junit.Test;
+import org.sonar.wsclient.services.Review;
import org.sonar.wsclient.services.Violation;
import java.util.List;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
assertThat(violation.getResourceQualifier(), is("CLA"));
assertThat(violation.getResourceScope(), is("FIL"));
assertThat(violation.isSwitchedOff(), is(false));
- assertThat(violation.getReviewId(), nullValue());
+ assertThat(violation.getReview(), nullValue());
}
@Test
public void testSwitchedOff() {
Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/false-positive.json"));
assertThat(violation.isSwitchedOff(), is(true));
- assertThat(violation.getReviewId(), is(123L));
+ }
+
+ @Test
+ public void testViolationDecoratedWithReview() {
+ Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/violation-with-review.json"));
+ Review review = violation.getReview();
+ assertNotNull(review);
+ assertThat(review.getId(), is(3L));
+ assertThat(review.getComments().size(), is(4));
+ assertThat(review.getComments().get(1).getText(), is("<em>Bold on multiple line?</em>"));
}
/**
[
- {"message":"throw java.lang.Exception","switchedOff": true, "review": 123, "priority":"MAJOR","rule":{"key":"pmd:SignatureDeclareThrowsException","name":"Signature Declare Throws Exception","category":"Maintainability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}}
+ {"message":"throw java.lang.Exception","switchedOff": true, "priority":"MAJOR","rule":{"key":"pmd:SignatureDeclareThrowsException","name":"Signature Declare Throws Exception","category":"Maintainability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}}
]
\ No newline at end of file