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

80 lines
1.9 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
2017-09-10 17:25:29 +05:30
include Gitlab::Routing
2015-04-26 12:48:37 +05:30
include GitlabRoutingHelper
2016-06-02 11:05:42 +05:30
include ActionView::RecordIdentifier
2014-09-02 18:07:02 +05:30
2020-01-01 13:55:28 +05:30
attr_reader :object, :opts
2014-09-02 18:07:02 +05:30
2020-01-01 13:55:28 +05:30
def self.build(object, opts = {})
new(object, opts).url
2016-06-02 11:05:42 +05:30
end
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
def url
2020-04-08 14:13:33 +05:30
# Objects are sometimes wrapped in a BatchLoader instance
case object.itself
2016-06-02 11:05:42 +05:30
when Commit
commit_url
when Issue
issue_url(object)
when MergeRequest
merge_request_url(object)
when Note
note_url
when WikiPage
wiki_page_url
2017-08-17 22:00:37 +05:30
when Snippet
2020-01-01 13:55:28 +05:30
opts[:raw].present? ? gitlab_raw_snippet_url(object) : gitlab_snippet_url(object)
2018-11-08 19:23:39 +05:30
when Milestone
milestone_url(object)
2019-07-31 22:56:46 +05:30
when ::Ci::Build
project_job_url(object.project, object)
when User
user_url(object)
2016-06-02 11:05:42 +05:30
else
2020-04-08 14:13:33 +05:30
raise NotImplementedError.new("No URL builder defined for #{object.inspect}")
2014-09-02 18:07:02 +05:30
end
end
private
2020-01-01 13:55:28 +05:30
def initialize(object, opts = {})
2016-06-02 11:05:42 +05:30
@object = object
2020-01-01 13:55:28 +05:30
@opts = opts
2015-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
def commit_url(opts = {})
return '' if object.project.nil?
namespace_project_commit_url({
namespace_id: object.project.namespace,
project_id: object.project,
id: object.id
}.merge!(opts))
2015-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
def note_url
if object.for_commit?
commit_url(id: object.commit_id, anchor: dom_id(object))
elsif object.for_issue?
2017-09-10 17:25:29 +05:30
issue_url(object.noteable, anchor: dom_id(object))
2016-06-02 11:05:42 +05:30
elsif object.for_merge_request?
2017-09-10 17:25:29 +05:30
merge_request_url(object.noteable, anchor: dom_id(object))
2016-06-02 11:05:42 +05:30
elsif object.for_snippet?
2020-01-01 13:55:28 +05:30
gitlab_snippet_url(object.noteable, anchor: dom_id(object))
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
def wiki_page_url
2017-09-10 17:25:29 +05:30
project_wiki_url(object.wiki.project, object.slug)
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')