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).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.


14
15
16
17
18
19
20
21
# File 'lib/gitlab/reference_extractor.rb', line 14

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

  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

#stateful_not_visible_counterObject (readonly)

This counter is increased by a number of references filtered out by banzai reference exctractor. Note that this counter is stateful and not idempotent and is increased whenever you call `references`.


12
13
14
# File 'lib/gitlab/reference_extractor.rb', line 12

def stateful_not_visible_counter
  @stateful_not_visible_counter
end

Class Method Details

.references_patternObject


64
65
66
67
68
69
70
71
72
# File 'lib/gitlab/reference_extractor.rb', line 64

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


59
60
61
62
# File 'lib/gitlab/reference_extractor.rb', line 59

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

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


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

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

#issuesObject


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitlab/reference_extractor.rb', line 46

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) ⇒ Object


27
28
29
30
31
32
# File 'lib/gitlab/reference_extractor.rb', line 27

def references(type)
  refs = super(type, project, current_user)
  @stateful_not_visible_counter += refs[:not_visible].count

  refs[:visible]
end

#reset_memoized_valuesObject


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

def reset_memoized_values
  @references = {}
  @stateful_not_visible_counter = 0
  super()
end