BigW Consortium Gitlab

global_milestone.rb 2.97 KB
Newer Older
1
class GlobalMilestone
2 3
  include Milestoneish

4 5
  EPOCH = DateTime.parse('1970-01-01')

6
  attr_accessor :title, :milestones
7 8
  alias_attribute :name, :title

9 10 11 12
  def for_display
    @first_milestone
  end

13 14
  def self.build_collection(projects, params)
    child_milestones = MilestonesFinder.new.execute(projects, params)
15

16 17
    milestones = child_milestones.select(:id, :title).group_by(&:title).map do |title, grouped|
      milestones_relation = Milestone.where(id: grouped.map(&:id))
18
      new(title, milestones_relation)
19
    end
20 21 22 23 24 25 26 27 28

    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)
29 30
  end

31 32 33 34 35 36 37 38
  def self.states_count(projects)
    relation = MilestonesFinder.new.execute(projects, state: 'all')
    milestones_by_state_and_title = relation.reorder(nil).group(:state, :title).count

    opened = count_by_state(milestones_by_state_and_title, 'active')
    closed = count_by_state(milestones_by_state_and_title, 'closed')
    all = milestones_by_state_and_title.map { |(_, title), _| title }.uniq.count

39
    {
40 41 42 43 44 45 46 47 48 49 50 51 52
      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

53 54
  def initialize(title, milestones)
    @title = title
55
    @name = title
56
    @milestones = milestones
57
    @first_milestone = milestones.find {|m| m.description.present? } || milestones.first
58 59
  end

60 61 62 63
  def milestoneish_ids
    milestones.select(:id)
  end

64
  def safe_title
65
    @title.to_slug.normalize.to_s
66
  end
67

68
  def projects
69
    @projects ||= Project.for_milestones(milestoneish_ids)
70 71
  end

72
  def state
73 74
    milestones.each do |milestone|
      return 'active' if milestone.state != 'closed'
75
    end
76 77

    'closed'
78
  end
79 80 81 82 83 84 85 86 87

  def active?
    state == 'active'
  end

  def closed?
    state == 'closed'
  end

88
  def issues
89
    @issues ||= Issue.of_milestones(milestoneish_ids).includes(:project, :assignees, :labels)
90 91
  end

92
  def merge_requests
93
    @merge_requests ||= MergeRequest.of_milestones(milestoneish_ids).includes(:target_project, :assignee, :labels)
94 95 96
  end

  def participants
97
    @participants ||= milestones.map(&:participants).flatten.uniq
98
  end
99

100
  def labels
101
    @labels ||= GlobalLabel.build_collection(milestones.includes(:labels).map(&:labels).flatten)
102
                           .sort_by!(&:title)
103
  end
104 105 106 107 108 109 110 111 112 113

  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

114 115 116 117 118 119
  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
120 121
      end
  end
122
end