|
| 1 | +# Description: |
| 2 | +# Show current GitHub status and messages |
| 3 | +# |
| 4 | +# Dependencies: |
| 5 | +# None |
| 6 | +# |
| 7 | +# Configuration: |
| 8 | +# None |
| 9 | +# |
| 10 | +# Commands: |
| 11 | +# hubot github status - Returns the current system status and timestamp. |
| 12 | +# hubot github status last - Returns the last human communication, status, and timestamp. |
| 13 | +# hubot github status messages - Returns the most recent human communications with status and timestamp. |
| 14 | +# |
| 15 | +# Author: |
| 16 | +# voke |
| 17 | + |
| 18 | +module.exports = (robot) -> |
| 19 | + robot.respond /github status$/i, (msg) -> |
| 20 | + status msg |
| 21 | + |
| 22 | + robot.respond /github status last$/i, (msg) -> |
| 23 | + lastMessage msg |
| 24 | + |
| 25 | + robot.respond /github status messages$/i, (msg) -> |
| 26 | + statusMessages msg |
| 27 | + |
| 28 | +# NOTE: messages contains new lines for some reason. |
| 29 | +formatString = (string) -> |
| 30 | + decodeURIComponent(string.replace(/(\n)/gm," ")) |
| 31 | + |
| 32 | +status = (msg) -> |
| 33 | + msg.http('https://status.github.com/api/status.json') |
| 34 | + .get() (err, res, body) -> |
| 35 | + json = JSON.parse(body) |
| 36 | + now = new Date() |
| 37 | + date = new Date(json['last_updated']) |
| 38 | + secondsAgo = Math.round((now.getTime() - date.getTime()) / 1000) |
| 39 | + msg.send "Status: #{json['status']} (#{secondsAgo} seconds ago)" |
| 40 | + |
| 41 | +lastMessage = (msg) -> |
| 42 | + msg.http('https://status.github.com/api/last-message.json') |
| 43 | + .get() (err, res, body) -> |
| 44 | + json = JSON.parse(body) |
| 45 | + date = new Date(json['created_on']) |
| 46 | + msg.send "Status: #{json['status']}\n" + |
| 47 | + "Message: #{formatString(json['body'])}\n" + |
| 48 | + "Date: #{date.toLocaleString()}" |
| 49 | + |
| 50 | +statusMessages = (msg) -> |
| 51 | + msg.http('https://status.github.com/api/messages.json') |
| 52 | + .get() (err, res, body) -> |
| 53 | + json = JSON.parse(body) |
| 54 | + buildMessage = (message) -> |
| 55 | + date = new Date(message['created_on']) |
| 56 | + "[#{message['status']}] #{formatString(message['body'])} (#{date.toLocaleString()})" |
| 57 | + msg.send (buildMessage message for message in json).join('\n') |
0 commit comments