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.

Constant Summary

Constants inherited from AbstractReferenceFilter

AbstractReferenceFilter::REFERENCE_PLACEHOLDER, AbstractReferenceFilter::REFERENCE_PLACEHOLDER_PATTERN

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReferenceFilter

#call, #current_parent_path, #current_project_namespace_path, #data_attributes_for, #find_for_paths, #find_object_cached, #find_object_from_link, #find_object_from_link_cached, #from_ref_cached, identifier, #identifier, #object_class, #object_link_filter, #object_link_text, #object_link_title, object_name, #object_sym, object_sym, #parent_per_reference, #record_identifier, #records_per_parent, #references_in, #references_per_parent, #relation_for_paths, symbol_from_match, #url_for_object_cached, #wrap_link

Methods included from CrossProjectReference

#parent_from_ref

Methods inherited from ReferenceFilter

call, #call_and_update_nodes, #data_attribute, #each_node, #element_node?, #group, #ignore_ancestor_query, #initialize, #nodes, #project, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #skip_project_check?, #text_node?, #user, #validate, #yield_valid_link

Methods included from OutputSafety

#escape_once

Methods included from RequestStoreReferenceCache

#cached_call, #get_or_set_cache

Constructor Details

This class inherits a constructor from Banzai::Filter::ReferenceFilter

Class Method Details

.object_classObject


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

def self.object_class
  Commit
end

.parse_symbol(sha_hash, _match) ⇒ Object

The default behaviour is `#to_i` - we just pass the hash through.


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

def self.parse_symbol(sha_hash, _match)
  sha_hash
end

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


15
16
17
18
19
# File 'lib/banzai/filter/commit_reference_filter.rb', line 15

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

Instance Method Details

#find_object(project, id) ⇒ Object


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

def find_object(project, id)
  return unless project.is_a?(Project) && project.valid_repo?

  _, record = records_per_parent[project].detect { |k, _v| Gitlab::Git.shas_eql?(k, id) }

  record
end

60
61
62
63
64
65
66
67
68
69
# File 'lib/banzai/filter/commit_reference_filter.rb', line 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

#referenced_merge_request_commit_shasObject


29
30
31
32
33
34
35
36
37
38
# File 'lib/banzai/filter/commit_reference_filter.rb', line 29

def referenced_merge_request_commit_shas
  return [] unless noteable.is_a?(MergeRequest)

  @referenced_merge_request_commit_shas ||= begin
    referenced_shas = references_per_parent.values.reduce(:|).to_a
    noteable.all_commit_shas.select do |sha|
      referenced_shas.any? { |ref| Gitlab::Git.shas_eql?(sha, ref) }
    end
  end
end

#url_for_object(commit, project) ⇒ Object


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

def url_for_object(commit, project)
  h = Gitlab::Routing.url_helpers

  if referenced_merge_request_commit_shas.include?(commit.id)
    h.diffs_project_merge_request_url(project,
                                      noteable,
                                      commit_id: commit.id,
                                      only_path: only_path?)
  else
    h.project_commit_url(project,
                         commit,
                         only_path: only_path?)
  end
end