2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class Milestone < ApplicationRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
include Sortable
|
2020-05-24 23:13:21 +05:30
|
|
|
include Timebox
|
2016-06-02 11:05:42 +05:30
|
|
|
include Milestoneish
|
2019-12-04 20:38:33 +05:30
|
|
|
include FromUnion
|
2020-03-13 15:44:24 +05:30
|
|
|
include Importable
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
prepend_mod_with('Milestone') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
class Predefined
|
|
|
|
ALL = [::Timebox::None, ::Timebox::Any, ::Timebox::Started, ::Timebox::Upcoming].freeze
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
has_many :milestone_releases
|
|
|
|
has_many :releases, through: :milestone_releases
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
has_internal_id :iid, scope: :project, track_if: -> { !importing? }
|
|
|
|
has_internal_id :iid, scope: :group, track_if: -> { !importing? }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
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
|
|
|
|
|
|
|
scope :active, -> { with_state(:active) }
|
2019-07-07 11:18:12 +05:30
|
|
|
scope :started, -> { active.where('milestones.start_date <= CURRENT_DATE') }
|
2020-05-24 23:13:21 +05:30
|
|
|
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)
|
2020-03-13 15:44:24 +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')) }
|
2020-06-23 00:09:42 +05:30
|
|
|
scope :with_api_entity_associations, -> { preload(project: [:project_feature, :route, namespace: :route]) }
|
2021-03-08 18:12:59 +05:30
|
|
|
scope :order_by_dates_and_title, -> { order(due_date: :asc, start_date: :asc, title: :asc) }
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
|
2021-09-04 01:27:46 +05:30
|
|
|
validate :uniqueness_of_title, if: :title_changed?
|
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
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def self.min_chars_for_partial_matching
|
|
|
|
2
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def self.reference_prefix
|
|
|
|
'%'
|
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
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
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.link_reference_pattern
|
2016-06-02 11:05:42 +05:30
|
|
|
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
|
2016-01-14 18:37:52 +05:30
|
|
|
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
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def self.with_web_entity_associations
|
|
|
|
preload(:group, project: [:project_feature, group: [:parent], namespace: :route])
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def participants
|
2021-06-08 01:23:25 +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
|
|
|
|
|
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?
|
2020-10-24 23:57:45 +05:30
|
|
|
active? && issues.opened.count == 0
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def author_id
|
|
|
|
nil
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
# 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?
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def parent
|
2020-01-01 13:55:28 +05:30
|
|
|
if group_milestone?
|
2020-05-24 23:13:21 +05:30
|
|
|
group
|
|
|
|
else
|
|
|
|
project
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def subgroup_milestone?
|
|
|
|
group_milestone? && parent.subgroup?
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
private
|
|
|
|
|
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
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
# 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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|