gitlab-org--gitlab-foss/lib/gitlab/reference_extractor.rb

54 lines
1.3 KiB
Ruby
Raw Normal View History

module Gitlab
# Extract possible GFM references from an arbitrary String for further processing.
class ReferenceExtractor < Banzai::ReferenceExtractor
REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range)
attr_accessor :project, :current_user, :author
def initialize(project, current_user = nil)
2015-03-27 10:37:45 +00:00
@project = project
2015-03-27 11:58:23 +00:00
@current_user = current_user
2015-10-22 13:40:36 +00:00
@references = {}
super()
end
def analyze(text, context = {})
super(text, context.merge(project: project))
end
def references(type)
super(type, project, current_user)
end
REFERABLES.each do |type|
define_method("#{type}s") do
@references[type] ||= references(type)
end
end
2015-12-17 22:08:14 +00:00
def issues
if project && project.jira_tracker?
@references[:external_issue] ||= references(:external_issue)
2015-12-17 22:08:14 +00:00
else
@references[:issue] ||= references(:issue)
2015-12-17 22:08:14 +00:00
end
end
def all
REFERABLES.each { |referable| send(referable.to_s.pluralize) }
@references.values.flatten
end
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
end
end