Commit 7399eb43 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix-500-on-old-merge-requests' into 'master'

Fix 500 error for old (somewhat) MRs

Closes #36540

See merge request gitlab-org/gitlab-ce!14945
parents b5d47d87 7ba7fa50
---
title: Fix 500 errors caused by empty diffs in some discussions
merge_request: 14945
author: Alexander Popov
type: fixed
...@@ -94,7 +94,9 @@ module Gitlab ...@@ -94,7 +94,9 @@ module Gitlab
end end
def diff_file(repository) def diff_file(repository)
@diff_file ||= begin return @diff_file if defined?(@diff_file)
@diff_file = begin
if RequestStore.active? if RequestStore.active?
key = { key = {
project_id: repository.project.id, project_id: repository.project.id,
...@@ -122,8 +124,8 @@ module Gitlab ...@@ -122,8 +124,8 @@ module Gitlab
def find_diff_file(repository) def find_diff_file(repository)
return unless diff_refs.complete? return unless diff_refs.complete?
return unless comparison = diff_refs.compare_in(repository.project)
diff_refs.compare_in(repository.project).diffs(paths: paths, expanded: true).diff_files.first comparison.diffs(paths: paths, expanded: true).diff_files.first
end end
def get_formatter_class(type) def get_formatter_class(type)
......
...@@ -364,6 +364,43 @@ describe Gitlab::Diff::Position do ...@@ -364,6 +364,43 @@ describe Gitlab::Diff::Position do
end end
end end
describe "position for a missing ref" do
let(:diff_refs) do
Gitlab::Diff::DiffRefs.new(
base_sha: "not_existing_sha",
head_sha: "existing_sha"
)
end
subject do
described_class.new(
old_path: "files/ruby/feature.rb",
new_path: "files/ruby/feature.rb",
old_line: 3,
new_line: nil,
diff_refs: diff_refs
)
end
describe "#diff_file" do
it "does not raise exception" do
expect { subject.diff_file(project.repository) }.not_to raise_error
end
end
describe "#diff_line" do
it "does not raise exception" do
expect { subject.diff_line(project.repository) }.not_to raise_error
end
end
describe "#line_code" do
it "does not raise exception" do
expect { subject.line_code(project.repository) }.not_to raise_error
end
end
end
describe "position for a file in the initial commit" do describe "position for a file in the initial commit" do
let(:commit) { project.commit("1a0b36b3cdad1d2ee32457c102a8c0b7056fa863") } let(:commit) { project.commit("1a0b36b3cdad1d2ee32457c102a8c0b7056fa863") }
......
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