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
|
2021-01-07 19:10:44 -05:00
|
|
|
when Board
|
|
|
|
board_url(object, **options)
|
2020-04-06 14:09:37 -04:00
|
|
|
when ::Ci::Build
|
|
|
|
instance.project_job_url(object.project, object, **options)
|
|
|
|
when Commit
|
|
|
|
commit_url(object, **options)
|
2021-08-24 20:11:06 -04:00
|
|
|
when Compare
|
|
|
|
compare_url(object, **options)
|
2020-04-06 14:09:37 -04:00
|
|
|
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)
|
2020-11-06 19:08:58 -05:00
|
|
|
when Release
|
|
|
|
instance.release_url(object, **options)
|
2020-04-06 14:09:37 -04:00
|
|
|
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
|
2020-06-11 11:08:36 -04:00
|
|
|
wiki_url(object, **options)
|
2020-04-06 14:09:37 -04:00
|
|
|
when WikiPage
|
2020-06-03 20:08:17 -04:00
|
|
|
wiki_page_url(object.wiki, object, **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
|
2021-05-04 11:10:36 -04:00
|
|
|
raise NotImplementedError, "No URL builder defined for #{object.inspect}"
|
2020-04-06 14:09:37 -04:00
|
|
|
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
|
|
|
|
2021-01-07 19:10:44 -05:00
|
|
|
def board_url(board, **options)
|
|
|
|
if board.project_board?
|
|
|
|
instance.project_board_url(board.resource_parent, board, **options)
|
|
|
|
else
|
|
|
|
instance.group_board_url(board.resource_parent, board, **options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2021-08-24 20:11:06 -04:00
|
|
|
def compare_url(compare, **options)
|
|
|
|
return '' unless compare.project
|
|
|
|
|
|
|
|
instance.project_compare_url(compare.project, **options.merge(compare.to_param))
|
|
|
|
end
|
|
|
|
|
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)
|
2020-07-01 11:08:45 -04:00
|
|
|
if options[:file].present?
|
|
|
|
file, ref = options.values_at(:file, :ref)
|
|
|
|
|
|
|
|
instance.gitlab_raw_snippet_blob_url(snippet, file, ref)
|
|
|
|
elsif options.delete(:raw).present?
|
2020-04-06 14:09:37 -04:00
|
|
|
instance.gitlab_raw_snippet_url(snippet, **options)
|
|
|
|
else
|
|
|
|
instance.gitlab_snippet_url(snippet, **options)
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 08:09:29 -04:00
|
|
|
|
2020-06-11 11:08:36 -04:00
|
|
|
def wiki_url(wiki, **options)
|
|
|
|
return wiki_page_url(wiki, Wiki::HOMEPAGE, **options) unless options[:action]
|
|
|
|
|
2020-07-12 05:09:08 -04:00
|
|
|
if wiki.container.is_a?(Project)
|
|
|
|
options[:controller] = 'projects/wikis'
|
|
|
|
options[:namespace_id] = wiki.container.namespace
|
|
|
|
options[:project_id] = wiki.container
|
|
|
|
end
|
2020-06-11 11:08:36 -04:00
|
|
|
|
|
|
|
instance.url_for(**options)
|
|
|
|
end
|
|
|
|
|
2020-06-03 20:08:17 -04:00
|
|
|
def wiki_page_url(wiki, page, **options)
|
2020-06-11 11:08:36 -04:00
|
|
|
options[:action] ||= :show
|
|
|
|
options[:id] = page
|
|
|
|
|
|
|
|
wiki_url(wiki, **options)
|
2020-04-22 08:09:29 -04:00
|
|
|
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
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
::Gitlab::UrlBuilder.prepend_mod_with('Gitlab::UrlBuilder')
|