Use a path for the related merge requests endpoint

Hosts using a non-standard configuration may have a different
hostname/port/scheme than what may be configured on the GitLab
server. While expose_url should generate a proper URL, there are cases
where it may not work. Since we don't need the full URL, we can use the
relative path.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/61280
This commit is contained in:
Stan Hu 2019-05-07 00:28:35 -07:00
parent fb688545e5
commit 58c09eb709
4 changed files with 44 additions and 1 deletions

View file

@ -79,7 +79,7 @@
= render_if_exists 'projects/issues/related_issues'
#js-related-merge-requests{ data: { endpoint: expose_url(api_v4_projects_issues_related_merge_requests_path(id: @project.id, issue_iid: @issue.iid)), project_namespace: @project.namespace.path, project_path: @project.path } }
#js-related-merge-requests{ data: { endpoint: expose_path(api_v4_projects_issues_related_merge_requests_path(id: @project.id, issue_iid: @issue.iid)), project_namespace: @project.namespace.path, project_path: @project.path } }
- if can?(current_user, :download_code, @project)
#related-branches{ data: { url: related_branches_project_issue_path(@project, @issue) } }

View file

@ -0,0 +1,5 @@
---
title: Use a path for the related merge requests endpoint
merge_request: 28171
author:
type: fixed

View file

@ -13,6 +13,10 @@ module API
available?(:merge_requests, project, options[:current_user])
end
def expose_path(path)
Gitlab::Utils.append_path(Gitlab.config.gitlab.relative_url_root, path)
end
def expose_url(path)
url_options = Gitlab::Application.routes.default_url_options
protocol, host, port, script_name = url_options.values_at(:protocol, :host, :port, :script_name)

View file

@ -5,6 +5,40 @@ describe API::Helpers::RelatedResourcesHelpers do
Class.new.include(described_class).new
end
describe '#expose_path' do
let(:path) { '/api/v4/awesome_endpoint' }
context 'empty relative URL root' do
before do
stub_config_setting(relative_url_root: '')
end
it 'returns the existing path' do
expect(helpers.expose_path(path)).to eq(path)
end
end
context 'slash relative URL root' do
before do
stub_config_setting(relative_url_root: '/')
end
it 'returns the existing path' do
expect(helpers.expose_path(path)).to eq(path)
end
end
context 'with relative URL root' do
before do
stub_config_setting(relative_url_root: '/gitlab/root')
end
it 'returns the existing path' do
expect(helpers.expose_path(path)).to eq("/gitlab/root" + path)
end
end
end
describe '#expose_url' do
let(:path) { '/api/v4/awesome_endpoint' }
subject(:url) { helpers.expose_url(path) }