2018-08-01 04:58:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
class ResourceLabelEvent < ResourceEvent
|
2018-09-07 09:39:20 -04:00
|
|
|
include CacheMarkdownField
|
2020-05-07 08:09:46 -04:00
|
|
|
include IssueResourceEvent
|
|
|
|
include MergeRequestResourceEvent
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
cache_markdown_field :reference
|
|
|
|
|
2018-08-01 04:58:49 -04:00
|
|
|
belongs_to :label
|
|
|
|
|
2019-09-17 08:38:09 -04:00
|
|
|
scope :inc_relations, -> { includes(:label, :user) }
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
validates :label, presence: { unless: :importing? }, on: :create
|
2020-12-16 10:10:18 -05:00
|
|
|
validate :exactly_one_issuable, unless: :importing?
|
2018-08-01 04:58:49 -04:00
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
after_save :expire_etag_cache
|
|
|
|
after_destroy :expire_etag_cache
|
|
|
|
|
2018-08-01 04:58:49 -04:00
|
|
|
enum action: {
|
|
|
|
add: 1,
|
|
|
|
remove: 2
|
|
|
|
}
|
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
def self.issuable_attrs
|
|
|
|
%i(issue merge_request).freeze
|
2018-08-01 04:58:49 -04:00
|
|
|
end
|
|
|
|
|
2019-09-17 08:38:09 -04:00
|
|
|
def self.preload_label_subjects(events)
|
|
|
|
labels = events.map(&:label).compact
|
|
|
|
project_labels, group_labels = labels.partition { |label| label.is_a? ProjectLabel }
|
|
|
|
|
|
|
|
preloader = ActiveRecord::Associations::Preloader.new
|
|
|
|
preloader.preload(project_labels, { project: :project_feature })
|
|
|
|
preloader.preload(group_labels, :group)
|
|
|
|
end
|
|
|
|
|
2018-08-01 04:58:49 -04:00
|
|
|
def issuable
|
|
|
|
issue || merge_request
|
|
|
|
end
|
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
def project
|
|
|
|
issuable.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def group
|
|
|
|
issuable.group if issuable.respond_to?(:group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def outdated_markdown?
|
|
|
|
return true if label_id.nil? && reference.present?
|
|
|
|
|
|
|
|
reference.nil? || latest_cached_markdown_version != cached_markdown_version
|
|
|
|
end
|
|
|
|
|
|
|
|
def banzai_render_context(field)
|
2020-04-21 11:21:10 -04:00
|
|
|
super.merge(pipeline: :label, only_path: true)
|
2018-09-07 09:39:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_invalid_reference
|
|
|
|
# label_id could be nullified on label delete
|
|
|
|
self.reference = '' if label_id.nil?
|
|
|
|
|
|
|
|
# reference is not set for events which were not rendered yet
|
|
|
|
self.reference ||= label_reference
|
|
|
|
|
|
|
|
if changed?
|
|
|
|
save
|
|
|
|
elsif invalidated_markdown_cache?
|
|
|
|
refresh_markdown_cache!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-10 05:08:35 -04:00
|
|
|
def self.visible_to_user?(user, events)
|
|
|
|
ResourceLabelEvent.preload_label_subjects(events)
|
|
|
|
|
|
|
|
events.select do |event|
|
|
|
|
Ability.allowed?(user, :read_label, event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-01 04:58:49 -04:00
|
|
|
private
|
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
def label_reference
|
|
|
|
if local_label?
|
|
|
|
label.to_reference(format: :id)
|
|
|
|
elsif label.is_a?(GroupLabel)
|
|
|
|
label.to_reference(label.group, target_project: resource_parent, format: :id)
|
|
|
|
else
|
|
|
|
label.to_reference(resource_parent, format: :id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expire_etag_cache
|
|
|
|
issuable.expire_note_etag_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def local_label?
|
|
|
|
params = { include_ancestor_groups: true }
|
|
|
|
if resource_parent.is_a?(Project)
|
|
|
|
params[:project_id] = resource_parent.id
|
|
|
|
else
|
|
|
|
params[:group_id] = resource_parent.id
|
|
|
|
end
|
|
|
|
|
|
|
|
LabelsFinder.new(nil, params).execute(skip_authorization: true).where(id: label.id).any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_parent
|
|
|
|
issuable.project || issuable.group
|
2018-08-01 04:58:49 -04:00
|
|
|
end
|
2020-03-04 04:08:20 -05:00
|
|
|
|
|
|
|
def discussion_id_key
|
|
|
|
[self.class.name, created_at, user_id]
|
|
|
|
end
|
2018-08-01 04:58:49 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
ResourceLabelEvent.prepend_mod_with('ResourceLabelEvent')
|