Skip to content

Instantly share code, notes, and snippets.

@llimllib
Last active December 12, 2015 05:38
Show Gist options
  • Save llimllib/4723111 to your computer and use it in GitHub Desktop.
Save llimllib/4723111 to your computer and use it in GitHub Desktop.
require 'json'
require 'open-uri'
class Hubstat < Linkbot::Plugin
@@config = Linkbot::Config["plugins"]["hubstatus"]
if @@config
Linkbot::Plugin.register('hubstat', self, {
:message => {:regex => /\A!hubstat/, :handler => :on_message, :help => :help}
:periodic => {:handler => :periodic}
})
if Linkbot.db.table_info('hubstatus').empty?
Linkbot.db.execute('CREATE TABLE hubstatus (dt TEXT)');
end
end
def self.help
'!hubstat - see whether your trouble with GitHub is just you'
end
def self.periodic
#by default, post the message if it's within the last day
last_pulled = Time.now.utc - 60*60*24
rows = Linkbot.db.execute("select dt from hubstatus")
last_pulled = Time.parse(rows[0][0]) if !rows.empty? && rows[0][0]
max_time = self.post_status(last_pulled)
Linkbot.db.execute("delete from hubstatus")
Linkbot.db.execute("insert into hubstatus (dt) VALUES ('#{max_time}')")
end
def self.post_status(last_pulled)
colors = {
"good" => "green",
"minor" => "yellow",
"major" => "red"
}
message = JSON.parse(open('https://status.github.com/api/last-message.json').read)
#don't print the message if time exists and is newer than the message
message_time = Time.parse(message["created_on"])
return last_pulled if last_pulled && last_pulled > message_time
color = colors.fetch(message['status'], "gray")
message = "As of #{message["created_on"]}, GitHub is <a href='https://status.github.com/'>reporting</a>: #{message["body"]}"
self.hipchat_send(color, message)
message_time
end
def self.on_message(message, matches)
self.post_status(nil)
end
def self.hipchat_send(color, message)
message = CGI.escape(message)
url = "https://api.hipchat.com/v1/rooms/message?" \
+ "auth_token=#{@@config['api_token']}&" \
+ "message=#{message}&" \
+ "color=#{color}&" \
+ "room_id=#{@@config['room']}&" \
+ "from=GitHub+Status"
puts "sending message to hipchat url #{url}"
open(url)
rescue => e
puts e.inspect
puts e.backtrace.join("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment