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
|
2023-06-20 00:43:36 +05:30
|
|
|
merge_request snippet commit commit_range directly_addressed_user epic vulnerability
|
2023-03-04 22:38:38 +05:30
|
|
|
alert).freeze
|
2016-01-14 18:37:52 +05:30
|
|
|
attr_accessor :project, :current_user, :author
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-16 23:09:34 +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
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def references(type, ids_only: false)
|
|
|
|
refs = super(type, project, current_user, ids_only: ids_only)
|
2021-11-11 11:23:49 +05:30
|
|
|
update_visible_nodes_set(refs[:nodes], refs[:visible_nodes])
|
2020-02-01 01:16:34 +05:30
|
|
|
|
|
|
|
refs[:visible]
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
# this method is stateful, it tracks if all nodes from `references`
|
|
|
|
# calls are visible or not
|
|
|
|
def all_visible?
|
|
|
|
not_visible_nodes.empty?
|
|
|
|
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|
|
2021-01-29 00:20:46 +05:30
|
|
|
define_method(type.to_s.pluralize) do
|
2016-06-16 23:09:34 +05:30
|
|
|
@references[type] ||= references(type)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
next unless %w(mentioned_user mentioned_group mentioned_project).include?(type.to_s)
|
|
|
|
|
|
|
|
define_method("#{type}_ids") do
|
|
|
|
@references[type] ||= references(type, ids_only: true)
|
2021-09-30 23:02:18 +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
|
2016-06-16 23:09:34 +05:30
|
|
|
@references[:issue] ||= references(:issue)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def all
|
2023-06-20 00:43:36 +05:30
|
|
|
self.class.referrables.each { |referable| send(referable.to_s.pluralize) } # rubocop:disable GitlabSecurity/PublicSend
|
2016-06-02 11:05:42 +05:30
|
|
|
@references.values.flatten
|
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
class << self
|
|
|
|
def references_pattern
|
|
|
|
return @pattern if @pattern
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
patterns = referrables.map do |type|
|
|
|
|
Banzai::ReferenceParser[type].reference_class.try(:reference_pattern)
|
|
|
|
end.uniq
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
@pattern = Regexp.union(patterns.compact)
|
|
|
|
end
|
|
|
|
|
|
|
|
def referrables
|
|
|
|
@referrables ||= REFERABLES
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
Gitlab::ReferenceExtractor.prepend_mod
|