2014-07-29 14:10:15 +00:00
|
|
|
class Label < ActiveRecord::Base
|
2016-10-06 21:17:11 +00:00
|
|
|
include CacheMarkdownField
|
2015-05-03 03:11:21 +00:00
|
|
|
include Referable
|
2016-02-12 14:58:39 +00:00
|
|
|
include Subscribable
|
|
|
|
|
2015-10-06 16:57:13 +00:00
|
|
|
# Represents a "No Label" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no label assigned.
|
2015-10-09 04:24:55 +00:00
|
|
|
LabelStruct = Struct.new(:title, :name)
|
|
|
|
None = LabelStruct.new('No Label', 'No Label')
|
2015-12-02 13:10:43 +00:00
|
|
|
Any = LabelStruct.new('Any Label', '')
|
2015-05-03 03:11:21 +00:00
|
|
|
|
2016-10-06 21:17:11 +00:00
|
|
|
cache_markdown_field :description, pipeline: :single_line
|
|
|
|
|
2017-02-21 23:32:18 +00:00
|
|
|
DEFAULT_COLOR = '#428BCA'.freeze
|
2014-08-14 08:17:52 +00:00
|
|
|
|
2015-04-17 15:38:31 +00:00
|
|
|
default_value_for :color, DEFAULT_COLOR
|
|
|
|
|
2016-08-08 19:14:22 +00:00
|
|
|
has_many :lists, dependent: :destroy
|
2016-10-14 19:12:07 +00:00
|
|
|
has_many :priorities, class_name: 'LabelPriority'
|
2014-07-29 14:10:15 +00:00
|
|
|
has_many :label_links, dependent: :destroy
|
2014-07-30 12:15:39 +00:00
|
|
|
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
|
2016-03-02 02:04:13 +00:00
|
|
|
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
|
2014-07-29 14:10:15 +00:00
|
|
|
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :color, color: true, allow_blank: false
|
2014-07-30 10:26:54 +00:00
|
|
|
|
2016-06-16 23:09:13 +00:00
|
|
|
# Don't allow ',' for label titles
|
2016-09-20 03:17:04 +00:00
|
|
|
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
|
2016-09-21 20:47:58 +00:00
|
|
|
validates :title, uniqueness: { scope: [:group_id, :project_id] }
|
2016-08-11 07:37:09 +00:00
|
|
|
validates :title, length: { maximum: 255 }
|
2014-07-29 16:19:26 +00:00
|
|
|
|
2015-02-06 04:29:41 +00:00
|
|
|
default_scope { order(title: :asc) }
|
2014-07-30 16:13:35 +00:00
|
|
|
|
2016-09-21 20:47:58 +00:00
|
|
|
scope :templates, -> { where(template: true) }
|
|
|
|
scope :with_title, ->(title) { where(title: title) }
|
2016-05-13 15:26:18 +00:00
|
|
|
|
2016-10-14 21:32:44 +00:00
|
|
|
def self.prioritized(project)
|
2017-02-22 22:54:59 +00:00
|
|
|
joins(:priorities)
|
|
|
|
.where(label_priorities: { project_id: project })
|
|
|
|
.reorder('label_priorities.priority ASC, labels.title ASC')
|
2016-06-02 19:57:31 +00:00
|
|
|
end
|
2016-05-13 15:26:18 +00:00
|
|
|
|
2016-10-14 21:32:44 +00:00
|
|
|
def self.unprioritized(project)
|
2016-10-18 06:00:33 +00:00
|
|
|
labels = Label.arel_table
|
|
|
|
priorities = LabelPriority.arel_table
|
|
|
|
|
2017-02-22 22:50:15 +00: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 06:00:33 +00:00
|
|
|
|
|
|
|
joins(label_priorities).where(priorities[:priority].eq(nil))
|
2016-03-14 09:46:26 +00:00
|
|
|
end
|
|
|
|
|
2016-10-14 23:06:26 +00:00
|
|
|
def self.left_join_priorities
|
|
|
|
labels = Label.arel_table
|
|
|
|
priorities = LabelPriority.arel_table
|
|
|
|
|
2017-02-22 22:50:15 +00:00
|
|
|
label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin).
|
|
|
|
on(labels[:id].eq(priorities[:label_id])).
|
|
|
|
join_sources
|
2016-10-14 23:06:26 +00:00
|
|
|
|
|
|
|
joins(label_priorities)
|
|
|
|
end
|
|
|
|
|
2014-08-12 12:16:25 +00:00
|
|
|
alias_attribute :name, :title
|
2014-07-30 12:15:39 +00:00
|
|
|
|
2015-05-03 03:11:21 +00:00
|
|
|
def self.reference_prefix
|
|
|
|
'~'
|
|
|
|
end
|
|
|
|
|
2016-03-01 10:39:49 +00:00
|
|
|
##
|
2015-05-14 20:59:39 +00:00
|
|
|
# Pattern used to extract label references from text
|
2016-03-01 10:39:49 +00:00
|
|
|
#
|
|
|
|
# This pattern supports cross-project references.
|
|
|
|
#
|
2015-05-14 20:59:39 +00:00
|
|
|
def self.reference_pattern
|
2016-07-13 00:33:25 +00: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 15:41:48 +00:00
|
|
|
@reference_pattern ||= %r{
|
2016-02-25 12:49:42 +00:00
|
|
|
(#{Project.reference_pattern})?
|
|
|
|
#{Regexp.escape(reference_prefix)}
|
2015-05-14 20:59:39 +00:00
|
|
|
(?:
|
2016-07-13 00:33:25 +00:00
|
|
|
(?<label_id>\d+(?!\S\w)\b) | # Integer-based label ID, or
|
2015-05-14 20:59:39 +00:00
|
|
|
(?<label_name>
|
2016-07-13 00:48:31 +00:00
|
|
|
[A-Za-z0-9_\-\?\.&]+ | # String-based single-word label title, or
|
2016-07-13 19:01:27 +00:00
|
|
|
".+?" # String-based multi-word label surrounded in quotes
|
2015-05-14 20:59:39 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
2016-02-25 12:49:42 +00:00
|
|
|
def self.link_reference_pattern
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2016-10-28 02:43:31 +00:00
|
|
|
def open_issues_count(user = nil)
|
|
|
|
issues_count(user, state: 'opened')
|
2014-07-30 12:15:39 +00:00
|
|
|
end
|
2015-09-03 12:50:23 +00:00
|
|
|
|
2016-10-28 02:43:31 +00:00
|
|
|
def closed_issues_count(user = nil)
|
|
|
|
issues_count(user, state: 'closed')
|
2016-01-25 17:07:04 +00:00
|
|
|
end
|
|
|
|
|
2016-10-28 02:43:31 +00: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-02 02:04:34 +00:00
|
|
|
end
|
|
|
|
|
2016-10-17 18:34:22 +00: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)
|
|
|
|
priorities.find_by(project: project).try(:priority)
|
|
|
|
end
|
|
|
|
|
2015-09-03 12:50:23 +00:00
|
|
|
def template?
|
|
|
|
template
|
|
|
|
end
|
2016-02-25 09:14:12 +00:00
|
|
|
|
2016-04-20 16:00:12 +00:00
|
|
|
def text_color
|
2016-10-13 22:38:22 +00:00
|
|
|
LabelsHelper.text_color_for_bg(self.color)
|
2016-04-20 16:00:12 +00:00
|
|
|
end
|
|
|
|
|
2016-05-09 14:58:20 +00:00
|
|
|
def title=(value)
|
2016-06-16 23:09:13 +00:00
|
|
|
write_attribute(:title, sanitize_title(value)) if value.present?
|
2016-05-04 21:21:57 +00:00
|
|
|
end
|
|
|
|
|
2016-10-11 19:51:11 +00:00
|
|
|
##
|
|
|
|
# Returns the String necessary to reference this Label in Markdown
|
|
|
|
#
|
|
|
|
# format - Symbol format to use (default: :id, optional: :name)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2016-11-02 23:49:13 +00:00
|
|
|
# Label.first.to_reference # => "~1"
|
|
|
|
# Label.first.to_reference(format: :name) # => "~\"bug\""
|
2016-12-21 16:41:33 +00:00
|
|
|
# Label.first.to_reference(project, target_project: same_namespace_project) # => "gitlab-ce~1"
|
|
|
|
# Label.first.to_reference(project, target_project: another_namespace_project) # => "gitlab-org/gitlab-ce~1"
|
2016-10-11 19:51:11 +00:00
|
|
|
#
|
|
|
|
# Returns a String
|
|
|
|
#
|
2016-12-21 16:41:33 +00:00
|
|
|
def to_reference(from_project = nil, target_project: nil, format: :id, full: false)
|
2016-10-11 19:51:11 +00:00
|
|
|
format_reference = label_format_reference(format)
|
|
|
|
reference = "#{self.class.reference_prefix}#{format_reference}"
|
|
|
|
|
2016-12-21 16:41:33 +00:00
|
|
|
if from_project
|
|
|
|
"#{from_project.to_reference(target_project, full: full)}#{reference}"
|
2016-10-11 19:51:11 +00:00
|
|
|
else
|
|
|
|
reference
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 23:51:41 +00:00
|
|
|
def as_json(options = {})
|
|
|
|
super(options).tap do |json|
|
2016-10-17 18:34:22 +00:00
|
|
|
json[:priority] = priority(options[:project]) if options.has_key?(:project)
|
2016-10-14 23:51:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-15 17:30:53 +00:00
|
|
|
def hook_attrs
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
2016-02-25 09:14:12 +00:00
|
|
|
private
|
|
|
|
|
2016-09-20 03:09:57 +00:00
|
|
|
def issues_count(user, params = {})
|
2016-10-28 02:43:31 +00:00
|
|
|
params.merge!(subject_foreign_key => subject.id, label_name: title, scope: 'all')
|
|
|
|
IssuesFinder.new(user, params.with_indifferent_access).execute.count
|
2016-09-20 03:09:57 +00:00
|
|
|
end
|
|
|
|
|
2016-02-25 09:14:12 +00:00
|
|
|
def label_format_reference(format = :id)
|
|
|
|
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
|
|
|
|
|
|
|
|
if format == :name && !name.include?('"')
|
2016-03-03 09:39:18 +00:00
|
|
|
%("#{name}")
|
2016-02-25 09:14:12 +00:00
|
|
|
else
|
2016-03-03 09:39:18 +00:00
|
|
|
id
|
2016-02-25 09:14:12 +00:00
|
|
|
end
|
|
|
|
end
|
2016-03-14 09:46:26 +00:00
|
|
|
|
2016-06-16 23:09:13 +00:00
|
|
|
def sanitize_title(value)
|
2016-06-29 20:47:37 +00:00
|
|
|
CGI.unescapeHTML(Sanitize.clean(value.to_s))
|
2016-06-16 23:09:13 +00:00
|
|
|
end
|
2014-07-29 14:10:15 +00:00
|
|
|
end
|