url_builder.rb 1.86 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module Gitlab
  class UrlBuilder
5
    include Gitlab::Routing
6
    include GitlabRoutingHelper
7
    include ActionView::RecordIdentifier
8

9
    attr_reader :object
10

11 12 13
    def self.build(object)
      new(object).url
    end
14

15 16 17 18 19 20 21 22 23 24
    def url
      case object
      when Commit
        commit_url
      when Issue
        issue_url(object)
      when MergeRequest
        merge_request_url(object)
      when Note
        note_url
25 26
      when WikiPage
        wiki_page_url
27
      when ProjectSnippet
28
        project_snippet_url(object.project, object)
29
      when Snippet
30
        snippet_url(object)
31 32
      when Milestone
        milestone_url(object)
33 34
      when ::Ci::Build
        project_job_url(object.project, object)
35 36
      when User
        user_url(object)
37 38
      else
        raise NotImplementedError.new("No URL builder defined for #{object.class}")
39 40 41 42 43
      end
    end

    private

44 45
    def initialize(object)
      @object = object
46
    end
47

48 49 50 51 52 53 54 55
    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))
56
    end
57

58 59 60 61 62
    def note_url
      if object.for_commit?
        commit_url(id: object.commit_id, anchor: dom_id(object))

      elsif object.for_issue?
63
        issue_url(object.noteable, anchor: dom_id(object))
64 65

      elsif object.for_merge_request?
66
        merge_request_url(object.noteable, anchor: dom_id(object))
67 68

      elsif object.for_snippet?
69
        snippet = object.noteable
70 71 72 73

        if snippet.is_a?(PersonalSnippet)
          snippet_url(snippet, anchor: dom_id(object))
        else
74
          project_snippet_url(snippet.project, snippet, anchor: dom_id(object))
75
        end
76 77
      end
    end
78 79

    def wiki_page_url
80
      project_wiki_url(object.wiki.project, object.slug)
81
    end
82 83
  end
end