Class: BuildMetricsLogger

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

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ BuildMetricsLogger

Returns a new instance of BuildMetricsLogger.



4
5
6
7
8
9
10
# File 'lib/build_metrics_logger.rb', line 4

def initialize(token = nil)
  puts "Listener Initialized"
  @token = token
  @build_id = ENV.fetch('BUILD_METRICS_LOGGER_ID')
  puts "Token: #{@token}"
  puts "ID: #{@build_id}"
end

Instance Method Details

#build_result_urlObject



64
65
66
# File 'lib/build_metrics_logger.rb', line 64

def build_result_url
  "https://build-metrics-web.herokuapp.com/builds/#{@build_id}/result"
end

#dump_summary(notification) ⇒ Object



16
17
18
19
# File 'lib/build_metrics_logger.rb', line 16

def dump_summary(notification)
  # "**Running after the entire suite finishes**"
  suite_finished(notification)
end

#example_failed(notification) ⇒ Object



27
28
29
30
31
# File 'lib/build_metrics_logger.rb', line 27

def example_failed(notification)
  # "**This example failed :(**"

  example_finished(notification)
end

#example_finished(notification) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/build_metrics_logger.rb', line 33

def example_finished(notification)
  if notification.example.[:e2e] == true #&& ENV.fetch('IS_LOCAL', 'true') == 'false'
    id = notification.example.id.to_s
    description = notification.example.full_description.to_s
    location = notification.example.location.to_s
    passed = notification.example.execution_result.status.to_s == 'passed' ? true : false
    # prev_result = notification.example.metadata[:last_run_status].to_s
    time = notification.example.execution_result.run_time.to_s
    exception = notification.example.execution_result.exception.to_s.empty? ? "nil" : notification.example.execution_result.exception.to_s
    test_data = {id: id, description: description, location: location, passed: passed, time: time, exception: exception}
    if @token
      log_example_result(test_data)
    end
  end
end

#example_passed(notification) ⇒ Object



21
22
23
24
25
# File 'lib/build_metrics_logger.rb', line 21

def example_passed(notification)
  # "**This example passed. Neat!**"

  example_finished(notification)
end

#example_result_urlObject



60
61
62
# File 'lib/build_metrics_logger.rb', line 60

def example_result_url
  "https://build-metrics-web.herokuapp.com/builds/#{@build_id}/tests/result"
end

#log_build_result(data) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/build_metrics_logger.rb', line 85

def log_build_result(data)
  response = HTTParty.post(build_result_url,
    headers: {
      "Authorization" => "Bearer #{@token}"
    },
    body: {
      passed: data[:passed],
      time: data[:time],
      build: @build_id
    }
  )

  puts response
end

#log_example_result(data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/build_metrics_logger.rb', line 68

def log_example_result(data)
  response = HTTParty.post(example_result_url,
    headers: {
      "Authorization" => "Bearer #{@token}"
    },
    body: {
      passed: data[:passed],
      rspecID: data[:id],
      description: data[:description],
      path: data[:location],
      runtime: data[:time]
    }
  )

  puts response
end

#start(notification) ⇒ Object



12
13
14
# File 'lib/build_metrics_logger.rb', line 12

def start(notification)
  # "**Running before the entire suite**"
end

#suite_finished(notification) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/build_metrics_logger.rb', line 49

def suite_finished(notification)
  if notification.examples[0].[:e2e] == true #&& ENV.fetch('IS_LOCAL', 'true') == 'false'
    time = notification.duration
    passed = notification.failure_count == 0 ? true : false
    build_data = {time: time, passed: passed}
    if @token
      log_build_result(build_data)
    end
  end
end