Class: BuildkiteDing

Inherits:
Object
  • Object
show all
Defined in:
lib/buildkite-ding.rb

Overview

Goes ‘ding’ when your builds fail. or pass. or start.

To run without daemon/binary: BuildkiteDing.run

Class Method Summary collapse

Class Method Details

.check_buildsObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/buildkite-ding.rb', line 68

def self.check_builds
  new_builds = @client.builds(created_from: @last_check.iso8601) +
               @client.builds(finished_from: @last_check.iso8601)
  new_builds.each do |b|
    if @tracked_builds.key?(b['id']) && @tracked_builds[b['id']] == b['state']
      next
    end
    BuildkiteDing.interogate_build(b)
  end
end

.generate_configObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/buildkite-ding.rb', line 45

def self.generate_config
  @config = {}
  puts "No .bkdingrc file detected in #{CONFIG_PATH}
        Please provide a Buildkite API access token with read_builds scope:
        ( Generate at https://buildkite.com/user/api-access-tokens/new )"
  @config[:buildkite_api_token] = STDIN.gets.chomp
  File.open(CONFIG_PATH, 'w') do |f|
    f.write(JSON.pretty_generate(@config))
  end
end

.interogate_build(build) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/buildkite-ding.rb', line 79

def self.interogate_build(build)
  puts "Build is new or updated: #{build['message']}"
  @tracked_builds[build['id']] = build['state']
  return unless BUILD_EVENTS.key? build['state']
  print "Build #{build['state']} - "
  BuildkiteDing.take_action(BUILD_EVENTS[build['state']])
end

.load_configObject



36
37
38
39
40
41
42
43
# File 'lib/buildkite-ding.rb', line 36

def self.load_config
  if File.exist?(CONFIG_PATH)
    @config = JSON.parse(File.read(CONFIG_PATH), symbolize_names: true)
    puts "Loaded configuration from #{CONFIG_PATH}"
  else
    BuildkiteDing.generate_config
  end
end

.play_sound(sound) ⇒ Object

Sound name standard is class_numberofdings



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/buildkite-ding.rb', line 92

def self.play_sound(sound) # Sound name standard is class_numberofdings
  puts 'Finding play command...'
  if File.which('afplay') # Included in MacOS
    puts 'Found afplay (this is probably MacOS)'
    puts "Playing #{DATA_PATH}/#{sound}.mp3"
    system "afplay #{DATA_PATH}/#{sound}.mp3"
  elsif File.which('play') # Sox package?
    puts 'Found play (this is presumably Linux)'
    system "play #{DATA_PATH}/#{sound}.mp3"
  end
end

.runObject

Alternative to starting with the bin



31
32
33
34
# File 'lib/buildkite-ding.rb', line 31

def self.run # Alternative to starting with the bin
  BuildkiteDing.load_config
  BuildkiteDing.run_client
end

.run_clientObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/buildkite-ding.rb', line 56

def self.run_client
  @client = Buildkit.new(token: @config[:buildkite_api_token])
  @tracked_builds = {} # Simple hash tracking build id and state
  @last_check = Time.now.utc
  loop do
    puts 'Checking builds...'
    BuildkiteDing.check_builds
    @last_check = Time.now.utc
    sleep(5)
  end
end

.take_action(action) ⇒ Object



87
88
89
90
# File 'lib/buildkite-ding.rb', line 87

def self.take_action(action)
  puts action['action']
  BuildkiteDing.play_sound(action['sound'])
end