2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
2018-08-03 19:52:37 +00:00
|
|
|
# Global Milestones are milestones that can be shared across multiple projects
|
2015-11-13 18:20:48 +00:00
|
|
|
class GlobalMilestone
|
2016-03-07 04:07:19 +00:00
|
|
|
include Milestoneish
|
|
|
|
|
2017-07-07 15:08:49 +00:00
|
|
|
STATE_COUNT_HASH = { opened: 0, closed: 0, all: 0 }.freeze
|
2017-01-06 12:47:18 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
attr_reader :milestone
|
2015-10-09 04:24:55 +00:00
|
|
|
alias_attribute :name, :title
|
|
|
|
|
2019-04-23 19:58:20 +00:00
|
|
|
delegate :title, :state, :due_date, :start_date, :participants, :project,
|
|
|
|
:group, :expires_at, :closed?, :iid, :group_milestone?, :safe_title,
|
2020-05-01 00:09:59 +00:00
|
|
|
:timebox_id, :milestoneish_id, :resource_parent, :releases, to: :milestone
|
2018-12-20 11:14:33 +00:00
|
|
|
|
|
|
|
def to_hash
|
|
|
|
{
|
|
|
|
name: title,
|
|
|
|
title: title,
|
|
|
|
group_name: group&.full_name,
|
|
|
|
project_name: project&.full_name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-06 21:17:11 +00:00
|
|
|
def for_display
|
2018-12-20 11:14:33 +00:00
|
|
|
@milestone
|
2016-10-06 21:17:11 +00:00
|
|
|
end
|
|
|
|
|
2017-01-06 12:47:18 +00:00
|
|
|
def self.build_collection(projects, params)
|
2018-12-20 11:14:33 +00:00
|
|
|
items = Milestone.of_projects(projects)
|
|
|
|
.reorder_by_due_date_asc
|
|
|
|
.order_by_name_asc
|
2019-01-17 14:35:23 +00:00
|
|
|
items = items.search_title(params[:search_title]) if params[:search_title].present?
|
2017-01-06 12:47:18 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
Milestone.filter_by_state(items, params[:state]).map { |m| new(m) }
|
2017-01-06 12:47:18 +00:00
|
|
|
end
|
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
# necessary for legacy milestones
|
2017-01-06 12:47:18 +00:00
|
|
|
def self.build(projects, title)
|
2018-12-20 11:14:33 +00:00
|
|
|
milestones = Milestone.of_projects(projects).where(title: title)
|
|
|
|
return if milestones.blank?
|
2017-01-06 12:47:18 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
new(milestones.first)
|
2015-11-13 18:20:48 +00:00
|
|
|
end
|
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
def self.states_count(projects, group = nil)
|
|
|
|
legacy_group_milestones_count = legacy_group_milestone_states_count(projects)
|
|
|
|
group_milestones_count = group_milestones_states_count(group)
|
|
|
|
|
|
|
|
legacy_group_milestones_count.merge(group_milestones_count) do |k, legacy_group_milestones_count, group_milestones_count|
|
|
|
|
legacy_group_milestones_count + group_milestones_count
|
2016-12-13 04:29:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
def self.group_milestones_states_count(group)
|
|
|
|
return STATE_COUNT_HASH unless group
|
2014-06-26 08:57:36 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
counts_by_state = Milestone.of_groups(group).count_by_state
|
2016-12-16 15:52:27 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
{
|
|
|
|
opened: counts_by_state['active'] || 0,
|
|
|
|
closed: counts_by_state['closed'] || 0,
|
|
|
|
all: counts_by_state.values.sum
|
|
|
|
}
|
2014-06-27 08:14:57 +00:00
|
|
|
end
|
2015-11-13 18:20:48 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
def self.legacy_group_milestone_states_count(projects)
|
|
|
|
return STATE_COUNT_HASH unless projects
|
2014-06-26 08:57:36 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
# We need to reorder(nil) on the projects, because the controller passes them in sorted.
|
|
|
|
relation = Milestone.of_projects(projects.reorder(nil)).count_by_state
|
2016-11-15 17:48:30 +00:00
|
|
|
|
2018-12-20 11:14:33 +00:00
|
|
|
{
|
|
|
|
opened: relation['active'] || 0,
|
|
|
|
closed: relation['closed'] || 0,
|
|
|
|
all: relation.values.sum
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(milestone)
|
|
|
|
@milestone = milestone
|
2014-06-26 09:58:40 +00:00
|
|
|
end
|
2014-06-27 08:14:57 +00:00
|
|
|
|
|
|
|
def active?
|
|
|
|
state == 'active'
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
state == 'closed'
|
|
|
|
end
|
|
|
|
|
2014-06-30 08:54:57 +00:00
|
|
|
def issues
|
2018-12-20 11:14:33 +00:00
|
|
|
@issues ||= Issue.of_milestones(milestone).includes(:project, :assignees, :labels)
|
2014-06-27 08:14:57 +00:00
|
|
|
end
|
|
|
|
|
2014-06-30 08:54:57 +00:00
|
|
|
def merge_requests
|
2020-04-03 15:09:56 +00:00
|
|
|
@merge_requests ||= MergeRequest.of_milestones(milestone).includes(:target_project, :assignees, :labels)
|
2014-06-27 08:14:57 +00:00
|
|
|
end
|
2014-06-30 10:41:27 +00:00
|
|
|
|
2016-02-23 21:22:36 +00:00
|
|
|
def labels
|
2018-12-20 11:14:33 +00:00
|
|
|
@labels ||= GlobalLabel.build_collection(milestone.labels).sort_by!(&:title)
|
2015-11-30 20:47:44 +00:00
|
|
|
end
|
2019-11-07 21:06:14 +00:00
|
|
|
|
|
|
|
def global_milestone?
|
|
|
|
true
|
|
|
|
end
|
2014-06-26 08:57:36 +00:00
|
|
|
end
|
2020-05-14 12:08:21 +00:00
|
|
|
|
|
|
|
GlobalMilestone.include_if_ee('::EE::GlobalMilestone')
|