Skip to content

Instantly share code, notes, and snippets.

@muhh
Created May 30, 2012 16:45
Show Gist options
  • Select an option

  • Save muhh/2837539 to your computer and use it in GitHub Desktop.

Select an option

Save muhh/2837539 to your computer and use it in GitHub Desktop.
partial hubot pagerduty integration
# Interacts with PagerDuty
# Author: Markus Heurung <[email protected]>
#
# pd inc(idents) [all|ack(nowledged)|res(olved)] - list incidents (only all for now)
# pd ack(nowledge) <key> - acknowledge incident - not fully working yet
# pd res(olve) <key> - resolve incident - not fully working yet
# pd #ID - show incident details - not working yet
# pd trigger <description> - create new incident
module.exports = (robot) ->
robot.respond /pd inc(idents)?$/i, (msg) ->
PagerDutyIncident msg, "incidents", escape(msg.match[1]), (reply) ->
msg.send reply
robot.respond /pd trigger (.+)$/i, (msg) ->
PagerDutyIntegrationAPI msg, "trigger", (reply) ->
msg.send reply
robot.respond /pd ack(nowledge)? (.+)$/i, (msg) ->
PagerDutyIntegrationAPI msg, "acknowledge", (reply) ->
msg.send reply
robot.respond /pd res(olve)? (.+)$/i, (msg) ->
PagerDutyIntegrationAPI msg, "resolve", (reply) ->
msg.send reply
PagerDutyIntegrationAPI = (msg, cmd, callback) ->
apikey = process.env.HUBOT_PAGERDUTY_APIKEY
unless apikey?
msg.send "PagerDuty API service key is missing."
msg.send "Ensure that HUBOT_PAGERDUTY_APIKEY is set."
return
switch cmd
when "trigger"
data = JSON.stringify { service_key: "#{apikey}", event_type: "trigger", description: "#{msg.match[1]}"}
when "acknowledge"
data = JSON.stringify { service_key: "#{apikey}", event_type: "acknowledge", incident_key: "#{msg.match[1]}", description: "#{msg.match[2]}"}
when "resolve"
data = JSON.stringify { service_key: "#{apikey}", event_type: "resolve", incident_key: "#{msg.match[1]}", description: "#{msg.match[2]}"}
msg.http('https://events.pagerduty.com/generic/2010-04-15/create_event.json')
.headers
"Content-Length": '0'
.post(data) (err, res, body) ->
switch res.statusCode
when 200
#msg.send "#{res.statusCode}"
json = JSON.parse(body)
msg.send "#{json.status}, key: #{json.incident_key}"
when 400
#msg.send "400: #{res.statusCode}"
json = JSON.parse(body)
msg.send "Error: #{json.status}, #{json.message}"
else
msg.send "unknown status: #{res.statusCode}"
PagerDutyIncident = (msg, cmd, callback) ->
username = process.env.HUBOT_PAGERDUTY_USERNAME
password = process.env.HUBOT_PAGERDUTY_PASSWORD
subdomain = process.env.HUBOT_PAGERDUTY_SUBDOMAIN
unless username? and password? and subdomain?
msg.send "PagerDuty account isn't setup."
msg.send "Ensure the following environment variables are set:"
msg.send "HUBOT_PAGERDUTY_USERNAME"
msg.send "HUBOT_PAGERDUTY_PASSWORD"
msg.send "HUBOT_PAGERDUTY_SUBDOMAIN"
return
#msg.send "Got ya. Connecting to PD as #{username} to #{subdomain}.pagerduty.com"
#msg.send "Command is #{cmd}"
#msg.send "Subcommand is #{subcommand}"
# connect
auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
msg.http('https://' + subdomain + '.pagerduty.com/api/v1/incidents')
.headers(Authorization: auth, Accept: 'application/json')
.query(
status: "triggered,acknowledged"
sort_by: "incident_number:asc"
)
.get() (err, res, body) ->
switch res.statusCode
when 200
#msg.send "Everythings fine. Here´s your result"
try
json = JSON.parse(body)
if json.total > 0
msg.send "Total Incidents #{json.total}"
else
msg.send "No problems here, hooray!"
for i, inc of json.incidents
message = "##{inc.incident_number}, #{inc.status}, #{inc.service.name}, #{inc.html_url}, #{inc.assigned_to_user.name} #{inc.assigned_to_user.id}"
msg.send message
catch error
msg.send "i don´t understand that server stuff #{error.text}"
when 400
msg.send "Bad Boy!"
json = JSON.parse(body)
msg.send json.text
when 401
msg.send "Unauthorized!"
when 500
msg.send "Bad Server."
else
msg.send "Unable to process your request and we're not sure why :("
msg.send "#{res.statusCode}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment