Commit a58385247d06b238296e35c17c9e3f58b3234094

Authored by Riyad Preukschas
1 parent 9b919939

Add discussions for merge requests to notes controller

Showing 1 changed file with 35 additions and 4 deletions   Show diff stats
app/controllers/notes_controller.rb
@@ -6,10 +6,15 @@ class NotesController < ProjectResourceController @@ -6,10 +6,15 @@ class NotesController < ProjectResourceController
6 respond_to :js 6 respond_to :js
7 7
8 def index 8 def index
9 - notes 9 + @notes = Notes::LoadContext.new(project, current_user, params).execute
  10 +
10 if params[:target_type] == "merge_request" 11 if params[:target_type] == "merge_request"
11 - @mixed_targets = true 12 + @mixed_targets = true
12 @main_target_type = params[:target_type].camelize 13 @main_target_type = params[:target_type].camelize
  14 + @discussions = discussions_from_notes
  15 + @has_diff = true
  16 + elsif params[:target_type] == "commit"
  17 + @has_diff = true
13 end 18 end
14 19
15 respond_with(@notes) 20 respond_with(@notes)
@@ -40,7 +45,33 @@ class NotesController < ProjectResourceController @@ -40,7 +45,33 @@ class NotesController < ProjectResourceController
40 45
41 protected 46 protected
42 47
43 - def notes  
44 - @notes = Notes::LoadContext.new(project, current_user, params).execute 48 + def discussion_notes_for(note)
  49 + @notes.select do |other_note|
  50 + note.discussion_id == other_note.discussion_id
  51 + end
  52 + end
  53 +
  54 + def discussions_from_notes
  55 + discussion_ids = []
  56 + discussions = []
  57 +
  58 + @notes.each do |note|
  59 + next if discussion_ids.include?(note.discussion_id)
  60 +
  61 + # don't group notes for the main target
  62 + if for_main_target?(note)
  63 + discussions << [note]
  64 + else
  65 + discussions << discussion_notes_for(note)
  66 + discussion_ids << note.discussion_id
  67 + end
  68 + end
  69 +
  70 + discussions
  71 + end
  72 +
  73 + # Helps to distinguish e.g. commit notes in mr notes list
  74 + def for_main_target?(note)
  75 + !@mixed_targets || (@main_target_type == note.noteable_type && !note.for_diff_line?)
45 end 76 end
46 end 77 end