2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-13 06:34:15 -04:00
|
|
|
module Gitlab
|
|
|
|
class UrlBuilder
|
2017-07-07 11:43:37 -04:00
|
|
|
include Gitlab::Routing
|
2015-09-09 08:37:34 -04:00
|
|
|
include GitlabRoutingHelper
|
2016-03-31 03:21:20 -04:00
|
|
|
include ActionView::RecordIdentifier
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
attr_reader :object
|
2014-06-13 06:34:15 -04:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
def self.build(object)
|
|
|
|
new(object).url
|
|
|
|
end
|
2015-03-05 13:38:23 -05:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
def url
|
|
|
|
case object
|
|
|
|
when Commit
|
|
|
|
commit_url
|
|
|
|
when Issue
|
|
|
|
issue_url(object)
|
|
|
|
when MergeRequest
|
|
|
|
merge_request_url(object)
|
|
|
|
when Note
|
|
|
|
note_url
|
2016-04-05 01:36:09 -04:00
|
|
|
when WikiPage
|
|
|
|
wiki_page_url
|
2016-08-11 18:11:16 -04:00
|
|
|
when ProjectSnippet
|
2017-06-29 13:06:35 -04:00
|
|
|
project_snippet_url(object.project, object)
|
2016-11-26 10:37:26 -05:00
|
|
|
when Snippet
|
2017-06-29 13:06:35 -04:00
|
|
|
snippet_url(object)
|
2018-06-12 18:40:16 -04:00
|
|
|
when Milestone
|
|
|
|
milestone_url(object)
|
2019-04-26 17:08:41 -04:00
|
|
|
when ::Ci::Build
|
|
|
|
project_job_url(object.project, object)
|
2019-05-02 12:07:26 -04:00
|
|
|
when User
|
|
|
|
user_url(object)
|
2016-04-13 05:25:42 -04:00
|
|
|
else
|
|
|
|
raise NotImplementedError.new("No URL builder defined for #{object.class}")
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
def initialize(object)
|
|
|
|
@object = object
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
2015-02-24 01:50:40 -05:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
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-02-24 01:50:40 -05:00
|
|
|
end
|
2015-03-05 13:38:23 -05:00
|
|
|
|
2016-04-13 05:25:42 -04:00
|
|
|
def note_url
|
|
|
|
if object.for_commit?
|
|
|
|
commit_url(id: object.commit_id, anchor: dom_id(object))
|
|
|
|
|
|
|
|
elsif object.for_issue?
|
2017-07-11 05:53:58 -04:00
|
|
|
issue_url(object.noteable, anchor: dom_id(object))
|
2016-04-13 05:25:42 -04:00
|
|
|
|
|
|
|
elsif object.for_merge_request?
|
2017-07-11 05:53:58 -04:00
|
|
|
merge_request_url(object.noteable, anchor: dom_id(object))
|
2016-04-13 05:25:42 -04:00
|
|
|
|
|
|
|
elsif object.for_snippet?
|
2017-07-11 05:53:58 -04:00
|
|
|
snippet = object.noteable
|
2017-06-07 14:33:50 -04:00
|
|
|
|
|
|
|
if snippet.is_a?(PersonalSnippet)
|
|
|
|
snippet_url(snippet, anchor: dom_id(object))
|
|
|
|
else
|
2017-06-29 13:06:35 -04:00
|
|
|
project_snippet_url(snippet.project, snippet, anchor: dom_id(object))
|
2017-06-07 14:33:50 -04:00
|
|
|
end
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
end
|
2016-04-05 01:36:09 -04:00
|
|
|
|
|
|
|
def wiki_page_url
|
2017-06-29 13:06:35 -04:00
|
|
|
project_wiki_url(object.wiki.project, object.slug)
|
2016-04-05 01:36:09 -04:00
|
|
|
end
|
2014-06-13 06:34:15 -04:00
|
|
|
end
|
|
|
|
end
|