Commit b5802d14 authored by Douwe Maan's avatar Douwe Maan Committed by Robert Speicher

project_from_ref returns nil when reference doesn't exist.

parent b5952494
...@@ -16,15 +16,12 @@ module Gitlab ...@@ -16,15 +16,12 @@ module Gitlab
# #
# Returns a Project, or nil if the reference can't be accessed # Returns a Project, or nil if the reference can't be accessed
def project_from_ref(ref) def project_from_ref(ref)
if ref && other = Project.find_with_namespace(ref) return context[:project] unless ref
if user_can_reference_project?(other)
other other = Project.find_with_namespace(ref)
else return nil unless other && user_can_reference_project?(other)
nil
end other
else
context[:project]
end
end end
def user_can_reference_project?(project, user = context[:current_user]) def user_can_reference_project?(project, user = context[:current_user])
......
...@@ -13,9 +13,15 @@ module Gitlab::Markdown ...@@ -13,9 +13,15 @@ module Gitlab::Markdown
include described_class include described_class
describe '#project_from_ref' do 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 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
end end
......
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