2015-11-13 13:20:48 -05:00
|
|
|
class GlobalMilestone
|
2016-03-06 23:07:19 -05:00
|
|
|
include Milestoneish
|
|
|
|
|
2017-01-06 07:47:18 -05:00
|
|
|
EPOCH = DateTime.parse('1970-01-01')
|
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
|
|
|
|
2015-10-14 06:33:39 -04:00
|
|
|
attr_accessor :title, :milestones
|
2015-10-09 00:24:55 -04:00
|
|
|
alias_attribute :name, :title
|
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
def for_display
|
|
|
|
@first_milestone
|
|
|
|
end
|
|
|
|
|
2017-01-06 07:47:18 -05:00
|
|
|
def self.build_collection(projects, params)
|
2017-07-07 11:08:49 -04:00
|
|
|
params =
|
|
|
|
{ project_ids: projects.map(&:id), state: params[:state] }
|
|
|
|
|
|
|
|
child_milestones = MilestonesFinder.new(params).execute
|
2015-11-13 13:20:48 -05:00
|
|
|
|
2017-01-06 07:47:18 -05:00
|
|
|
milestones = child_milestones.select(:id, :title).group_by(&:title).map do |title, grouped|
|
|
|
|
milestones_relation = Milestone.where(id: grouped.map(&:id))
|
2016-09-23 07:34:39 -04:00
|
|
|
new(title, milestones_relation)
|
2015-11-13 13:20:48 -05:00
|
|
|
end
|
2017-01-06 07:47:18 -05:00
|
|
|
|
|
|
|
milestones.sort_by { |milestone| milestone.due_date || EPOCH }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build(projects, title)
|
|
|
|
child_milestones = Milestone.of_projects(projects).where(title: title)
|
|
|
|
return if child_milestones.blank?
|
|
|
|
|
|
|
|
new(title, child_milestones)
|
2015-11-13 13:20:48 -05:00
|
|
|
end
|
|
|
|
|
2017-07-07 11:08:49 -04: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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.group_milestones_states_count(group)
|
|
|
|
return STATE_COUNT_HASH unless group
|
|
|
|
|
2018-01-05 12:53:31 -05:00
|
|
|
params = { group_ids: [group.id], state: 'all' }
|
2017-07-07 11:08:49 -04:00
|
|
|
|
|
|
|
relation = MilestonesFinder.new(params).execute
|
2018-01-05 12:53:31 -05:00
|
|
|
grouped_by_state = relation.reorder(nil).group(:state).count
|
2017-07-07 11:08:49 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
opened: grouped_by_state['active'] || 0,
|
|
|
|
closed: grouped_by_state['closed'] || 0,
|
|
|
|
all: grouped_by_state.values.sum
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Counts the legacy group milestones which must be grouped by title
|
|
|
|
def self.legacy_group_milestone_states_count(projects)
|
|
|
|
return STATE_COUNT_HASH unless projects
|
|
|
|
|
2018-01-05 12:53:31 -05:00
|
|
|
params = { project_ids: projects.map(&:id), state: 'all' }
|
2017-07-07 11:08:49 -04:00
|
|
|
|
|
|
|
relation = MilestonesFinder.new(params).execute
|
2018-01-05 12:53:31 -05:00
|
|
|
project_milestones_by_state_and_title = relation.reorder(nil).group(:state, :title).count
|
2016-12-12 23:29:12 -05:00
|
|
|
|
2017-07-07 11:08:49 -04:00
|
|
|
opened = count_by_state(project_milestones_by_state_and_title, 'active')
|
|
|
|
closed = count_by_state(project_milestones_by_state_and_title, 'closed')
|
|
|
|
all = project_milestones_by_state_and_title.map { |(_, title), _| title }.uniq.count
|
2016-12-12 23:29:12 -05:00
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
{
|
2016-12-12 23:29:12 -05:00
|
|
|
opened: opened,
|
|
|
|
closed: closed,
|
|
|
|
all: all
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.count_by_state(milestones_by_state_and_title, state)
|
|
|
|
milestones_by_state_and_title.count do |(milestone_state, _), _|
|
|
|
|
milestone_state == state
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_class_method :count_by_state
|
|
|
|
|
2014-06-26 04:57:36 -04:00
|
|
|
def initialize(title, milestones)
|
|
|
|
@title = title
|
2016-03-24 11:20:35 -04:00
|
|
|
@name = title
|
2014-06-26 04:57:36 -04:00
|
|
|
@milestones = milestones
|
2016-10-06 17:17:11 -04:00
|
|
|
@first_milestone = milestones.find {|m| m.description.present? } || milestones.first
|
2014-06-26 04:57:36 -04:00
|
|
|
end
|
|
|
|
|
2016-12-16 10:52:27 -05:00
|
|
|
def milestoneish_ids
|
|
|
|
milestones.select(:id)
|
|
|
|
end
|
|
|
|
|
2014-06-27 04:14:57 -04:00
|
|
|
def safe_title
|
2015-12-22 16:15:32 -05:00
|
|
|
@title.to_slug.normalize.to_s
|
2014-06-27 04:14:57 -04:00
|
|
|
end
|
2015-11-13 13:20:48 -05:00
|
|
|
|
2014-06-26 07:32:35 -04:00
|
|
|
def projects
|
2016-12-16 10:52:27 -05:00
|
|
|
@projects ||= Project.for_milestones(milestoneish_ids)
|
2014-06-26 04:57:36 -04:00
|
|
|
end
|
|
|
|
|
2014-06-26 05:58:40 -04:00
|
|
|
def state
|
2016-11-15 12:48:30 -05:00
|
|
|
milestones.each do |milestone|
|
|
|
|
return 'active' if milestone.state != 'closed'
|
2014-06-26 05:58:40 -04:00
|
|
|
end
|
2016-11-15 12:48:30 -05:00
|
|
|
|
|
|
|
'closed'
|
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
|
2017-05-04 08:11:15 -04:00
|
|
|
@issues ||= Issue.of_milestones(milestoneish_ids).includes(:project, :assignees, :labels)
|
2014-06-27 04:14:57 -04:00
|
|
|
end
|
|
|
|
|
2014-06-30 04:54:57 -04:00
|
|
|
def merge_requests
|
2016-12-16 10:52:27 -05:00
|
|
|
@merge_requests ||= MergeRequest.of_milestones(milestoneish_ids).includes(:target_project, :assignee, :labels)
|
2014-06-27 04:14:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def participants
|
2017-05-04 08:11:15 -04:00
|
|
|
@participants ||= milestones.map(&:participants).flatten.uniq
|
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
|
2016-09-23 07:34:39 -04:00
|
|
|
@labels ||= GlobalLabel.build_collection(milestones.includes(:labels).map(&:labels).flatten)
|
2016-03-06 23:07:19 -05:00
|
|
|
.sort_by!(&:title)
|
2015-11-16 13:55:58 -05:00
|
|
|
end
|
2015-11-30 15:47:44 -05:00
|
|
|
|
|
|
|
def due_date
|
|
|
|
return @due_date if defined?(@due_date)
|
|
|
|
|
|
|
|
@due_date =
|
|
|
|
if @milestones.all? { |x| x.due_date == @milestones.first.due_date }
|
|
|
|
@milestones.first.due_date
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-15 12:48:30 -05:00
|
|
|
def start_date
|
|
|
|
return @start_date if defined?(@start_date)
|
|
|
|
|
|
|
|
@start_date =
|
|
|
|
if @milestones.all? { |x| x.start_date == @milestones.first.start_date }
|
|
|
|
@milestones.first.start_date
|
2015-11-30 15:47:44 -05:00
|
|
|
end
|
|
|
|
end
|
2014-06-26 04:57:36 -04:00
|
|
|
end
|