Class: IssuePrinter

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

Instance Method Summary collapse

Constructor Details

#initialize(issue) ⇒ IssuePrinter

Returns a new instance of IssuePrinter.



4
5
6
# File 'lib/jirametrics/issue_printer.rb', line 4

def initialize issue
  @issue = issue
end

Instance Method Details

#assignee_lineObject



21
22
23
24
25
26
# File 'lib/jirametrics/issue_printer.rb', line 21

def assignee_line
  assignee = @issue.raw['fields']['assignee']
  return '' if assignee.nil?

  "  [assignee] #{assignee['name'].inspect} <#{assignee['emailAddress']}>\n"
end

#build_historyObject

Each history entry is [time, type, detail, artificial?].



52
53
54
# File 'lib/jirametrics/issue_printer.rb', line 52

def build_history
  start_stop_entries + discarded_change_entries + change_entries
end

#change_entriesObject



72
73
74
75
76
# File 'lib/jirametrics/issue_printer.rb', line 72

def change_entries
  (@issue.changes + (@issue.discarded_changes || [])).map do |change|
    [change.time, change.field, create_change_message(change: change, issue: @issue), change.artificial?]
  end
end

#create_change_message(change:, issue:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jirametrics/issue_printer.rb', line 87

def create_change_message change:, issue:
  value, old_value = format_change_values(change: change, issue: issue)

  message = +''
  message << "#{old_value} -> " unless old_value.nil? || old_value.empty?
  message << value
  if change.artificial?
    message << ' (Artificial entry)'
  else
    message << " (Author: #{change.author})"
  end
  message
end

#cycletime_warningObject



45
46
47
48
49
# File 'lib/jirametrics/issue_printer.rb', line 45

def cycletime_warning
  return '' if @issue.board.cycletime

  "  Unable to determine start/end times as board #{@issue.board.id} has no cycletime specified\n"
end

#discarded_change_entriesObject



66
67
68
69
70
# File 'lib/jirametrics/issue_printer.rb', line 66

def discarded_change_entries
  (@issue.discarded_change_times || []).map do |time|
    [time, nil, '^^^^ Changes discarded ^^^^', true]
  end
end

#format_change_values(change:, issue:) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jirametrics/issue_printer.rb', line 101

def format_change_values change:, issue:
  if change.status?
    value = "#{change.value.inspect}:#{change.value_id.inspect}"
    old_value = change.old_value ? "#{change.old_value.inspect}:#{change.old_value_id.inspect}" : nil
  elsif change.sprint?
    added = change.value_id - change.old_value_id
    removed = change.old_value_id - change.value_id
    value = "#{change.value.inspect} #{change.value_id}"
    value << " (added: #{added})" unless added.empty?
    value << " (removed: #{removed})" unless removed.empty?
    old_value = nil
  else
    value = issue.compact_text(change.value).inspect
    old_value = change.old_value ? issue.compact_text(change.old_value).inspect : nil
  end
  [value, old_value]
end

#header_lineObject



17
18
19
# File 'lib/jirametrics/issue_printer.rb', line 17

def header_line
  "#{@issue.key} (#{@issue.type}): #{@issue.compact_text @issue.summary, max: 200}\n"
end

#history_sectionObject



37
38
39
40
41
42
43
# File 'lib/jirametrics/issue_printer.rb', line 37

def history_section
  result = +''
  result << cycletime_warning
  result << "  History:\n"
  result << render_history(build_history)
  result
end


28
29
30
31
32
33
34
35
# File 'lib/jirametrics/issue_printer.rb', line 28

def links_section
  result = +''
  @issue.raw['fields']['issuelinks']&.each do |link|
    result << "  [link] #{link['type']['outward']} #{link['outwardIssue']['key']}\n" if link['outwardIssue']
    result << "  [link] #{link['type']['inward']} #{link['inwardIssue']['key']}\n" if link['inwardIssue']
  end
  result
end

#render_history(history) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/jirametrics/issue_printer.rb', line 78

def render_history history
  type_width = history.collect { |_time, type, _detail, _artificial| type&.length || 0 }.max
  sort_history!(history)
  history.map do |time, type, detail, _artificial|
    type = type.nil? ? '-' * type_width : type.rjust(type_width)
    "    #{time.strftime '%Y-%m-%d %H:%M:%S %z'} [#{type}] #{detail}\n"
  end.join
end

#sort_history!(history) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jirametrics/issue_printer.rb', line 119

def sort_history! history
  history.sort! do |a, b|
    if a[0] == b[0]
      if a[1].nil?
        1
      elsif b[1].nil?
        -1
      else
        a[1] <=> b[1]
      end
    else
      a[0] <=> b[0]
    end
  end
end

#start_stop_entriesObject



56
57
58
59
60
61
62
63
64
# File 'lib/jirametrics/issue_printer.rb', line 56

def start_stop_entries
  return [] unless @issue.board.cycletime

  started_at, stopped_at = @issue.started_stopped_times
  entries = []
  entries << [started_at, nil, 'vvvv Started here vvvv', true] if started_at
  entries << [stopped_at, nil, '^^^^ Finished here ^^^^', true] if stopped_at
  entries
end

#to_sObject



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

def to_s
  result = +''
  result << header_line
  result << assignee_line
  result << links_section
  result << history_section
  result
end