Commit 2823c058 authored by Robert Speicher's avatar Robert Speicher

project_from_ref returns nil when reference can't be accessed

Prior it would return the project from the current context, which wasn't
the intended behavior.
parent 3a0b4340
......@@ -91,7 +91,7 @@ module Gitlab
end
def valid_range?(project, from_id, to_id)
project.valid_repo? && commit(from_id) && commit(to_id)
project && project.valid_repo? && commit(from_id) && commit(to_id)
end
def url_for_commit_range(project, from_id, to_id)
......
......@@ -47,7 +47,7 @@ module Gitlab
self.class.references_in(text) do |match, commit_ref, project_ref|
project = self.project_from_ref(project_ref)
if project.valid_repo? && commit = project.repository.commit(commit_ref)
if commit = commit_from_ref(project, commit_ref)
url = url_for_commit(project, commit)
title = escape_once(commit.link_title)
......@@ -64,6 +64,12 @@ module Gitlab
end
end
def commit_from_ref(project, commit_ref)
if project && project.valid_repo?
project.repository.commit(commit_ref)
end
end
def url_for_commit(project, commit)
h = Rails.application.routes.url_helpers
h.namespace_project_commit_url(project.namespace, project, commit,
......
......@@ -10,18 +10,17 @@ module Gitlab
#
# Defaults to value of `context[:project]` if:
# * No reference is given OR
# * Reference given doesn't exist OR
# * Reference given can't be read by the current user
# * Reference given doesn't exist
#
# ref - String reference.
#
# Returns a Project
# 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
context[:project]
nil
end
else
context[:project]
......
......@@ -47,7 +47,7 @@ module Gitlab
self.class.references_in(text) do |match, issue, project_ref|
project = self.project_from_ref(project_ref)
if project.issue_exists?(issue)
if project && project.issue_exists?(issue)
url = url_for_issue(issue, project, only_path: context[:only_path])
title = escape_once("Issue: #{title_for_issue(issue, project)}")
......
......@@ -47,7 +47,7 @@ module Gitlab
self.class.references_in(text) do |match, id, project_ref|
project = self.project_from_ref(project_ref)
if merge_request = project.merge_requests.find_by(iid: id)
if project && merge_request = project.merge_requests.find_by(iid: id)
title = escape_once("Merge Request: #{merge_request.title}")
klass = reference_class(:merge_request)
......
......@@ -47,7 +47,7 @@ module Gitlab
self.class.references_in(text) do |match, id, project_ref|
project = self.project_from_ref(project_ref)
if snippet = project.snippets.find_by(id: id)
if project && snippet = project.snippets.find_by(id: id)
title = escape_once("Snippet: #{snippet.title}")
klass = reference_class(:snippet)
......
......@@ -37,11 +37,11 @@ module Gitlab::Markdown
end
context 'and the user does not have permission to read it' do
it 'returns the project from context' do
it 'returns nil' do
expect(self).to receive(:user_can_reference_project?).
with(project2).and_return(false)
expect(project_from_ref('cross/reference')).to eq context[:project]
expect(project_from_ref('cross/reference')).to be_nil
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