debian-mirror-gitlab/app/models/resource_label_event.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
2.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2020-04-08 14:13:33 +05:30
class ResourceLabelEvent < ResourceEvent
2018-11-20 20:47:30 +05:30
include CacheMarkdownField
2020-05-24 23:13:21 +05:30
include MergeRequestResourceEvent
2018-11-20 20:47:30 +05:30
cache_markdown_field :reference
2018-11-18 11:00:15 +05:30
belongs_to :label
2019-09-30 23:59:55 +05:30
scope :inc_relations, -> { includes(:label, :user) }
2018-11-20 20:47:30 +05:30
validates :label, presence: { unless: :importing? }, on: :create
2021-02-22 17:27:13 +05:30
validate :exactly_one_issuable, unless: :importing?
2018-11-18 11:00:15 +05:30
2018-11-20 20:47:30 +05:30
after_destroy :expire_etag_cache
2023-03-04 22:38:38 +05:30
after_save :expire_etag_cache
2018-11-20 20:47:30 +05:30
2018-11-18 11:00:15 +05:30
enum action: {
add: 1,
remove: 2
}
2018-11-20 20:47:30 +05:30
def self.issuable_attrs
%i(issue merge_request).freeze
2018-11-18 11:00:15 +05:30
end
2019-09-30 23:59:55 +05:30
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-11-18 11:00:15 +05:30
def issuable
issue || merge_request
end
2023-03-17 16:20:25 +05:30
def synthetic_note_class
LabelNote
end
2018-11-20 20:47:30 +05:30
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)
2022-04-04 11:22:00 +05:30
super.merge(pipeline: :label, only_path: true, label_url_method: label_url_method)
2018-11-20 20:47:30 +05:30
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-23 00:09:42 +05:30
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-11-18 11:00:15 +05:30
private
2018-11-20 20:47:30 +05:30
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
2022-04-04 11:22:00 +05:30
def label_url_method
issuable.is_a?(MergeRequest) ? :project_merge_requests_url : :project_issues_url
end
2018-11-20 20:47:30 +05:30
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-11-18 11:00:15 +05:30
end
2019-09-30 21:07:59 +05:30
def discussion_id_key
2022-11-25 23:54:43 +05:30
[self.class.name, created_at.to_f, user_id]
2019-09-30 21:07:59 +05:30
end
2018-11-18 11:00:15 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ResourceLabelEvent.prepend_mod_with('ResourceLabelEvent')