2013-05-30 19:16:49 -04:00
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
|
|
|
class ReferenceExtractor
|
2015-03-27 06:38:22 -04:00
|
|
|
attr_accessor :project, :current_user, :references
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2015-03-27 06:38:22 -04:00
|
|
|
def initialize(project, current_user = nil)
|
2015-03-27 06:37:45 -04:00
|
|
|
@project = project
|
2015-03-27 07:58:23 -04:00
|
|
|
@current_user = current_user
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def analyze(text)
|
2015-04-15 16:01:00 -04:00
|
|
|
@_text = text.dup
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def users
|
2015-04-15 16:01:00 -04:00
|
|
|
result = pipeline_result(:user)
|
|
|
|
result[:references][:user].flatten.compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def labels
|
2015-04-15 16:01:00 -04:00
|
|
|
result = pipeline_result(:label)
|
|
|
|
result[:references][:label].compact.uniq
|
2015-02-07 06:14:55 -05:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def issues
|
2015-04-15 16:01:00 -04:00
|
|
|
# TODO (rspeicher): What about external issues?
|
|
|
|
|
|
|
|
result = pipeline_result(:issue)
|
|
|
|
result[:references][:issue].compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def merge_requests
|
2015-04-15 16:01:00 -04:00
|
|
|
result = pipeline_result(:merge_request)
|
|
|
|
result[:references][:merge_request].compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def snippets
|
2015-04-15 16:01:00 -04:00
|
|
|
result = pipeline_result(:snippet)
|
|
|
|
result[:references][:snippet].compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def commits
|
2015-04-15 16:01:00 -04:00
|
|
|
result = pipeline_result(:commit)
|
|
|
|
result[:references][:commit].compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def commit_ranges
|
2015-04-15 16:01:00 -04:00
|
|
|
result = pipeline_result(:commit_range)
|
|
|
|
result[:references][:commit_range].compact.uniq
|
2015-03-06 17:08:28 -05:00
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
private
|
|
|
|
|
2015-04-15 16:01:00 -04:00
|
|
|
# Instantiate and call HTML::Pipeline with a single reference filter type,
|
|
|
|
# returning the result
|
|
|
|
#
|
|
|
|
# filter_type - Symbol reference type (e.g., :commit, :issue, etc.)
|
|
|
|
#
|
|
|
|
# Returns the results Hash
|
|
|
|
def pipeline_result(filter_type)
|
|
|
|
klass = filter_type.to_s.camelize + 'ReferenceFilter'
|
|
|
|
filter = "Gitlab::Markdown::#{klass}".constantize
|
|
|
|
|
|
|
|
context = {
|
|
|
|
project: project,
|
|
|
|
current_user: current_user,
|
|
|
|
# We don't actually care about the links generated
|
|
|
|
only_path: true
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline = HTML::Pipeline.new([filter], context)
|
|
|
|
pipeline.call(@_text)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|