2013-05-30 23:16:49 +00:00
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
2015-12-15 14:51:16 +00:00
|
|
|
class ReferenceExtractor < Banzai::ReferenceExtractor
|
2016-03-07 12:09:53 +00:00
|
|
|
REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range)
|
2015-12-24 14:33:51 +00:00
|
|
|
attr_accessor :project, :current_user, :author
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
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
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-10-22 13:40:36 +00:00
|
|
|
@references = {}
|
2015-12-15 14:51:16 +00:00
|
|
|
|
|
|
|
super()
|
2015-10-14 19:29:35 +00:00
|
|
|
end
|
2015-10-14 17:27:23 +00:00
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
def analyze(text, context = {})
|
|
|
|
super(text, context.merge(project: project))
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
def references(type)
|
|
|
|
super(type, project, current_user)
|
|
|
|
end
|
|
|
|
|
2016-03-07 12:09:53 +00:00
|
|
|
REFERABLES.each do |type|
|
2015-06-02 11:17:11 +00:00
|
|
|
define_method("#{type}s") do
|
2016-05-26 11:16:43 +00:00
|
|
|
@references[type] ||= references(type)
|
2015-06-02 11:17:11 +00:00
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
2015-12-17 22:08:14 +00:00
|
|
|
|
|
|
|
def issues
|
|
|
|
if project && project.jira_tracker?
|
2016-05-26 11:16:43 +00:00
|
|
|
@references[:external_issue] ||= references(:external_issue)
|
2015-12-17 22:08:14 +00:00
|
|
|
else
|
2016-05-26 11:16:43 +00:00
|
|
|
@references[:issue] ||= references(:issue)
|
2015-12-17 22:08:14 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-24 14:33:51 +00:00
|
|
|
|
2016-03-07 12:09:53 +00:00
|
|
|
def all
|
|
|
|
REFERABLES.each { |referable| send(referable.to_s.pluralize) }
|
|
|
|
@references.values.flatten
|
|
|
|
end
|
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
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
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
end
|