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 vulnerability
alert].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.



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

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.



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

def author
  @author
end

#current_userObject

Returns the value of attribute current_user.



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

def current_user
  @current_user
end

#projectObject

Returns the value of attribute project.



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

def project
  @project
end

Class Method Details

.references_patternObject



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

def references_pattern
  return @pattern if @pattern

  patterns = referrables.map do |type|
    Banzai::ReferenceParser[type].reference_class.try(:reference_pattern)
  end.uniq

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

.referrablesObject



82
83
84
# File 'lib/gitlab/reference_extractor.rb', line 82

def referrables
  @referrables ||= REFERABLES
end

Instance Method Details

#allObject



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

def all
  self.class.referrables.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)


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

def all_visible?
  not_visible_nodes.empty?
end

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



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

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

#issuesObject



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

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



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

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



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

def reset_memoized_values
  @references = {}
  super()
end