summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/references/merge_request_reference_filter.rb
blob: 6c5ad83d9ae810009d1ef9a6c69ef07e68389219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

module Banzai
  module Filter
    module References
      # HTML filter that replaces merge request references with links. References
      # to merge requests that do not exist are ignored.
      #
      # This filter supports cross-project references.
      class MergeRequestReferenceFilter < IssuableReferenceFilter
        self.reference_type = :merge_request
        self.object_class   = MergeRequest

        def url_for_object(mr, project)
          h = Gitlab::Routing.url_helpers
          h.project_merge_request_url(project, mr,
                                              only_path: context[:only_path])
        end

        def object_link_title(object, matches)
          # The method will return `nil` if object is not a commit
          # allowing for properly handling the extended MR Tooltip
          object_link_commit_title(object, matches)
        end

        def object_link_text_extras(object, matches)
          extras = super

          if commit_ref = object_link_commit_ref(object, matches)
            klass = reference_class(:commit, tooltip: false)
            commit_ref_tag = %(<span class="#{klass}">#{commit_ref}</span>)

            return extras.unshift(commit_ref_tag)
          end

          path = matches[:path] if matches.names.include?("path")

          case path
          when '/diffs'
            extras.unshift "diffs"
          when '/commits'
            extras.unshift "commits"
          when '/builds'
            extras.unshift "builds"
          end

          extras
        end

        def parent_records(parent, ids)
          parent.merge_requests
            .where(iid: ids.to_a)
            .includes(target_project: :namespace)
        end

        def reference_class(object_sym, options = {})
          super(object_sym, tooltip: false)
        end

        def data_attributes_for(text, parent, object, **data)
          super.merge(project_path: parent.full_path, iid: object.iid, mr_title: object.title)
        end

        private

        def object_link_commit_title(object, matches)
          object_link_commit(object, matches)&.title
        end

        def object_link_commit_ref(object, matches)
          object_link_commit(object, matches)&.short_id
        end

        def object_link_commit(object, matches)
          return unless matches.names.include?('query') && query = matches[:query]

          # Removes leading "?". CGI.parse expects "arg1&arg2&arg3"
          params = CGI.parse(query.sub(/^\?/, ''))

          return unless commit_sha = params['commit_id']&.first

          if commit = find_commit_by_sha(object, commit_sha)
            Commit.from_hash(commit.to_hash, object.project)
          end
        end

        def find_commit_by_sha(object, commit_sha)
          @all_commits ||= {}
          @all_commits[object.id] ||= object.all_commits

          @all_commits[object.id].find { |commit| commit.sha == commit_sha }
        end
      end
    end
  end
end