Created
October 29, 2021 10:11
-
-
Save joawan/0a1a97cccef2cfdfaabdae6d024afa73 to your computer and use it in GitHub Desktop.
Get oncall from PagerDuty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getOncallForSchedule = async (id, pd) => { | |
const userOnCall = await pd.schedules.listUsersOnCall(id, { | |
time_zone: 'UTC', | |
since: new Date().toISOString(), | |
until: new Date(Date.now() + 1000).toISOString(), | |
}); | |
const user = JSON.parse(userOnCall.body).users.pop(); | |
const contactMethods = await pd.users.listContactMethods(user.id); | |
const userDetails = JSON.parse(contactMethods.body); | |
const contact = userDetails.contact_methods.find((e) => e.type === 'phone_contact_method'); | |
return { | |
id: user.id, | |
name: user.name, | |
email: user.email, | |
phone: contact ? `+${contact.country_code}${contact.address}` : null, | |
time_zone: user.time_zone, | |
}; | |
}; | |
// Usage | |
const PD = require('node-pagerduty'); | |
const pagerdutyClient = new PD(process.env.PD_TOKEN); | |
getOncallForSchedule('PCBO9T1', pagerdutyClient).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment