diff --git a/lib/gitlab/markdown/cross_project_reference.rb b/lib/gitlab/markdown/cross_project_reference.rb index de05102751f..887c205cdc9 100644 --- a/lib/gitlab/markdown/cross_project_reference.rb +++ b/lib/gitlab/markdown/cross_project_reference.rb @@ -16,15 +16,12 @@ module Gitlab # # Returns a Project, or nil if the reference can't be accessed def project_from_ref(ref) - if ref && other = Project.find_with_namespace(ref) - if user_can_reference_project?(other) - other - else - nil - end - else - context[:project] - end + return context[:project] unless ref + + other = Project.find_with_namespace(ref) + return nil unless other && user_can_reference_project?(other) + + other end def user_can_reference_project?(project, user = context[:current_user]) diff --git a/spec/lib/gitlab/markdown/cross_project_reference_spec.rb b/spec/lib/gitlab/markdown/cross_project_reference_spec.rb index d79b2c1af92..4698d6138c2 100644 --- a/spec/lib/gitlab/markdown/cross_project_reference_spec.rb +++ b/spec/lib/gitlab/markdown/cross_project_reference_spec.rb @@ -13,9 +13,15 @@ module Gitlab::Markdown include described_class describe '#project_from_ref' do - context 'when referenced project does not exist' do + context 'when no project was referenced' do it 'returns the project from context' do - expect(project_from_ref('invalid/reference')).to eq context[:project] + expect(project_from_ref(nil)).to eq context[:project] + end + end + + context 'when referenced project does not exist' do + it 'returns nil' do + expect(project_from_ref('invalid/reference')).to be_nil end end