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

347 lines
9.8 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class Milestone < ApplicationRecord
2015-09-11 14:41:01 +05:30
# Represents a "No Milestone" state used for filtering Issues and Merge
# Requests that have no milestone assigned.
2015-12-23 02:04:40 +05:30
MilestoneStruct = Struct.new(:title, :name, :id)
None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
Any = MilestoneStruct.new('Any Milestone', '', -1)
2016-06-02 11:05:42 +05:30
Upcoming = MilestoneStruct.new('Upcoming', '#upcoming', -2)
2017-08-17 22:00:37 +05:30
Started = MilestoneStruct.new('Started', '#started', -3)
2015-09-11 14:41:01 +05:30
2016-11-03 12:29:30 +05:30
include CacheMarkdownField
2018-10-15 14:42:47 +05:30
include AtomicInternalId
2018-11-08 19:23:39 +05:30
include IidRoutes
2015-04-26 12:48:37 +05:30
include Sortable
include Referable
2015-12-23 02:04:40 +05:30
include StripAttribute
2016-06-02 11:05:42 +05:30
include Milestoneish
2019-12-04 20:38:33 +05:30
include FromUnion
2018-03-17 18:26:18 +05:30
include Gitlab::SQL::Pattern
2014-09-02 18:07:02 +05:30
2019-12-04 20:38:33 +05:30
prepend_if_ee('::EE::Milestone') # rubocop: disable Cop/InjectEnterpriseEditionModule
2016-11-03 12:29:30 +05:30
cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
2014-09-02 18:07:02 +05:30
belongs_to :project
2017-09-10 17:25:29 +05:30
belongs_to :group
2019-12-04 20:38:33 +05:30
has_many :milestone_releases
has_many :releases, through: :milestone_releases
2018-10-15 14:42:47 +05:30
has_internal_id :iid, scope: :project, init: ->(s) { s&.project&.milestones&.maximum(:iid) }
has_internal_id :iid, scope: :group, init: ->(s) { s&.group&.milestones&.maximum(:iid) }
2014-09-02 18:07:02 +05:30
has_many :issues
2019-03-02 22:35:43 +05:30
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
2014-09-02 18:07:02 +05:30
has_many :merge_requests
2019-12-04 20:38:33 +05:30
has_many :events, as: :target, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
scope :of_projects, ->(ids) { where(project_id: ids) }
scope :of_groups, ->(ids) { where(group_id: ids) }
2014-09-02 18:07:02 +05:30
scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) }
2017-09-10 17:25:29 +05:30
scope :for_projects, -> { where(group: nil).includes(:project) }
2019-07-07 11:18:12 +05:30
scope :started, -> { active.where('milestones.start_date <= CURRENT_DATE') }
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
scope :for_projects_and_groups, -> (projects, groups) do
projects = projects.compact if projects.is_a? Array
projects = [] if projects.nil?
2014-09-02 18:07:02 +05:30
2019-03-02 22:35:43 +05:30
groups = groups.compact if groups.is_a? Array
groups = [] if groups.nil?
where(project_id: projects).or(where(group_id: groups))
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
scope :order_by_name_asc, -> { order(Arel::Nodes::Ascending.new(arel_table[:title].lower)) }
scope :reorder_by_due_date_asc, -> { reorder(Gitlab::Database.nulls_last_order('due_date', 'ASC')) }
2017-09-10 17:25:29 +05:30
validates :group, presence: true, unless: :project
validates :project, presence: true, unless: :group
2019-12-26 22:10:19 +05:30
validates :title, presence: true
2017-09-10 17:25:29 +05:30
validate :uniqueness_of_title, if: :title_changed?
validate :milestone_type_check
2017-08-17 22:00:37 +05:30
validate :start_date_should_be_less_than_due_date, if: proc { |m| m.start_date.present? && m.due_date.present? }
2019-09-04 21:01:54 +05:30
validate :dates_within_4_digits
2019-12-04 20:38:33 +05:30
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
strip_attributes :title
2014-09-02 18:07:02 +05:30
state_machine :state, initial: :active do
event :close do
transition active: :closed
end
event :activate do
transition closed: :active
end
state :closed
state :active
end
2015-10-24 18:46:33 +05:30
alias_attribute :name, :title
2015-09-25 12:07:36 +05:30
class << self
2019-03-02 22:35:43 +05:30
# Searches for milestones with a matching title or description.
2016-06-02 11:05:42 +05:30
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String
#
# Returns an ActiveRecord::Relation.
2015-09-25 12:07:36 +05:30
def search(query)
2018-03-17 18:26:18 +05:30
fuzzy_search(query, [:title, :description])
2015-09-25 12:07:36 +05:30
end
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
# Searches for milestones with a matching title.
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String
#
# Returns an ActiveRecord::Relation.
def search_title(query)
fuzzy_search(query, [:title])
end
2017-09-10 17:25:29 +05:30
def filter_by_state(milestones, state)
case state
when 'closed' then milestones.closed
when 'all' then milestones
else milestones.active
end
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
def count_by_state
reorder(nil).group(:state).count
end
2018-03-17 18:26:18 +05:30
def predefined?(milestone)
milestone == Any ||
milestone == None ||
milestone == Upcoming ||
milestone == Started
end
2015-09-25 12:07:36 +05:30
end
2016-06-02 11:05:42 +05:30
def self.reference_prefix
'%'
end
def self.reference_pattern
2016-06-02 11:05:42 +05:30
# NOTE: The iid pattern only matches when all characters on the expression
# are digits, so it will match %2 but not %2.1 because that's probably a
# milestone name and we want it to be matched as such.
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}
(?:
(?<milestone_iid>
\d+(?!\S\w)\b # Integer-based milestone iid, or
) |
(?<milestone_name>
[^"\s]+\b | # String-based single-word milestone title, or
"[^"]+" # String-based multi-word milestone surrounded in quotes
)
)
}x
end
def self.link_reference_pattern
2016-06-02 11:05:42 +05:30
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
end
2019-03-02 22:35:43 +05:30
def self.upcoming_ids(projects, groups)
2019-10-12 21:52:04 +05:30
unscoped
.for_projects_and_groups(projects, groups)
.active.where('milestones.due_date > CURRENT_DATE')
.order(:project_id, :group_id, :due_date).select('DISTINCT ON (project_id, group_id) id')
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
def participants
2018-12-13 13:39:08 +05:30
User.joins(assigned_issues: :milestone).where("milestones.id = ?", id).distinct
2017-08-17 22:00:37 +05:30
end
2018-05-09 12:01:36 +05:30
def self.sort_by_attribute(method)
2018-11-18 11:00:15 +05:30
sorted =
case method.to_s
when 'due_date_asc'
2018-12-05 23:21:45 +05:30
reorder_by_due_date_asc
2018-11-18 11:00:15 +05:30
when 'due_date_desc'
reorder(Gitlab::Database.nulls_last_order('due_date', 'DESC'))
when 'name_asc'
reorder(Arel::Nodes::Ascending.new(arel_table[:title].lower))
when 'name_desc'
reorder(Arel::Nodes::Descending.new(arel_table[:title].lower))
when 'start_date_asc'
reorder(Gitlab::Database.nulls_last_order('start_date', 'ASC'))
when 'start_date_desc'
reorder(Gitlab::Database.nulls_last_order('start_date', 'DESC'))
else
order_by(method)
end
sorted.with_order_id_desc
2017-08-17 22:00:37 +05:30
end
2018-12-13 13:39:08 +05:30
def self.states_count(projects, groups = nil)
return STATE_COUNT_HASH unless projects || groups
counts = Milestone
2019-03-02 22:35:43 +05:30
.for_projects_and_groups(projects, groups)
2018-12-13 13:39:08 +05:30
.reorder(nil)
.group(:state)
.count
{
opened: counts['active'] || 0,
closed: counts['closed'] || 0,
all: counts.values.sum
}
end
2016-06-02 11:05:42 +05:30
##
2017-09-10 17:25:29 +05:30
# Returns the String necessary to reference this Milestone in Markdown. Group
# milestones only support name references, and do not support cross-project
# references.
2016-06-02 11:05:42 +05:30
#
# format - Symbol format to use (default: :iid, optional: :name)
#
# Examples:
#
2017-08-17 22:00:37 +05:30
# Milestone.first.to_reference # => "%1"
# Milestone.first.to_reference(format: :name) # => "%\"goal\""
2019-12-04 20:38:33 +05:30
# Milestone.first.to_reference(cross_namespace_project) # => "gitlab-org/gitlab-foss%1"
# Milestone.first.to_reference(same_namespace_project) # => "gitlab-foss%1"
2016-06-02 11:05:42 +05:30
#
2018-03-17 18:26:18 +05:30
def to_reference(from = nil, format: :name, full: false)
2016-06-02 11:05:42 +05:30
format_reference = milestone_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
2017-09-10 17:25:29 +05:30
if project
2018-03-17 18:26:18 +05:30
"#{project.to_reference(from, full: full)}#{reference}"
2017-09-10 17:25:29 +05:30
else
reference
end
end
2018-03-17 18:26:18 +05:30
def reference_link_text(from = nil)
2019-02-15 15:39:39 +05:30
self.class.reference_prefix + self.title
end
2019-02-15 15:39:39 +05:30
def milestoneish_id
2017-08-17 22:00:37 +05:30
id
2014-09-02 18:07:02 +05:30
end
2017-09-10 17:25:29 +05:30
def for_display
self
end
2014-09-02 18:07:02 +05:30
def can_be_closed?
active? && issues.opened.count.zero?
end
def author_id
nil
end
2015-11-26 14:37:03 +05:30
2016-06-02 11:05:42 +05:30
def title=(value)
2016-11-03 12:29:30 +05:30
write_attribute(:title, sanitize_title(value)) if value.present?
2016-06-02 11:05:42 +05:30
end
2017-09-10 17:25:29 +05:30
def safe_title
title.to_slug.normalize.to_s
end
2015-11-26 14:37:03 +05:30
2019-12-21 20:55:43 +05:30
def resource_parent
2017-09-10 17:25:29 +05:30
group || project
end
2015-11-26 14:37:03 +05:30
2019-10-31 01:37:42 +05:30
def to_ability_name
model_name.singular
end
2018-03-17 18:26:18 +05:30
def group_milestone?
2017-09-10 17:25:29 +05:30
group_id.present?
end
2015-11-26 14:37:03 +05:30
2018-03-17 18:26:18 +05:30
def project_milestone?
2017-09-10 17:25:29 +05:30
project_id.present?
2015-11-26 14:37:03 +05:30
end
2016-06-02 11:05:42 +05:30
2020-01-01 13:55:28 +05:30
def merge_requests_enabled?
if group_milestone?
# Assume that groups have at least one project with merge requests enabled.
# Otherwise, we would need to load all of the projects from the database.
true
elsif project_milestone?
project&.merge_requests_enabled?
end
end
2016-06-02 11:05:42 +05:30
private
2017-09-10 17:25:29 +05:30
# Milestone titles must be unique across project milestones and group milestones
def uniqueness_of_title
if project
relation = Milestone.for_projects_and_groups([project_id], [project.group&.id])
elsif group
2019-03-02 22:35:43 +05:30
relation = Milestone.for_projects_and_groups(group.projects.select(:id), [group.id])
2017-09-10 17:25:29 +05:30
end
title_exists = relation.find_by_title(title)
2019-07-31 22:56:46 +05:30
errors.add(:title, _("already being used for another group or project milestone.")) if title_exists
2017-09-10 17:25:29 +05:30
end
# Milestone should be either a project milestone or a group milestone
def milestone_type_check
if group_id && project_id
field = project_id_changed? ? :project_id : :group_id
2019-07-31 22:56:46 +05:30
errors.add(field, _("milestone should belong either to a project or a group."))
2017-09-10 17:25:29 +05:30
end
end
2016-06-02 11:05:42 +05:30
def milestone_format_reference(format = :iid)
2019-07-31 22:56:46 +05:30
raise ArgumentError, _('Unknown format') unless [:iid, :name].include?(format)
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
if group_milestone? && format == :iid
2019-07-31 22:56:46 +05:30
raise ArgumentError, _('Cannot refer to a group milestone by an internal id!')
2018-03-17 18:26:18 +05:30
end
2016-06-02 11:05:42 +05:30
if format == :name && !name.include?('"')
%("#{name}")
else
iid
end
end
2016-11-03 12:29:30 +05:30
def sanitize_title(value)
CGI.unescape_html(Sanitize.clean(value.to_s))
end
2017-08-17 22:00:37 +05:30
def start_date_should_be_less_than_due_date
if due_date <= start_date
2019-07-31 22:56:46 +05:30
errors.add(:due_date, _("must be greater than start date"))
2017-08-17 22:00:37 +05:30
end
end
2019-09-04 21:01:54 +05:30
def dates_within_4_digits
if start_date && start_date > Date.new(9999, 12, 31)
errors.add(:start_date, _("date must not be after 9999-12-31"))
end
if due_date && due_date > Date.new(9999, 12, 31)
errors.add(:due_date, _("date must not be after 9999-12-31"))
end
end
2017-08-17 22:00:37 +05:30
def issues_finder_params
2020-01-01 13:55:28 +05:30
{ project_id: project_id, group_id: group_id, include_subgroups: group_id.present? }.compact
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end