Class: Tebako::BuildReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/tebako/build_reporter.rb

Overview

Collects machine-readable build decisions and optionally explains them.

Constant Summary collapse

THREAD_KEY =
:tebako_build_reporter
SCHEMA_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(explain: false, format: nil, output: $stdout) ⇒ BuildReporter

Returns a new instance of BuildReporter.



37
38
39
40
41
42
43
44
# File 'lib/tebako/build_reporter.rb', line 37

def initialize(explain: false, format: nil, output: $stdout)
  @explain = explain
  @format = format
  @output = output
  @events = []
  @started_at = monotonic_time
  @finished = false
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



35
36
37
# File 'lib/tebako/build_reporter.rb', line 35

def events
  @events
end

Class Method Details

.currentObject



18
19
20
# File 'lib/tebako/build_reporter.rb', line 18

def current
  Thread.current[THREAD_KEY]
end

.current=(reporter) ⇒ Object



26
27
28
# File 'lib/tebako/build_reporter.rb', line 26

def current=(reporter)
  Thread.current[THREAD_KEY] = reporter
end

.record(**event) ⇒ Object



30
31
32
# File 'lib/tebako/build_reporter.rb', line 30

def record(**event)
  current&.record(**event)
end

.start(explain: false, format: nil, output: $stdout) ⇒ Object



22
23
24
# File 'lib/tebako/build_reporter.rb', line 22

def start(explain: false, format: nil, output: $stdout)
  self.current = new(explain: explain, format: format, output: output)
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/tebako/build_reporter.rb', line 46

def enabled?
  @explain || @format
end

#finish(success:) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/tebako/build_reporter.rb', line 66

def finish(success:)
  return if @finished

  @finished = true
  print_explanation(success) if @explain
  print_json(success) if @format == "json"
end

#record(stage:, status:, reason: nil, key: nil, duration: nil, details: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tebako/build_reporter.rb', line 50

def record(stage:, status:, reason: nil, key: nil, duration: nil, details: nil)
  return unless enabled?

  event = {
    "stage" => stage.to_s,
    "status" => status.to_s,
    "elapsed_seconds" => rounded(monotonic_time - @started_at)
  }
  event["duration_seconds"] = rounded(duration) if duration
  event["reason"] = reason if reason
  event["key"] = key if key
  event["details"] = details if details && !details.empty?
  @events << event
  event
end