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

78 lines
1.7 KiB
Ruby
Raw Normal View History

module Gitlab
# Extract possible GFM references from an arbitrary String for further processing.
class ReferenceExtractor
attr_accessor :project, :current_user, :references
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
end
2015-03-27 10:37:45 +00:00
def analyze(text)
@_text = text.dup
end
2015-03-27 10:37:45 +00:00
def users
result = pipeline_result(:user)
2015-04-23 18:02:07 +00:00
result.uniq
end
2015-03-27 10:37:45 +00:00
def labels
result = pipeline_result(:label)
2015-04-23 18:02:07 +00:00
result.uniq
2015-02-07 11:14:55 +00:00
end
2015-03-27 10:37:45 +00:00
def issues
# TODO (rspeicher): What about external issues?
result = pipeline_result(:issue)
2015-04-23 18:02:07 +00:00
result.uniq
end
2015-03-27 10:37:45 +00:00
def merge_requests
result = pipeline_result(:merge_request)
2015-04-23 18:02:07 +00:00
result.uniq
end
2015-03-27 10:37:45 +00:00
def snippets
result = pipeline_result(:snippet)
2015-04-23 18:02:07 +00:00
result.uniq
end
2015-03-27 10:37:45 +00:00
def commits
result = pipeline_result(:commit)
2015-04-23 18:02:07 +00:00
result.uniq
end
2015-03-27 10:37:45 +00:00
def commit_ranges
result = pipeline_result(:commit_range)
2015-04-23 18:02:07 +00:00
result.uniq
end
private
# Instantiate and call HTML::Pipeline with a single reference filter type,
# returning the result
#
# filter_type - Symbol reference type (e.g., :commit, :issue, etc.)
#
2015-04-23 18:02:07 +00:00
# Returns the results Array for the requested filter type
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)
2015-04-23 18:02:07 +00:00
result = pipeline.call(@_text)
result[:references][filter_type]
end
end
end