Skip to content

Commit 79214be

Browse files
committed
Add Discussion model to represent MR/diff discussion
1 parent 5a77eb1 commit 79214be

27 files changed

+340
-1096
lines changed

app/assets/javascripts/notes.js.coffee

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class @Notes
162162
@last_fetched_at = data.last_fetched_at
163163
@setPollingInterval(data.notes.length)
164164
$.each notes, (i, note) =>
165-
if note.discussion_with_diff_html?
165+
if note.discussion_html?
166166
@renderDiscussionNote(note)
167167
else
168168
@renderNote(note)
@@ -251,7 +251,7 @@ class @Notes
251251
discussionContainer = $(".notes[data-discussion-id='" + note.original_discussion_id + "']")
252252
if discussionContainer.length is 0
253253
# insert the note and the reply button after the temp row
254-
row.after note.discussion_html
254+
row.after note.diff_discussion_html
255255

256256
# remove the note (will be added again below)
257257
row.next().find(".note").remove()
@@ -265,7 +265,7 @@ class @Notes
265265
# Init discussion on 'Discussion' page if it is merge request page
266266
if $('body').attr('data-page').indexOf('projects:merge_request') is 0
267267
$('ul.main-notes-list')
268-
.append(note.discussion_with_diff_html)
268+
.append(note.discussion_html)
269269
.syntaxHighlight()
270270
else
271271
# append new note to all matching discussions

app/controllers/projects/commit_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ def define_commit_vars
115115
end
116116

117117
def define_note_vars
118-
@grouped_diff_notes = commit.notes.grouped_diff_notes
118+
@grouped_diff_discussions = commit.notes.grouped_diff_discussions
119119
@notes = commit.notes.non_diff_notes.fresh
120120

121121
Banzai::NoteRenderer.render(
122-
@grouped_diff_notes.values.flatten + @notes,
122+
@grouped_diff_discussions.values.flat_map(&:notes) + @notes,
123123
@project,
124124
current_user,
125125
)

app/controllers/projects/compare_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def define_diff_vars
5454
)
5555

5656
@diff_notes_disabled = true
57-
@grouped_diff_notes = {}
57+
@grouped_diff_discussions = {}
5858
end
5959
end
6060

app/controllers/projects/merge_requests_controller.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def diff_for_path
9797
else
9898
build_merge_request
9999
@diff_notes_disabled = true
100-
@grouped_diff_notes = {}
100+
@grouped_diff_discussions = {}
101101
end
102102

103103
define_commit_vars
@@ -378,7 +378,7 @@ def define_discussion_vars
378378

379379
# This is not executed lazily
380380
@notes = Banzai::NoteRenderer.render(
381-
@discussions.flatten,
381+
@discussions.flat_map(&:notes),
382382
@project,
383383
current_user,
384384
@path,
@@ -404,10 +404,10 @@ def define_diff_comment_vars
404404
}
405405

406406
@use_legacy_diff_notes = !@merge_request.support_new_diff_notes?
407-
@grouped_diff_notes = @merge_request.notes.grouped_diff_notes
407+
@grouped_diff_discussions = @merge_request.notes.grouped_diff_discussions
408408

409409
Banzai::NoteRenderer.render(
410-
@grouped_diff_notes.values.flatten,
410+
@grouped_diff_discussions.values.flat_map(&:notes),
411411
@project,
412412
current_user,
413413
@path,

app/controllers/projects/notes_controller.rb

+36-29
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def note
7373
end
7474
alias_method :awardable, :note
7575

76-
def note_to_html(note)
76+
def note_html(note)
7777
render_to_string(
7878
"projects/notes/_note",
7979
layout: false,
@@ -82,20 +82,20 @@ def note_to_html(note)
8282
)
8383
end
8484

85-
def note_to_discussion_html(note)
86-
return unless note.diff_note?
85+
def diff_discussion_html(discussion)
86+
return unless discussion.diff_discussion?
8787

8888
if params[:view] == 'parallel'
89-
template = "projects/notes/_diff_notes_with_reply_parallel"
89+
template = "discussions/_parallel_diff_discussion"
9090
locals =
9191
if params[:line_type] == 'old'
92-
{ notes_left: [note], notes_right: [] }
92+
{ discussion_left: discussion, discussion_right: nil }
9393
else
94-
{ notes_left: [], notes_right: [note] }
94+
{ discussion_left: nil, discussion_right: discussion }
9595
end
9696
else
97-
template = "projects/notes/_diff_notes_with_reply"
98-
locals = { notes: [note] }
97+
template = "discussions/_diff_discussion"
98+
locals = { discussion: discussion }
9999
end
100100

101101
render_to_string(
@@ -106,14 +106,14 @@ def note_to_discussion_html(note)
106106
)
107107
end
108108

109-
def note_to_discussion_with_diff_html(note)
110-
return unless note.diff_note?
109+
def discussion_html(discussion)
110+
return unless discussion.diff_discussion?
111111

112112
render_to_string(
113-
"projects/notes/_discussion",
113+
"discussions/_discussion",
114114
layout: false,
115115
formats: [:html],
116-
locals: { discussion_notes: [note] }
116+
locals: { discussion: discussion }
117117
)
118118
end
119119

@@ -132,26 +132,33 @@ def note_json(note)
132132
valid: true,
133133
id: note.id,
134134
discussion_id: note.discussion_id,
135-
html: note_to_html(note),
135+
html: note_html(note),
136136
award: false,
137-
note: note.note,
138-
discussion_html: note_to_discussion_html(note),
139-
discussion_with_diff_html: note_to_discussion_with_diff_html(note)
137+
note: note.note
140138
}
141139

142-
# The discussion_id is used to add the comment to the correct discussion
143-
# element on the merge request page. Among other things, the discussion_id
144-
# contains the sha of head commit of the merge request.
145-
# When new commits are pushed into the merge request after the initial
146-
# load of the merge request page, the discussion elements will still have
147-
# the old discussion_ids, with the old head commit sha. The new comment,
148-
# however, will have the new discussion_id with the new commit sha.
149-
# To ensure that these new comments will still end up in the correct
150-
# discussion element, we also send the original discussion_id, with the
151-
# old commit sha, along, and fall back on this value when no discussion
152-
# element with the new discussion_id could be found.
153-
if note.new_diff_note? && note.position != note.original_position
154-
attrs[:original_discussion_id] = note.original_discussion_id
140+
if note.diff_note?
141+
discussion = Discussion.new([note])
142+
143+
attrs.merge!(
144+
diff_discussion_html: diff_discussion_html(discussion),
145+
discussion_html: discussion_html(discussion)
146+
)
147+
148+
# The discussion_id is used to add the comment to the correct discussion
149+
# element on the merge request page. Among other things, the discussion_id
150+
# contains the sha of head commit of the merge request.
151+
# When new commits are pushed into the merge request after the initial
152+
# load of the merge request page, the discussion elements will still have
153+
# the old discussion_ids, with the old head commit sha. The new comment,
154+
# however, will have the new discussion_id with the new commit sha.
155+
# To ensure that these new comments will still end up in the correct
156+
# discussion element, we also send the original discussion_id, with the
157+
# old commit sha, along, and fall back on this value when no discussion
158+
# element with the new discussion_id could be found.
159+
if note.new_diff_note? && note.position != note.original_position
160+
attrs[:original_discussion_id] = note.original_discussion_id
161+
end
155162
end
156163

157164
attrs

app/helpers/diff_helper.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ def diff_line_content(line, line_type = nil)
5454
end
5555
end
5656

57-
def organize_comments(left, right)
58-
notes_left = notes_right = nil
57+
def parallel_diff_discussions(left, right, diff_file)
58+
discussion_left = discussion_right = nil
5959

60-
unless left[:type].nil? && right[:type] == 'new'
61-
notes_left = @grouped_diff_notes[left[:line_code]]
60+
if left && (left.unchanged? || left.removed?)
61+
line_code = diff_file.line_code(left)
62+
discussion_left = @grouped_diff_discussions[line_code]
6263
end
6364

64-
unless left[:type].nil? && right[:type].nil?
65-
notes_right = @grouped_diff_notes[right[:line_code]]
65+
if right && right.added?
66+
line_code = diff_file.line_code(right)
67+
discussion_right = @grouped_diff_discussions[line_code]
6668
end
6769

68-
[notes_left, notes_right]
70+
[discussion_left, discussion_right]
6971
end
7072

7173
def inline_diff_btn

app/helpers/notes_helper.rb

+10-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
module NotesHelper
2-
# Helps to distinguish e.g. commit notes in mr notes list
3-
def note_for_main_target?(note)
4-
@noteable.class.name == note.noteable_type && !note.diff_note?
5-
end
6-
72
def note_target_fields(note)
83
if note.noteable
94
hidden_field_tag(:target_type, note.noteable.class.name.underscore) +
@@ -44,8 +39,8 @@ def diff_view_line_data(line_code, position, line_type)
4439
# If we didn't, diff notes that would show for the same line on the changes
4540
# tab, would show in different discussions on the discussion tab.
4641
use_legacy_diff_note ||= begin
47-
line_diff_notes = @grouped_diff_notes[line_code]
48-
line_diff_notes && line_diff_notes.any?(&:legacy_diff_note?)
42+
discussion = @grouped_diff_discussions[line_code]
43+
discussion && discussion.legacy_diff_discussion?
4944
end
5045

5146
data = {
@@ -81,22 +76,10 @@ def diff_view_line_data(line_code, position, line_type)
8176
data
8277
end
8378

84-
def link_to_reply_discussion(note, line_type = nil)
79+
def link_to_reply_discussion(discussion, line_type = nil)
8580
return unless current_user
8681

87-
data = {
88-
noteable_type: note.noteable_type,
89-
noteable_id: note.noteable_id,
90-
commit_id: note.commit_id,
91-
discussion_id: note.discussion_id,
92-
line_type: line_type
93-
}
94-
95-
if note.diff_note?
96-
data[:note_type] = note.type
97-
98-
data.merge!(note.diff_attributes)
99-
end
82+
data = discussion.reply_attributes.merge(line_type: line_type)
10083

10184
content_tag(:div, class: "discussion-reply-holder") do
10285
button_tag 'Reply...', class: 'btn btn-text-field js-discussion-reply-button',
@@ -114,13 +97,13 @@ def note_max_access_for_user(note)
11497
@max_access_by_user_id[full_key]
11598
end
11699

117-
def diff_note_path(note)
118-
return unless note.diff_note?
100+
def discussion_diff_path(discussion)
101+
return unless discussion.diff_discussion?
119102

120-
if note.for_merge_request? && note.active?
121-
diffs_namespace_project_merge_request_path(note.project.namespace, note.project, note.noteable, anchor: note.line_code)
122-
elsif note.for_commit?
123-
namespace_project_commit_path(note.project.namespace, note.project, note.noteable, anchor: note.line_code)
103+
if discussion.for_merge_request? && discussion.active?
104+
diffs_namespace_project_merge_request_path(discussion.project.namespace, discussion.project, discussion.noteable, anchor: discussion.line_code)
105+
elsif discussion.for_commit?
106+
namespace_project_commit_path(discussion.project.namespace, discussion.project, discussion.noteable, anchor: discussion.line_code)
124107
end
125108
end
126109
end

app/models/concerns/note_on_diff.rb

-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
module NoteOnDiff
22
extend ActiveSupport::Concern
33

4-
NUMBER_OF_TRUNCATED_DIFF_LINES = 16
5-
6-
included do
7-
delegate :blob, :highlighted_diff_lines, to: :diff_file, allow_nil: true
8-
end
9-
104
def diff_note?
115
true
126
end
@@ -30,23 +24,4 @@ def diff_attributes
3024
def can_be_award_emoji?
3125
false
3226
end
33-
34-
# Returns an array of at most 16 highlighted lines above a diff note
35-
def truncated_diff_lines
36-
prev_lines = []
37-
38-
highlighted_diff_lines.each do |line|
39-
if line.meta?
40-
prev_lines.clear
41-
else
42-
prev_lines << line
43-
44-
break if for_line?(line)
45-
46-
prev_lines.shift if prev_lines.length >= NUMBER_OF_TRUNCATED_DIFF_LINES
47-
end
48-
end
49-
50-
prev_lines
51-
end
5227
end

0 commit comments

Comments
 (0)