Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/label.rb')
-rw-r--r--app/models/label.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/app/models/label.rb b/app/models/label.rb
index 0831ba40536..d0d278b68fd 100644
--- a/app/models/label.rb
+++ b/app/models/label.rb
@@ -25,8 +25,10 @@ class Label < ApplicationRecord
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
before_validation :strip_whitespace_from_title
+ before_destroy :prevent_locked_label_destroy, prepend: true
validates :color, color: true, presence: true
+ validate :ensure_lock_on_merge_allowed
# Don't allow ',' for label titles
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
@@ -42,6 +44,7 @@ class Label < ApplicationRecord
scope :templates, -> { where(template: true, type: [Label.name, nil]) }
scope :with_title, ->(title) { where(title: title) }
scope :with_lists_and_board, -> { joins(lists: :board).merge(List.movable) }
+ scope :with_lock_on_merge, -> { where(lock_on_merge: true) }
scope :on_project_boards, ->(project_id) { with_lists_and_board.where(boards: { project_id: project_id }) }
scope :on_board, ->(board_id) { with_lists_and_board.where(boards: { id: board_id }) }
scope :order_name_asc, -> { reorder(title: :asc) }
@@ -319,6 +322,20 @@ class Label < ApplicationRecord
def strip_whitespace_from_title
self[:title] = title&.strip
end
+
+ def prevent_locked_label_destroy
+ return unless lock_on_merge
+
+ errors.add(:base, format(_('%{label_name} is locked and was not removed'), label_name: name))
+ throw :abort # rubocop:disable Cop/BanCatchThrow
+ end
+
+ def ensure_lock_on_merge_allowed
+ return unless template?
+ return unless lock_on_merge || will_save_change_to_lock_on_merge?
+
+ errors.add(:lock_on_merge, _('can not be set for template labels'))
+ end
end
Label.prepend_mod_with('Label')