2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
2015-12-15 09:51:16 -05:00
|
|
|
class ReferenceExtractor < Banzai::ReferenceExtractor
|
2019-12-10 10:07:52 -05:00
|
|
|
REFERABLES = %i(user issue label milestone mentioned_user mentioned_group mentioned_project
|
2020-11-17 16:09:19 -05:00
|
|
|
merge_request snippet commit commit_range directly_addressed_user epic iteration vulnerability).freeze
|
2015-12-24 09:33:51 -05:00
|
|
|
attr_accessor :project, :current_user, :author
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2016-05-26 07:16:43 -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
|
2015-10-22 09:40:36 -04:00
|
|
|
@references = {}
|
2015-12-15 09:51:16 -05:00
|
|
|
|
|
|
|
super()
|
2015-10-14 15:29:35 -04:00
|
|
|
end
|
2015-10-14 13:27:23 -04:00
|
|
|
|
2015-12-15 09:51:16 -05:00
|
|
|
def analyze(text, context = {})
|
|
|
|
super(text, context.merge(project: project))
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2021-06-28 17:10:13 -04:00
|
|
|
def references(type, ids_only: false)
|
|
|
|
refs = super(type, project, current_user, ids_only: ids_only)
|
2021-08-26 14:10:35 -04:00
|
|
|
update_visible_nodes_set(refs[:nodes], refs[:visible_nodes])
|
2020-01-30 16:08:47 -05:00
|
|
|
|
|
|
|
refs[:visible]
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
2021-08-26 14:10:35 -04:00
|
|
|
# this method is stateful, it tracks if all nodes from `references`
|
|
|
|
# calls are visible or not
|
|
|
|
def all_visible?
|
|
|
|
not_visible_nodes.empty?
|
|
|
|
end
|
|
|
|
|
2016-12-05 07:12:22 -05:00
|
|
|
def reset_memoized_values
|
|
|
|
@references = {}
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2016-03-07 07:09:53 -05:00
|
|
|
REFERABLES.each do |type|
|
2020-11-17 16:09:19 -05:00
|
|
|
define_method(type.to_s.pluralize) do
|
2016-05-26 07:16:43 -04:00
|
|
|
@references[type] ||= references(type)
|
2015-06-02 07:17:11 -04:00
|
|
|
end
|
2021-06-28 17:10:13 -04:00
|
|
|
|
|
|
|
if %w(mentioned_user mentioned_group mentioned_project).include?(type.to_s)
|
|
|
|
define_method("#{type}_ids") do
|
|
|
|
@references[type] ||= references(type, ids_only: true)
|
|
|
|
end
|
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
|
|
|
|
def issues
|
2020-03-13 02:09:37 -04:00
|
|
|
if project&.external_references_supported?
|
2017-07-10 03:38:42 -04:00
|
|
|
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-17 17:08:14 -05:00
|
|
|
else
|
2016-05-26 07:16:43 -04:00
|
|
|
@references[:issue] ||= references(:issue)
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
end
|
2015-12-24 09:33:51 -05:00
|
|
|
|
2016-03-07 07:09:53 -05:00
|
|
|
def all
|
2017-08-03 22:20:34 -04:00
|
|
|
REFERABLES.each { |referable| send(referable.to_s.pluralize) } # rubocop:disable GitlabSecurity/PublicSend
|
2016-03-07 07:09:53 -05:00
|
|
|
@references.values.flatten
|
|
|
|
end
|
|
|
|
|
2016-03-18 09:48:55 -04:00
|
|
|
def self.references_pattern
|
|
|
|
return @pattern if @pattern
|
|
|
|
|
2019-10-17 08:07:33 -04:00
|
|
|
patterns = REFERABLES.map do |type|
|
|
|
|
Banzai::ReferenceParser[type].reference_type.to_s.classify.constantize.try(:reference_pattern)
|
|
|
|
end.uniq
|
2016-03-18 09:48:55 -04:00
|
|
|
|
|
|
|
@pattern = Regexp.union(patterns.compact)
|
|
|
|
end
|
2021-08-26 14:10:35 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_visible_nodes_set(all, visible)
|
|
|
|
not_visible_nodes.merge(all)
|
|
|
|
not_visible_nodes.subtract(visible)
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_visible_nodes
|
|
|
|
@not_visible_nodes ||= Set.new
|
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
end
|