2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Milestone < ApplicationRecord
|
2015-06-28 16:12:32 -04:00
|
|
|
# Represents a "No Milestone" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no milestone assigned.
|
2020-02-27 19:09:08 -05:00
|
|
|
MilestoneStruct = Struct.new(:title, :name, :id) do
|
|
|
|
# Ensure these models match the interface required for exporting
|
|
|
|
def serializable_hash(_opts = {})
|
|
|
|
{ title: title, name: name, id: id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-09 05:59:38 -04:00
|
|
|
None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
|
|
|
|
Any = MilestoneStruct.new('Any Milestone', '', -1)
|
2016-03-11 12:46:14 -05:00
|
|
|
Upcoming = MilestoneStruct.new('Upcoming', '#upcoming', -2)
|
2017-03-10 14:01:05 -05:00
|
|
|
Started = MilestoneStruct.new('Started', '#started', -3)
|
2015-06-28 16:12:32 -04:00
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
include CacheMarkdownField
|
2018-04-20 10:00:15 -04:00
|
|
|
include AtomicInternalId
|
2018-05-11 03:52:48 -04:00
|
|
|
include IidRoutes
|
2015-02-05 19:49:41 -05:00
|
|
|
include Sortable
|
2015-12-24 08:43:07 -05:00
|
|
|
include Referable
|
2015-11-26 10:16:50 -05:00
|
|
|
include StripAttribute
|
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
|
2017-11-24 05:45:19 -05:00
|
|
|
include Gitlab::SQL::Pattern
|
2013-08-21 05:16:26 -04:00
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
prepend_if_ee('::EE::Milestone') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
cache_markdown_field :title, pipeline: :single_line
|
|
|
|
cache_markdown_field :description
|
|
|
|
|
2012-04-08 17:28:58 -04:00
|
|
|
belongs_to :project
|
2017-07-07 11:08:49 -04:00
|
|
|
belongs_to :group
|
|
|
|
|
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-01-14 10:07:55 -05:00
|
|
|
has_internal_id :iid, scope: :project, track_if: -> { !importing? }, init: ->(s) { s&.project&.milestones&.maximum(:iid) }
|
|
|
|
has_internal_id :iid, scope: :group, track_if: -> { !importing? }, init: ->(s) { s&.group&.milestones&.maximum(:iid) }
|
2018-04-20 10:00:15 -04:00
|
|
|
|
2012-04-08 17:28:58 -04:00
|
|
|
has_many :issues
|
2019-01-16 07:09:29 -05:00
|
|
|
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
|
2012-10-26 09:53:45 -04:00
|
|
|
has_many :merge_requests
|
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
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
scope :of_projects, ->(ids) { where(project_id: ids) }
|
|
|
|
scope :of_groups, ->(ids) { where(group_id: ids) }
|
2013-02-18 04:10:09 -05:00
|
|
|
scope :active, -> { with_state(:active) }
|
|
|
|
scope :closed, -> { with_state(:closed) }
|
2017-07-07 11:08:49 -04:00
|
|
|
scope :for_projects, -> { where(group: nil).includes(:project) }
|
2019-03-11 10:15:05 -04:00
|
|
|
scope :started, -> { active.where('milestones.start_date <= CURRENT_DATE') }
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2018-12-17 07:59:23 -05:00
|
|
|
scope :for_projects_and_groups, -> (projects, groups) do
|
|
|
|
projects = projects.compact if projects.is_a? Array
|
|
|
|
projects = [] if projects.nil?
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2018-12-17 07:59:23 -05:00
|
|
|
groups = groups.compact if groups.is_a? Array
|
|
|
|
groups = [] if groups.nil?
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2019-01-11 09:58:18 -05:00
|
|
|
where(project_id: projects).or(where(group_id: groups))
|
2017-07-07 11:08:49 -04:00
|
|
|
end
|
|
|
|
|
2020-02-05 07:09:15 -05:00
|
|
|
scope :within_timeframe, -> (start_date, end_date) do
|
|
|
|
where('start_date is not NULL or due_date is not NULL')
|
|
|
|
.where('start_date is NULL or start_date <= ?', end_date)
|
|
|
|
.where('due_date is NULL or due_date >= ?', start_date)
|
|
|
|
end
|
|
|
|
|
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')) }
|
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
validates :group, presence: true, unless: :project
|
|
|
|
validates :project, presence: true, unless: :group
|
2019-11-14 19:06:05 -05:00
|
|
|
validates :title, presence: true
|
2012-12-14 00:34:05 -05:00
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
validate :uniqueness_of_title, if: :title_changed?
|
|
|
|
validate :milestone_type_check
|
2017-03-31 11:11:28 -04:00
|
|
|
validate :start_date_should_be_less_than_due_date, if: proc { |m| m.start_date.present? && m.due_date.present? }
|
2019-05-31 06:31:47 -04:00
|
|
|
validate :dates_within_4_digits
|
2019-09-16 11:06:26 -04:00
|
|
|
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
|
2013-02-18 04:10:09 -05:00
|
|
|
|
2015-11-26 10:16:50 -05:00
|
|
|
strip_attributes :title
|
|
|
|
|
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
|
|
|
|
2015-10-09 00:24:55 -04:00
|
|
|
alias_attribute :name, :title
|
|
|
|
|
2015-08-13 10:48:21 -04:00
|
|
|
class << self
|
2019-01-17 09:35:23 -05:00
|
|
|
# Searches for milestones with a matching title or description.
|
2016-03-01 11:05:26 -05:00
|
|
|
#
|
|
|
|
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
|
|
|
|
#
|
|
|
|
# query - The search query as a String
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
2015-08-13 10:48:21 -04:00
|
|
|
def search(query)
|
2017-11-24 06:24:24 -05:00
|
|
|
fuzzy_search(query, [:title, :description])
|
2015-08-13 10:48:21 -04:00
|
|
|
end
|
2017-07-07 11:08:49 -04:00
|
|
|
|
2019-01-17 09:35:23 -05:00
|
|
|
# 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-07-07 11:08:49 -04:00
|
|
|
def filter_by_state(milestones, state)
|
|
|
|
case state
|
|
|
|
when 'closed' then milestones.closed
|
|
|
|
when 'all' then milestones
|
|
|
|
else milestones.active
|
|
|
|
end
|
|
|
|
end
|
2017-12-01 14:08:38 -05:00
|
|
|
|
2018-12-20 06:14:33 -05:00
|
|
|
def count_by_state
|
|
|
|
reorder(nil).group(:state).count
|
|
|
|
end
|
|
|
|
|
2020-02-27 19:09:08 -05:00
|
|
|
def predefined_id?(id)
|
|
|
|
[Any.id, None.id, Upcoming.id, Started.id].include?(id)
|
|
|
|
end
|
|
|
|
|
2017-12-01 14:08:38 -05:00
|
|
|
def predefined?(milestone)
|
2020-02-27 19:09:08 -05:00
|
|
|
predefined_id?(milestone&.id)
|
2017-12-01 14:08:38 -05:00
|
|
|
end
|
2015-08-13 10:48:21 -04:00
|
|
|
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
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
def participants
|
2018-10-26 12:19:28 -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
|
|
|
|
|
2016-03-31 20:54:00 -04:00
|
|
|
##
|
2017-08-03 07:50:06 -04:00
|
|
|
# Returns the String necessary to reference this Milestone in Markdown. Group
|
|
|
|
# milestones only support name references, and do not support cross-project
|
|
|
|
# references.
|
2016-03-31 20:54:00 -04:00
|
|
|
#
|
|
|
|
# format - Symbol format to use (default: :iid, optional: :name)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2016-11-02 19:49:13 -04:00
|
|
|
# Milestone.first.to_reference # => "%1"
|
|
|
|
# Milestone.first.to_reference(format: :name) # => "%\"goal\""
|
2019-09-18 10:02:45 -04:00
|
|
|
# Milestone.first.to_reference(cross_namespace_project) # => "gitlab-org/gitlab-foss%1"
|
|
|
|
# Milestone.first.to_reference(same_namespace_project) # => "gitlab-foss%1"
|
2016-03-31 20:54:00 -04:00
|
|
|
#
|
2017-11-22 08:20:35 -05:00
|
|
|
def to_reference(from = nil, format: :name, full: false)
|
2016-03-30 22:12:34 -04:00
|
|
|
format_reference = milestone_format_reference(format)
|
|
|
|
reference = "#{self.class.reference_prefix}#{format_reference}"
|
2016-01-07 06:26:05 -05:00
|
|
|
|
2017-08-03 07:50:06 -04:00
|
|
|
if project
|
2020-01-29 07:09:08 -05:00
|
|
|
"#{project.to_reference_base(from, full: full)}#{reference}"
|
2017-08-03 07:50:06 -04:00
|
|
|
else
|
|
|
|
reference
|
|
|
|
end
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 08:20:35 -05:00
|
|
|
def reference_link_text(from = nil)
|
2018-12-19 06:12:19 -05:00
|
|
|
self.class.reference_prefix + self.title
|
2015-12-24 08:43:07 -05:00
|
|
|
end
|
|
|
|
|
2018-12-20 06:14:33 -05:00
|
|
|
def milestoneish_id
|
2016-12-16 10:52:27 -05:00
|
|
|
id
|
|
|
|
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?
|
2013-02-18 04:10:09 -05:00
|
|
|
active? && issues.opened.count.zero?
|
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
|
|
|
|
2016-05-09 10:58:20 -04:00
|
|
|
def title=(value)
|
2016-09-26 19:47:34 -04:00
|
|
|
write_attribute(:title, sanitize_title(value)) if value.present?
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
def safe_title
|
|
|
|
title.to_slug.normalize.to_s
|
|
|
|
end
|
|
|
|
|
2019-10-17 08:07:33 -04:00
|
|
|
def resource_parent
|
2017-07-07 11:08:49 -04:00
|
|
|
group || project
|
|
|
|
end
|
|
|
|
|
2019-10-17 01:15:07 -04:00
|
|
|
def to_ability_name
|
|
|
|
model_name.singular
|
|
|
|
end
|
|
|
|
|
2017-08-24 12:41:44 -04:00
|
|
|
def group_milestone?
|
2017-07-07 11:08:49 -04:00
|
|
|
group_id.present?
|
|
|
|
end
|
|
|
|
|
2017-08-24 12:37:37 -04:00
|
|
|
def project_milestone?
|
2017-07-07 11:08:49 -04:00
|
|
|
project_id.present?
|
|
|
|
end
|
|
|
|
|
2019-12-03 13:06:49 -05:00
|
|
|
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-03-30 22:12:34 -04:00
|
|
|
private
|
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
# 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-01-11 09:58:18 -05:00
|
|
|
relation = Milestone.for_projects_and_groups(group.projects.select(:id), [group.id])
|
2017-07-07 11:08:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
title_exists = relation.find_by_title(title)
|
2019-04-12 08:28:07 -04:00
|
|
|
errors.add(:title, _("already being used for another group or project milestone.")) if title_exists
|
2017-07-07 11:08:49 -04:00
|
|
|
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-04-12 08:28:07 -04:00
|
|
|
errors.add(field, _("milestone should belong either to a project or a group."))
|
2017-07-07 11:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-31 20:54:00 -04:00
|
|
|
def milestone_format_reference(format = :iid)
|
2019-04-12 08:28:07 -04:00
|
|
|
raise ArgumentError, _('Unknown format') unless [:iid, :name].include?(format)
|
2016-03-30 22:12:34 -04:00
|
|
|
|
2017-09-20 05:55:54 -04:00
|
|
|
if group_milestone? && format == :iid
|
2019-04-12 08:28:07 -04:00
|
|
|
raise ArgumentError, _('Cannot refer to a group milestone by an internal id!')
|
2017-09-20 05:55:54 -04:00
|
|
|
end
|
|
|
|
|
2016-03-30 22:12:34 -04:00
|
|
|
if format == :name && !name.include?('"')
|
|
|
|
%("#{name}")
|
|
|
|
else
|
2016-03-31 20:54:00 -04:00
|
|
|
iid
|
2016-03-30 22:12:34 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-26 19:47:34 -04:00
|
|
|
|
|
|
|
def sanitize_title(value)
|
|
|
|
CGI.unescape_html(Sanitize.clean(value.to_s))
|
|
|
|
end
|
2016-11-15 12:48:30 -05:00
|
|
|
|
|
|
|
def start_date_should_be_less_than_due_date
|
|
|
|
if due_date <= start_date
|
2019-04-12 08:28:07 -04:00
|
|
|
errors.add(:due_date, _("must be greater than start date"))
|
2016-11-15 12:48:30 -05:00
|
|
|
end
|
|
|
|
end
|
2017-01-06 07:47:18 -05:00
|
|
|
|
2019-05-31 06:31:47 -04:00
|
|
|
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-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
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|