2014-09-02 18:07:02 +05:30
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
2015-12-23 02:04:40 +05:30
|
|
|
class ReferenceExtractor < Banzai::ReferenceExtractor
|
2016-06-02 11:05:42 +05:30
|
|
|
REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range)
|
2016-01-14 18:37:52 +05:30
|
|
|
attr_accessor :project, :current_user, :author
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def initialize(project, current_user = nil)
|
2015-04-26 12:48:37 +05:30
|
|
|
@project = project
|
|
|
|
@current_user = current_user
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
@references = {}
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
super()
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def analyze(text, context = {})
|
|
|
|
super(text, context.merge(project: project))
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def references(type)
|
|
|
|
super(type, project, current_user)
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
REFERABLES.each do |type|
|
2015-12-23 02:04:40 +05:30
|
|
|
define_method("#{type}s") do
|
2016-06-16 23:09:34 +05:30
|
|
|
@references[type] ||= references(type)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def issues
|
|
|
|
if project && project.jira_tracker?
|
2016-06-16 23:09:34 +05:30
|
|
|
@references[:external_issue] ||= references(:external_issue)
|
2015-12-23 02:04:40 +05:30
|
|
|
else
|
2016-06-16 23:09:34 +05:30
|
|
|
@references[:issue] ||= references(:issue)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|