-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
92 lines (75 loc) · 2.25 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'sinatra'
require 'json'
require 'digest/sha2'
require 'dotenv'
require 'logger'
require 'base64'
require 'open-uri'
require 'openssl'
require 'httparty'
# Setting the encoding
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
FILE = File.new("#{settings.root}/log/#{settings.environment}.log", 'a+')
LOGGER = Logger.new(FILE)
class TravisHookAPI < Sinatra::Base
set :token, ENV['TRAVIS_USER_TOKEN']
TRAVIS_CONFIG_URL = 'https://api.travis-ci.org/config'.freeze
Dotenv.load
configure do
enable :logging
FILE.sync = true
use Rack::CommonLogger, FILE
LOGGER.formatter = proc do |severity, datetime, progname, msg|
"LOG: #{msg}\n"
end
end
get '/' do
LOGGER.info("Hello World")
'Hello World'
end
post '/' do
if not valid_request?
LOGGER.info("Invalid payload request for repository #{repo_slug}")
else
payload = JSON.parse(params[:payload])
LOGGER.info("Received valid payload for repository #{repo_slug}")
LOGGER.info("Build status message: #{payload['status_message']}")
LOGGER.info("Build status number: #{payload['status']}")
status_message = payload['status_message'].to_s
if status_message == "Fixed" || status_message == "Passed"
LOGGER.info("Fixed Passed")
return if HTTParty.post(
ENV["RPI_RELAY_API_CHANGE_URL"],
{ :body => { "pin_number" => '0', "action" => "off" }}
)
end
if status_message == "Broken" || status_message == "Failed" || status_message == "Still Failing"
LOGGER.info("Broken Failed Still Failing")
return if HTTParty.post(
ENV["RPI_RELAY_API_CHANGE_URL"],
{ :body => {"pin_number" => '0', "action" => "on" }}
)
end
end
end
private
def valid_request?
signature = request.env["HTTP_SIGNATURE"]
json_payload = params.fetch('payload', '')
public_key.verify(
OpenSSL::Digest::SHA1.new,
Base64.decode64(signature),
json_payload
)
end
def public_key
config = JSON.parse(open(TRAVIS_CONFIG_URL).read)
OpenSSL::PKey::RSA.new(
config['config']['notifications']['webhook']['public_key']
)
end
def repo_slug
env['HTTP_TRAVIS_REPO_SLUG']
end
end