From 3416fc08bf2efbb28d377b0c6c2f371823370a39 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 27 Apr 2011 08:50:14 +0200 Subject: [PATCH] SONAR-2382 Display source in review detail --- .../app/controllers/reviews_controller.rb | 2 +- .../WEB-INF/app/helpers/source_helper.rb | 169 ++++++++++++++++++ .../WEB-INF/app/views/source/_source.html.erb | 80 +++++++++ 3 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/source/_source.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 03472fde201..894a6a48129 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 @@ -26,7 +26,7 @@ class ReviewsController < ApplicationController :only => [:assign, :comment_form, :flag_as_false_positive, :violation_assign, :violation_flag_as_false_positive,:violation_save_comment, :violation_delete_comment], :redirect_to => {:action => :error_not_post} - helper(:reviews,:markdown) + helper ReviewsHelper, MarkdownHelper, SourceHelper def index init_params() diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb new file mode 100644 index 00000000000..c6ed7856961 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb @@ -0,0 +1,169 @@ + # + # 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 SourceHelper + + # + # Options : + # - display_scm : boolean (default is true) + # - display_violations: boolean (default is false) + # - display_coverage: boolean (default is false) + # - expand: boolean (default is false). Only if display_violations or display_coverage is true + # - min_date + # - line_range + # + def snapshot_source_to_html(snapshot, options={}) + return '' unless snapshot && snapshot.source + + panel=SourcePanel.new + revisions_by_line={} + authors_by_line={} + dates_by_line={} + if options[:display_scm]||nil + panel.display_scm=(snapshot.measure('last_commit_datetimes_by_line')!=nil) + authors_by_line=load_distribution(snapshot,'authors_by_line') + revisions_by_line=load_distribution(snapshot,'revisions_by_line') + dates_by_line=load_distribution(snapshot,'last_commit_datetimes_by_line') + end + + panel.html_lines=[] + line_range=sanitize_line_range(options[:line_range]) + snapshot.source.syntax_highlighted_lines()[line_range].each_with_index do |source, index| + html_line=HtmlLine.new(source) + html_line.revision=revisions_by_line[index+1] + html_line.author=authors_by_line[index+1] + date_string=dates_by_line[index+1] + html_line.datetime=(date_string ? Java::OrgSonarApiUtils::DateUtils.parseDateTime(date_string): nil) + panel.html_lines< 'source/source', :locals => {:panel => panel} + end + + def load_distribution(snapshot, metric_key) + m=snapshot.measure(metric_key) + m ? m.data_as_line_distribution() : {} + end + + def sanitize_line_range(range) + if range + ([range.min, 1].max)..(range.max) + else + [0..-1] + end + end + + class SourcePanel + attr_accessor :filtered, :expanded, :html_lines, :display_scm + + def empty? + @html_lines.nil? || @html_lines.empty? + end + + def filter_min_date(min_date) + @filtered=true + to=Java::JavaUtil::Date.new(min_date.to_f * 1000) + if to + @html_lines.each do |line| + line.flag_as_hidden() if !line.after(to) + end + end + end + end + + class HtmlLine + attr_accessor :source, :revision, :author, :datetime, :violations, :hits, :conditions, :covered_conditions, :hidden, :highlighted, :deprecated_conditions_label + + def initialize(source) + @source=source + end + + def add_violation(violation) + @violations||=[] + @violations<0 + end + + def violation_severity + if @violations && @violations.size>0 + @violations[0].failure_level + else + nil + end + end + + def after(date) + if date && @datetime + @datetime.after(date) + else + true + end + end + + def flag_as_highlighted + @highlighted=true + @hidden=false + end + + def flag_as_highlight_context + # do not force if highlighted has already been set to true + @highlighted=false if @highlighted.nil? + @hidden=false + end + + def flag_as_hidden + # do not force if it has already been flagged as visible + if @hidden.nil? + @hidden=true + @highlighted=false + end + end + + def hidden? + @hidden==true + end + + def highlighted? + # highlighted if the @highlighted has not been set or has been set to true + !hidden? && @highlighted!=false + end + + def deprecated_conditions_label=(label) + if label + @deprecated_conditions_label=label + if label=='0%' + @conditions=2 + @covered_conditions=0 + elsif label=='100%' + @conditions=2 + @covered_conditions=2 + else + @conditions=2 + @covered_conditions=1 + end + end + end + end +end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/source/_source.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/source/_source.html.erb new file mode 100644 index 00000000000..fe339ed35d5 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/source/_source.html.erb @@ -0,0 +1,80 @@ + +<% if !panel.empty? %> + + <% + current_revision=nil + colspan=2 + colspan+=1 if panel.display_scm +# colspan+=2 if @display_coverage + previous_hidden=false + first_section=true + has_displayed_lines=false + panel.html_lines.each_with_index do |line, index| + if line.hidden? && panel.expanded + previous_hidden=true + next + end + + if previous_hidden && !first_section + current_revision=nil +%> + + + +<% + end + previous_hidden=false + first_section=false + + status=hits_status=conditions_status='' + if line.highlighted? + has_displayed_lines=true +# if @display_coverage && line.hits +# hits_status=(line.hits>0 ? 'ok' : 'ko') +# if line.conditions && line.conditions>0 && line.covered_conditions +# if line.covered_conditions==0 +# status='ko' +# conditions_status='ko' +# elsif line.covered_conditions==line.conditions +# status='' +# conditions_status='ok' +# else +# conditions_status='warn' +# status='warn' +# end +# elsif line.hits +# status=(line.hits>0 ? '' : 'ko') +# end +# elsif @display_violations && line.violations? +# status="ko" +# end + end + %> + + <% + if panel.display_scm + if current_revision!=line.revision + current_revision=line.revision + title = "Revision #{h(line.revision)}" + %> + + <% else %> + + <% end + end + %> + + + + + + + <% end %> +
<%= Java::OrgSonarApiUtils::DateUtils.formatDate(line.datetime) if line.datetime -%> <%= h(line.author) -%><%= index + 1 -%> +
<%= line.source -%>
+
+ + <% if panel.filtered && !has_displayed_lines %> +

No lines match your filter criteria.

+ <% end %> +<% end %> \ No newline at end of file -- 2.39.5