2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-13 06:34:15 -04:00
|
|
|
module Gitlab
|
|
|
|
class UrlBuilder
|
2020-04-06 14:09:37 -04:00
|
|
|
include Singleton
|
2017-07-07 11:43:37 -04:00
|
|
|
include Gitlab::Routing
|
2015-09-09 08:37:34 -04:00
|
|
|
include GitlabRoutingHelper
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
delegate :build, to: :class
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
class << self
|
|
|
|
include ActionView::RecordIdentifier
|
2015-03-05 13:38:23 -05:00
|
|
|
|
2020-05-12 11:10:33 -04: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-11 20:10:11 -04:00
|
|
|
# rubocop:disable Metrics/CyclomaticComplexity
|
2020-04-06 14:09:37 -04: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 08:09:29 -04:00
|
|
|
when Wiki
|
|
|
|
wiki_url(object, **options)
|
2020-04-06 14:09:37 -04:00
|
|
|
when WikiPage
|
|
|
|
instance.project_wiki_url(object.wiki.project, object.slug, **options)
|
2020-05-11 20:10:11 -04:00
|
|
|
when ::DesignManagement::Design
|
|
|
|
design_url(object, **options)
|
2020-04-06 14:09:37 -04:00
|
|
|
else
|
|
|
|
raise NotImplementedError.new("No URL builder defined for #{object.inspect}")
|
|
|
|
end
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
2020-05-11 20:10:11 -04:00
|
|
|
# rubocop:enable Metrics/CyclomaticComplexity
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
def commit_url(commit, **options)
|
|
|
|
return '' unless commit.project
|
2016-04-13 05:25:42 -04:00
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
instance.commit_url(commit, **options)
|
|
|
|
end
|
2016-04-13 05:25:42 -04:00
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
def note_url(note, **options)
|
|
|
|
if note.for_commit?
|
|
|
|
return '' unless note.project
|
2016-04-13 05:25:42 -04:00
|
|
|
|
2020-04-06 14:09:37 -04: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 13:38:23 -05:00
|
|
|
end
|
2016-04-05 01:36:09 -04:00
|
|
|
|
2020-04-06 14:09:37 -04: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 08:09:29 -04:00
|
|
|
|
|
|
|
def wiki_url(object, **options)
|
2020-05-05 11:09:42 -04:00
|
|
|
if object.container.is_a?(Project)
|
2020-04-22 08:09:29 -04:00
|
|
|
instance.project_wiki_url(object.container, Wiki::HOMEPAGE, **options)
|
|
|
|
else
|
|
|
|
raise NotImplementedError.new("No URL builder defined for #{object.inspect}")
|
|
|
|
end
|
|
|
|
end
|
2020-05-11 20:10:11 -04: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 01:36:09 -04:00
|
|
|
end
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-25 05:06:04 -04:00
|
|
|
|
|
|
|
::Gitlab::UrlBuilder.prepend_if_ee('EE::Gitlab::UrlBuilder')
|