gitlab-org--gitlab-foss/lib/api/helpers/related_resources_helpers.rb
Stan Hu 58c09eb709 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
2019-05-07 06:16:39 -07:00

39 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module API
module Helpers
module RelatedResourcesHelpers
include GrapePathHelpers::NamedRouteMatcher
def issues_available?(project, options)
available?(:issues, project, options[:current_user])
end
def mrs_available?(project, options)
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)
# Using a blank component at the beginning of the join we ensure
# that the resulted path will start with '/'. If the resulted path
# does not start with '/', URI::Generic#build will fail
path_with_script_name = File.join('', [script_name, path].select(&:present?))
URI::Generic.build(scheme: protocol, host: host, port: port, path: path_with_script_name).to_s
end
private
def available?(feature, project, current_user)
project.feature_available?(feature, current_user)
end
end
end
end