BigW Consortium Gitlab

url_builder.rb 1.76 KB
Newer Older
1 2 3
module Gitlab
  class UrlBuilder
    include Rails.application.routes.url_helpers
4
    include GitlabRoutingHelper
5 6 7 8 9 10 11 12

    def initialize(type)
      @type = type
    end

    def build(id)
      case @type
      when :issue
13
        build_issue_url(id)
14
      when :merge_request
15
        build_merge_request_url(id)
16 17 18
      when :note
        build_note_url(id)

19 20 21 22 23
      end
    end

    private

24
    def build_issue_url(id)
25
      issue = Issue.find(id)
26
      issue_url(issue, host: Gitlab.config.gitlab['url'])
27
    end
28

29
    def build_merge_request_url(id)
30
      merge_request = MergeRequest.find(id)
31
      merge_request_url(merge_request, host: Gitlab.config.gitlab['url'])
32
    end
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    def build_note_url(id)
      note = Note.find(id)
      if note.for_commit?
        namespace_project_commit_url(namespace_id: note.project.namespace,
                                     id: note.commit_id,
                                     project_id: note.project,
                                     host: Gitlab.config.gitlab['url'],
                                     anchor: "note_#{note.id}")
      elsif note.for_issue?
        issue = Issue.find(note.noteable_id)
        issue_url(issue,
                  host: Gitlab.config.gitlab['url'],
                  anchor: "note_#{note.id}")
      elsif note.for_merge_request?
        merge_request = MergeRequest.find(note.noteable_id)
        merge_request_url(merge_request,
                          host: Gitlab.config.gitlab['url'],
                          anchor: "note_#{note.id}")
      elsif note.for_project_snippet?
        snippet = Snippet.find(note.noteable_id)
54 55 56
        project_snippet_url(snippet,
                            host: Gitlab.config.gitlab['url'],
                            anchor: "note_#{note.id}")
57 58
      end
    end
59 60
  end
end