class MergetrainCheck::TraintableFormatter

Public Class Methods

new(max_length, firstname_only) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 5
def initialize(max_length, firstname_only)
  @max_length = max_length
  @firstname_only = firstname_only
end

Public Instance Methods

format(body) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 10
def format(body)
  values = [['St', 'Waiting', 'Running', 'MR', 'Pipe ID', 'User', 'Title']]
  values << spacer = nil

  previous_state = body.first['status']
  body.each do |carriage|
    begin_time = date_from_string carriage['created_at']
    pipeline_begin_time = date_from_string carriage['pipeline']['created_at']
    end_time = carriage['merged_at'].nil? ? DateTime.now : date_from_string(carriage['merged_at'])

    is_finished_section = previous_state != carriage['status']
    previous_state = carriage['status']
    values << spacer if is_finished_section

    values << [pipeline_status(carriage['status']),
               pretty_date_difference(begin_time, pipeline_begin_time),
               pretty_date_difference(pipeline_begin_time, end_time),
               carriage['merge_request']['iid'],
               carriage['pipeline']['id'],
               @firstname_only ? carriage['user']['name'].split.first : carriage['user']['name'],
               truncate_string(carriage['merge_request']['title'], @max_length)]
  end

  header = ''
  if has_train_finished? body
    header = "\n  ✋ 🚉 The train is at the station: There are currently no running merge trains!\n\n"
  end
   header + values.to_table
end

Private Instance Methods

date_from_string(datestring) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 52
def date_from_string(datestring)
  DateTime.parse(datestring, '%Q')
end
has_train_finished?(data) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 60
def has_train_finished?(data)
  data.first['status'] != 'fresh'
end
pipeline_status(status) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 41
def pipeline_status(status)
  return '🚂' if status == 'fresh'
  return '✅' if status == 'merged'
  status
end
pretty_date_difference(from, to) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 56
def pretty_date_difference(from, to)
  (to.to_time - from.to_time).duration
end
truncate_string(string, len) click to toggle source
# File lib/mergetrain_check/formatter.rb, line 47
def truncate_string(string, len)
  return string if string.length <= len
  "#{string[0...len]}..."
end