debian-mirror-gitlab/lib/gitlab/url_builder.rb

154 lines
4.8 KiB
Ruby
Raw Permalink Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Gitlab
class UrlBuilder
2020-04-22 19:07:51 +05:30
include Singleton
2017-09-10 17:25:29 +05:30
include Gitlab::Routing
2015-04-26 12:48:37 +05:30
include GitlabRoutingHelper
2014-09-02 18:07:02 +05:30
2020-04-22 19:07:51 +05:30
delegate :build, to: :class
2014-09-02 18:07:02 +05:30
2020-04-22 19:07:51 +05:30
class << self
include ActionView::RecordIdentifier
2015-04-26 12:48:37 +05:30
2020-05-24 23:13:21 +05:30
# Using a case statement here is preferable for readability and maintainability.
# See discussion in https://gitlab.com/gitlab-org/gitlab/-/issues/217397
#
# rubocop:disable Metrics/CyclomaticComplexity
2020-04-22 19:07:51 +05:30
def build(object, **options)
# Objects are sometimes wrapped in a BatchLoader instance
case object.itself
2021-03-08 18:12:59 +05:30
when Board
board_url(object, **options)
2020-04-22 19:07:51 +05:30
when ::Ci::Build
instance.project_job_url(object.project, object, **options)
when Commit
commit_url(object, **options)
2021-11-11 11:23:49 +05:30
when Compare
compare_url(object, **options)
2020-04-22 19:07:51 +05:30
when Group
instance.group_canonical_url(object, **options)
2022-07-16 23:28:13 +05:30
when WorkItem
instance.work_item_url(object, **options)
2020-04-22 19:07:51 +05:30
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)
2021-01-29 00:20:46 +05:30
when Release
instance.release_url(object, **options)
2020-04-22 19:07:51 +05:30
when Project
instance.project_url(object, **options)
when Snippet
snippet_url(object, **options)
when User
instance.user_url(object, **options)
2020-05-24 23:13:21 +05:30
when Wiki
wiki_url(object, **options)
2020-04-22 19:07:51 +05:30
when WikiPage
2020-06-23 00:09:42 +05:30
wiki_page_url(object.wiki, object, **options)
2020-05-24 23:13:21 +05:30
when ::DesignManagement::Design
design_url(object, **options)
2023-01-13 00:05:48 +05:30
when ::Packages::Package
package_url(object, **options)
2020-04-22 19:07:51 +05:30
else
2021-06-08 01:23:25 +05:30
raise NotImplementedError, "No URL builder defined for #{object.inspect}"
2020-04-22 19:07:51 +05:30
end
2014-09-02 18:07:02 +05:30
end
2020-05-24 23:13:21 +05:30
# rubocop:enable Metrics/CyclomaticComplexity
2014-09-02 18:07:02 +05:30
2021-03-08 18:12:59 +05:30
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-22 19:07:51 +05:30
def commit_url(commit, **options)
return '' unless commit.project
2016-06-02 11:05:42 +05:30
2020-04-22 19:07:51 +05:30
instance.commit_url(commit, **options)
end
2016-06-02 11:05:42 +05:30
2021-11-11 11:23:49 +05:30
def compare_url(compare, **options)
return '' unless compare.project
instance.project_compare_url(compare.project, **options.merge(compare.to_param))
end
2020-04-22 19:07:51 +05:30
def note_url(note, **options)
if note.for_commit?
return '' unless note.project
2016-06-02 11:05:42 +05:30
2020-04-22 19:07:51 +05:30
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-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
2020-04-22 19:07:51 +05:30
def snippet_url(snippet, **options)
2020-07-28 23:09:34 +05:30
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-22 19:07:51 +05:30
instance.gitlab_raw_snippet_url(snippet, **options)
else
instance.gitlab_snippet_url(snippet, **options)
end
end
2020-05-24 23:13:21 +05:30
2020-06-23 00:09:42 +05:30
def wiki_url(wiki, **options)
return wiki_page_url(wiki, Wiki::HOMEPAGE, **options) unless options[:action]
2020-07-28 23:09:34 +05:30
if wiki.container.is_a?(Project)
options[:controller] = 'projects/wikis'
options[:namespace_id] = wiki.container.namespace
options[:project_id] = wiki.container
end
2020-06-23 00:09:42 +05:30
instance.url_for(**options)
end
def wiki_page_url(wiki, page, **options)
options[:action] ||= :show
options[:id] = page
wiki_url(wiki, **options)
2020-05-24 23:13:21 +05:30
end
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
2023-01-13 00:05:48 +05:30
def package_url(package, **options)
project = package.project
if package.infrastructure_package?
return instance.project_infrastructure_registry_url(project, package,
**options)
end
instance.project_package_url(project, package, **options)
end
2016-06-02 11:05:42 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
2019-12-21 20:55:43 +05:30
2021-06-08 01:23:25 +05:30
::Gitlab::UrlBuilder.prepend_mod_with('Gitlab::UrlBuilder')