class Api::ReviewsController < Api::ApiController
+ include MarkdownHelper
+
def index
- @reviews = []
- @reviews << Review.find ( params[:id] )
+ convert_markdown=(params[:html]=='true')
+ reviews=Review.search(params)
respond_to do |format|
- format.json { render :json => jsonp(to_json) }
- format.xml {render :xml => to_xml}
+ format.json { render :json => jsonp(to_json(reviews, convert_markdown)) }
+ format.xml {render :xml => to_xml(reviews, convert_markdown)}
format.text { render :text => text_not_supported }
end
end
-
- def to_xml
+
+
+ private
+
+ def to_xml(reviews, convert_markdown)
xml = Builder::XmlMarkup.new(:indent => 0)
xml.instruct!
xml.reviews do
- @reviews.each do |review|
+ reviews.each do |review|
xml.review do
- xml.id(review.id)
- xml.updatedAt(review.updated_at)
+ xml.id(review.id.to_i)
+ xml.updatedAt(format_datetime(review.updated_at))
xml.user(review.user.login)
xml.assignee(review.assignee.login)
xml.title(review.title)
xml.type(review.review_type)
xml.status(review.status)
xml.severity(review.severity)
- xml.resourceLine(review.resource_line)
+ xml.resource(review.resource.kee) if review.resource
+ xml.line(review.resource_line) if review.resource_line
- # Continue here with resource + violation + comments
+ # Continue here with resource + comments
end
end
end
end
- def to_json
- JSON(@reviews.collect{|review| review.to_hash_json(true)})
+ 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['updatedAt'] = 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
+ comments = []
+ review.review_comments.each do |comment|
+ comments << {
+ 'author' => comment.user.login,
+ 'updatedAt' => format_datetime(comment.updated_at),
+ 'comment' => (html ? markdown_to_html(comment.review_text): comment.review_text)
+ }
+ end
+ json['comments'] = comments
+ json['line'] = review.resource_line if review.resource_line
+ json['resource'] = review.resource.kee if review.resource
+ json
+ end
+
end
\ No newline at end of file
end.compact
end
- if params[:switched_off] && params[:switched_off] == "true"
+ if params[:switched_off] == "true"
conditions << 'switched_off=:switched_off'
values[:switched_off] = true
else
limit = (params[:limit] ? [params[:limit].to_i,5000].min : 5000)
violations = RuleFailure.find(:all,
:conditions => [ conditions.join(' AND '), values],
- :include => [:snapshot, {:snapshot => :project}, :rule],
+ :include => [:snapshot, {:snapshot => :project}, :rule, :review],
:order => 'rule_failures.failure_level DESC',
:limit => limit)
rest_render(violations)
module MarkdownHelper
def markdown_to_html(input)
- input ? Java::OrgSonarServerUi::JRubyFacade.markdownToHtml(h(input)) : ''
+ input ? Java::OrgSonarServerUi::JRubyFacade.markdownToHtml(ERB::Util.html_escape(input)) : ''
end
end
\ No newline at end of file
rule_failure ? rule_failure.rule : nil
end
end
+
+ def self.search(options={})
+ conditions=['review_type<>:not_type']
+ values={:not_type => Review::TYPE_FALSE_POSITIVE}
+
+ ids=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
+
+ statuses=options['statuses']
+ if statuses && statuses.size>0 && !statuses[0].blank?
+ conditions << "status in (:statuses)"
+ values[:statuses]=statuses
+ end
+
+ severities=options['severities']
+ if severities && severities.size>0 && !severities[0].blank?
+ conditions << "severity in (:severities)"
+ values[:severities]=severities
+ end
+
+ authors=options['authors']
+ if authors && authors.size>0 && !authors[0].blank?
+ conditions << "user_id in (:authors)"
+ values[:authors]=User.logins_to_ids(authors)
+ end
+
+ assignees=options['assignees']
+ if assignees && assignees.size>0 && !assignees[0].blank?
+ conditions << "assignee_id in (:assignees)"
+ values[:assignees]=User.logins_to_ids(assignees)
+ end
+
+ Review.find(:all, :order => "created_at DESC", :conditions => [conditions.join(" AND "), values], :limit => 200)
+ end
private
self.project=self.resource.project
end
end
-
- def to_hash_json ( extended )
- json = {}
- json['id'] = id
- json['updatedAt'] = updated_at
- json['author'] = user.login
- json['assignee'] = assignee.login if assignee
- json['title'] = title
- json['type'] = review_type
- json['status'] = status
- json['severity'] = severity
- comments = []
- review_comments.each do |comment|
- comments << {
- 'author' => comment.user.login,
- 'updatedAt' => comment.updated_at,
- 'comment' => comment.review_text
- }
- end
- json['comments'] = comments
- if ( extended )
- json['line'] = resource_line if resource_line
- json['resource'] = resource.kee if resource
- end
- json
- end
end
json['message'] = message
json['line'] = line if line
json['priority'] = Sonar::RulePriority.to_s(failure_level).upcase
- json['switchedOff'] = switched_off ? true : false
+ json['falsePositive']=true if false_positive?
if created_at
json['createdAt'] = format_datetime(created_at)
end
:qualifier => snapshot.project.qualifier,
:language => snapshot.project.language
}
- json['review'] = review.to_hash_json(false) if review
+ json['review']=review.id if review
json
end
xml.message(message)
xml.line(line) if line
xml.priority(Sonar::RulePriority.to_s(failure_level))
- xml.switchedOff(switched_off ? true : false)
+ xml.falsePositive(true) if false_positive?
if created_at
xml.createdAt(format_datetime(created_at))
end
xml.qualifier(snapshot.project.qualifier)
xml.language(snapshot.project.language)
end
+ xml.review(review.id) if review
end
end
properties.delete(prop)
end
end
+
+ def self.logins_to_ids(logins=[])
+ if logins.size>0
+ User.find(:all, :select => 'id', :conditions => ['login in (?)', logins]).map{|user| user.id}
+ else
+ []
+ end
+ end
#---------------------------------------------------------------------
# FAVOURITES
private String resourceScope = null;
private String resourceQualifier = null;
private Date createdAt = null;
- private boolean switchedOff;
+ private boolean falsePositive;
+ private Long reviewId = null;
public String getMessage() {
return message;
/**
* @since 2.8
*/
- public Violation setSwitchedOff(boolean switchedOff) {
- this.switchedOff = switchedOff;
+ public Violation setFalsePositive(Boolean b) {
+ this.falsePositive = (b != null && b);
return this;
}
/**
* @since 2.8
*/
- public boolean isSwitchedOff() {
- return switchedOff;
+ public boolean isFalsePositive() {
+ return falsePositive;
}
+ public Long getReviewId() {
+ return reviewId;
+ }
+
+ public Violation setReviewId(Long l) {
+ this.reviewId = l;
+ return this;
+ }
}
violation.setLine(utils.getInteger(json, "line"));
violation.setSeverity(utils.getString(json, "priority"));
violation.setCreatedAt(utils.getDateTime(json, "createdAt"));
- violation.setSwitchedOff(utils.getBoolean(json, "switchedOff"));
+ violation.setFalsePositive(utils.getBoolean(json, "falsePositive"));
+ violation.setReviewId(utils.getLong(json, "review"));
Object rule = utils.getField(json, "rule");
if (rule != null) {
*/
package org.sonar.wsclient.unmarshallers;
+import org.junit.Test;
+import org.sonar.wsclient.services.Violation;
+
+import java.util.List;
+
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
-import java.util.List;
-
-import org.junit.Test;
-import org.sonar.wsclient.services.Violation;
-
public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
@Test
- public void toModels() {
+ public void testToModels() {
Violation violation = new ViolationUnmarshaller().toModel("[]");
assertThat(violation, nullValue());
assertThat(violation.getResourceName(), is("TraceableResourceLimitingPool"));
assertThat(violation.getResourceQualifier(), is("CLA"));
assertThat(violation.getResourceScope(), is("FIL"));
- assertThat(violation.isSwitchedOff(), is(true));
+ assertThat(violation.isFalsePositive(), is(false));
+ assertThat(violation.getReviewId(), nullValue());
}
@Test
- public void violationWithoutLineNumber() {
+ public void testViolationWithoutLineNumber() {
Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/violation-without-optional-fields.json"));
assertThat(violation.getMessage(), not(nullValue()));
assertThat(violation.getLine(), nullValue());
assertThat(violation.getCreatedAt(), nullValue());
}
+ @Test
+ public void testFalsePositive() {
+ Violation violation = new ViolationUnmarshaller().toModel(loadFile("/violations/false-positive.json"));
+ assertThat(violation.isFalsePositive(), is(true));
+ assertThat(violation.getReviewId(), is(123L));
+ }
}
--- /dev/null
+[
+ {"message":"throw java.lang.Exception","falsePositive": 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"}}
+]
\ No newline at end of file
[
- {"message":"throw java.lang.Exception","priority":"MAJOR","switchedOff":true,"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","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
[
- {"message":"throw java.lang.Exception","line":97,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","switchedOff":true,"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":"The user-supplied array 'threads' is stored directly.","line":242,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","switchedOff":false,"rule":{"key":"pmd:ArrayIsStoredDirectly","name":"Security - Array is stored directly","category":"Reliability"},"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","line":97,"createdAt":"2010-12-01T13:55:22+0300","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":"The user-supplied array 'threads' is stored directly.","line":242,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","rule":{"key":"pmd:ArrayIsStoredDirectly","name":"Security - Array is stored directly","category":"Reliability"},"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