2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Milestone < ApplicationRecord
|
2015-02-05 19:49:41 -05:00
|
|
|
include Sortable
|
2020-04-30 20:09:59 -04:00
|
|
|
include Timebox
|
2016-03-06 23:07:19 -05:00
|
|
|
include Milestoneish
|
2019-09-04 12:19:31 -04:00
|
|
|
include FromUnion
|
2020-01-14 10:07:55 -05:00
|
|
|
include Importable
|
2013-08-21 05:16:26 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
prepend_mod_with('Milestone') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2020-12-14 10:09:40 -05:00
|
|
|
class Predefined
|
|
|
|
ALL = [::Timebox::None, ::Timebox::Any, ::Timebox::Started, ::Timebox::Upcoming].freeze
|
|
|
|
end
|
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
has_many :milestone_releases
|
|
|
|
has_many :releases, through: :milestone_releases
|
2019-09-03 05:38:59 -04:00
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
has_internal_id :iid, scope: :project, track_if: -> { !importing? }
|
|
|
|
has_internal_id :iid, scope: :group, track_if: -> { !importing? }
|
2018-04-20 10:00:15 -04:00
|
|
|
|
2019-09-06 05:54:58 -04:00
|
|
|
has_many :events, as: :target, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
|
2012-04-08 17:28:58 -04:00
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
scope :active, -> { with_state(:active) }
|
2019-03-11 10:15:05 -04:00
|
|
|
scope :started, -> { active.where('milestones.start_date <= CURRENT_DATE') }
|
2020-05-06 23:09:46 -04:00
|
|
|
scope :not_started, -> { active.where('milestones.start_date > CURRENT_DATE') }
|
|
|
|
scope :not_upcoming, -> do
|
|
|
|
active
|
|
|
|
.where('milestones.due_date <= CURRENT_DATE')
|
|
|
|
.order(:project_id, :group_id, :due_date)
|
|
|
|
end
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2018-09-17 12:35:39 -04:00
|
|
|
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')) }
|
2020-06-04 14:08:32 -04:00
|
|
|
scope :with_api_entity_associations, -> { preload(project: [:project_feature, :route, namespace: :route]) }
|
2020-12-23 13:10:19 -05:00
|
|
|
scope :order_by_dates_and_title, -> { order(due_date: :asc, start_date: :asc, title: :asc) }
|
2018-09-17 12:35:39 -04:00
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
|
2021-06-10 08:10:09 -04:00
|
|
|
validate :uniqueness_of_title, if: :title_changed?
|
2013-02-18 04:10:09 -05:00
|
|
|
|
2013-02-18 08:22:18 -05:00
|
|
|
state_machine :state, initial: :active do
|
2013-02-18 04:10:09 -05:00
|
|
|
event :close do
|
2013-02-18 08:22:18 -05:00
|
|
|
transition active: :closed
|
2013-02-18 04:10:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
event :activate do
|
2013-02-18 08:22:18 -05:00
|
|
|
transition closed: :active
|
2013-02-18 04:10:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
state :closed
|
|
|
|
|
|
|
|
state :active
|
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
|
2020-10-13 23:08:32 -04:00
|
|
|
def self.min_chars_for_partial_matching
|
|
|
|
2
|
|
|
|
end
|
|
|
|
|
2016-03-30 22:12:34 -04:00
|
|
|
def self.reference_prefix
|
|
|
|
'%'
|
|
|
|
end
|
|
|
|
|
2015-12-24 08:43:07 -05:00
|
|
|
def self.reference_pattern
|
2016-04-05 20:35:43 -04:00
|
|
|
# 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.
|
2016-05-19 00:45:25 -04:00
|
|
|
@reference_pattern ||= %r{
|
2016-03-30 22:12:34 -04:00
|
|
|
(#{Project.reference_pattern})?
|
|
|
|
#{Regexp.escape(reference_prefix)}
|
|
|
|
(?:
|
2016-04-05 20:35:43 -04:00
|
|
|
(?<milestone_iid>
|
|
|
|
\d+(?!\S\w)\b # Integer-based milestone iid, or
|
|
|
|
) |
|
2016-03-30 22:12:34 -04:00
|
|
|
(?<milestone_name>
|
2016-04-05 20:35:43 -04:00
|
|
|
[^"\s]+\b | # String-based single-word milestone title, or
|
|
|
|
"[^"]+" # String-based multi-word milestone surrounded in quotes
|
2016-03-30 22:12:34 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}x
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.link_reference_pattern
|
2016-03-24 11:41:48 -04:00
|
|
|
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
2018-11-15 01:56:51 -05:00
|
|
|
def self.upcoming_ids(projects, groups)
|
2019-07-24 09:59:55 -04:00
|
|
|
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-03-11 12:46:14 -05:00
|
|
|
end
|
|
|
|
|
2021-03-31 02:08:58 -04:00
|
|
|
def self.with_web_entity_associations
|
|
|
|
preload(:group, project: [:project_feature, group: [:parent], namespace: :route])
|
|
|
|
end
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
def participants
|
2021-04-26 08:09:44 -04:00
|
|
|
User.joins(assigned_issues: :milestone).where(milestones: { id: id }).distinct
|
2017-05-04 08:11:15 -04:00
|
|
|
end
|
|
|
|
|
2018-04-04 05:19:47 -04:00
|
|
|
def self.sort_by_attribute(method)
|
2018-08-06 15:38:37 -04:00
|
|
|
sorted =
|
|
|
|
case method.to_s
|
|
|
|
when 'due_date_asc'
|
2018-09-17 12:35:39 -04:00
|
|
|
reorder_by_due_date_asc
|
2018-08-06 15:38:37 -04:00
|
|
|
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-03-23 20:39:12 -04:00
|
|
|
end
|
|
|
|
|
2018-10-16 09:18:25 -04:00
|
|
|
def self.states_count(projects, groups = nil)
|
|
|
|
return STATE_COUNT_HASH unless projects || groups
|
|
|
|
|
|
|
|
counts = Milestone
|
2019-01-11 09:58:18 -05:00
|
|
|
.for_projects_and_groups(projects, groups)
|
2018-10-16 09:18:25 -04:00
|
|
|
.reorder(nil)
|
|
|
|
.group(:state)
|
|
|
|
.count
|
|
|
|
|
|
|
|
{
|
|
|
|
opened: counts['active'] || 0,
|
|
|
|
closed: counts['closed'] || 0,
|
|
|
|
all: counts.values.sum
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
def for_display
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-12-14 00:34:05 -05:00
|
|
|
def can_be_closed?
|
2020-08-12 02:09:53 -04:00
|
|
|
active? && issues.opened.count == 0
|
2012-12-18 22:14:05 -05:00
|
|
|
end
|
|
|
|
|
2012-12-14 15:05:10 -05:00
|
|
|
def author_id
|
2014-03-25 08:01:52 -04:00
|
|
|
nil
|
2012-12-14 15:05:10 -05:00
|
|
|
end
|
2015-10-15 12:10:35 -04:00
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
# TODO: remove after all code paths use `timebox_id`
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/215688
|
|
|
|
alias_method :milestoneish_id, :timebox_id
|
|
|
|
# TODO: remove after all code paths use (group|project)_timebox?
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/215690
|
|
|
|
alias_method :group_milestone?, :group_timebox?
|
|
|
|
alias_method :project_milestone?, :project_timebox?
|
2019-12-03 13:06:49 -05:00
|
|
|
|
2020-05-15 08:08:28 -04:00
|
|
|
def parent
|
|
|
|
if group_milestone?
|
|
|
|
group
|
|
|
|
else
|
|
|
|
project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-26 05:08:06 -04:00
|
|
|
def subgroup_milestone?
|
|
|
|
group_milestone? && parent.subgroup?
|
|
|
|
end
|
|
|
|
|
2016-03-30 22:12:34 -04:00
|
|
|
private
|
|
|
|
|
2017-01-06 07:47:18 -05:00
|
|
|
def issues_finder_params
|
2019-12-10 02:53:40 -05:00
|
|
|
{ project_id: project_id, group_id: group_id, include_subgroups: group_id.present? }.compact
|
2017-01-06 07:47:18 -05:00
|
|
|
end
|
2021-06-10 08:10:09 -04:00
|
|
|
|
|
|
|
# milestone titles must be unique across project and group milestones
|
|
|
|
def uniqueness_of_title
|
|
|
|
if project
|
|
|
|
relation = self.class.for_projects_and_groups([project_id], [project.group&.id])
|
|
|
|
elsif group
|
|
|
|
relation = self.class.for_projects_and_groups(group.projects.select(:id), [group.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
title_exists = relation.find_by_title(title)
|
|
|
|
errors.add(:title, _("already being used for another group or project %{timebox_name}.") % { timebox_name: timebox_name }) if title_exists
|
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|