debian-mirror-gitlab/lib/gitlab/reference_extractor.rb

54 lines
1.3 KiB
Ruby
Raw Normal View History

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)
attr_accessor :project, :current_user, :author
2014-09-02 18:07:02 +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
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
@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?
@references[:external_issue] ||= references(:external_issue)
2015-12-23 02:04:40 +05:30
else
@references[:issue] ||= references(:issue)
2015-10-24 18:46:33 +05:30
end
2014-09-02 18:07:02 +05:30
end
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