Class: Gitlab::ReferenceExtractor

Inherits:
Banzai::ReferenceExtractor show all
Defined in:
lib/gitlab/reference_extractor.rb

Overview

Extract possible GFM references from an arbitrary String for further processing.

Constant Summary collapse

REFERABLES =
%i(user issue label milestone mentioned_user mentioned_group mentioned_project
merge_request snippet commit commit_range directly_addressed_user epic iteration vulnerability).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, current_user = nil) ⇒ ReferenceExtractor

Returns a new instance of ReferenceExtractor.


10
11
12
13
14
15
16
# File 'lib/gitlab/reference_extractor.rb', line 10

def initialize(project, current_user = nil)
  @project = project
  @current_user = current_user
  @references = {}

  super()
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.


8
9
10
# File 'lib/gitlab/reference_extractor.rb', line 8

def author
  @author
end

#current_userObject

Returns the value of attribute current_user.


8
9
10
# File 'lib/gitlab/reference_extractor.rb', line 8

def current_user
  @current_user
end

#projectObject

Returns the value of attribute project.


8
9
10
# File 'lib/gitlab/reference_extractor.rb', line 8

def project
  @project
end

Class Method Details

.references_patternObject


70
71
72
73
74
75
76
77
78
# File 'lib/gitlab/reference_extractor.rb', line 70

def self.references_pattern
  return @pattern if @pattern

  patterns = REFERABLES.map do |type|
    Banzai::ReferenceParser[type].reference_type.to_s.classify.constantize.try(:reference_pattern)
  end.uniq

  @pattern = Regexp.union(patterns.compact)
end

Instance Method Details

#allObject


65
66
67
68
# File 'lib/gitlab/reference_extractor.rb', line 65

def all
  REFERABLES.each { |referable| send(referable.to_s.pluralize) } # rubocop:disable GitlabSecurity/PublicSend
  @references.values.flatten
end

#all_visible?Boolean

this method is stateful, it tracks if all nodes from `references` calls are visible or not

Returns:

  • (Boolean)

31
32
33
# File 'lib/gitlab/reference_extractor.rb', line 31

def all_visible?
  not_visible_nodes.empty?
end

#analyze(text, context = {}) ⇒ Object


18
19
20
# File 'lib/gitlab/reference_extractor.rb', line 18

def analyze(text, context = {})
  super(text, context.merge(project: project))
end

#issuesObject


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab/reference_extractor.rb', line 52

def issues
  if project&.external_references_supported?
    if project.issues_enabled?
      @references[:all_issues] ||= references(:external_issue) + references(:issue)
    else
      @references[:external_issue] ||= references(:external_issue) +
        references(:issue).select { |i| i.project_id != project.id }
    end
  else
    @references[:issue] ||= references(:issue)
  end
end

#references(type, ids_only: false) ⇒ Object


22
23
24
25
26
27
# File 'lib/gitlab/reference_extractor.rb', line 22

def references(type, ids_only: false)
  refs = super(type, project, current_user, ids_only: ids_only)
  update_visible_nodes_set(refs[:nodes], refs[:visible_nodes])

  refs[:visible]
end

#reset_memoized_valuesObject


35
36
37
38
# File 'lib/gitlab/reference_extractor.rb', line 35

def reset_memoized_values
  @references = {}
  super()
end