Commit 6a79e237 authored by Patrick Bajao's avatar Patrick Bajao Committed by Nick Thomas

Fix showing diff when it has legacy diff notes

Do not try to unfold `LegacyDiffNote`s as they don't support
`position` even though they're in the same `notes` table as
`DiffNote`s.

This is done by using `Gitlab::Diff::PositionCollection` which
will be responsible for filtering the positions.
parent f25f4e2d
...@@ -51,9 +51,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic ...@@ -51,9 +51,7 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
def render_diffs def render_diffs
@environment = @merge_request.environments_for(current_user).last @environment = @merge_request.environments_for(current_user).last
note_positions = renderable_notes.map(&:position).compact @diffs.unfold_diff_files(note_positions.unfoldable)
@diffs.unfold_diff_files(note_positions)
@diffs.write_cache @diffs.write_cache
request = { request = {
...@@ -140,6 +138,10 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic ...@@ -140,6 +138,10 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
@notes = prepare_notes_for_rendering(@grouped_diff_discussions.values.flatten.flat_map(&:notes), @merge_request) @notes = prepare_notes_for_rendering(@grouped_diff_discussions.values.flatten.flat_map(&:notes), @merge_request)
end end
def note_positions
@note_positions ||= Gitlab::Diff::PositionCollection.new(renderable_notes.map(&:position))
end
def renderable_notes def renderable_notes
define_diff_comment_vars unless @notes define_diff_comment_vars unless @notes
......
---
title: Fix showing diff when it has legacy diff notes
merge_request: 18510
author:
type: fixed
...@@ -6,13 +6,13 @@ module Gitlab ...@@ -6,13 +6,13 @@ module Gitlab
include Enumerable include Enumerable
# collection - An array of Gitlab::Diff::Position # collection - An array of Gitlab::Diff::Position
def initialize(collection, diff_head_sha) def initialize(collection, diff_head_sha = nil)
@collection = collection @collection = collection
@diff_head_sha = diff_head_sha @diff_head_sha = diff_head_sha
end end
def each(&block) def each(&block)
@collection.each(&block) filtered_positions.each(&block)
end end
def concat(positions) def concat(positions)
...@@ -23,9 +23,21 @@ module Gitlab ...@@ -23,9 +23,21 @@ module Gitlab
# positions (https://gitlab.com/gitlab-org/gitlab/issues/33271). # positions (https://gitlab.com/gitlab-org/gitlab/issues/33271).
def unfoldable def unfoldable
select do |position| select do |position|
position.unfoldable? && position.head_sha == @diff_head_sha position.unfoldable? && valid_head_sha?(position)
end end
end end
private
def filtered_positions
@collection.select { |item| item.is_a?(Position) }
end
def valid_head_sha?(position)
return true unless @diff_head_sha
position.head_sha == @diff_head_sha
end
end end
end end
end end
...@@ -82,9 +82,9 @@ describe Projects::MergeRequests::DiffsController do ...@@ -82,9 +82,9 @@ describe Projects::MergeRequests::DiffsController do
end end
end end
context 'when note has no position' do context 'when note is a legacy diff note' do
before do before do
create(:legacy_diff_note_on_merge_request, project: project, noteable: merge_request, position: nil) create(:legacy_diff_note_on_merge_request, project: project, noteable: merge_request)
end end
it 'serializes merge request diff collection' do it 'serializes merge request diff collection' do
......
...@@ -35,14 +35,15 @@ describe Gitlab::Diff::PositionCollection do ...@@ -35,14 +35,15 @@ describe Gitlab::Diff::PositionCollection do
let(:text_position) { build_text_position } let(:text_position) { build_text_position }
let(:folded_text_position) { build_text_position(old_line: 1, new_line: 1) } let(:folded_text_position) { build_text_position(old_line: 1, new_line: 1) }
let(:image_position) { build_image_position } let(:image_position) { build_image_position }
let(:invalid_position) { 'a position' }
let(:head_sha) { merge_request.diff_head_sha } let(:head_sha) { merge_request.diff_head_sha }
let(:collection) do let(:collection) do
described_class.new([text_position, folded_text_position, image_position], head_sha) described_class.new([text_position, folded_text_position, image_position, invalid_position], head_sha)
end end
describe '#to_a' do describe '#to_a' do
it 'returns all positions' do it 'returns all positions that are Gitlab::Diff::Position' do
expect(collection.to_a).to eq([text_position, folded_text_position, image_position]) expect(collection.to_a).to eq([text_position, folded_text_position, image_position])
end end
end end
...@@ -59,6 +60,14 @@ describe Gitlab::Diff::PositionCollection do ...@@ -59,6 +60,14 @@ describe Gitlab::Diff::PositionCollection do
expect(collection.unfoldable).to be_empty expect(collection.unfoldable).to be_empty
end end
end end
context 'when given head_sha is nil' do
let(:head_sha) { nil }
it 'returns unfoldable diff positions unfiltered by head_sha' do
expect(collection.unfoldable).to eq([folded_text_position])
end
end
end end
describe '#concat' do describe '#concat' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment