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

78 lines
2.4 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-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)
when ProjectWiki
instance.project_wiki_url(object.project, :home, **options)
when WikiPage
instance.project_wiki_url(object.wiki.project, object.slug, **options)
else
raise NotImplementedError.new("No URL builder defined for #{object.inspect}")
end
2014-09-02 18:07:02 +05:30
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
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
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')