2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-13 10:34:15 +00:00
|
|
|
module Gitlab
|
|
|
|
class UrlBuilder
|
2020-04-06 18:09:37 +00:00
|
|
|
include Singleton
|
2017-07-07 15:43:37 +00:00
|
|
|
include Gitlab::Routing
|
2015-09-09 12:37:34 +00:00
|
|
|
include GitlabRoutingHelper
|
2014-06-13 10:34:15 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
delegate :build, to: :class
|
2014-06-13 10:34:15 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
class << self
|
|
|
|
include ActionView::RecordIdentifier
|
2015-03-05 18:38:23 +00:00
|
|
|
|
2020-05-12 15:10:33 +00:00
|
|
|
# Using a case statement here is preferable for readability and maintainability.
|
|
|
|
# See discussion in https://gitlab.com/gitlab-org/gitlab/-/issues/217397
|
|
|
|
#
|
2020-05-12 00:10:11 +00:00
|
|
|
# rubocop:disable Metrics/CyclomaticComplexity
|
2020-04-06 18:09:37 +00:00
|
|
|
def build(object, **options)
|
|
|
|
# Objects are sometimes wrapped in a BatchLoader instance
|
|
|
|
case object.itself
|
|
|
|
when ::Ci::Build
|
|
|
|
instance.project_job_url(object.project, object, **options)
|
|
|
|
when Commit
|
|
|
|
commit_url(object, **options)
|
|
|
|
when Group
|
|
|
|
instance.group_canonical_url(object, **options)
|
|
|
|
when Issue
|
|
|
|
instance.issue_url(object, **options)
|
|
|
|
when MergeRequest
|
|
|
|
instance.merge_request_url(object, **options)
|
|
|
|
when Milestone
|
|
|
|
instance.milestone_url(object, **options)
|
|
|
|
when Note
|
|
|
|
note_url(object, **options)
|
|
|
|
when Project
|
|
|
|
instance.project_url(object, **options)
|
|
|
|
when Snippet
|
|
|
|
snippet_url(object, **options)
|
|
|
|
when User
|
|
|
|
instance.user_url(object, **options)
|
2020-04-22 12:09:29 +00:00
|
|
|
when Wiki
|
2020-06-11 15:08:36 +00:00
|
|
|
wiki_url(object, **options)
|
2020-04-06 18:09:37 +00:00
|
|
|
when WikiPage
|
2020-06-04 00:08:17 +00:00
|
|
|
wiki_page_url(object.wiki, object, **options)
|
2020-05-12 00:10:11 +00:00
|
|
|
when ::DesignManagement::Design
|
|
|
|
design_url(object, **options)
|
2020-04-06 18:09:37 +00:00
|
|
|
else
|
|
|
|
raise NotImplementedError.new("No URL builder defined for #{object.inspect}")
|
|
|
|
end
|
2014-06-13 10:34:15 +00:00
|
|
|
end
|
2020-05-12 00:10:11 +00:00
|
|
|
# rubocop:enable Metrics/CyclomaticComplexity
|
2014-06-13 10:34:15 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
def commit_url(commit, **options)
|
|
|
|
return '' unless commit.project
|
2016-04-13 09:25:42 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
instance.commit_url(commit, **options)
|
|
|
|
end
|
2016-04-13 09:25:42 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
def note_url(note, **options)
|
|
|
|
if note.for_commit?
|
|
|
|
return '' unless note.project
|
2016-04-13 09:25:42 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
instance.project_commit_url(note.project, note.commit_id, anchor: dom_id(note), **options)
|
|
|
|
elsif note.for_issue?
|
|
|
|
instance.issue_url(note.noteable, anchor: dom_id(note), **options)
|
|
|
|
elsif note.for_merge_request?
|
|
|
|
instance.merge_request_url(note.noteable, anchor: dom_id(note), **options)
|
|
|
|
elsif note.for_snippet?
|
|
|
|
instance.gitlab_snippet_url(note.noteable, anchor: dom_id(note), **options)
|
|
|
|
end
|
2015-03-05 18:38:23 +00:00
|
|
|
end
|
2016-04-05 05:36:09 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
def snippet_url(snippet, **options)
|
|
|
|
if options.delete(:raw).present?
|
|
|
|
instance.gitlab_raw_snippet_url(snippet, **options)
|
|
|
|
else
|
|
|
|
instance.gitlab_snippet_url(snippet, **options)
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 12:09:29 +00:00
|
|
|
|
2020-06-11 15:08:36 +00:00
|
|
|
def wiki_url(wiki, **options)
|
|
|
|
return wiki_page_url(wiki, Wiki::HOMEPAGE, **options) unless options[:action]
|
|
|
|
|
|
|
|
options[:controller] = 'projects/wikis'
|
|
|
|
options[:namespace_id] = wiki.container.namespace
|
|
|
|
options[:project_id] = wiki.container
|
|
|
|
|
|
|
|
instance.url_for(**options)
|
|
|
|
end
|
|
|
|
|
2020-06-04 00:08:17 +00:00
|
|
|
def wiki_page_url(wiki, page, **options)
|
2020-06-11 15:08:36 +00:00
|
|
|
options[:action] ||= :show
|
|
|
|
options[:id] = page
|
|
|
|
|
|
|
|
wiki_url(wiki, **options)
|
2020-04-22 12:09:29 +00:00
|
|
|
end
|
2020-05-12 00:10:11 +00:00
|
|
|
|
|
|
|
def design_url(design, **options)
|
|
|
|
size, ref = options.values_at(:size, :ref)
|
|
|
|
options.except!(:size, :ref)
|
|
|
|
|
|
|
|
if size
|
|
|
|
instance.project_design_management_designs_resized_image_url(design.project, design, ref, size, **options)
|
|
|
|
else
|
|
|
|
instance.project_design_management_designs_raw_image_url(design.project, design, ref, **options)
|
|
|
|
end
|
|
|
|
end
|
2016-04-05 05:36:09 +00:00
|
|
|
end
|
2014-06-13 10:34:15 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-25 09:06:04 +00:00
|
|
|
|
|
|
|
::Gitlab::UrlBuilder.prepend_if_ee('EE::Gitlab::UrlBuilder')
|