BigW Consortium Gitlab

reference_extractor.rb 1.3 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)
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
10

Douwe Maan committed
11
      @references = {}
12 13

      super()
14
    end
15

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

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

24
    REFERABLES.each do |type|
25
      define_method("#{type}s") do
26
        @references[type] ||= references(type)
27
      end
28
    end
Drew Blessing committed
29 30 31

    def issues
      if project && project.jira_tracker?
32
        @references[:external_issue] ||= references(:external_issue)
Drew Blessing committed
33
      else
34
        @references[:issue] ||= references(:issue)
Drew Blessing committed
35 36
      end
    end
37

38 39 40 41 42
    def all
      REFERABLES.each { |referable| send(referable.to_s.pluralize) }
      @references.values.flatten
    end

43 44 45 46 47 48 49 50 51
    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
52 53
  end
end