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

65 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
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
2018-03-17 18:26:18 +05:30
REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range directly_addressed_user epic).freeze
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
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
2017-08-17 22:00:37 +05:30
def reset_memoized_values
@references = {}
super()
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?
2017-09-10 17:25:29 +05:30
if project.issues_enabled?
@references[:all_issues] ||= references(:external_issue) + references(:issue)
else
@references[:external_issue] ||= references(:external_issue) +
references(:issue).select { |i| i.project_id != project.id }
end
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
2018-03-17 18:26:18 +05:30
REFERABLES.each { |referable| send(referable.to_s.pluralize) } # rubocop:disable GitlabSecurity/PublicSend
2016-06-02 11:05:42 +05:30
@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