2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Label < ApplicationRecord
|
2016-10-06 17:17:11 -04:00
|
|
|
include CacheMarkdownField
|
2015-05-02 23:11:21 -04:00
|
|
|
include Referable
|
2016-02-12 09:58:39 -05:00
|
|
|
include Subscribable
|
2018-07-20 10:19:58 -04:00
|
|
|
include Gitlab::SQL::Pattern
|
2018-09-07 10:16:26 -04:00
|
|
|
include OptionallySearch
|
2018-09-10 07:38:29 -04:00
|
|
|
include Sortable
|
2018-09-11 11:31:34 -04:00
|
|
|
include FromUnion
|
2019-04-23 15:58:20 -04:00
|
|
|
include Presentable
|
2016-02-12 09:58:39 -05:00
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
cache_markdown_field :description, pipeline: :single_line
|
|
|
|
|
2018-10-29 05:50:18 -04:00
|
|
|
DEFAULT_COLOR = '#428BCA'
|
2014-08-14 04:17:52 -04:00
|
|
|
|
2015-04-17 11:38:31 -04:00
|
|
|
default_value_for :color, DEFAULT_COLOR
|
|
|
|
|
2017-06-08 11:16:27 -04:00
|
|
|
has_many :lists, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2016-10-14 15:12:07 -04:00
|
|
|
has_many :priorities, class_name: 'LabelPriority'
|
2017-06-08 11:16:27 -04:00
|
|
|
has_many :label_links, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2018-04-18 09:41:42 -04:00
|
|
|
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
|
|
|
|
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
|
2014-07-29 10:10:15 -04:00
|
|
|
|
2017-04-11 17:56:33 -04:00
|
|
|
before_validation :strip_whitespace_from_title_and_color
|
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :color, color: true, allow_blank: false
|
2014-07-30 06:26:54 -04:00
|
|
|
|
2016-06-16 19:09:13 -04:00
|
|
|
# Don't allow ',' for label titles
|
2016-09-19 23:17:04 -04:00
|
|
|
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
|
2016-09-21 16:47:58 -04:00
|
|
|
validates :title, uniqueness: { scope: [:group_id, :project_id] }
|
2016-08-11 03:37:09 -04:00
|
|
|
validates :title, length: { maximum: 255 }
|
2014-07-29 12:19:26 -04:00
|
|
|
|
2020-06-10 08:08:58 -04:00
|
|
|
default_scope { order(title: :asc) } # rubocop:disable Cop/DefaultScope
|
2014-07-30 12:13:35 -04:00
|
|
|
|
2019-07-24 02:53:29 -04:00
|
|
|
scope :templates, -> { where(template: true, type: [Label.name, nil]) }
|
2016-09-21 16:47:58 -04:00
|
|
|
scope :with_title, ->(title) { where(title: title) }
|
2017-08-28 17:56:49 -04:00
|
|
|
scope :with_lists_and_board, -> { joins(lists: :board).merge(List.movable) }
|
|
|
|
scope :on_project_boards, ->(project_id) { with_lists_and_board.where(boards: { project_id: project_id }) }
|
2018-11-01 20:10:53 -04:00
|
|
|
scope :on_board, ->(board_id) { with_lists_and_board.where(boards: { id: board_id }) }
|
2018-09-10 07:38:29 -04:00
|
|
|
scope :order_name_asc, -> { reorder(title: :asc) }
|
|
|
|
scope :order_name_desc, -> { reorder(title: :desc) }
|
2018-09-27 10:19:28 -04:00
|
|
|
scope :subscribed_by, ->(user_id) { joins(:subscriptions).where(subscriptions: { user_id: user_id, subscribed: true }) }
|
2016-05-13 11:26:18 -04:00
|
|
|
|
2020-02-14 19:08:48 -05:00
|
|
|
scope :top_labels_by_target, -> (target_relation) {
|
|
|
|
label_id_column = arel_table[:id]
|
|
|
|
|
|
|
|
# Window aggregation to count labels
|
|
|
|
count_by_id = Arel::Nodes::Over.new(
|
|
|
|
Arel::Nodes::NamedFunction.new('count', [label_id_column]),
|
|
|
|
Arel::Nodes::Window.new.partition(label_id_column)
|
|
|
|
).as('count_by_id')
|
|
|
|
|
|
|
|
select(arel_table[Arel.star], count_by_id)
|
|
|
|
.joins(:label_links)
|
|
|
|
.merge(LabelLink.where(target: target_relation))
|
|
|
|
.reorder(count_by_id: :desc)
|
|
|
|
.distinct
|
|
|
|
}
|
|
|
|
|
2016-10-14 17:32:44 -04:00
|
|
|
def self.prioritized(project)
|
2017-02-22 17:54:59 -05:00
|
|
|
joins(:priorities)
|
|
|
|
.where(label_priorities: { project_id: project })
|
|
|
|
.reorder('label_priorities.priority ASC, labels.title ASC')
|
2016-06-02 15:57:31 -04:00
|
|
|
end
|
2016-05-13 11:26:18 -04:00
|
|
|
|
2016-10-14 17:32:44 -04:00
|
|
|
def self.unprioritized(project)
|
2016-10-18 02:00:33 -04:00
|
|
|
labels = Label.arel_table
|
|
|
|
priorities = LabelPriority.arel_table
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
|
|
|
|
.on(labels[:id].eq(priorities[:label_id]).and(priorities[:project_id].eq(project.id)))
|
|
|
|
.join_sources
|
2016-10-18 02:00:33 -04:00
|
|
|
|
|
|
|
joins(label_priorities).where(priorities[:priority].eq(nil))
|
2016-03-14 05:46:26 -04:00
|
|
|
end
|
|
|
|
|
2016-10-14 19:06:26 -04:00
|
|
|
def self.left_join_priorities
|
|
|
|
labels = Label.arel_table
|
|
|
|
priorities = LabelPriority.arel_table
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
|
|
|
|
.on(labels[:id].eq(priorities[:label_id]))
|
|
|
|
.join_sources
|
2016-10-14 19:06:26 -04:00
|
|
|
|
|
|
|
joins(label_priorities)
|
|
|
|
end
|
|
|
|
|
2018-09-27 10:19:28 -04:00
|
|
|
def self.optionally_subscribed_by(user_id)
|
|
|
|
if user_id
|
|
|
|
subscribed_by(user_id)
|
|
|
|
else
|
|
|
|
all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-12 08:16:25 -04:00
|
|
|
alias_attribute :name, :title
|
2014-07-30 08:15:39 -04:00
|
|
|
|
2015-05-02 23:11:21 -04:00
|
|
|
def self.reference_prefix
|
|
|
|
'~'
|
|
|
|
end
|
|
|
|
|
2016-03-01 05:39:49 -05:00
|
|
|
##
|
2015-05-14 16:59:39 -04:00
|
|
|
# Pattern used to extract label references from text
|
2016-03-01 05:39:49 -05:00
|
|
|
#
|
|
|
|
# This pattern supports cross-project references.
|
|
|
|
#
|
2015-05-14 16:59:39 -04:00
|
|
|
def self.reference_pattern
|
2016-07-12 20:33:25 -04:00
|
|
|
# NOTE: The id pattern only matches when all characters on the expression
|
|
|
|
# are digits, so it will match ~2 but not ~2fa because that's probably a
|
|
|
|
# label name and we want it to be matched as such.
|
2016-03-24 11:41:48 -04:00
|
|
|
@reference_pattern ||= %r{
|
2016-02-25 07:49:42 -05:00
|
|
|
(#{Project.reference_pattern})?
|
|
|
|
#{Regexp.escape(reference_prefix)}
|
2015-05-14 16:59:39 -04:00
|
|
|
(?:
|
2018-06-18 11:47:20 -04:00
|
|
|
(?<label_id>\d+(?!\S\w)\b)
|
|
|
|
| # Integer-based label ID, or
|
|
|
|
(?<label_name>
|
|
|
|
# String-based single-word label title, or
|
|
|
|
[A-Za-z0-9_\-\?\.&]+
|
|
|
|
(?<!\.|\?)
|
|
|
|
|
|
|
|
|
# String-based multi-word label surrounded in quotes
|
|
|
|
".+?"
|
|
|
|
)
|
2015-05-14 16:59:39 -04:00
|
|
|
)
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
2016-02-25 07:49:42 -05:00
|
|
|
def self.link_reference_pattern
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-07-20 10:19:58 -04:00
|
|
|
# Searches for labels with a matching title or description.
|
|
|
|
#
|
2020-06-15 23:08:24 -04:00
|
|
|
# This method uses ILIKE on PostgreSQL.
|
2018-07-20 10:19:58 -04:00
|
|
|
#
|
|
|
|
# query - The search query as a String.
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
2020-03-24 11:08:44 -04:00
|
|
|
def self.search(query, **options)
|
2018-07-20 10:19:58 -04:00
|
|
|
fuzzy_search(query, [:title, :description])
|
|
|
|
end
|
|
|
|
|
2019-03-12 04:13:21 -04:00
|
|
|
# Override Gitlab::SQL::Pattern.min_chars_for_partial_matching as
|
|
|
|
# label queries are never global, and so will not use a trigram
|
|
|
|
# index. That means we can have just one character in the LIKE.
|
|
|
|
def self.min_chars_for_partial_matching
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
2019-07-29 18:35:29 -04:00
|
|
|
def self.on_project_board?(project_id, label_id)
|
2019-08-12 20:40:39 -04:00
|
|
|
return false if label_id.blank?
|
|
|
|
|
2019-07-29 18:35:29 -04:00
|
|
|
on_project_boards(project_id).where(id: label_id).exists?
|
|
|
|
end
|
|
|
|
|
2020-04-01 05:07:45 -04:00
|
|
|
# Generate a hex color based on hex-encoded value
|
|
|
|
def self.color_for(value)
|
|
|
|
"##{Digest::MD5.hexdigest(value)[0..5]}"
|
|
|
|
end
|
|
|
|
|
2016-10-27 22:43:31 -04:00
|
|
|
def open_issues_count(user = nil)
|
|
|
|
issues_count(user, state: 'opened')
|
2014-07-30 08:15:39 -04:00
|
|
|
end
|
2015-09-03 08:50:23 -04:00
|
|
|
|
2016-10-27 22:43:31 -04:00
|
|
|
def closed_issues_count(user = nil)
|
|
|
|
issues_count(user, state: 'closed')
|
2016-01-25 12:07:04 -05:00
|
|
|
end
|
|
|
|
|
2016-10-27 22:43:31 -04:00
|
|
|
def open_merge_requests_count(user = nil)
|
|
|
|
params = {
|
|
|
|
subject_foreign_key => subject.id,
|
|
|
|
label_name: title,
|
|
|
|
scope: 'all',
|
|
|
|
state: 'opened'
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeRequestsFinder.new(user, params.with_indifferent_access).execute.count
|
2016-03-01 21:04:34 -05:00
|
|
|
end
|
|
|
|
|
2016-10-17 14:34:22 -04:00
|
|
|
def prioritize!(project, value)
|
|
|
|
label_priority = priorities.find_or_initialize_by(project_id: project.id)
|
|
|
|
label_priority.priority = value
|
|
|
|
label_priority.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def unprioritize!(project)
|
|
|
|
priorities.where(project: project).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def priority(project)
|
2017-09-12 15:49:01 -04:00
|
|
|
priority = if priorities.loaded?
|
|
|
|
priorities.first { |p| p.project == project }
|
|
|
|
else
|
|
|
|
priorities.find_by(project: project)
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-09-12 15:49:01 -04:00
|
|
|
priority.try(:priority)
|
2016-10-17 14:34:22 -04:00
|
|
|
end
|
|
|
|
|
2018-04-18 20:20:38 -04:00
|
|
|
def priority?
|
|
|
|
priorities.present?
|
|
|
|
end
|
|
|
|
|
2017-05-24 08:37:55 -04:00
|
|
|
def color
|
|
|
|
super || DEFAULT_COLOR
|
|
|
|
end
|
|
|
|
|
2016-04-20 12:00:12 -04:00
|
|
|
def text_color
|
2016-10-13 18:38:22 -04:00
|
|
|
LabelsHelper.text_color_for_bg(self.color)
|
2016-04-20 12:00:12 -04:00
|
|
|
end
|
|
|
|
|
2016-05-09 10:58:20 -04:00
|
|
|
def title=(value)
|
2019-07-15 07:29:56 -04:00
|
|
|
write_attribute(:title, sanitize_value(value)) if value.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def description=(value)
|
|
|
|
write_attribute(:description, sanitize_value(value)) if value.present?
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
|
|
|
|
2016-10-11 15:51:11 -04:00
|
|
|
##
|
|
|
|
# Returns the String necessary to reference this Label in Markdown
|
|
|
|
#
|
|
|
|
# format - Symbol format to use (default: :id, optional: :name)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2016-11-02 19:49:13 -04:00
|
|
|
# Label.first.to_reference # => "~1"
|
|
|
|
# Label.first.to_reference(format: :name) # => "~\"bug\""
|
2019-09-18 10:02:45 -04:00
|
|
|
# Label.first.to_reference(project, target_project: same_namespace_project) # => "gitlab-foss~1"
|
|
|
|
# Label.first.to_reference(project, target_project: another_namespace_project) # => "gitlab-org/gitlab-foss~1"
|
2016-10-11 15:51:11 -04:00
|
|
|
#
|
|
|
|
# Returns a String
|
|
|
|
#
|
2017-11-22 08:20:35 -05:00
|
|
|
def to_reference(from = nil, target_project: nil, format: :id, full: false)
|
2016-10-11 15:51:11 -04:00
|
|
|
format_reference = label_format_reference(format)
|
|
|
|
reference = "#{self.class.reference_prefix}#{format_reference}"
|
|
|
|
|
2017-11-22 08:20:35 -05:00
|
|
|
if from
|
2020-01-29 07:09:08 -05:00
|
|
|
"#{from.to_reference_base(target_project, full: full)}#{reference}"
|
2016-10-11 15:51:11 -04:00
|
|
|
else
|
|
|
|
reference
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 19:51:41 -04:00
|
|
|
def as_json(options = {})
|
|
|
|
super(options).tap do |json|
|
2017-09-06 12:25:15 -04:00
|
|
|
json[:type] = self.try(:type)
|
2017-06-02 13:11:26 -04:00
|
|
|
json[:priority] = priority(options[:project]) if options.key?(:project)
|
2019-01-08 08:10:48 -05:00
|
|
|
json[:textColor] = text_color
|
2016-10-14 19:51:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-15 13:30:53 -04:00
|
|
|
def hook_attrs
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
2019-04-23 15:58:20 -04:00
|
|
|
def present(attributes)
|
2020-12-09 16:09:43 -05:00
|
|
|
super(**attributes.merge(presenter_class: ::LabelPresenter))
|
2019-04-23 15:58:20 -04:00
|
|
|
end
|
|
|
|
|
2016-02-25 04:14:12 -05:00
|
|
|
private
|
|
|
|
|
2016-09-19 23:09:57 -04:00
|
|
|
def issues_count(user, params = {})
|
2016-10-27 22:43:31 -04:00
|
|
|
params.merge!(subject_foreign_key => subject.id, label_name: title, scope: 'all')
|
|
|
|
IssuesFinder.new(user, params.with_indifferent_access).execute.count
|
2016-09-19 23:09:57 -04:00
|
|
|
end
|
|
|
|
|
2016-02-25 04:14:12 -05:00
|
|
|
def label_format_reference(format = :id)
|
|
|
|
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
|
|
|
|
|
|
|
|
if format == :name && !name.include?('"')
|
2016-03-03 04:39:18 -05:00
|
|
|
%("#{name}")
|
2016-02-25 04:14:12 -05:00
|
|
|
else
|
2016-03-03 04:39:18 -05:00
|
|
|
id
|
2016-02-25 04:14:12 -05:00
|
|
|
end
|
|
|
|
end
|
2016-03-14 05:46:26 -04:00
|
|
|
|
2019-07-15 07:29:56 -04:00
|
|
|
def sanitize_value(value)
|
2016-06-29 16:47:37 -04:00
|
|
|
CGI.unescapeHTML(Sanitize.clean(value.to_s))
|
2016-06-16 19:09:13 -04:00
|
|
|
end
|
2017-04-11 17:56:33 -04:00
|
|
|
|
|
|
|
def strip_whitespace_from_title_and_color
|
|
|
|
%w(color title).each { |attr| self[attr] = self[attr]&.strip }
|
|
|
|
end
|
2014-07-29 10:10:15 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Label.prepend_if_ee('EE::Label')
|