]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2382 Display source in review detail
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 27 Apr 2011 06:50:14 +0000 (08:50 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 27 Apr 2011 06:50:14 +0000 (08:50 +0200)
sonar-server/src/main/webapp/WEB-INF/app/controllers/reviews_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/views/source/_source.html.erb [new file with mode: 0644]

index 03472fde20166e687ad8bcb78848ed69717d46a3..894a6a481297b83a9e36633659daaa82fb9da5bd 100644 (file)
@@ -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 (file)
index 0000000..c6ed785
--- /dev/null
@@ -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<<html_line
+    end
+
+    panel.filter_min_date(options[:min_date]) if options[:min_date]
+    
+    render :partial => '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<<violation
+      @visible=true
+    end
+
+    def violations?
+      @violations && @violations.size>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 (file)
index 0000000..fe339ed
--- /dev/null
@@ -0,0 +1,80 @@
+
+<% if !panel.empty? %>
+<table id="sources" class="sources2 code" cellpadding="0" cellspacing="0" border="0">
+  <%
+    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
+%>
+  <tr>
+    <td colspan="<%= colspan -%>" class="new_section"> </td>
+  </tr>
+<%
+      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
+  %>
+  <tr>
+    <%
+      if panel.display_scm
+         if current_revision!=line.revision
+           current_revision=line.revision
+           title = "Revision #{h(line.revision)}"
+    %>
+            <td class="scm revision"><span class="date"><a href="#" title="<%= title -%>" alt="<%= title -%>"><%= Java::OrgSonarApiUtils::DateUtils.formatDate(line.datetime) if line.datetime -%></a></span> <span class="author"><%= h(line.author) -%></span></td>
+    <%   else %>
+            <td class="scm"></td>
+    <%   end
+       end
+    %>
+    <td class="lid <%= ' section' if line.violations? -%>" id="L<%= index+1 -%>"><a name="L<%= index+1 -%>" href="#L<%= index+1 -%>"><%= index + 1 -%></a></td>
+
+
+    <td class="line <%= status -%>">
+      <pre><%= line.source -%></pre>
+    </td>
+  </tr>
+
+  <% end %>
+</table>
+
+  <% if panel.filtered && !has_displayed_lines %>
+    <p>No lines match your filter criteria.</p>
+  <% end %>
+<% end %>
\ No newline at end of file