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