def define_show_vars
# Build a note object for comment form
@note = @project.notes.new(noteable: @merge_request)
+ @notes = @merge_request.mr_and_commit_notes.inc_author.fresh
+ @discussions = Note.discussions_from_notes(@notes)
+ @target_type = 'MergeRequest'
+ @target_id = @merge_request.id
# Get commits from repository
# or from cache if already merged
@allowed_to_merge = allowed_to_merge?
@show_merge_controls = @merge_request.opened? && @commits.any? && @allowed_to_merge
-
- @target_type = :merge_request
- @target_id = @merge_request.id
end
def allowed_to_merge?
@target_id = params[:target_id]
if params[:target_type] == "merge_request"
- @discussions = discussions_from_notes
+ @discussions = Note.discussions_from_notes(@notes)
end
respond_to do |format|
def preview
render text: view_context.markdown(params[:note])
end
-
- protected
-
- def discussion_notes_for(note)
- @notes.select do |other_note|
- note.discussion_id == other_note.discussion_id
- end
- end
-
- def discussions_from_notes
- discussion_ids = []
- discussions = []
-
- @notes.each do |note|
- next if discussion_ids.include?(note.discussion_id)
-
- # don't group notes for the main target
- if note_for_main_target?(note)
- discussions << [note]
- else
- discussions << discussion_notes_for(note)
- discussion_ids << note.discussion_id
- end
- end
-
- discussions
- end
-
- # Helps to distinguish e.g. commit notes in mr notes list
- def note_for_main_target?(note)
- (@target_type.camelize == note.noteable_type && !note.for_diff_line?)
- end
end
serialize :st_diff
before_create :set_diff, if: ->(n) { n.line_code.present? }
- def self.create_status_change_note(noteable, project, author, status, source)
- body = "_Status changed to #{status}#{' by ' + source.gfm_reference if source}_"
-
- create({
- noteable: noteable,
- project: project,
- author: author,
- note: body,
- system: true
- }, without_protection: true)
- end
+ class << self
+ def create_status_change_note(noteable, project, author, status, source)
+ body = "_Status changed to #{status}#{' by ' + source.gfm_reference if source}_"
+
+ create({
+ noteable: noteable,
+ project: project,
+ author: author,
+ note: body,
+ system: true
+ }, without_protection: true)
+ end
+
+ # +noteable+ was referenced from +mentioner+, by including GFM in either +mentioner+'s description or an associated Note.
+ # Create a system Note associated with +noteable+ with a GFM back-reference to +mentioner+.
+ def create_cross_reference_note(noteable, mentioner, author, project)
+ create({
+ noteable: noteable,
+ commit_id: (noteable.sha if noteable.respond_to? :sha),
+ project: project,
+ author: author,
+ note: "_mentioned in #{mentioner.gfm_reference}_",
+ system: true
+ }, without_protection: true)
+ end
- # +noteable+ was referenced from +mentioner+, by including GFM in either +mentioner+'s description or an associated Note.
- # Create a system Note associated with +noteable+ with a GFM back-reference to +mentioner+.
- def self.create_cross_reference_note(noteable, mentioner, author, project)
- create({
- noteable: noteable,
- commit_id: (noteable.sha if noteable.respond_to? :sha),
- project: project,
- author: author,
- note: "_mentioned in #{mentioner.gfm_reference}_",
- system: true
- }, without_protection: true)
+ def discussions_from_notes(notes)
+ discussion_ids = []
+ discussions = []
+
+ notes.each do |note|
+ next if discussion_ids.include?(note.discussion_id)
+
+ # don't group notes for the main target
+ if !note.for_diff_line? && note.noteable_type == "MergeRequest"
+ discussions << [note]
+ else
+ discussions << notes.select do |other_note|
+ note.discussion_id == other_note.discussion_id
+ end
+ discussion_ids << note.discussion_id
+ end
+ end
+
+ discussions
+ end
end
# Determine whether or not a cross-reference note already exists.
def commit_author
@commit_author ||=
project.users.find_by_email(noteable.author_email) ||
- project.users.find_by_name(noteable.author_name)
+ project.users.find_by_name(noteable.author_name)
rescue
nil
end