BigW Consortium Gitlab

global_milestone.rb 2.5 KB
Newer Older
1
class GlobalMilestone
2
  attr_accessor :title, :milestones
3 4
  alias_attribute :name, :title

5 6 7 8 9 10 11 12
  def self.build_collection(milestones)
    milestones = milestones.group_by(&:title)

    milestones.map do |title, milestones|
      new(title, milestones)
    end
  end

13 14 15 16 17
  def initialize(title, milestones)
    @title = title
    @milestones = milestones
  end

18
  def safe_title
19
    @title.to_slug.normalize.to_s
20
  end
21

22 23 24 25 26 27 28 29
  def expired?
    if due_date
      due_date.past?
    else
      false
    end
  end

30
  def projects
Marin Jankovski committed
31
    milestones.map { |milestone| milestone.project }
32 33 34
  end

  def issue_count
Marin Jankovski committed
35
    milestones.map { |milestone| milestone.issues.count }.sum
36 37 38
  end

  def merge_requests_count
Marin Jankovski committed
39
    milestones.map { |milestone| milestone.merge_requests.count }.sum
40 41
  end

42
  def open_items_count
Marin Jankovski committed
43
    milestones.map { |milestone| milestone.open_items_count }.sum
44 45
  end

46
  def closed_items_count
Marin Jankovski committed
47
    milestones.map { |milestone| milestone.closed_items_count }.sum
48 49 50
  end

  def total_items_count
Marin Jankovski committed
51
    milestones.map { |milestone| milestone.total_items_count }.sum
52 53 54 55 56
  end

  def percent_complete
    ((closed_items_count * 100) / total_items_count).abs
  rescue ZeroDivisionError
57
    0
58
  end
59 60

  def state
Marin Jankovski committed
61
    state = milestones.map { |milestone| milestone.state }
62

63
    if state.count('closed') == state.size
64
      'closed'
65 66
    else
      'active'
67 68
    end
  end
69 70 71 72 73 74 75 76 77

  def active?
    state == 'active'
  end

  def closed?
    state == 'closed'
  end

78
  def issues
79
    @issues ||= milestones.map(&:issues).flatten.group_by(&:state)
80 81
  end

82
  def merge_requests
83
    @merge_requests ||= milestones.map(&:merge_requests).flatten.group_by(&:state)
84 85 86
  end

  def participants
87
    @participants ||= milestones.map(&:participants).flatten.compact.uniq
88
  end
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  def opened_issues
    issues.values_at("opened", "reopened").compact.flatten
  end

  def closed_issues
    issues['closed']
  end

  def opened_merge_requests
    merge_requests.values_at("opened", "reopened").compact.flatten
  end

  def closed_merge_requests
    merge_requests.values_at("closed", "merged", "locked").compact.flatten
  end
105 106 107 108

  def complete?
    total_items_count == closed_items_count
  end
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  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
      else
        nil
      end
  end

  def expires_at
    if due_date
      if due_date.past?
124
        "expired on #{due_date.to_s(:medium)}"
125
      else
126
        "expires on #{due_date.to_s(:medium)}"
127 128 129
      end
    end
  end
130
end