Commit 4a535e17c93dbe0ed8c309633064db6bd27e4061

Authored by Dmitriy Zaporozhets
2 parents 2d2ffc6f 81ee6938

Merge pull request #1630 from riyad/show-commit-notes-with-mr-notes

Mix commit notes with merge request notes on MR show page
app/assets/javascripts/notes.js
... ... @@ -230,7 +230,7 @@ var NoteList = {
230 230 updateVotes:
231 231 function() {
232 232 var votes = $("#votes .votes");
233   - var notes = $("#notes-list, #new-notes-list").find(".note.vote");
  233 + var notes = $("#notes-list, #new-notes-list").find(".note .vote");
234 234  
235 235 // only update if there is a vote display
236 236 if (votes.size()) {
... ...
app/contexts/notes/load_context.rb
... ... @@ -13,7 +13,7 @@ module Notes
13 13 when "issue"
14 14 project.issues.find(target_id).notes.inc_author.fresh.limit(20)
15 15 when "merge_request"
16   - project.merge_requests.find(target_id).notes.inc_author.fresh.limit(20)
  16 + project.merge_requests.find(target_id).mr_and_commit_notes.inc_author.fresh.limit(20)
17 17 when "snippet"
18 18 project.snippets.find(target_id).notes.fresh
19 19 when "wall"
... ...
app/controllers/notes_controller.rb
... ... @@ -7,6 +7,11 @@ class NotesController < ProjectResourceController
7 7  
8 8 def index
9 9 notes
  10 + if params[:target_type] == "merge_request"
  11 + @mixed_targets = true
  12 + @main_target_type = params[:target_type].camelize
  13 + end
  14 +
10 15 respond_with(@notes)
11 16 end
12 17  
... ...
app/helpers/notes_helper.rb
... ... @@ -7,11 +7,20 @@ module NotesHelper
7 7 params[:loading_new].present?
8 8 end
9 9  
10   - def note_vote_class(note)
11   - if note.upvote?
12   - "vote upvote"
13   - elsif note.downvote?
14   - "vote downvote"
15   - end
  10 + # Helps to distinguish e.g. commit notes in mr notes list
  11 + def note_for_main_target?(note)
  12 + !@mixed_targets || @main_target_type == note.noteable_type
  13 + end
  14 +
  15 + def link_to_commit_diff_line_note(note)
  16 + return unless note.line_note?
  17 +
  18 + commit = note.target
  19 + diff_index, diff_old_line, diff_new_line = note.line_code.split('_')
  20 +
  21 + link_file = commit.diffs[diff_index.to_i].new_path
  22 + link_line = diff_new_line
  23 +
  24 + link_to "#{link_file}:L#{link_line}", project_commit_path(@project, commit, anchor: note.line_code)
16 25 end
17 26 end
... ...
app/models/note.rb
... ... @@ -49,7 +49,7 @@ class Note < ActiveRecord::Base
49 49 end
50 50  
51 51 def target
52   - if noteable_type == "Commit"
  52 + if commit?
53 53 project.commit(noteable_id)
54 54 else
55 55 noteable
... ... @@ -82,6 +82,10 @@ class Note < ActiveRecord::Base
82 82 noteable_type == "Commit"
83 83 end
84 84  
  85 + def line_note?
  86 + line_code.present?
  87 + end
  88 +
85 89 def commit_author
86 90 @commit_author ||=
87 91 project.users.find_by_email(target.author_email) ||
... ...
app/views/commits/_text_file.html.haml
... ... @@ -4,7 +4,7 @@
4 4  
5 5 %table{class: "#{'hide' if too_big}"}
6 6 - each_diff_line(diff.diff.lines.to_a, index) do |line, type, line_code, line_new, line_old|
7   - %tr.line_holder
  7 + %tr.line_holder{ id: line_code }
8 8 - if type == "match"
9 9 %td.old_line= "..."
10 10 %td.new_line= "..."
... ...
app/views/notes/_note.html.haml
1   -%li{id: dom_id(note), class: "note #{note_vote_class(note)}"}
  1 +%li{id: dom_id(note), class: "note"}
2 2 = image_tag gravatar_icon(note.author.email), class: "avatar s32"
3 3 %div.note-author
4 4 %strong= note.author_name
... ... @@ -6,14 +6,25 @@
6 6 %cite.cgray
7 7 = time_ago_in_words(note.updated_at)
8 8 ago
9   - - if note.upvote?
10   - %span.label.label-success
11   - %i.icon-thumbs-up
12   - \+1
13   - - if note.downvote?
14   - %span.label.label-error
15   - %i.icon-thumbs-down
16   - \-1
  9 +
  10 + - unless note_for_main_target?(note)
  11 + - if note.commit?
  12 + %span.cgray
  13 + on #{link_to note.target.short_id, project_commit_path(@project, note.target)}
  14 + = link_to_commit_diff_line_note(note) if note.line_note?
  15 +
  16 + -# only show vote if it's a note for the main target
  17 + - if note_for_main_target?(note)
  18 + - if note.upvote?
  19 + %span.vote.upvote.label.label-success
  20 + %i.icon-thumbs-up
  21 + \+1
  22 + - if note.downvote?
  23 + %span.vote.downvote.label.label-error
  24 + %i.icon-thumbs-down
  25 + \-1
  26 +
  27 + -# remove button
17 28 - if(note.author_id == current_user.id) || can?(current_user, :admin_note, @project)
18 29 = link_to [@project, note], confirm: 'Are you sure?', method: :delete, remote: true, class: "cred delete-note btn very_small" do
19 30 %i.icon-trash
... ...