Read about our upcoming Code of Conduct on this issue

commit_reference_filter.rb 1.1 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
    class CommitReferenceFilter < AbstractReferenceFilter
7
8
      self.reference_type = :commit

9
10
11
      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], $~[:namespace], $~
Robert Speicher's avatar
Robert Speicher committed
16
17
18
        end
      end

19
      def find_object(project, id)
20
        if project && project.valid_repo?
21
          project.commit(id)
22
23
24
        end
      end

25
      def url_for_object(commit, project)
26
        h = Gitlab::Routing.url_helpers
Robert Speicher's avatar
Robert Speicher committed
27
28
29
        h.namespace_project_commit_url(project.namespace, project, commit,
                                        only_path: context[:only_path])
      end
30

31
32
33
34
35
36
37
38
39
40
      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
41
42
43
    end
  end
end