Class: AggregateConfig

Inherits:
Object show all
Defined in:
lib/jirametrics/aggregate_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_config:, block:) ⇒ AggregateConfig

Returns a new instance of AggregateConfig.



8
9
10
11
12
13
# File 'lib/jirametrics/aggregate_config.rb', line 8

def initialize project_config:, block:
  @project_config = project_config
  @block = block

  @included_projects = []
end

Instance Attribute Details

#project_configObject (readonly)

Returns the value of attribute project_config.



6
7
8
# File 'lib/jirametrics/aggregate_config.rb', line 6

def project_config
  @project_config
end

Instance Method Details



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jirametrics/aggregate_config.rb', line 28

def adjust_issue_links
  issues = @project_config.issues
  issues.each do |issue|
    issue.issue_links.each do |link|
      other_issue_key = link.other_issue.key
      other_issue = issues.find { |i| i.key == other_issue_key }

      link.other_issue = other_issue if other_issue
    end
  end
end

#date_range(range) ⇒ Object



58
59
60
61
62
# File 'lib/jirametrics/aggregate_config.rb', line 58

def date_range range
  @project_config.time_range = date_range_to_time_range(
    date_range: range, timezone_offset: project_config.exporter.timezone_offset
  )
end

#date_range_to_time_range(date_range:, timezone_offset:) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/jirametrics/aggregate_config.rb', line 64

def date_range_to_time_range date_range:, timezone_offset:
  start_of_first_day = Time.new(
    date_range.begin.year, date_range.begin.month, date_range.begin.day, 0, 0, 0, timezone_offset
  )
  end_of_last_day = Time.new(
    date_range.end.year, date_range.end.month, date_range.end.day, 23, 59, 59, timezone_offset
  )

  start_of_first_day..end_of_last_day
end

#evaluate_next_levelObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jirametrics/aggregate_config.rb', line 15

def evaluate_next_level
  instance_eval(&@block)

  if @included_projects.empty?
    raise "#{@project_config.name}: When aggregating, you must include at least one other project"
  end

  # If the date range wasn't set then calculate it now
  @project_config.time_range = find_time_range projects: @included_projects if @project_config.time_range.nil?

  adjust_issue_links
end

#find_time_range(projects:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jirametrics/aggregate_config.rb', line 75

def find_time_range projects:
  raise "Can't calculate aggregated range as no projects were included." if projects.empty?

  earliest = nil
  latest = nil
  projects.each do |project|
    range = project.time_range
    earliest = range.begin if earliest.nil? || range.begin < earliest
    latest = range.end if latest.nil? || range.end > latest
  end

  raise "Can't calculate range" if earliest.nil? || latest.nil?
  earliest..latest
end

#include_issues_from(project_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jirametrics/aggregate_config.rb', line 40

def include_issues_from project_name
  project = @project_config.exporter.project_configs.find { |p| p.name == project_name }
  if project.nil?
    puts "Warning: Aggregated project #{@project_config.name.inspect} is attempting to load " \
      "project #{project_name.inspect} but it can't be found. Is it disabled?"
    return
  end

  @project_config.jira_url = project.jira_url if @project_config.jira_url.nil?
  unless @project_config.jira_url == project.jira_url
    raise 'Not allowed to aggregate projects from different Jira instances: ' \
      "#{@project_config.jira_url.inspect} and #{project.jira_url.inspect}"
  end

  @included_projects << project
  @project_config.add_issues project.issues
end