From 9d66cd90629de1ccfc144636f66a96efbdfd53d8 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Mon, 30 May 2011 12:28:47 +0200 Subject: [PATCH] SONAR-2404 Extend the Review web service API to create & update - Server side: delete last comment of a review --- .../app/controllers/api/reviews_controller.rb | 49 ++++++++++++++++++- .../main/webapp/WEB-INF/app/models/review.rb | 1 + 2 files changed, 49 insertions(+), 1 deletion(-) 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 acef45b743a..4d485008464 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 @@ -25,6 +25,7 @@ class Api::ReviewsController < Api::ApiController # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :put, :only => [ :update ] verify :method => :post, :only => [ :create ] + verify :method => :delete, :only => [ :delete ] def index convert_markdown=(params[:output]=='HTML') @@ -38,8 +39,8 @@ class Api::ReviewsController < Api::ApiController # # POST /api/reviews # Required parameters: - # - 'text' : the text of the comment # - 'violation_id' : the violation on which the review shoul be created + # - 'text' : the text of the comment # # Optional parameters : # - 'assignee' : login used to create a review directly assigned @@ -133,6 +134,7 @@ class Api::ReviewsController < Api::ApiController # 3- Modify the review if !false_positive.blank? raise "'false_positive' parameter must be either 'true' or 'false'." unless false_positive=='true' || false_positive=='false' + raise "Review 'false_positive' status is already " + false_positive if review.false_positive == false_positive raise "'new_text' parameter is mandatory with 'false_positive'." if new_text.blank? review.set_false_positive(false_positive=='true', :user => current_user, :text => new_text) elsif !assignee.blank? @@ -165,6 +167,51 @@ class Api::ReviewsController < Api::ApiController end + # + # --- Delete the last comment of a review --- + # + # DELETE /api/reviews + # Required parameters: + # - 'id' : the review id + # - 'comment_id' : for the moment, only 'last_comment' value is accepted + # + # Example : + # - DELETE "/api/reviews/update?id=1&comment=last_comment + # + def delete + begin + # 1- Get some parameters + convert_markdown=(params[:output]=='HTML') + comment_id = params[:comment_id] + raise "'comment' parameter is missing." unless comment_id + raise "Currently, only 'last_comment' is accepted for the 'comment' parameter." unless comment_id == 'last_comment' + + # 2- Get the review or create one + raise "No 'id' parameter has been provided." unless params[:id] + review = Review.find(params[:id], :include => ['project', 'review_comments']) + unless has_rights_to_modify?(review.project) + access_denied + return + end + + # 3- Delete the last comment if possible + raise "Cannot delete the only existing comment of this review." if review.comments.size == 1 + last_comment = review.comments.last + raise "You do not have the rights to edit this comment as it is not yours." unless last_comment.user == current_user + review.delete_comment(last_comment.id) + + # 4- And finally send back the review + render_reviews([review], convert_markdown) + + rescue ApiException => e + render_error(e.msg, e.code) + + rescue Exception => e + render_error(e.message, 400) + end + end + + private def render_reviews(reviews, convert_markdown) 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 8b0a46f746f..fe2d2073f4d 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 @@ -79,6 +79,7 @@ class Review < ActiveRecord::Base def delete_comment(comment_id) comment=comments.find(comment_id) + comments.pop if comment comment.delete touch -- 2.39.5