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