commit_reference_filter.rb 1.51 KB
Newer Older
1
require 'banzai'
2

3
4
module Banzai
  module Filter
5
    # HTML filter that replaces commit references with links.
Robert Speicher's avatar
Robert Speicher committed
6
7
    #
    # This filter supports cross-project references.
8
9
10
11
    class CommitReferenceFilter < AbstractReferenceFilter
      def self.object_class
        Commit
      end
Robert Speicher's avatar
Robert Speicher committed
12

13
14
      def self.references_in(text, pattern = Commit.reference_pattern)
        text.gsub(pattern) do |match|
15
          yield match, $~[:commit], $~[:project], $~
Robert Speicher's avatar
Robert Speicher committed
16
17
18
        end
      end

19
20
21
22
23
      def self.referenced_by(node)
        project = Project.find(node.attr("data-project")) rescue nil
        return unless project

        id = node.attr("data-commit")
24
        commit = find_object(project, id)
25
26
27
28
29
30

        return unless commit

        { commit: commit }
      end

31
      def self.find_object(project, id)
32
        if project && project.valid_repo?
33
          project.commit(id)
34
35
36
        end
      end

37
38
39
40
41
      def find_object(*args)
        self.class.find_object(*args)
      end

      def url_for_object(commit, project)
Douwe Maan's avatar
Douwe Maan committed
42
        h = Gitlab::Application.routes.url_helpers
Robert Speicher's avatar
Robert Speicher committed
43
44
45
        h.namespace_project_commit_url(project.namespace, project, commit,
                                        only_path: context[:only_path])
      end
46
47
48
49

      def object_link_title(commit)
        commit.link_title
      end
50
51
52
53
54
55
56
57
58
59
60

      def object_link_text_extras(object, matches)
        extras = super

        path = matches[:path] if matches.names.include?("path")
        if path == '/builds'
          extras.unshift "builds"
        end

        extras
      end
Robert Speicher's avatar
Robert Speicher committed
61
62
63
    end
  end
end