Class: Banzai::Filter::CommitReferenceFilter

Inherits:
AbstractReferenceFilter show all
Defined in:
lib/banzai/filter/commit_reference_filter.rb

Overview

HTML filter that replaces commit references with links.

This filter supports cross-project references.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReferenceFilter

#call, data_reference, #find_object_cached, #object_class, object_class_title, #object_class_title, #object_link_filter, #object_link_text, object_name, #object_sym, object_sym, #project_from_ref_cache, #references_in, #url_for_object_cached

Methods included from CrossProjectReference

#project_from_ref

Methods inherited from ReferenceFilter

#data_attribute, #each_node, #element_node?, #escape_once, #ignore_ancestor_query, #project, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #text_node?, user_can_reference?, user_can_see_reference?, #validate, #yield_valid_link

Class Method Details

.find_object(project, id) ⇒ Object


29
30
31
32
33
# File 'lib/banzai/filter/commit_reference_filter.rb', line 29

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

.object_classObject


7
8
9
# File 'lib/banzai/filter/commit_reference_filter.rb', line 7

def self.object_class
  Commit
end

.referenced_by(node) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/banzai/filter/commit_reference_filter.rb', line 17

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

.references_in(text, pattern = Commit.reference_pattern) ⇒ Object


11
12
13
14
15
# File 'lib/banzai/filter/commit_reference_filter.rb', line 11

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

Instance Method Details

#find_object(*args) ⇒ Object


35
36
37
# File 'lib/banzai/filter/commit_reference_filter.rb', line 35

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

49
50
51
52
53
54
55
56
57
58
# File 'lib/banzai/filter/commit_reference_filter.rb', line 49

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

45
46
47
# File 'lib/banzai/filter/commit_reference_filter.rb', line 45

def object_link_title(commit)
  commit.link_title
end

#url_for_object(commit, project) ⇒ Object


39
40
41
42
43
# File 'lib/banzai/filter/commit_reference_filter.rb', line 39

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