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

75 lines
2.2 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
2020-01-01 13:55:28 +05:30
REFERABLES = %i(user issue label milestone mentioned_user mentioned_group mentioned_project
2019-12-21 20:55:43 +05:30
merge_request snippet commit commit_range directly_addressed_user epic).freeze
attr_accessor :project, :current_user, :author
2020-02-01 01:16:34 +05:30
# This counter is increased by a number of references filtered out by
# banzai reference exctractor. Note that this counter is stateful and
# not idempotent and is increased whenever you call `references`.
attr_reader :stateful_not_visible_counter
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 = {}
2020-02-01 01:16:34 +05:30
@stateful_not_visible_counter = 0
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)
2020-02-01 01:16:34 +05:30
refs = super(type, project, current_user)
@stateful_not_visible_counter += refs[:not_visible].count
refs[:visible]
end
2017-08-17 22:00:37 +05:30
def reset_memoized_values
@references = {}
2020-02-01 01:16:34 +05:30
@stateful_not_visible_counter = 0
2017-08-17 22:00:37 +05:30
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
2020-04-08 14:13:33 +05:30
if project&.external_references_supported?
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
2019-12-21 20:55:43 +05:30
patterns = REFERABLES.map do |type|
Banzai::ReferenceParser[type].reference_type.to_s.classify.constantize.try(:reference_pattern)
end.uniq
2016-06-02 11:05:42 +05:30
@pattern = Regexp.union(patterns.compact)
end
2014-09-02 18:07:02 +05:30
end
end