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

113 lines
3.5 KiB
Ruby
Raw 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
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-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)
2020-04-22 19:07:51 +05:30
else
raise NotImplementedError.new("No URL builder defined for #{object.inspect}")
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
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
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)
if options.delete(:raw).present?
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]
options[:controller] = 'projects/wikis'
options[:namespace_id] = wiki.container.namespace
options[:project_id] = wiki.container
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
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
::Gitlab::UrlBuilder.prepend_if_ee('EE::Gitlab::UrlBuilder')