2014-07-29 10:10:15 -04:00
|
|
|
class Label < ActiveRecord::Base
|
2015-05-02 23:11:21 -04:00
|
|
|
include Referable
|
2016-02-12 09:58:39 -05:00
|
|
|
include Subscribable
|
|
|
|
|
2015-10-06 12:57:13 -04:00
|
|
|
# Represents a "No Label" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no label assigned.
|
2015-10-09 00:24:55 -04:00
|
|
|
LabelStruct = Struct.new(:title, :name)
|
|
|
|
None = LabelStruct.new('No Label', 'No Label')
|
2015-12-02 08:10:43 -05:00
|
|
|
Any = LabelStruct.new('Any Label', '')
|
2015-05-02 23:11:21 -04:00
|
|
|
|
2014-08-15 05:02:05 -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
|
|
|
|
|
2014-07-29 10:10:15 -04:00
|
|
|
belongs_to :project
|
|
|
|
has_many :label_links, dependent: :destroy
|
2014-07-30 08:15:39 -04:00
|
|
|
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
|
2016-03-01 21:04:13 -05:00
|
|
|
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
|
2014-07-29 10:10:15 -04:00
|
|
|
|
2015-12-01 18:53:44 -05:00
|
|
|
validates :color, color: true, allow_blank: false
|
2015-09-03 08:50:23 -04:00
|
|
|
validates :project, presence: true, unless: Proc.new { |service| service.template? }
|
2014-07-30 06:26:54 -04:00
|
|
|
|
2016-06-16 19:09:13 -04:00
|
|
|
# Don't allow ',' for label titles
|
2014-08-12 04:53:50 -04:00
|
|
|
validates :title,
|
|
|
|
presence: true,
|
2016-06-16 19:09:13 -04:00
|
|
|
format: { with: /\A[^,]+\z/ },
|
2014-08-13 08:12:05 -04:00
|
|
|
uniqueness: { scope: :project_id }
|
2014-07-29 12:19:26 -04:00
|
|
|
|
2016-06-02 16:21:00 -04:00
|
|
|
before_save :nullify_priority
|
2016-03-14 05:46:26 -04:00
|
|
|
|
2015-02-05 23:29:41 -05:00
|
|
|
default_scope { order(title: :asc) }
|
2014-07-30 12:13:35 -04:00
|
|
|
|
2015-09-03 08:50:23 -04:00
|
|
|
scope :templates, -> { where(template: true) }
|
2016-05-13 11:26:18 -04:00
|
|
|
|
2016-06-02 15:57:31 -04:00
|
|
|
def self.prioritized
|
2016-06-07 06:57:09 -04:00
|
|
|
where.not(priority: nil).reorder(:priority, :title)
|
2016-06-02 15:57:31 -04:00
|
|
|
end
|
2016-05-13 11:26:18 -04:00
|
|
|
|
2016-06-02 15:57:31 -04:00
|
|
|
def self.unprioritized
|
2016-06-07 06:57:09 -04:00
|
|
|
where(priority: nil)
|
2016-03-14 05:46:26 -04:00
|
|
|
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
|
|
|
(?:
|
2016-07-12 20:33:25 -04:00
|
|
|
(?<label_id>\d+(?!\S\w)\b) | # Integer-based label ID, or
|
2015-05-14 16:59:39 -04:00
|
|
|
(?<label_name>
|
2016-07-12 20:48:31 -04:00
|
|
|
[A-Za-z0-9_\-\?\.&]+ | # String-based single-word label title, or
|
2016-07-13 15:01:27 -04:00
|
|
|
".+?" # 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
|
|
|
|
|
2016-02-25 08:26:41 -05:00
|
|
|
##
|
2015-05-02 23:11:21 -04:00
|
|
|
# Returns the String necessary to reference this Label in Markdown
|
|
|
|
#
|
|
|
|
# format - Symbol format to use (default: :id, optional: :name)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2016-02-25 02:37:49 -05:00
|
|
|
# Label.first.to_reference # => "~1"
|
|
|
|
# Label.first.to_reference(format: :name) # => "~\"bug\""
|
2016-02-25 04:14:12 -05:00
|
|
|
# Label.first.to_reference(project) # => "gitlab-org/gitlab-ce~1"
|
2015-05-02 23:11:21 -04:00
|
|
|
#
|
|
|
|
# Returns a String
|
2016-02-25 08:26:41 -05:00
|
|
|
#
|
2016-02-25 04:14:12 -05:00
|
|
|
def to_reference(from_project = nil, format: :id)
|
2016-03-03 04:39:18 -05:00
|
|
|
format_reference = label_format_reference(format)
|
|
|
|
reference = "#{self.class.reference_prefix}#{format_reference}"
|
2016-02-25 04:14:12 -05:00
|
|
|
|
|
|
|
if cross_project_reference?(from_project)
|
|
|
|
project.to_reference + reference
|
2015-05-02 23:11:21 -04:00
|
|
|
else
|
2016-02-25 04:14:12 -05:00
|
|
|
reference
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-21 19:21:23 -04:00
|
|
|
def open_issues_count(user = nil)
|
|
|
|
issues.visible_to_user(user).opened.count
|
2014-07-30 08:15:39 -04:00
|
|
|
end
|
2015-09-03 08:50:23 -04:00
|
|
|
|
2016-03-21 19:21:23 -04:00
|
|
|
def closed_issues_count(user = nil)
|
|
|
|
issues.visible_to_user(user).closed.count
|
2016-01-25 12:07:04 -05:00
|
|
|
end
|
|
|
|
|
2016-03-01 21:04:34 -05:00
|
|
|
def open_merge_requests_count
|
|
|
|
merge_requests.opened.count
|
|
|
|
end
|
|
|
|
|
2015-09-03 08:50:23 -04:00
|
|
|
def template?
|
|
|
|
template
|
|
|
|
end
|
2016-02-25 04:14:12 -05:00
|
|
|
|
2016-04-20 12:00:12 -04:00
|
|
|
def text_color
|
|
|
|
LabelsHelper::text_color_for_bg(self.color)
|
|
|
|
end
|
|
|
|
|
2016-05-09 10:58:20 -04:00
|
|
|
def title=(value)
|
2016-06-16 19:09:13 -04:00
|
|
|
write_attribute(:title, sanitize_title(value)) if value.present?
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
|
|
|
|
2016-02-25 04:14:12 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-02 16:21:00 -04:00
|
|
|
def nullify_priority
|
2016-05-13 11:26:18 -04:00
|
|
|
self.priority = nil if priority.blank?
|
2016-03-14 05:46:26 -04:00
|
|
|
end
|
2016-06-16 19:09:13 -04:00
|
|
|
|
|
|
|
def sanitize_title(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
|
2014-07-29 10:10:15 -04:00
|
|
|
end
|