2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
# Global Milestones are milestones that can be shared across multiple projects
|
2015-11-26 14:37:03 +05:30
|
|
|
class GlobalMilestone
|
2016-06-02 11:05:42 +05:30
|
|
|
include Milestoneish
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
EPOCH = DateTime.parse('1970-01-01')
|
2017-09-10 17:25:29 +05:30
|
|
|
STATE_COUNT_HASH = { opened: 0, closed: 0, all: 0 }.freeze
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
attr_accessor :title, :milestones
|
|
|
|
alias_attribute :name, :title
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def for_display
|
|
|
|
@first_milestone
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.build_collection(projects, params)
|
2017-09-10 17:25:29 +05:30
|
|
|
params =
|
|
|
|
{ project_ids: projects.map(&:id), state: params[:state] }
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
child_milestones = MilestonesFinder.new(params).execute # rubocop: disable CodeReuse/Finder
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
milestones = child_milestones.select(:id, :title).group_by(&:title).map do |title, grouped|
|
|
|
|
milestones_relation = Milestone.where(id: grouped.map(&:id))
|
2016-11-03 12:29:30 +05:30
|
|
|
new(title, milestones_relation)
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
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-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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-03-17 18:26:18 +05:30
|
|
|
params = { group_ids: [group.id], state: 'all' }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
relation = MilestonesFinder.new(params).execute # rubocop: disable CodeReuse/Finder
|
2018-03-17 18:26:18 +05:30
|
|
|
grouped_by_state = relation.reorder(nil).group(:state).count
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
{
|
|
|
|
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-03-17 18:26:18 +05:30
|
|
|
params = { project_ids: projects.map(&:id), state: 'all' }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
relation = MilestonesFinder.new(params).execute # rubocop: disable CodeReuse/Finder
|
2018-03-17 18:26:18 +05:30
|
|
|
project_milestones_by_state_and_title = relation.reorder(nil).group(:state, :title).count
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
{
|
|
|
|
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-09-02 18:07:02 +05:30
|
|
|
def initialize(title, milestones)
|
|
|
|
@title = title
|
2016-06-02 11:05:42 +05:30
|
|
|
@name = title
|
2014-09-02 18:07:02 +05:30
|
|
|
@milestones = milestones
|
2016-11-03 12:29:30 +05:30
|
|
|
@first_milestone = milestones.find {|m| m.description.present? } || milestones.first
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def milestoneish_ids
|
|
|
|
milestones.select(:id)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def safe_title
|
|
|
|
@title.to_slug.normalize.to_s
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def projects
|
2017-08-17 22:00:37 +05:30
|
|
|
@projects ||= Project.for_milestones(milestoneish_ids)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def state
|
2017-08-17 22:00:37 +05:30
|
|
|
milestones.each do |milestone|
|
|
|
|
return 'active' if milestone.state != 'closed'
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
'closed'
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
|
|
|
state == 'active'
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
state == 'closed'
|
|
|
|
end
|
|
|
|
|
|
|
|
def issues
|
2017-08-17 22:00:37 +05:30
|
|
|
@issues ||= Issue.of_milestones(milestoneish_ids).includes(:project, :assignees, :labels)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge_requests
|
2017-08-17 22:00:37 +05:30
|
|
|
@merge_requests ||= MergeRequest.of_milestones(milestoneish_ids).includes(:target_project, :assignee, :labels)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def participants
|
2017-08-17 22:00:37 +05:30
|
|
|
@participants ||= milestones.map(&:participants).flatten.uniq
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def labels
|
2016-11-03 12:29:30 +05:30
|
|
|
@labels ||= GlobalLabel.build_collection(milestones.includes(:labels).map(&:labels).flatten)
|
2016-06-02 11:05:42 +05:30
|
|
|
.sort_by!(&:title)
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|