debian-mirror-gitlab/app/models/global_milestone.rb

103 lines
2.7 KiB
Ruby
Raw Normal View History

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-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
2019-02-15 15:39:39 +05:30
attr_reader :milestone
2015-10-24 18:46:33 +05:30
alias_attribute :name, :title
2014-09-02 18:07:02 +05:30
2019-07-31 22:56:46 +05:30
delegate :title, :state, :due_date, :start_date, :participants, :project,
:group, :expires_at, :closed?, :iid, :group_milestone?, :safe_title,
:milestoneish_id, :parent, to: :milestone
2019-02-15 15:39:39 +05:30
def to_hash
{
name: title,
title: title,
group_name: group&.full_name,
project_name: project&.full_name
}
end
2016-11-03 12:29:30 +05:30
def for_display
2019-02-15 15:39:39 +05:30
@milestone
2016-11-03 12:29:30 +05:30
end
2017-08-17 22:00:37 +05:30
def self.build_collection(projects, params)
2019-02-15 15:39:39 +05:30
items = Milestone.of_projects(projects)
.reorder_by_due_date_asc
.order_by_name_asc
2019-03-02 22:35:43 +05:30
items = items.search_title(params[:search_title]) if params[:search_title].present?
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
Milestone.filter_by_state(items, params[:state]).map { |m| new(m) }
2017-08-17 22:00:37 +05:30
end
2019-02-15 15:39:39 +05:30
# necessary for legacy milestones
2017-08-17 22:00:37 +05:30
def self.build(projects, title)
2019-02-15 15:39:39 +05:30
milestones = Milestone.of_projects(projects).where(title: title)
return if milestones.blank?
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
new(milestones.first)
2015-11-26 14:37:03 +05:30
end
2019-02-15 15:39:39 +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
2017-08-17 22:00:37 +05:30
end
end
2019-02-15 15:39:39 +05:30
def self.group_milestones_states_count(group)
return STATE_COUNT_HASH unless group
2014-09-02 18:07:02 +05:30
2019-02-15 15:39:39 +05:30
counts_by_state = Milestone.of_groups(group).count_by_state
2015-12-23 02:04:40 +05:30
2019-02-15 15:39:39 +05:30
{
opened: counts_by_state['active'] || 0,
closed: counts_by_state['closed'] || 0,
all: counts_by_state.values.sum
}
2014-09-02 18:07:02 +05:30
end
2015-11-26 14:37:03 +05:30
2019-02-15 15:39:39 +05:30
def self.legacy_group_milestone_states_count(projects)
return STATE_COUNT_HASH unless projects
2014-09-02 18:07:02 +05:30
2019-02-15 15:39:39 +05:30
# 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
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
{
opened: relation['active'] || 0,
closed: relation['closed'] || 0,
all: relation.values.sum
}
end
def initialize(milestone)
@milestone = milestone
2014-09-02 18:07:02 +05:30
end
def active?
state == 'active'
end
def closed?
state == 'closed'
end
def issues
2019-02-15 15:39:39 +05:30
@issues ||= Issue.of_milestones(milestone).includes(:project, :assignees, :labels)
2014-09-02 18:07:02 +05:30
end
def merge_requests
2019-02-15 15:39:39 +05:30
@merge_requests ||= MergeRequest.of_milestones(milestone).includes(:target_project, :assignee, :labels)
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
def labels
2019-02-15 15:39:39 +05:30
@labels ||= GlobalLabel.build_collection(milestone.labels).sort_by!(&:title)
2015-12-23 02:04:40 +05:30
end
2014-09-02 18:07:02 +05:30
end