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

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

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

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

        return unless commit

        { commit: commit }
      end

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

35
36
37
38
39
      def find_object(*args)
        self.class.find_object(*args)
      end

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

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

      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
59
60
61
    end
  end
end