Last active
August 29, 2015 13:57
-
-
Save micahbf/9498156 to your computer and use it in GitHub Desktop.
Get HipChat notifications when GitHub has service issues. Now dependency-free!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/https' | |
require 'json' | |
LAST_MESSAGE_FILE = "#{File.dirname(__FILE__)}/last_github_status.json" | |
HIPCHAT_API_KEY = 'secret' | |
HIPCHAT_ROOM = 'Notifications' | |
class GithubStatus | |
BASE_URI = 'https://status.github.com/api' | |
def current_status | |
get_and_parse('/status.json') | |
end | |
def last_message | |
get_and_parse('/last-message.json') | |
end | |
def messages | |
get_and_parse('/messages.json') | |
end | |
def get_and_parse(path) | |
uri = URI(self.class::BASE_URI + path) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
get = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(get) | |
JSON.parse(response.body) | |
end | |
end | |
class HipchatClient | |
BASE_URI = 'https://api.hipchat.com/v1' | |
def initialize(api_key) | |
@api_key = api_key | |
end | |
def send_message(from, room, message, color) | |
params = { | |
'from' => from, | |
'room_id' => room, | |
'message' => message, | |
'color' => color | |
} | |
post('/rooms/message', params) | |
end | |
def post(path, params) | |
uri = URI(self.class::BASE_URI + path + "?auth_token=#{@api_key}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
post = Net::HTTP::Post.new(uri.request_uri) | |
post.set_form_data(params) | |
http.request(post) | |
end | |
end | |
hipchat_client = HipchatClient.new(HIPCHAT_API_KEY) | |
github_client = GithubStatus.new | |
last_message = JSON.parse(File.read(LAST_MESSAGE_FILE)) rescue {} | |
current_message = github_client.last_message | |
should_notify = last_message['created_on'] != current_message['created_on'] && | |
(current_message['status'] != 'good' || last_message['status'] != 'good') | |
if should_notify | |
message = current_message['body'] | |
color = case current_message['status'] | |
when 'good' then 'green' | |
when 'minor' then 'yellow' | |
when 'major' then 'red' | |
end | |
hipchat_client.send_message('GitHub Status', HIPCHAT_ROOM, message, color) | |
end | |
File.open(LAST_MESSAGE_FILE, 'w') { |f| f.write(current_message.to_json) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment