Class: Banzai::Filter::References::CommitReferenceFilter

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

Overview

HTML filter that replaces commit references with links.

This filter supports cross-project references.

Constant Summary

Constants included from Concerns::TimeoutFilterHandler

Concerns::TimeoutFilterHandler::COMPLEX_MARKDOWN_MESSAGE, Concerns::TimeoutFilterHandler::RENDER_TIMEOUT, Concerns::TimeoutFilterHandler::SANITIZATION_RENDER_TIMEOUT

Constants included from Concerns::PipelineTimingCheck

Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS

Constants inherited from ReferenceFilter

ReferenceFilter::REFERENCE_TYPE_ATTRIBUTE, ReferenceFilter::REFERENCE_TYPE_DATA_ATTRIBUTE_NAME

Constants included from Concerns::TextReplacer

Concerns::TextReplacer::REFERENCE_PLACEHOLDER, Concerns::TextReplacer::REFERENCE_PLACEHOLDER_PATTERN

Instance Method Summary collapse

Methods inherited from AbstractReferenceFilter

#call, #data_attributes_for, #find_object_cached, #find_object_from_link, #find_object_from_link_cached, #from_ref_cached, #identifier, #initialize, #object_link_content_html, #object_link_filter, #object_link_title, #parent, #parent_type, #record_identifier, #symbol_from_match_data, #url_for_object_cached, #wrap_link

Methods included from CrossProjectReference

#parent_from_ref

Methods included from Concerns::TimeoutFilterHandler

#call

Methods included from Concerns::PipelineTimingCheck

#call, #exceeded_pipeline_max?

Methods inherited from ReferenceFilter

call, #call, #call_and_update_nodes, #each_node, #group, #initialize, #nodes, #nodes?, #object_class, #project, #requires_unescaping?

Methods included from Concerns::TextReplacer

#replace_references_in_text_with_html

Methods included from Concerns::HtmlWriter

#write_opening_tag

Methods included from Concerns::OutputSafety

#escape_once

Methods included from RequestStoreReferenceCache

#cached_call, #get_or_set_cache

Constructor Details

This class inherits a constructor from Banzai::Filter::References::AbstractReferenceFilter

Instance Method Details

#find_object(project, id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/banzai/filter/references/commit_reference_filter.rb', line 21

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

  # Optimization: try exact commit hash match first
  record = reference_cache.records_per_parent[project].fetch(id, nil)

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

  record
end


65
66
67
68
69
70
71
72
73
74
# File 'lib/banzai/filter/references/commit_reference_filter.rb', line 65

def object_link_content_html_extras(object, matches)
  extras = super

  path = matches[:path] if matches.names.include?("path")
  if path == '/builds'
    extras.unshift CGI.escapeHTML("builds")
  end

  extras
end

#parent_records(parent, ids) ⇒ Object



76
77
78
79
80
# File 'lib/banzai/filter/references/commit_reference_filter.rb', line 76

def parent_records(parent, ids)
  return [] unless parent.respond_to?(:commits_by)

  parent.commits_by(oids: ids.to_a)
end

#parse_symbol(sha_hash, _match) ⇒ Object

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



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

def parse_symbol(sha_hash, _match)
  sha_hash
end

#referenced_merge_request_commit_shasObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/banzai/filter/references/commit_reference_filter.rb', line 34

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

  @referenced_merge_request_commit_shas ||= begin
    referenced_shas = reference_cache.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

#references_in(text, pattern = object_reference_pattern) ⇒ Object



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

def references_in(text, pattern = object_reference_pattern)
  replace_references_in_text_with_html(Gitlab::Utils::Gsub.gsub_with_limit(text, pattern,
    limit: Banzai::Filter::FILTER_ITEM_LIMIT)) do |match_data|
    yield match_data[0], match_data[:commit], match_data[:project], match_data[:namespace],
      match_data
  end
end

#url_for_object(commit, project) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/banzai/filter/references/commit_reference_filter.rb', line 50

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