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

240 lines
6.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class Label < ActiveRecord::Base
2016-11-03 12:29:30 +05:30
include CacheMarkdownField
2015-09-11 14:41:01 +05:30
include Referable
2016-06-02 11:05:42 +05:30
include Subscribable
2018-11-18 11:00:15 +05:30
include Gitlab::SQL::Pattern
2016-06-02 11:05:42 +05:30
2015-10-24 18:46:33 +05:30
# Represents a "No Label" state used for filtering Issues and Merge
# Requests that have no label assigned.
LabelStruct = Struct.new(:title, :name)
None = LabelStruct.new('No Label', 'No Label')
2015-12-23 02:04:40 +05:30
Any = LabelStruct.new('Any Label', '')
2015-09-11 14:41:01 +05:30
2016-11-03 12:29:30 +05:30
cache_markdown_field :description, pipeline: :single_line
2017-08-17 22:00:37 +05:30
DEFAULT_COLOR = '#428BCA'.freeze
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
default_value_for :color, DEFAULT_COLOR
2017-09-10 17:25:29 +05:30
has_many :lists, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2016-11-03 12:29:30 +05:30
has_many :priorities, class_name: 'LabelPriority'
2017-09-10 17:25:29 +05:30
has_many :label_links, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2014-09-02 18:07:02 +05:30
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
2016-06-02 11:05:42 +05:30
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
before_validation :strip_whitespace_from_title_and_color
2015-12-23 02:04:40 +05:30
validates :color, color: true, allow_blank: false
2014-09-02 18:07:02 +05:30
2016-08-24 12:49:21 +05:30
# Don't allow ',' for label titles
2016-11-03 12:29:30 +05:30
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
validates :title, uniqueness: { scope: [:group_id, :project_id] }
2017-08-17 22:00:37 +05:30
validates :title, length: { maximum: 255 }
2015-04-26 12:48:37 +05:30
default_scope { order(title: :asc) }
2014-09-02 18:07:02 +05:30
2016-11-03 12:29:30 +05:30
scope :templates, -> { where(template: true) }
scope :with_title, ->(title) { where(title: title) }
2018-03-17 18:26:18 +05:30
scope :with_lists_and_board, -> { joins(lists: :board).merge(List.movable) }
2018-03-27 19:54:05 +05:30
scope :on_group_boards, ->(group_id) { with_lists_and_board.where(boards: { group_id: group_id }) }
2018-03-17 18:26:18 +05:30
scope :on_project_boards, ->(project_id) { with_lists_and_board.where(boards: { project_id: project_id }) }
2016-11-03 12:29:30 +05:30
def self.prioritized(project)
joins(:priorities)
.where(label_priorities: { project_id: project })
.reorder('label_priorities.priority ASC, labels.title ASC')
end
def self.unprioritized(project)
labels = Label.arel_table
priorities = LabelPriority.arel_table
2015-09-25 12:07:36 +05:30
2017-09-10 17:25:29 +05:30
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-11-03 12:29:30 +05:30
joins(label_priorities).where(priorities[:priority].eq(nil))
end
2016-11-03 12:29:30 +05:30
def self.left_join_priorities
labels = Label.arel_table
priorities = LabelPriority.arel_table
2017-09-10 17:25:29 +05:30
label_priorities = labels.join(priorities, Arel::Nodes::OuterJoin)
.on(labels[:id].eq(priorities[:label_id]))
.join_sources
2016-11-03 12:29:30 +05:30
joins(label_priorities)
end
2014-09-02 18:07:02 +05:30
alias_attribute :name, :title
2015-09-11 14:41:01 +05:30
def self.reference_prefix
'~'
end
2016-06-02 11:05:42 +05:30
##
2015-09-11 14:41:01 +05:30
# Pattern used to extract label references from text
2016-06-02 11:05:42 +05:30
#
# This pattern supports cross-project references.
#
2015-09-11 14:41:01 +05:30
def self.reference_pattern
2016-08-24 12:49:21 +05:30
# 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-06-02 11:05:42 +05:30
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}
2015-09-11 14:41:01 +05:30
(?:
2018-11-08 19:23:39 +05:30
(?<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-09-11 14:41:01 +05:30
)
}x
end
2016-06-02 11:05:42 +05:30
def self.link_reference_pattern
nil
end
2018-11-18 11:00:15 +05:30
# Searches for labels with a matching title or description.
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String.
#
# Returns an ActiveRecord::Relation.
def self.search(query)
fuzzy_search(query, [:title, :description])
end
2016-11-24 13:41:30 +05:30
def open_issues_count(user = nil)
issues_count(user, state: 'opened')
2016-11-03 12:29:30 +05:30
end
2016-11-24 13:41:30 +05:30
def closed_issues_count(user = nil)
issues_count(user, state: 'closed')
2016-11-03 12:29:30 +05:30
end
2016-11-24 13:41:30 +05:30
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-11-03 12:29:30 +05:30
end
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)
2018-03-17 18:26:18 +05:30
priority = if priorities.loaded?
priorities.first { |p| p.project == project }
else
priorities.find_by(project: project)
end
priority.try(:priority)
2016-11-03 12:29:30 +05:30
end
2018-11-08 19:23:39 +05:30
def priority?
priorities.present?
end
2016-11-03 12:29:30 +05:30
def template?
template
end
2017-08-17 22:00:37 +05:30
def color
super || DEFAULT_COLOR
end
2016-11-03 12:29:30 +05:30
def text_color
LabelsHelper.text_color_for_bg(self.color)
end
def title=(value)
write_attribute(:title, sanitize_title(value)) if value.present?
end
2016-06-02 11:05:42 +05:30
##
2015-09-11 14:41:01 +05:30
# Returns the String necessary to reference this Label in Markdown
#
# format - Symbol format to use (default: :id, optional: :name)
#
# Examples:
#
2017-08-17 22:00:37 +05:30
# Label.first.to_reference # => "~1"
# Label.first.to_reference(format: :name) # => "~\"bug\""
# 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"
2015-09-11 14:41:01 +05:30
#
# Returns a String
2016-06-02 11:05:42 +05:30
#
2018-03-17 18:26:18 +05:30
def to_reference(from = nil, target_project: nil, format: :id, full: false)
2016-06-02 11:05:42 +05:30
format_reference = label_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
2018-03-17 18:26:18 +05:30
if from
"#{from.to_reference(target_project, full: full)}#{reference}"
2015-09-11 14:41:01 +05:30
else
2016-06-02 11:05:42 +05:30
reference
2015-09-11 14:41:01 +05:30
end
end
2016-11-03 12:29:30 +05:30
def as_json(options = {})
super(options).tap do |json|
2018-03-17 18:26:18 +05:30
json[:type] = self.try(:type)
2017-09-10 17:25:29 +05:30
json[:priority] = priority(options[:project]) if options.key?(:project)
2016-11-03 12:29:30 +05:30
end
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
def hook_attrs
attributes
2015-09-25 12:07:36 +05:30
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
private
2016-11-03 12:29:30 +05:30
def issues_count(user, params = {})
2016-11-24 13:41:30 +05:30
params.merge!(subject_foreign_key => subject.id, label_name: title, scope: 'all')
IssuesFinder.new(user, params.with_indifferent_access).execute.count
2016-06-02 11:05:42 +05:30
end
def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
if format == :name && !name.include?('"')
%("#{name}")
else
id
end
end
2016-08-24 12:49:21 +05:30
def sanitize_title(value)
CGI.unescapeHTML(Sanitize.clean(value.to_s))
end
2017-08-17 22:00:37 +05:30
def strip_whitespace_from_title_and_color
%w(color title).each { |attr| self[attr] = self[attr]&.strip }
end
2014-09-02 18:07:02 +05:30
end