summaryrefslogtreecommitdiff
path: root/lib/gitlab/markdown/filter/commit_range_reference_filter.rb
blob: 36b3258ef761077974e1d070684bae07d5bf6dd6 (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
require 'gitlab/markdown'

module Gitlab
  module Markdown
    # HTML filter that replaces commit range references with links.
    #
    # This filter supports cross-project references.
    class CommitRangeReferenceFilter < AbstractReferenceFilter
      def self.object_class
        CommitRange
      end

      def self.references_in(text, pattern = CommitRange.reference_pattern)
        text.gsub(pattern) do |match|
          yield match, $~[:commit_range], $~[:project], $~
        end
      end

      def self.referenced_by(node)
        project = Project.find(node.attr("data-project")) rescue nil
        return unless project

        id = node.attr("data-commit-range")
        range = find_object(project, id)

        return unless range

        { commit_range: range }
      end

      def initialize(*args)
        super

        @commit_map = {}
      end

      def self.find_object(project, id)
        range = CommitRange.new(id, project)

        range.valid_commits? ? range : nil
      end

      def find_object(*args)
        self.class.find_object(*args)
      end

      def url_for_object(range, project)
        h = Gitlab::Application.routes.url_helpers
        h.namespace_project_compare_url(project.namespace, project,
                                        range.to_param.merge(only_path: context[:only_path]))
      end

      def object_link_title(range)
        range.reference_title
      end
    end
  end
end