Skip to content

Instantly share code, notes, and snippets.

@ocxo
Created October 27, 2016 20:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocxo/ed1f0a13600dcf91a56e095e19d3bbaf to your computer and use it in GitHub Desktop.
Save ocxo/ed1f0a13600dcf91a56e095e19d3bbaf to your computer and use it in GitHub Desktop.
# Description:
# Incident management
#
# Commands:
# hubot (create|open|start) new incident (on|for|about|of) service-name
moment = require('moment')
# create the incident channel with a custom name + date created
createChannel = (channelName, timestamp, user, robot, msg) ->
# channels must have 21 chars or less so we'll truncate the custom name
channelName = channelName.substring(0, 11).replace(/-$/, '')
channelName = "#i-#{channelName}-#{timestamp}"
params = { name: channelName }
robot.slack.channels.create(params).then((res) ->
json = JSON.parse(JSON.stringify(res))
channelId = json.channel.id
inviteUser(channelId, user, robot) # invite invoking user
inviteUser(channelId, 'UXXXXXXXX', robot) # invite Botzo
postMessage(channelId, robot)
).catch (err) ->
robot.logger.error 'Failed to create channel: ' + err
# invite botzo
inviteUser = (channel, user, robot) ->
params = { channel: channel, user: user }
robot.slack.channels.invite(params)
# post the incident checklist
postMessage = (channel, robot) ->
checklist = '*New Incident Checklist*\n
:dancers: Invite people to this channel who are involved in the incident \n
:crown: Assign or claim *Incident Leader* role \n
:speaking_head_in_silhouette: Assign or claim *Incident Communicator* role \n
:loudspeaker: Communicate with customers via <#CXXXXXXXX|status-sync> or <https://manage.statuspage.io/pages/xxxxxx/|StatusPage.io> \n
:sleuth_or_spy: Share all findings, actions, notes, assumptions in this channel in realtime \n
:loop: Troubleshoot, communicate, fix, repeat until resolved \n
:checkered_flag: Resolve \n
:memo: Conduct <https://github.com/your-org/postmortems|postmortem> and ping <#CXXXXXXXX|postmortems> if you need general help \n
:raising_hand: Get ownership on postmortem action items \n
:package: Archive this channel (`/archive`)'
params = { channel: channel, text: checklist }
robot.slack.chat.postMessage(params)
# link the new channel
linkChannel = (channelName, timestamp, robot, msg) ->
params = { exclude_archived: 1 }
robot.slack.channels.list(params).then((res) ->
channelList = JSON.parse(JSON.stringify(res))
channelName = "#i-#{channelName}-#{timestamp}"
for i of channelList.channels
name = channelList.channels[i].name
if channelName == "##{name}"
msg.send "Incident channel created: <##{channelList.channels[i].id}|#{name}>"
break
).catch (err) ->
robot.logger.error 'Failed to list channels: ' + err
module.exports = (robot) ->
robot.respond /(?:create|open|start)? ?(new)? incident ?(?:on|for|about|of)? (.*)$/i, {id: "channels.create"}, (msg) ->
channelName = msg.match[2].replace(/\s/g, '-')
timestamp = moment.utc().format('YYMMDD')
user = msg.envelope.user.id
createChannel channelName, timestamp, user, robot, msg
setTimeout () ->
linkChannel channelName, timestamp, robot, msg
, 1000
@ocxo
Copy link
Author

ocxo commented Oct 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment