BigW Consortium Gitlab

reference_extractor.rb 1.4 KB
Newer Older
1 2
module Gitlab
  # Extract possible GFM references from an arbitrary String for further processing.
3
  class ReferenceExtractor < Banzai::ReferenceExtractor
4
    REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range directly_addressed_user).freeze
5
    attr_accessor :project, :current_user, :author
6

7
    def initialize(project, current_user = nil)
8
      @project = project
Douwe Maan committed
9
      @current_user = current_user
Douwe Maan committed
10
      @references = {}
11 12

      super()
13
    end
14

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

19 20 21 22
    def references(type)
      super(type, project, current_user)
    end

23 24 25 26 27
    def reset_memoized_values
      @references = {}
      super()
    end

28
    REFERABLES.each do |type|
29
      define_method("#{type}s") do
30
        @references[type] ||= references(type)
31
      end
32
    end
Drew Blessing committed
33 34 35

    def issues
      if project && project.jira_tracker?
36
        @references[:external_issue] ||= references(:external_issue)
Drew Blessing committed
37
      else
38
        @references[:issue] ||= references(:issue)
Drew Blessing committed
39 40
      end
    end
41

42 43 44 45 46
    def all
      REFERABLES.each { |referable| send(referable.to_s.pluralize) }
      @references.values.flatten
    end

47 48 49 50 51 52 53 54 55
    def self.references_pattern
      return @pattern if @pattern

      patterns = REFERABLES.map do |ref|
        ref.to_s.classify.constantize.try(:reference_pattern)
      end

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