summaryrefslogtreecommitdiff
path: root/lib/gitlab/reference_extractor.rb
blob: 01aff48b08b48205452ab1e3c2f98680e94fded7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

module Gitlab
  # Extract possible GFM references from an arbitrary String for further processing.
  class ReferenceExtractor < Banzai::ReferenceExtractor
    REFERABLES = %i(user issue label milestone mentioned_user mentioned_group mentioned_project
                    merge_request snippet commit commit_range directly_addressed_user epic iteration).freeze
    attr_accessor :project, :current_user, :author
    # 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`.
    attr_reader :stateful_not_visible_counter

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

      super()
    end

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

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

      refs[:visible]
    end

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

    REFERABLES.each do |type|
      define_method("#{type}s") do
        @references[type] ||= references(type)
      end
    end

    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

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

    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
  end
end