From f928dba99b0550cefa7534d7fd5bd1ea16609274 Mon Sep 17 00:00:00 2001 From: hhoopes Date: Thu, 25 Aug 2016 10:38:07 -0600 Subject: Change diff highlight/truncate for reusability Previously the `truncated_diff_lines` method for outputting a discussion diff took in already highlighted lines, which meant it wasn't reuseable for truncating ANY lines. In the way it was used, it also meant that for any email truncation, the whole diff was being highlighted before being truncated, meaning wasted time highlighting lines that wouldn't even be used (granted, they were being memoized, so perhaps this wasn't that great of an issue). I refactored truncation away from highlighting, in order to truncate formatted diffs for text templates in email, using `>`s to designate each line, but otherwise retaining the parsing already done to create `diff_lines`. Additionally, while notes on merge requests or commits had already been tested, there was no existing test for notes on a diff on an MR or commit. Added mailer tests for such, and a unit test for truncating diff lines. --- app/models/discussion.rb | 12 +++++++----- app/views/discussions/_diff_with_notes.html.haml | 2 +- app/views/notify/_note_mr_or_commit_email.html.haml | 9 +++++---- app/views/notify/_simple_diff.text.erb | 4 ++++ app/views/notify/note_commit_email.html.haml | 6 ++---- app/views/notify/note_commit_email.text.erb | 2 ++ app/views/notify/note_merge_request_email.html.haml | 6 ++---- app/views/notify/note_merge_request_email.text.erb | 2 ++ 8 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 app/views/notify/_simple_diff.text.erb (limited to 'app') diff --git a/app/models/discussion.rb b/app/models/discussion.rb index de06c13481a..486bfd2c766 100644 --- a/app/models/discussion.rb +++ b/app/models/discussion.rb @@ -25,7 +25,12 @@ class Discussion to: :last_resolved_note, allow_nil: true - delegate :blob, :highlighted_diff_lines, to: :diff_file, allow_nil: true + delegate :blob, + :highlighted_diff_lines, + :text_parsed_diff_lines, + + to: :diff_file, + allow_nil: true def self.for_notes(notes) notes.group_by(&:discussion_id).values.map { |notes| new(notes) } @@ -162,18 +167,15 @@ class Discussion def truncated_diff_lines prev_lines = [] - highlighted_diff_lines.each do |line| + diff_file.diff_lines.each do |line| if line.meta? prev_lines.clear else prev_lines << line - break if for_line?(line) - prev_lines.shift if prev_lines.length >= NUMBER_OF_TRUNCATED_DIFF_LINES end end - prev_lines end diff --git a/app/views/discussions/_diff_with_notes.html.haml b/app/views/discussions/_diff_with_notes.html.haml index 3a95a652810..06493ba0105 100644 --- a/app/views/discussions/_diff_with_notes.html.haml +++ b/app/views/discussions/_diff_with_notes.html.haml @@ -9,7 +9,7 @@ %table - discussions = { discussion.original_line_code => discussion } = render partial: "projects/diffs/line", - collection: discussion.truncated_diff_lines, + collection: discussion.highlighted_diff_lines(discussion.truncated_diff_lines), as: :line, locals: { diff_file: diff_file, discussions: discussions, diff --git a/app/views/notify/_note_mr_or_commit_email.html.haml b/app/views/notify/_note_mr_or_commit_email.html.haml index 3e2046aa9cf..7033842b557 100644 --- a/app/views/notify/_note_mr_or_commit_email.html.haml +++ b/app/views/notify/_note_mr_or_commit_email.html.haml @@ -1,15 +1,16 @@ = content_for :head do = stylesheet_link_tag 'mailers/highlighted_diff_email' -- if note.diff_note? && note.diff_file - = link_to note.diff_file.file_path, @target_url, class: 'details' +- if @note.diff_note? && @note.diff_file + on + = link_to @note.diff_file.file_path, @target_url, class: 'details' \: %table = render partial: "projects/diffs/line", - collection: discussion.truncated_diff_lines, + collection: @discussion.highlighted_diff_lines(@discussion.truncated_diff_lines), as: :line, - locals: { diff_file: note.diff_file, + locals: { diff_file: @note.diff_file, plain: true, email: true } diff --git a/app/views/notify/_simple_diff.text.erb b/app/views/notify/_simple_diff.text.erb new file mode 100644 index 00000000000..a5a796bc168 --- /dev/null +++ b/app/views/notify/_simple_diff.text.erb @@ -0,0 +1,4 @@ +<% lines = @discussion.truncated_diff_lines %> +<% @discussion.text_parsed_diff_lines(lines).each do |line| %> + <%= line %> +<% end %> diff --git a/app/views/notify/note_commit_email.html.haml b/app/views/notify/note_commit_email.html.haml index f4293a3aa50..17dcf36689f 100644 --- a/app/views/notify/note_commit_email.html.haml +++ b/app/views/notify/note_commit_email.html.haml @@ -1,7 +1,5 @@ %p.details New comment for Commit = @commit.short_id - on - = render partial: 'note_mr_or_commit_email', - locals: { note: @note, - discussion: @discussion} + + = render 'note_mr_or_commit_email' diff --git a/app/views/notify/note_commit_email.text.erb b/app/views/notify/note_commit_email.text.erb index a1ef9858021..715e58af61c 100644 --- a/app/views/notify/note_commit_email.text.erb +++ b/app/views/notify/note_commit_email.text.erb @@ -2,6 +2,8 @@ New comment for Commit <%= @commit.short_id %> <%= url_for(namespace_project_commit_url(@note.project.namespace, @note.project, id: @commit.id, anchor: "note_#{@note.id}")) %> +<%= render 'simple_diff' if @discussion %> + <% if current_application_settings.email_author_in_body %> <%= @note.author_name %> wrote: <% end %> diff --git a/app/views/notify/note_merge_request_email.html.haml b/app/views/notify/note_merge_request_email.html.haml index 75250b0383d..b7758f191dc 100644 --- a/app/views/notify/note_merge_request_email.html.haml +++ b/app/views/notify/note_merge_request_email.html.haml @@ -1,7 +1,5 @@ %p.details New comment for Merge Request = @merge_request.to_reference - on - = render partial: 'note_mr_or_commit_email', - locals: { note: @note, - discussion: @discussion} + + = render 'note_mr_or_commit_email' diff --git a/app/views/notify/note_merge_request_email.text.erb b/app/views/notify/note_merge_request_email.text.erb index 42d29b34ebc..d24e15af91f 100644 --- a/app/views/notify/note_merge_request_email.text.erb +++ b/app/views/notify/note_merge_request_email.text.erb @@ -2,6 +2,8 @@ New comment for Merge Request <%= @merge_request.to_reference %> <%= url_for(namespace_project_merge_request_url(@merge_request.target_project.namespace, @merge_request.target_project, @merge_request, anchor: "note_#{@note.id}")) %> +<%= render 'simple_diff' if @discussion %> + <% if current_application_settings.email_author_in_body %> <%= @note.author_name %> wrote: <% end %> -- cgit v1.2.1