Welcome to mirror list, hosted at ThFree Co, Russian Federation.

commit_reference_filter.rb « markdown « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc7abc08c8783ee70ae4afe61db488b378f80f70 (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
require 'gitlab/markdown'

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

      def self.references_in(text)
        text.gsub(Commit.reference_pattern) do |match|
          yield match, $~[:commit], $~[: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")
        commit = find_object(project, id)

        return unless commit

        { commit: commit }
      end

      def self.find_object(project, id)
        if project && project.valid_repo?
          project.commit(id)
        end
      end

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

      def url_for_object(commit, project)
        h = Gitlab::Application.routes.url_helpers
        h.namespace_project_commit_url(project.namespace, project, commit,
                                        only_path: context[:only_path])
      end

      def object_link_title(commit)
        commit.link_title
      end
    end
  end
end