Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update new production with 3.1.8 upstream #5

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Then add **hubot-pager-me** to your `external-scripts.json`:
| `HUBOT_PAGERDUTY_USER_ID` | No`*` | The user ID of a PagerDuty user for your bot. This is only required if you want chat users to be able to trigger incidents without their own PagerDuty user.
| `HUBOT_PAGERDUTY_SERVICE_API_KEY` | No`*` | The [Incident Service Key](https://v2.developer.pagerduty.com/docs/incident-creation-api) to use when creating a new incident. This should be assigned to a dummy escalation policy that doesn't actually notify, as Hubot will trigger on this before reassigning it.
| `HUBOT_PAGERDUTY_SERVICES` | No | Provide a comma separated list of service identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those services. |
| `HUBOT_PAGERDUTY_TEAMS` | No | Provide a comma separated list of teams identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those teams. You need teams function on |
| `HUBOT_PAGERDUTY_SCHEDULES` | No | Provide a comma separated list of schedules identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those schedules. |

`*` - May be required for certain actions.
Expand Down
19 changes: 12 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"async": "^3.2.0",
"lodash": "^4.17.20",
"moment-timezone": "^0.5.31",
"scoped-http-client": "^0.11.0"
},
Expand Down
42 changes: 41 additions & 1 deletion src/pagerduty.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
HttpClient = require 'scoped-http-client'
_ = require('lodash')
moment = require('moment-timezone')
timezone = 'UTC'

pagerDutyApiKey = process.env.HUBOT_PAGERDUTY_API_KEY
pagerDutySubdomain = process.env.HUBOT_PAGERDUTY_SUBDOMAIN
pagerDutyBaseUrl = 'https://api.pagerduty.com'
pagerDutyServices = process.env.HUBOT_PAGERDUTY_SERVICES
pagerDutyTeams = process.env.HUBOT_PAGERDUTY_TEAMS
pagerDutySchedules = process.env.HUBOT_PAGERDUTY_SCHEDULES
pagerDutyFromEmail = process.env.HUBOT_PAGERDUTY_FROM_EMAIL
pagerNoop = process.env.HUBOT_PAGERDUTY_NOOP
pagerNoop = false if pagerNoop is 'false' or pagerNoop is 'off'
Expand Down Expand Up @@ -33,8 +38,11 @@ module.exports =
cb = query
query = {}

if pagerDutyTeams? && url.match /\/incidents/
query['teams_ids[]'] = pagerDutyTeams.split(',')

if pagerDutyServices? && url.match /\/incidents/
query['service_id'] = pagerDutyServices
query['service_ids[]'] = pagerDutyServices.split(',')

@http(url)
.query(query)
Expand Down Expand Up @@ -134,6 +142,38 @@ module.exports =
return
cb(null, json.incidents)

getOncalls: (query, cb) ->
if typeof(query) is 'function'
cb = query
query = {}

if pagerDutySchedules?
query['schedule_ids[]'] = pagerDutySchedules.split(',')

if pagerDutyEscalationsPolicies?
query['escalation_policy_ids[]'] = pagerDutyEscalationsPolicies.split(',')

console.error query

@get "/oncalls", query, (err, json) ->
if err?
cb(err)
return
# escalation_level filtering
oncalls = _.map json.oncalls, (o) ->
if o.escalation_level is 1 then return o
filterdOncalls = _.without(oncalls, undefined)

oncallsBySchedules = _.transform(filterdOncalls, (result, value, key) ->
message = "(#{moment(value.start).tz(timezone).format('MMM Do, h:mm a')} - #{moment(value.end).tz(timezone).format('MMM Do, h:mm a')}) - *#{value.user.summary}*"
unless result[value.schedule.summary]
(result[value.schedule.summary] || (result[value.schedule.summary] = [])).push(message);
if result[value.schedule.summary].indexOf(message) == -1
(result[value.schedule.summary] || (result[value.schedule.summary] = [])).push(message);
, {})

cb(null, oncallsBySchedules)

getSchedules: (query, cb) ->
if typeof(query) is 'function'
cb = query
Expand Down
77 changes: 77 additions & 0 deletions src/scripts/pagerduty-custom.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
pagerduty = require('../pagerduty')
userSupportId = process.env.HUBOT_PAGERDUTY_SCHEDULE_USERSUP_ID
platformId = process.env.HUBOT_PAGERDUTY_SCHEDULE_PLATFORM_ID
escalationId = process.env.HUBOT_PAGERDUTY_SCHEDULE_ESCALATION_ID

# selects the right oncall based on current time
findOncall = (oncalls, timeFrame, timeNow) ->
if timeFrame is 'was'
return oncalls.find((oncall) ->
Date.parse(oncall.start) < timeNow - (24 * 3600000) < Date.parse(oncall.end)
)
if timeFrame is 'next'
return oncalls.find((oncall) ->
Date.parse(oncall.start) > timeNow
)
return oncalls.find((oncall) ->
Date.parse(oncall.start) < timeNow < Date.parse(oncall.end)
)

# formats time to show the name of the day, month, day and time
formatTime = (date) ->
dateTime = new Date(date).toString()
return "#{dateTime.substring(0, 10)} #{dateTime.substring(16, 21)}"

# according to requesed time (on call, now, on call next, was on call) prepares time query
# pagerduty limits the amount of returned data, so more precise time settings
setTimeQuery = (timeFrame, timeNow) ->
past = new Date(timeNow - (72 * 3600000)).toISOString()
future = new Date(timeNow + (72 * 3600000)).toISOString()
plusMinute = new Date(timeNow + 60000).toISOString()

if timeFrame is 'was'
return { since: past, untilParam: new Date(timeNow).toISOString() }

if timeFrame is 'next'
return { since: new Date(timeNow).toISOString(), untilParam: future }

return { since: new Date(timeNow).toISOString(), untilParam: plusMinute }

getCustomOncalls = (timeFrame, msg) ->
if not msg?
console.log('no msg sent')
return

timeNow = Date.now()
timeQuery = setTimeQuery(timeFrame, timeNow)
query = {
limit: 50
time_zone: 'UTC'
"schedule_ids[]": [userSupportId, platformId, escalationId],
since: timeQuery.since
until: timeQuery.untilParam
}

pagerduty.get('/oncalls', query, (err, json) ->
if err
msg.send(err)

userSupports = json.oncalls.filter((oncall) -> oncall.schedule.id is userSupportId)
escallations = json.oncalls.filter((oncall) -> oncall.schedule.id is escalationId)
platformOncalls = json.oncalls.filter((oncall) -> oncall.schedule.id is platformId)

userSupport = findOncall(userSupports, timeFrame, timeNow)
escallation = findOncall(escallations, timeFrame, timeNow)
platformOncall = findOncall(platformOncalls, timeFrame, timeNow)

message = "#{userSupport.schedule.summary} - (#{formatTime(userSupport.start)} - #{formatTime(userSupport.end)}) - *#{userSupport.user.summary}*\n"
message += "#{platformOncall.schedule.summary} - #{formatTime(platformOncall.start)} - #{formatTime(platformOncall.end)} - *#{platformOncall.user.summary}*\n"
message += "#{escallation.schedule.summary} - #{formatTime(escallation.start)} - #{formatTime(escallation.end)} - *#{escallation.user.summary}*\n"

msg.send(message)
)
getCustomOncalls.findOncall = findOncall
getCustomOncalls.formatTime = formatTime
getCustomOncalls.setTimeQuery = setTimeQuery

module.exports = getCustomOncalls
Loading