From 0c84e708498524c37ba00e941a1d63e04a3a2607 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Fri, 8 Apr 2011 18:12:07 +0200 Subject: [PATCH] [SONAR-2327] First implementation of the review service --- .../app/controllers/reviews_controller.rb | 188 ++++++++++-------- .../WEB-INF/app/helpers/reviews_helper.rb | 40 ++++ .../main/webapp/WEB-INF/app/models/review.rb | 7 + .../app/views/layouts/_layout.html.erb | 1 + .../app/views/resource/_violation.html.erb | 2 +- .../app/views/reviews/_form_comment.html.erb | 2 +- .../{_index.html.erb => _list.html.erb} | 0 .../reviews/create_comment_result.js.rjs | 2 +- .../app/views/reviews/create_result.js.rjs | 2 +- .../WEB-INF/app/views/reviews/index.html.erb | 45 +++++ 10 files changed, 206 insertions(+), 83 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/helpers/reviews_helper.rb rename sonar-server/src/main/webapp/WEB-INF/app/views/reviews/{_index.html.erb => _list.html.erb} (100%) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/reviews/index.html.erb diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb index 464bfdde0ce..8dea0fb9b63 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb @@ -20,88 +20,118 @@ class ReviewsController < ApplicationController - SECTION=Navigation::SECTION_RESOURCE - - verify :method => :post, :only => [ :create, :create_comment ], :redirect_to => { :action => :error_not_post } - - def index - reviews = findReviewsForRuleFailure params[:rule_failure_id] - render :partial => "index", :locals => { :reviews => reviews } - end - - def form - @review = Review.new - @review.rule_failure_id = params[:violation_id] - @review.user = current_user - @review.severity = Review.default_severity - @review_comment = ReviewComment.new - @review_comment.review_text = "" - render :partial => "form" - end - - def form_comment - @review_comment = ReviewComment.new - @review_comment.user = current_user - @review_comment.review_id = params[:review_id] - @review_comment.review_text = "" - @rule_failure_id = params[:rule_failure_id] - render :partial => "form_comment" - end - - def create - unless hasRightsToCreate? params[:review][:rule_failure_id] - render :text => "Cannot create the review : access denied." - return - end - - @review = Review.new(params[:review]) - @review.user = current_user - @review.status = Review.default_status - @review.review_type = Review.default_type - @review_comment = ReviewComment.new(params[:review_comment]) - @review_comment.user = current_user - @review.review_comments << @review_comment - if @review.valid? - @review.save - @reviews = findReviewsForRuleFailure @review.rule_failure_id - end - render "create_result" - end - - def create_comment - unless hasRightsToCreate? params[:rule_failure_id] - render :text => "Cannot create the comment : access denied." - return - end - + SECTION=Navigation::SECTION_HOME + + verify :method => :post, :only => [ :create, :create_comment ], :redirect_to => { :action => :error_not_post } + + def index + init_params + + @reviews = [] + unless params.blank? + @reviews = Review.find :all, :conditions => ['status=?', @statuses] + end + end + + def list + reviews = findReviewsForRuleFailure params[:rule_failure_id] + render :partial => "index", :locals => { :reviews => reviews } + end + + def form + @review = Review.new + @review.rule_failure_id = params[:violation_id] + @review.user = current_user + @review.severity = Review.default_severity + @review_comment = ReviewComment.new + @review_comment.review_text = "" + render :partial => "form" + end + + def form_comment + @review_comment = ReviewComment.new + @review_comment.user = current_user + @review_comment.review_id = params[:review_id] + @review_comment.review_text = "" + @rule_failure_id = params[:rule_failure_id] + render :partial => "form_comment" + end + + def create + unless hasRightsToCreate? params[:review][:rule_failure_id] + render :text => "Cannot create the review : access denied." + return + end + + @review = Review.new(params[:review]) + @review.user = current_user + @review.status = Review.default_status + @review.review_type = Review.default_type + @review_comment = ReviewComment.new(params[:review_comment]) + @review_comment.user = current_user + @review.review_comments << @review_comment + if @review.valid? + @review.save + @reviews = findReviewsForRuleFailure @review.rule_failure_id + end + render "create_result" + end + + def create_comment + unless hasRightsToCreate? params[:rule_failure_id] + render :text => "Cannot create the comment : access denied." + return + end + @review_comment = ReviewComment.new(params[:review_comment]) @review_comment.user = current_user @rule_failure_id = params[:rule_failure_id] if @review_comment.valid? - @review_comment.save - @reviews = findReviewsForRuleFailure @rule_failure_id - end + @review_comment.save + @reviews = findReviewsForRuleFailure @rule_failure_id + end render "create_comment_result" - end - - private - - def findReviewsForRuleFailure ( rule_failure_id ) - return Review.find :all, :conditions => ['rule_failure_id=?', rule_failure_id] - end - - def hasRightsToCreate? ( rule_failure_id ) - return false unless current_user - - project = RuleFailure.find( rule_failure_id, :include => ['snapshot'] ).snapshot.root_project - unless has_role?(:user, project) - return false - end - return true - end - - def error_not_post - render :text => "Create actions must use POST method." - end - + end + + + ## -------------- PRIVATE -------------- ## + private + + def init_params + users = User.find :all + @user_names = [["Any", ""]] + users.each do |user| + @user_names << [user.name, user.id.to_s] + end + @review_authors = filter_any(params[:review_authors]) || [""] + @comment_authors = filter_any(params[:comment_authors]) || [""] + @severities = filter_any(params[:severities]) || [""] + @statuses = filter_any(params[:statuses]) || ["open"] + end + + def filter_any(array) + if array && array.size>1 && array.include?('') + array=[''] #keep only 'any' + end + array + end + + def findReviewsForRuleFailure ( rule_failure_id ) + return Review.find :all, :conditions => ['rule_failure_id=?', rule_failure_id] + end + + def hasRightsToCreate? ( rule_failure_id ) + return false unless current_user + + project = RuleFailure.find( rule_failure_id, :include => ['snapshot'] ).snapshot.root_project + unless has_role?(:user, project) + return false + end + return true + end + + def error_not_post + render :text => "Create actions must use POST method." + end + end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/reviews_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/reviews_helper.rb new file mode 100644 index 00000000000..c615c99a82f --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/reviews_helper.rb @@ -0,0 +1,40 @@ +# +# Sonar, entreprise quality control tool. +# Copyright (C) 2008-2011 SonarSource +# mailto:contact AT sonarsource DOT com +# +# Sonar is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# Sonar is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with Sonar; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# +module ReviewsHelper + + def self.severity_options_with_any + severity_ops = [] + severity_ops << ["Any", ""] + Review.severity_options.each do |severity| + severity_ops << severity + end + return severity_ops + end + + def self.status_options_with_any + status_ops = [] + status_ops << ["Any", ""] + Review.status_options.each do |status| + status_ops << status + end + return status_ops + 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 2e42efb6f15..6fc2db26717 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 @@ -60,5 +60,12 @@ class Review < ActiveRecord::Base severity_ops << ["Blocker", SEVERITY_BLOCKER] return severity_ops end + + def self.status_options + status_ops = [] + status_ops << ["Open", STATUS_OPEN] + status_ops << ["Closed", STATUS_CLOSED] + return status_ops + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb index fe683319266..e6ab11b5d22 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb @@ -26,6 +26,7 @@